Permalink
Browse files

now we pass vertex coords to shader correctly

  • Loading branch information...
1 parent d149ae3 commit f8f0e3777e842a6f4ec7a91e24b351c1a7f340db @alihelmy committed Oct 11, 2011
@@ -16,6 +16,8 @@
import static org.lwjgl.opengl.ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB;
import java.nio.ByteBuffer;
+import org.lwjgl.opengl.GL20;
+
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL12.*;
import static org.lwjgl.opengl.GL20.*;
@@ -24,49 +26,44 @@
private int shaderProgram, vertexShader, fragmentShader;
private boolean useShaders;
- private int vboVertexAttributes, vboVertexIndices, vboVertexColours;
- private int vboVertexAttributesSize, vboVertexIndicesSize, vboVertexColoursSize;
+ private int vertexPositionsBuffer, vertexIndicesBuffer, vertexColoursBuffer;
+ private int vertexPositionsBufferSize, vertexIndicesBufferSize, vertexColoursBufferSize;
private final int numVertices=3, numAxisPerVertex=2, numColoursPerVertex=3;
- private int attributeVColor;
+ private int vColorAttributeIndex, coord2dAttributeIndex;
+ private final int bytesPerFloat=Float.SIZE/8, bytesPerInt=Integer.SIZE/8;
public ShaderTriangle() {
- this.shaderProgram = 0;
- this.vertexShader = 0;
- this.fragmentShader = 0;
- this.useShaders = false;
- this.initShaders();
- this.initBuffers();
-
+ shaderProgram = 0;
+ vertexShader = 0;
+ fragmentShader = 0;
+ useShaders = false;
+ initShaders();
initShaderAttributes();
+ initBuffers();
}
private void initShaderAttributes() {
- String attributeName="v_color";
- this.attributeVColor=glGetAttribLocation(this.shaderProgram, attributeName);
+ String coord2dAttributeName="coord2D";
+ coord2dAttributeIndex=glGetAttribLocation(shaderProgram, coord2dAttributeName);
+ String vColorAttributeName="vColor";
+ vColorAttributeIndex=glGetAttribLocation(shaderProgram, vColorAttributeName);
}
private void initBuffers() {
- this.vboVertexAttributes=glGenBuffersARB();
- this.vboVertexColours=glGenBuffersARB();
- this.vboVertexIndices=glGenBuffersARB();
- int bytesPerFloat=Float.SIZE/8;
- int bytesPerInt=Integer.SIZE/8;
- vboVertexAttributesSize=bytesPerFloat*numVertices*numAxisPerVertex;
- vboVertexIndicesSize=bytesPerInt*numVertices;
- vboVertexColoursSize=bytesPerFloat*numVertices*numColoursPerVertex;
-
setupVertexPositionBuffer();
setupVertexColoursBuffer();
setupVertexIndecesBuffer();
}
private void setupVertexIndecesBuffer() {
+ vertexIndicesBuffer=glGenBuffersARB();
+ vertexIndicesBufferSize=bytesPerInt*numVertices;
// set size of vertex index buffer
- glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, this.vboVertexIndices);
- glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vboVertexIndicesSize, GL_STATIC_DRAW_ARB);
+ glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vertexIndicesBuffer);
+ glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vertexIndicesBufferSize, GL_STATIC_DRAW_ARB);
ByteBuffer vertexIndeces = glMapBufferARB(
GL_ELEMENT_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB,
- vboVertexIndicesSize, null);
+ vertexIndicesBufferSize, null);
//put in vertex index data
int[] indecesData=new int[] {0, 1, 2};
vertexIndeces.asIntBuffer().put(indecesData);
@@ -77,12 +74,14 @@ private void setupVertexIndecesBuffer() {
}
private void setupVertexColoursBuffer() {
+ vertexColoursBuffer=glGenBuffersARB();
+ vertexColoursBufferSize=bytesPerFloat*numVertices*numColoursPerVertex;
// set size of vertex attributes buffer
- glBindBufferARB(GL_ARRAY_BUFFER_ARB, this.vboVertexColours);
- glBufferDataARB(GL_ARRAY_BUFFER_ARB, vboVertexColoursSize, GL_STATIC_DRAW_ARB);
+ glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexColoursBuffer);
+ glBufferDataARB(GL_ARRAY_BUFFER_ARB, vertexColoursBufferSize, GL_STATIC_DRAW_ARB);
ByteBuffer vertexColours = glMapBufferARB(
GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB,
- vboVertexColoursSize, null);
+ vertexColoursBufferSize, null);
//put int vertex position data
float[] vertexColoursData=new float[] {
1.0f, 1.0f, 0.0f, //v1
@@ -97,12 +96,14 @@ private void setupVertexColoursBuffer() {
}
private void setupVertexPositionBuffer() {
+ vertexPositionsBuffer=glGenBuffersARB();
+ vertexPositionsBufferSize=bytesPerFloat*numVertices*numAxisPerVertex;
// set size of vertex attributes buffer
- glBindBufferARB(GL_ARRAY_BUFFER_ARB, this.vboVertexAttributes);
- glBufferDataARB(GL_ARRAY_BUFFER_ARB, vboVertexAttributesSize, GL_STATIC_DRAW_ARB);
+ glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexPositionsBuffer);
+ glBufferDataARB(GL_ARRAY_BUFFER_ARB, vertexPositionsBufferSize, GL_STATIC_DRAW_ARB);
ByteBuffer vertexPositionAttributes = glMapBufferARB(
GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB,
- vboVertexAttributesSize, null);
+ vertexPositionsBufferSize, null);
//put int vertex position data
float[] vertexPositionsData=new float[] {
0.0f, 0.8f, //v1
@@ -118,54 +119,48 @@ private void setupVertexPositionBuffer() {
private void initShaders() {
// create the main shader program
- this.shaderProgram = glCreateProgramObjectARB();
- if (this.shaderProgram != 0) { // if passed, create sub shaders
- this.vertexShader = ShaderUtilities
+ shaderProgram = glCreateProgramObjectARB();
+ if (shaderProgram != 0) { // if passed, create sub shaders
+ vertexShader = ShaderUtilities
.createVertexShader("screen.vert");
- this.fragmentShader = ShaderUtilities
+ fragmentShader = ShaderUtilities
.createFragmentShader("screen.frag");
- if (this.vertexShader != 0 && this.fragmentShader != 0) {
+ if (vertexShader != 0 && fragmentShader != 0) {
// add in shaders to main program
- glAttachObjectARB(this.shaderProgram, this.vertexShader);
- glAttachObjectARB(this.shaderProgram, this.fragmentShader);
- glLinkProgramARB(this.shaderProgram);
- glValidateProgramARB(this.shaderProgram);
- this.useShaders = true;
+ glAttachObjectARB(shaderProgram, vertexShader);
+ glAttachObjectARB(shaderProgram, fragmentShader);
+ glLinkProgramARB(shaderProgram);
+ glValidateProgramARB(shaderProgram);
+ useShaders = true;
}
}
}
public void draw() {
if (useShaders) { // use the shader linked
- glUseProgramObjectARB(this.shaderProgram);
+ glUseProgramObjectARB(shaderProgram);
}
- glLoadIdentity();
- drawVertices();
- glUseProgramObjectARB(0);
- }
-
- protected void drawVertices() { //drawing code through buffers
//bind vertex array
- glEnableClientState(GL_VERTEX_ARRAY);
- glBindBufferARB(GL_ARRAY_BUFFER_ARB, this.vboVertexAttributes);
- glVertexPointer(this.numAxisPerVertex, GL_FLOAT, 0, 0);
+ glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexPositionsBuffer);
+ //pass in coords info to vertex shader
+ glEnableVertexAttribArray(coord2dAttributeIndex);
+ glVertexAttribPointer(coord2dAttributeIndex, numAxisPerVertex, GL_FLOAT, false, 0, 0);
//bind the colour attribute
- glEnableVertexAttribArray(this.attributeVColor);
- glBindBufferARB(GL_ARRAY_BUFFER_ARB, this.vboVertexColours);
- glVertexAttribPointer(this.attributeVColor, this.numColoursPerVertex, GL_FLOAT, false, 0, 0);
+ glEnableVertexAttribArray(vColorAttributeIndex);
+ glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexColoursBuffer);
+ glVertexAttribPointer(vColorAttributeIndex, numColoursPerVertex, GL_FLOAT, false, 0, 0);
//draw the vertices using the indices
- glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, this.vboVertexIndices);
- glDrawRangeElements(GL_TRIANGLES, 0, this.numVertices-1, this.numVertices, GL_UNSIGNED_INT, 0);
+ glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vertexIndicesBuffer);
+ glDrawRangeElements(GL_TRIANGLES, 0, numVertices-1, numVertices, GL_UNSIGNED_INT, 0);
//unbind the buffers
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
- glDisableClientState(GL_VERTEX_ARRAY);
-
- glDisableVertexAttribArray(this.attributeVColor);
+ glDisableVertexAttribArray(vColorAttributeIndex);
+
+ glUseProgramObjectARB(0);
}
-
}
@@ -1,7 +1,7 @@
#version 120
-varying vec3 f_color;
+varying vec3 fColor;
void main(void) {
- gl_FragColor = vec4(f_color.x, f_color.y, f_color.z, 1.0);
+ gl_FragColor = vec4(fColor.x, fColor.y, fColor.z, 1.0);
}
@@ -1,10 +1,10 @@
#version 120
-attribute vec2 coord2d;
-attribute vec3 v_color;
-varying vec3 f_color;
+attribute vec2 coord2D;
+attribute vec3 vColor;
+varying vec3 fColor;
void main(void) {
- gl_Position = vec4(coord2d, 0.0, 1.0);
- f_color = v_color;
+ gl_Position = vec4(coord2D, 0.0, 1.0);
+ fColor = vColor;
}
@@ -26,6 +26,7 @@
import java.nio.ByteBuffer;
import org.lwjgl.opengl.GL11;
+import org.lwjgl.opengl.GL20;
public class ShaderTriangle {
@@ -34,6 +35,7 @@
private int vboVertexAttributes, vboVertexIndices;
private int vboVertexAttributesSize, vboVertexIndicesSize;
private final int numVertices=3, numAxisPerVertex=2;
+ private int coord2dAttributeIndex;
public ShaderTriangle() {
this.shader = 0;
@@ -42,6 +44,9 @@ public ShaderTriangle() {
this.useShaders = false;
this.initShaders();
this.initBuffers();
+
+ String coord2dAttributeName="coord2d";
+ this.coord2dAttributeIndex=GL20.glGetAttribLocation(this.shader, coord2dAttributeName);
}
private void initBuffers() {
@@ -105,29 +110,24 @@ private void initShaders() {
}
public void draw() {
- if (useShaders) { // use the shader linked
+ if(this.useShaders) {
glUseProgramObjectARB(this.shader);
}
- glLoadIdentity();
- drawVertices();
- glUseProgramObjectARB(0);
- }
- protected void drawVertices() { //drawing code through buffers
//bind vertex array
- glEnableClientState(GL_VERTEX_ARRAY);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, this.vboVertexAttributes);
- glVertexPointer(this.numAxisPerVertex, GL_FLOAT, 0, 0);
-
+ //pass in coords info to vertex shader
+ GL20.glEnableVertexAttribArray(coord2dAttributeIndex);
+ GL20.glVertexAttribPointer(coord2dAttributeIndex, 2, GL_FLOAT, false, 0, 0);
+
//draw the vertices using the indices
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, this.vboVertexIndices);
glDrawRangeElements(GL_TRIANGLES, 0, this.numVertices-1, this.numVertices, GL_UNSIGNED_INT, 0);
-
+
//unbind the buffers
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
-
- glDisableClientState(GL_VERTEX_ARRAY);
- }
+ glUseProgramObjectARB(0);
+ }
}
@@ -1,5 +1,7 @@
#version 120
+
attribute vec2 coord2d;
+
void main(void) {
gl_Position = vec4(coord2d, 0.0, 1.0);
}

0 comments on commit f8f0e37

Please sign in to comment.