下面列出了java.nio.IntBuffer#flip ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public GLRenderableUpdateTask createBatch(final VisualAccess access) {
final int numVertices = access.getVertexCount();
final FloatBuffer colorBuffer = Buffers.newDirectFloatBuffer(COLOR_BUFFER_WIDTH * numVertices);
final IntBuffer iconBuffer = Buffers.newDirectIntBuffer(ICON_BUFFER_WIDTH * numVertices);
for (int pos = 0; pos < numVertices; pos++) {
bufferColorInfo(pos, colorBuffer, access);
bufferIconInfo(pos, iconBuffer, access);
}
colorBuffer.flip();
iconBuffer.flip();
return gl -> {
if (numVertices > 0) {
batch.initialise(numVertices);
batch.buffer(gl, colorTarget, colorBuffer);
batch.buffer(gl, iconTarget, iconBuffer);
batch.finalise(gl);
}
};
}
public static void main(String[] args) {
IntBuffer buf = IntBuffer.allocate(10); // 准备出10个大小的缓冲区
System.out.print("1、写入数据之前的position、limit和capacity:");
System.out.println("position = " + buf.position() + ",limit = " + buf.limit() + ",capacty = " + buf.capacity());
int[] temp = { 5, 7, 9 };// 定义一个int数组
buf.put(3); // 设置一个数据
buf.put(temp); // 此时已经存放了四个记录
System.out.print("2、写入数据之后的position、limit和capacity:");
System.out.println("position = " + buf.position() + ",limit = " + buf.limit() + ",capacty = " + buf.capacity());
buf.flip(); // 重设缓冲区
// postion = 0 ,limit = 原本position
System.out.print("3、准备输出数据时的position、limit和capacity:");
System.out.println("position = " + buf.position() + ",limit = " + buf.limit() + ",capacty = " + buf.capacity());
System.out.print("缓冲区中的内容:");
while (buf.hasRemaining()) {
int x = buf.get();
System.out.print(x + "、");
}
}
public static void upload(BufferedImage img, int format, int tex) {
int width = img.getWidth();
int height = img.getHeight();
if (Visage.trace) Visage.log.finest("Uploading "+width+"x"+height+" ("+(width*height)+" pixel) image");
BufferedImage unindexed = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
int[] argb = new int[width*height];
img.getRGB(0, 0, width, height, argb, 0, width);
unindexed.setRGB(0, 0, width, height, argb, 0, width);
unindexed.coerceData(true);
unindexed.getRGB(0, 0, width, height, argb, 0, width);
IntBuffer buf = BufferUtils.createIntBuffer(width*height);
buf.put(argb);
buf.flip();
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, buf);
checkGLError();
}
/**
* Generate a new IntBuffer using the given array of ints. The IntBuffer
* will be data.length long and contain the int data as data[0], data[1]...
* etc.
*
* @param data
* array of ints to place into a new IntBuffer
* @return a new direct, flipped IntBuffer, or null if data was null
*/
public static IntBuffer createIntBuffer(int... data) {
if (data == null) {
return null;
}
IntBuffer buff = createIntBuffer(data.length);
buff.clear();
buff.put(data);
buff.flip();
return buff;
}
@Override
public GLRenderableUpdateTask createBatch(final VisualAccess access) {
int lineCounter = 0;
for (int link = 0; link < access.getLinkCount(); link++) {
if (access.getLinkSource(link) != access.getLinkDestination(link)) {
connections.add(NEW_LINK);
for (int pos = 0; pos < access.getLinkConnectionCount(link); pos++) {
final int connection = access.getLinkConnection(link, pos);
connectionPosToBufferPos.put(connection, lineCounter++);
connections.add(connection);
}
}
}
final int numLines = lineCounter;
FloatBuffer colorBuffer = Buffers.newDirectFloatBuffer(numLines * 2 * COLOR_BUFFER_WIDTH);
IntBuffer dataBuffer = Buffers.newDirectIntBuffer(numLines * 2 * CONNECTION_INFO_BUFFER_WIDTH);
connections.forEach(pos -> {
if (pos == NEW_LINK) {
leftOffset = 0;
} else {
bufferColorInfo(pos, colorBuffer, access);
bufferConnectionInfo(pos, dataBuffer, access);
}
});
colorBuffer.flip();
dataBuffer.flip();
return gl -> {
if (numLines > 0) {
batch.initialise(numLines * 2);
batch.buffer(gl, colorTarget, colorBuffer);
batch.buffer(gl, connectionInfoTarget, dataBuffer);
batch.finalise(gl);
}
};
}
public static ByteBuffer createByteBuffer(int... values){
ByteBuffer byteBuffer = memAlloc(Integer.BYTES * values.length);
IntBuffer intBuffer = byteBuffer.asIntBuffer();
intBuffer.put(values);
intBuffer.flip();
return byteBuffer;
}
@Override
public IntBuffer buffer() {
int length = length();
IntBuffer buf = IntBuffer.allocate(length);
for (int i = 0; i < length; i++) {
buf.put(getCodePointAt(i));
}
buf.flip();
return buf;
}
public TransparencyFbo(int width, int height) {
GLTexture albedoAttachment = new TextureImage2D(width, height, ImageFormat.RGBA16FLOAT, SamplerFilter.Nearest);
GLTexture alphaAttachment = new TextureImage2D(width, height, ImageFormat.RGBA16FLOAT, SamplerFilter.Nearest);
GLTexture lightScatteringAttachment = new TextureImage2D(width, height, ImageFormat.RGBA16FLOAT, SamplerFilter.Nearest);
GLTexture depthAttachment = new TextureImage2D(width, height, ImageFormat.DEPTH32FLOAT, SamplerFilter.Nearest);
attachments.put(Attachment.COLOR, albedoAttachment);
attachments.put(Attachment.ALPHA, alphaAttachment);
attachments.put(Attachment.LIGHT_SCATTERING, lightScatteringAttachment);
attachments.put(Attachment.DEPTH, depthAttachment);
IntBuffer drawBuffers = BufferUtil.createIntBuffer(3);
drawBuffers.put(GL_COLOR_ATTACHMENT0);
drawBuffers.put(GL_COLOR_ATTACHMENT1);
drawBuffers.put(GL_COLOR_ATTACHMENT2);
drawBuffers.flip();
frameBuffer = new GLFramebuffer();
frameBuffer.bind();
frameBuffer.createColorTextureAttachment(albedoAttachment.getHandle(),0);
frameBuffer.createColorTextureAttachment(alphaAttachment.getHandle(),1);
frameBuffer.createColorTextureAttachment(lightScatteringAttachment.getHandle(),2);
frameBuffer.createDepthTextureAttachment(depthAttachment.getHandle());
frameBuffer.setDrawBuffers(drawBuffers);
frameBuffer.checkStatus();
frameBuffer.unbind();
}
public static IntBuffer CreateFlippedBuffer(int... values)
{
IntBuffer buffer = CreateIntBuffer(values.length);
buffer.put(values);
buffer.flip();
return buffer;
}
public static void main(String[] args) {
ByteBuffer bb=ByteBuffer.allocate(BSIZE);
IntBuffer ib=bb.asIntBuffer();
ib.put(new int[]{11,42,47,99,143,811,1016});
System.out.println(ib.getClass().getName());
System.out.println(ib.get(3));
ib.put(3,1811);
ib.flip();
while (ib.hasRemaining()){
int i=ib.get();
System.out.println(i);
}
}
public static IntBuffer createFlippedBuffer(int... values)
{
IntBuffer buffer = createIntBuffer(values.length);
buffer.put(values);
buffer.flip();
return buffer;
}
/**
* Specifies a list of color buffers to be drawn into.
*
* @param num Specifies the number of buffers in bufs.
* @param glEnum Points to an array of symbolic constants specifying the buffers into which fragment colors or data values will be written.
*/
public static void glDrawBuffers(int num, int[] glEnum) {
IntBuffer buf = MapRenderer.getIntBuffer(num);
buf.put(glEnum, 0, num);
buf.flip();
gl30.drawBuffers(num, buf);
}
/**
* flip 方法直译就是翻转的意思, 也就是说在读写之间切换, 做好准备工作
*/
@Test
public void testFlip() {
IntBuffer buffer = IntBuffer.allocate(1024);
buffer.put(1);
// 需要使用flip方法来进行模式的切换, 如果没有做读写操作就直接调用就会抛出 BufferOverflowException 异常
// 也就是说, 在写和读操作后, 需要调用该方法才能进行下一步的写和读
buffer.flip();
buffer.put(2);
buffer.flip();
int result = buffer.get();
log.info("result={}", result);
}
int initShader(GL2 gl, int nShaderType, String source) {
int shader = gl.glCreateShader(nShaderType);
if (shader != 0) {
String[] sources = new String[]{ source };
gl.glShaderSource(shader, 1, sources, null);
gl.glCompileShader(shader);
IntBuffer compiled = BufferUtils.createIntBuffer(1);
gl.glGetShaderiv(shader, GL2.GL_COMPILE_STATUS, compiled);
if (compiled.get() == 0) {
IntBuffer infoLen = BufferUtils.createIntBuffer(1);
gl.glGetShaderiv(shader, GL2.GL_INFO_LOG_LENGTH, infoLen);
int length = infoLen.get();
if (length > 0) {
ByteBuffer buf = BufferUtils.createByteBuffer(length);
infoLen.flip();
gl.glGetShaderInfoLog(shader, length, infoLen, buf);
byte[] b = new byte[infoLen.get()];
buf.get(b);
System.out.println("Prgram : " + fProgram);
System.out.println(source);
System.err.println("Error compiling shader " + vProgram + " " + fProgram + " -> " + new String(b));
}
}
}
return shader;
}
/**
* @see org.newdawn.slick.Graphics#destroy()
*/
public void destroy() {
super.destroy();
IntBuffer buffer = BufferUtils.createIntBuffer(1);
buffer.put(FBO);
buffer.flip();
EXTFramebufferObject.glDeleteFramebuffersEXT(buffer);
valid = false;
}
public static void glDeleteTextures(int num, int[] ids) {
IntBuffer buf = MapRenderer.getIntBuffer(num);
buf.put(ids, 0, num);
buf.flip();
gl.deleteTextures(num, buf);
}
public Water(int patches, GLShaderProgram shader, GLShaderProgram wireframeShader)
{
config = new WaterConfig();
config.loadFile("water-config.properties");
GLContext.getResources().setWaterConfig(config);
GLPatchVBO meshBuffer = new GLPatchVBO();
meshBuffer.addData(MeshGenerator.generatePatch2D4x4(patches),16);
renderConfig = new WaterRenderParameter();
GLRenderInfo renderInfo = new GLRenderInfo(shader, renderConfig, meshBuffer);
GLRenderInfo wireframeRenderInfo = new GLRenderInfo(wireframeShader, renderConfig, meshBuffer);
dudv = new TextureImage2D("textures/water/dudv/dudv1.jpg", SamplerFilter.Trilinear);
addComponent(NodeComponentType.MAIN_RENDERINFO, renderInfo);
addComponent(NodeComponentType.WIREFRAME_RENDERINFO, wireframeRenderInfo);
fft = new FFT(config.getN(), config.getL(),
config.getAmplitude(), config.getWindDirection(),
config.getAlignment(), config.getWindSpeed(),
config.getCapillarWavesSupression());
fft.setT_delta(config.getT_delta());
fft.setChoppy(config.isChoppy());
fft.init();
normalmapRenderer = new NormalRenderer(config.getN());
getNormalmapRenderer().setStrength(config.getNormalStrength());
reflection_texture = new TextureImage2D(BaseContext.getConfig().getFrameWidth()/2,
BaseContext.getConfig().getFrameHeight()/2,
ImageFormat.RGBA16FLOAT, SamplerFilter.Nearest);
IntBuffer drawBuffers = BufferUtil.createIntBuffer(1);
drawBuffers.put(GL_COLOR_ATTACHMENT0);
drawBuffers.flip();
reflection_fbo = new GLFramebuffer();
reflection_fbo.bind();
reflection_fbo.createColorTextureAttachment(reflection_texture.getHandle(),0);
reflection_fbo.createDepthBufferAttachment(BaseContext.getConfig().getFrameWidth()/2,
BaseContext.getConfig().getFrameHeight()/2);
reflection_fbo.setDrawBuffers(drawBuffers);
reflection_fbo.checkStatus();
reflection_fbo.unbind();
refraction_texture = new TextureImage2D(BaseContext.getConfig().getFrameWidth()/2,
BaseContext.getConfig().getFrameHeight()/2,
ImageFormat.RGBA16FLOAT, SamplerFilter.Nearest);
refraction_fbo = new GLFramebuffer();
refraction_fbo.bind();
refraction_fbo.createColorTextureAttachment(refraction_texture.getHandle(),0);
refraction_fbo.createDepthBufferAttachment(BaseContext.getConfig().getFrameWidth()/2,
BaseContext.getConfig().getFrameHeight()/2);
refraction_fbo.setDrawBuffers(drawBuffers);
refraction_fbo.checkStatus();
refraction_fbo.unbind();
refractionRenderList = new RenderList();
reflectionRenderList = new RenderList();
}
public static void glDeleteFrameBuffers(int num, int[] ids) {
IntBuffer buf = MapRenderer.getIntBuffer(num);
buf.put(ids, 0, num);
buf.flip();
gl.deleteFramebuffers(num, buf);
}
/**
* Starts the executable process and writes {@code int} values to a data file and the stdin
* of the executable. Captures stdout of the executable to a file.
*/
private void runBridgeTest() {
final List<String> command = ProcessUtils.buildSubProcessCommand(executable, executableArguments);
try {
final File dataFile = new File(fileOutputPrefix + ".data");
final File outputFile = new File(fileOutputPrefix + ".out");
final File errorFile = new File(fileOutputPrefix + ".err");
// Store int values
final IntBuffer buffer = IntBuffer.allocate(Integer.SIZE * 2);
try (BufferedWriter textOutput = Files.newBufferedWriter(dataFile.toPath())) {
// Write int data using a single bit in all possible positions
int value = 1;
for (int i = 0; i < Integer.SIZE; i++) {
writeInt(textOutput, buffer, value);
value <<= 1;
}
// Write random int data
while (buffer.remaining() != 0) {
writeInt(textOutput, buffer, ThreadLocalRandom.current().nextInt());
}
}
// Pass the same values to the output application
buffer.flip();
UniformRandomProvider rng = new IntProvider() {
@Override
public int next() {
return buffer.get();
}
};
// Start the application.
final ProcessBuilder builder = new ProcessBuilder(command);
builder.redirectOutput(ProcessBuilder.Redirect.to(outputFile));
builder.redirectError(ProcessBuilder.Redirect.to(errorFile));
final Process testingProcess = builder.start();
// Open the stdin of the process and write to a custom data sink.
// Note: The 'bridge' command only supports 32-bit data in order to
// demonstrate passing suitable data for TestU01 BigCrush.
final boolean raw64 = false;
try (RngDataOutput sink = RNGUtils.createDataOutput(rng, raw64,
testingProcess.getOutputStream(), buffer.capacity() * 4, byteOrder)) {
sink.write(rng);
}
final Integer exitValue = ProcessUtils.getExitValue(testingProcess);
if (exitValue == null) {
LogUtils.error("%s did not exit. Process was killed.", command.get(0));
} else {
if (exitValue.intValue() != 0) {
LogUtils.error("%s exit code = %d", command.get(0), exitValue.intValue());
}
}
} catch (IOException ex) {
throw new ApplicationException("Failed to run process: " + ex.getMessage(), ex);
}
}
public void storeData(int[] data){
IntBuffer buffer = BufferUtils.createIntBuffer(data.length);
buffer.put(data);
buffer.flip();
storeData(buffer);
}