Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
elect86 committed Apr 22, 2016
1 parent 45d0804 commit 250688c
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 100 deletions.
10 changes: 2 additions & 8 deletions jogl-samples/src/framework/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,14 @@ protected boolean begin(GL gl) {

@Override
public final void dispose(GLAutoDrawable drawable) {

GL gl = getGl(drawable);
assert end(gl);
BufferUtils.destroyDirectBuffer(clearColor);
BufferUtils.destroyDirectBuffer(clearDepth);
System.exit(0);
}

protected boolean end(GL gl) {
protected boolean end(GL gl) {
return true;
}

Expand Down Expand Up @@ -424,13 +423,8 @@ protected Vec3 cameraPosition() {
return new Vec3(0.0f, 0.0f, -translationCurrent.y);
}

protected final String getDataDirectory() {
return "/data/";
}

@Override
public void keyPressed(KeyEvent e) {

}

@Override
Expand All @@ -439,7 +433,7 @@ public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_ESCAPE:
animator.stop();
// glWindow.destroy();
glWindow.destroy();
break;
}
}
Expand Down
102 changes: 56 additions & 46 deletions jogl-samples/src/tests/gl_400/Gl_400_transform_feedback_object.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import glm.vec._4.Vec4;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import test.Semantic;

/**
Expand Down Expand Up @@ -51,14 +52,22 @@ public Gl_400_transform_feedback_object() {

private class Buffer {

public static final int TRANSFORM = 0;
public static final int TRANSFORM0 = 0;
public static final int FEEDBACK = 1;
public static final int TRANSFORM_ARRAY = 2;
public static final int MAX = 3;
}

private class VertexArray {

public static final int FEEDBACK = 0;
// public static final int FEEDBACK = 0;
public static final int MAX = 1;
public static final int TRANSFORM_FEEDBACK = 1;
public static final int MAX = 2;
}

private int[] feedbackName = {0}, transformArrayBufferName = {0}, transformVertexArrayName = {0},
feedbackArrayBufferName = {0}, feedbackVertexArrayName = {0}, bufferName = new int[Buffer.MAX];
private IntBuffer feedbackName = GLBuffers.newDirectIntBuffer(1),
vertexArrayName = GLBuffers.newDirectIntBuffer(VertexArray.MAX),
bufferName = GLBuffers.newDirectIntBuffer(Buffer.MAX);
private int transformProgramName, feedbackProgramName;

@Override
Expand Down Expand Up @@ -93,8 +102,8 @@ private boolean initProgram(GL4 gl4) {

ShaderProgram shaderProgram = new ShaderProgram();

ShaderCode vertexShaderCode = ShaderCode.create(gl4, GL_VERTEX_SHADER,
this.getClass(), SHADERS_ROOT, null, SHADERS_SOURCE_TRANSFORM, "vert", null, true);
ShaderCode vertexShaderCode = ShaderCode.create(gl4, GL_VERTEX_SHADER, this.getClass(), SHADERS_ROOT, null,
SHADERS_SOURCE_TRANSFORM, "vert", null, true);

shaderProgram.init(gl4);

Expand All @@ -113,10 +122,10 @@ private boolean initProgram(GL4 gl4) {

ShaderProgram shaderProgram = new ShaderProgram();

ShaderCode vertexShaderCode = ShaderCode.create(gl4, GL_VERTEX_SHADER,
this.getClass(), SHADERS_ROOT, null, SHADERS_SOURCE_FEEDBACK, "vert", null, true);
ShaderCode fragmentShaderCode = ShaderCode.create(gl4, GL_FRAGMENT_SHADER,
this.getClass(), SHADERS_ROOT, null, SHADERS_SOURCE_FEEDBACK, "frag", null, true);
ShaderCode vertexShaderCode = ShaderCode.create(gl4, GL_VERTEX_SHADER, this.getClass(), SHADERS_ROOT,
null, SHADERS_SOURCE_FEEDBACK, "vert", null, true);
ShaderCode fragmentShaderCode = ShaderCode.create(gl4, GL_FRAGMENT_SHADER, this.getClass(), SHADERS_ROOT,
null, SHADERS_SOURCE_FEEDBACK, "frag", null, true);

shaderProgram.init(gl4);

Expand All @@ -132,8 +141,8 @@ private boolean initProgram(GL4 gl4) {
// Get variables locations
if (validated) {

gl4.glUniformBlockBinding(transformProgramName,
gl4.glGetUniformBlockIndex(transformProgramName, "Transform"), Semantic.Uniform.TRANSFORM0);
gl4.glUniformBlockBinding(transformProgramName, gl4.glGetUniformBlockIndex(transformProgramName, "Transform"),
Semantic.Uniform.TRANSFORM0);
}

return validated & checkError(gl4, "initProgram");
Expand All @@ -144,10 +153,11 @@ private boolean initVertexArray(GL4 gl4) {
checkError(gl4, "initVertexArray 0");

// Build a vertex array object
gl4.glGenVertexArrays(1, transformVertexArrayName, 0);
gl4.glBindVertexArray(transformVertexArrayName[0]);
gl4.glGenVertexArrays(VertexArray.MAX, vertexArrayName);

gl4.glBindVertexArray(vertexArrayName.get(VertexArray.TRANSFORM_FEEDBACK));
{
gl4.glBindBuffer(GL_ARRAY_BUFFER, transformArrayBufferName[0]);
gl4.glBindBuffer(GL_ARRAY_BUFFER, bufferName.get(Buffer.TRANSFORM_ARRAY));
gl4.glVertexAttribPointer(Semantic.Attr.POSITION, 4, GL_FLOAT, false, 0, 0);
gl4.glBindBuffer(GL_ARRAY_BUFFER, 0);

Expand All @@ -158,10 +168,9 @@ private boolean initVertexArray(GL4 gl4) {
checkError(gl4, "initVertexArray 1");

// Build a vertex array object
gl4.glGenVertexArrays(1, feedbackVertexArrayName, 0);
gl4.glBindVertexArray(feedbackVertexArrayName[0]);
gl4.glBindVertexArray(vertexArrayName.get(VertexArray.FEEDBACK));
{
gl4.glBindBuffer(GL_ARRAY_BUFFER, feedbackArrayBufferName[0]);
gl4.glBindBuffer(GL_ARRAY_BUFFER, bufferName.get(Buffer.FEEDBACK));
gl4.glVertexAttribPointer(Semantic.Attr.POSITION, 4, GL_FLOAT, false, 2 * Vec4.SIZE, 0);
gl4.glVertexAttribPointer(Semantic.Attr.COLOR, 4, GL_FLOAT, false, 2 * Vec4.SIZE, Vec4.SIZE);
gl4.glBindBuffer(GL_ARRAY_BUFFER, 0);
Expand All @@ -177,38 +186,39 @@ private boolean initVertexArray(GL4 gl4) {
private boolean initFeedback(GL4 gl4) {

// Generate a buffer object
gl4.glGenTransformFeedbacks(1, feedbackName, 0);
gl4.glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, feedbackName[0]);
gl4.glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, feedbackArrayBufferName[0]);
gl4.glGenTransformFeedbacks(1, feedbackName);
gl4.glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, feedbackName.get(0));
gl4.glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, bufferName.get(Buffer.FEEDBACK));
gl4.glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, 0);

return checkError(gl4, "initFeedback");
}

private boolean initBuffer(GL4 gl4) {

gl4.glGenBuffers(Buffer.MAX, bufferName, 0);
IntBuffer uniformBufferOffset = GLBuffers.newDirectIntBuffer(1);
FloatBuffer positionBuffer = GLBuffers.newDirectFloatBuffer(positionData);

gl4.glGenBuffers(Buffer.MAX, bufferName);

int[] uniformBufferOffset = {0};
gl4.glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, uniformBufferOffset, 0);
int uniformBlockSize = Math.max(Mat4.SIZE, uniformBufferOffset[0]);
gl4.glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, uniformBufferOffset);
int uniformBlockSize = Math.max(Mat4.SIZE, uniformBufferOffset.get(0));

gl4.glBindBuffer(GL_UNIFORM_BUFFER, bufferName[Buffer.TRANSFORM]);
gl4.glBindBuffer(GL_UNIFORM_BUFFER, bufferName.get(Buffer.TRANSFORM0));
gl4.glBufferData(GL_UNIFORM_BUFFER, uniformBlockSize, null, GL_DYNAMIC_DRAW);
gl4.glBindBuffer(GL_UNIFORM_BUFFER, 0);

gl4.glGenBuffers(1, transformArrayBufferName, 0);
gl4.glBindBuffer(GL_ARRAY_BUFFER, transformArrayBufferName[0]);
FloatBuffer positionBuffer = GLBuffers.newDirectFloatBuffer(positionData);
gl4.glBindBuffer(GL_ARRAY_BUFFER, bufferName.get(Buffer.TRANSFORM_ARRAY));
gl4.glBufferData(GL_ARRAY_BUFFER, positionSize, positionBuffer, GL_STATIC_DRAW);
BufferUtils.destroyDirectBuffer(positionBuffer);
gl4.glBindBuffer(GL_ARRAY_BUFFER, 0);

gl4.glGenBuffers(1, feedbackArrayBufferName, 0);
gl4.glBindBuffer(GL_ARRAY_BUFFER, feedbackArrayBufferName[0]);
gl4.glBindBuffer(GL_ARRAY_BUFFER, bufferName.get(Buffer.FEEDBACK));
gl4.glBufferData(GL_ARRAY_BUFFER, 2 * Vec4.SIZE * vertexCount, null, GL_STATIC_DRAW);
gl4.glBindBuffer(GL_ARRAY_BUFFER, 0);

BufferUtils.destroyDirectBuffer(uniformBufferOffset);
BufferUtils.destroyDirectBuffer(positionBuffer);

return checkError(gl4, "initArrayBuffer");
}

Expand All @@ -219,7 +229,7 @@ protected boolean render(GL gl) {

// Compute the MVP (Model View Projection matrix)
{
gl4.glBindBuffer(GL_UNIFORM_BUFFER, bufferName[Buffer.TRANSFORM]);
gl4.glBindBuffer(GL_UNIFORM_BUFFER, bufferName.get(Buffer.TRANSFORM0));
ByteBuffer pointer = gl4.glMapBufferRange(GL_UNIFORM_BUFFER, 0,
Mat4.SIZE, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);

Expand All @@ -235,18 +245,18 @@ protected boolean render(GL gl) {
gl4.glViewport(0, 0, windowSize.x, windowSize.y);

// Clear color buffer
gl4.glClearBufferfv(GL_COLOR, 0, new float[]{0.0f, 0.0f, 0.0f, 1.0f}, 0);
gl4.glClearBufferfv(GL_COLOR, 0, clearColor.put(0, 0).put(1, 0).put(2, 0).put(3, 1));

// First draw, capture the attributes
// Disable rasterisation, vertices processing only!
gl4.glEnable(GL_RASTERIZER_DISCARD);

gl4.glUseProgram(transformProgramName);

gl4.glBindVertexArray(transformVertexArrayName[0]);
gl4.glBindBufferBase(GL_UNIFORM_BUFFER, Semantic.Uniform.TRANSFORM0, bufferName[Buffer.TRANSFORM]);
gl4.glBindVertexArray(vertexArrayName.get(VertexArray.TRANSFORM_FEEDBACK));
gl4.glBindBufferBase(GL_UNIFORM_BUFFER, Semantic.Uniform.TRANSFORM0, bufferName.get(Buffer.TRANSFORM0));

gl4.glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, feedbackName[0]);
gl4.glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, feedbackName.get(0));
gl4.glBeginTransformFeedback(GL_TRIANGLES);
gl4.glDrawArraysInstanced(GL_TRIANGLES, 0, vertexCount, 1);
gl4.glEndTransformFeedback();
Expand All @@ -257,8 +267,8 @@ protected boolean render(GL gl) {
// Second draw, reuse the captured attributes
gl4.glUseProgram(feedbackProgramName);

gl4.glBindVertexArray(feedbackVertexArrayName[0]);
gl4.glDrawTransformFeedback(GL_TRIANGLES, feedbackName[0]);
gl4.glBindVertexArray(vertexArrayName.get(VertexArray.FEEDBACK));
gl4.glDrawTransformFeedback(GL_TRIANGLES, feedbackName.get(0));

return true;
}
Expand All @@ -268,15 +278,15 @@ protected boolean end(GL gl) {

GL4 gl4 = (GL4) gl;

gl4.glDeleteVertexArrays(1, transformVertexArrayName, 0);
gl4.glDeleteBuffers(1, transformArrayBufferName, 0);
gl4.glDeleteVertexArrays(VertexArray.MAX, vertexArrayName);
gl4.glDeleteBuffers(Buffer.MAX, bufferName);
gl4.glDeleteProgram(transformProgramName);

gl4.glDeleteVertexArrays(1, feedbackVertexArrayName, 0);
gl4.glDeleteBuffers(1, feedbackArrayBufferName, 0);
gl4.glDeleteProgram(feedbackProgramName);
gl4.glDeleteTransformFeedbacks(1, feedbackName);

gl4.glDeleteTransformFeedbacks(1, feedbackName, 0);
BufferUtils.destroyDirectBuffer(vertexArrayName);
BufferUtils.destroyDirectBuffer(bufferName);
BufferUtils.destroyDirectBuffer(feedbackName);

return checkError(gl4, "end");
}
Expand Down
Loading

0 comments on commit 250688c

Please sign in to comment.