strange shader behavior
in
Programming Questions
•
6 months ago
I'm am getting very odd behavior with this program
- import javax.media.opengl.*;
- import java.nio.*;
- GL2 gl;
- PGraphicsOpenGL pgl;
- PShader myShader;
- int transformLoc;
- int vertexLoc;
- int colorLoc;
- int junkLoc;
- int vboHandle;
- int iboHandle;
- int vaoHandle;
- int numVertices;
- int numIndices;
- void setup() {
- size(640, 360, P3D);
- noStroke();
- myShader = loadShader("Frag.glsl", "Vert.glsl");
- myShader.bind();
- myShader.unbind();
- pgl = (PGraphicsOpenGL) g;
- gl = pgl.beginPGL().gl.getGL2();
- transformLoc = gl.glGetUniformLocation(myShader.glProgram, "transform");
- vertexLoc = gl.glGetAttribLocation(myShader.glProgram, "vertex");
- colorLoc = gl.glGetAttribLocation(myShader.glProgram, "color");
- junkLoc = gl.glGetAttribLocation(myShader.glProgram, "junk");
- float[] fVertices = new float[] {-100,-100,0, 0,100,0, 100,-100,0};
- FloatBuffer vertices = allocateFloatBuffer(fVertices.length);
- vertices.put(fVertices);
- vertices.rewind();
- vboHandle = genBuffer();
- gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vboHandle);
- gl.glBufferData(GL2.GL_ARRAY_BUFFER, fVertices.length * (Float.SIZE/8), vertices, GL2.GL_STATIC_DRAW);
- gl.glUseProgram(myShader.glProgram);
- gl.glEnableVertexAttribArray(vertexLoc);
- gl.glEnableVertexAttribArray(colorLoc);
- gl.glEnableVertexAttribArray(0);
- vaoHandle = genVertexArray();
- gl.glBindVertexArray(vaoHandle);
- gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vboHandle);
- gl.glVertexAttribPointer(vertexLoc, 2, GL2.GL_FLOAT, false, 0, 0);
- pgl.endPGL();
- }
- void draw() {
- clear();
- translate(width/2.0, height/2.0);
- pgl.beginPGL();
- gl.glUseProgram(myShader.glProgram);
- gl.glEnableVertexAttribArray(vertexLoc);
- gl.glEnableVertexAttribArray(colorLoc);
- gl.glEnableVertexAttribArray(junkLoc);
- gl.glUniformMatrix4fv(transformLoc, 1, false, glMatrix(pgl.projmodelview));
- float[] fColors = new float[] {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1};
- FloatBuffer colors = allocateFloatBuffer(fColors.length);
- colors.put(fColors);
- colors.rewind();
- gl.glVertexAttribPointer(colorLoc, 4, GL2.GL_FLOAT, false, 0, colors);
- gl.glDrawArrays(GL2.GL_TRIANGLES, 0, 3);
- pgl.endPGL();
- }
- FloatBuffer glMatrix(PMatrix3D mat)
- {
- FloatBuffer result = allocateFloatBuffer(16);
- result.put(mat.m00);
- result.put(mat.m10);
- result.put(mat.m20);
- result.put(mat.m30);
- result.put(mat.m01);
- result.put(mat.m11);
- result.put(mat.m21);
- result.put(mat.m31);
- result.put(mat.m02);
- result.put(mat.m12);
- result.put(mat.m22);
- result.put(mat.m32);
- result.put(mat.m03);
- result.put(mat.m13);
- result.put(mat.m23);
- result.put(mat.m33);
- result.rewind();
- return result;
- }
- FloatBuffer allocateFloatBuffer(int capacity)
- {
- return ByteBuffer.allocateDirect(capacity * (Float.SIZE/8)).order(ByteOrder.nativeOrder()).asFloatBuffer();
- }
- int genBuffer()
- {
- int[] buffer = new int[1];
- gl.glGenBuffers(1, buffer, 0);
- return buffer[0];
- }
- int genVertexArray()
- {
- int[] buffer = new int[1];
- gl.glGenVertexArrays(1, buffer, 0);
- return buffer[0];
- }
And this shader
- #define PROCESSING_COLOR_SHADER
- uniform mat4 transform;
- attribute vec4 vertex;
- attribute vec4 color;
- attribute float junk;
- varying vec4 vertColor;
- void main() {
- float junk1 = junk;
- vertColor = color;
- gl_Position = transform * vertex;
- }
- #ifdef GL_ES
- precision mediump float;
- precision mediump int;
- #endif
- varying vec4 vertColor;
- void main() {
- gl_FragColor = vertColor;
- }
junk attribute in the vertice shader will render a single pixel in the middle of my rendering. If i rename it to unused it works fine. If i completely remove it I get a single pixel again. As you can see there is zero dependency on this attribute in my program. Am i missing something??
1