Skip to content
Browse files

added in colouring attribute to shaders

  • Loading branch information...
1 parent 335b2ce commit d149ae342c38e913640bc563330e7878c8ddecd9 @alihelmy committed Oct 11, 2011
View
50 LessonThree/com/onehandstand/lwjgltutorial/lessonthree/ShaderTriangle.java
@@ -14,34 +14,35 @@
import static org.lwjgl.opengl.ARBShaderObjects.glValidateProgramARB;
import static org.lwjgl.opengl.ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB;
import static org.lwjgl.opengl.ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB;
-import static org.lwjgl.opengl.GL11.GL_FLOAT;
-import static org.lwjgl.opengl.GL11.GL_TRIANGLES;
-import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT;
-import static org.lwjgl.opengl.GL11.GL_VERTEX_ARRAY;
-import static org.lwjgl.opengl.GL11.glDisableClientState;
-import static org.lwjgl.opengl.GL11.glEnableClientState;
-import static org.lwjgl.opengl.GL11.glLoadIdentity;
-import static org.lwjgl.opengl.GL11.glVertexPointer;
-import static org.lwjgl.opengl.GL12.glDrawRangeElements;
import java.nio.ByteBuffer;
-import org.lwjgl.opengl.GL11;
+import static org.lwjgl.opengl.GL11.*;
+import static org.lwjgl.opengl.GL12.*;
+import static org.lwjgl.opengl.GL20.*;
public class ShaderTriangle {
- private int shader, vertexShader, fragmentShader;
+ private int shaderProgram, vertexShader, fragmentShader;
private boolean useShaders;
private int vboVertexAttributes, vboVertexIndices, vboVertexColours;
private int vboVertexAttributesSize, vboVertexIndicesSize, vboVertexColoursSize;
private final int numVertices=3, numAxisPerVertex=2, numColoursPerVertex=3;
+ private int attributeVColor;
public ShaderTriangle() {
- this.shader = 0;
+ this.shaderProgram = 0;
this.vertexShader = 0;
this.fragmentShader = 0;
this.useShaders = false;
this.initShaders();
this.initBuffers();
+
+ initShaderAttributes();
+ }
+
+ private void initShaderAttributes() {
+ String attributeName="v_color";
+ this.attributeVColor=glGetAttribLocation(this.shaderProgram, attributeName);
}
private void initBuffers() {
@@ -84,9 +85,9 @@ private void setupVertexColoursBuffer() {
vboVertexColoursSize, null);
//put int vertex position data
float[] vertexColoursData=new float[] {
- 1.0f, 1.0f, 0.0f, //v1
+ 1.0f, 1.0f, 0.0f, //v1
0.0f, 0.0f, 1.0f, //v2
- 1.0f, 0.0f, 0.0f //v3
+ 1.0f, 0.0f, 0.0f, //v3
};
vertexColours.asFloatBuffer().put(vertexColoursData);
//flip buffer, unmap and unbind
@@ -117,26 +118,26 @@ private void setupVertexPositionBuffer() {
private void initShaders() {
// create the main shader program
- this.shader = glCreateProgramObjectARB();
- if (this.shader != 0) { // if passed, create sub shaders
+ this.shaderProgram = glCreateProgramObjectARB();
+ if (this.shaderProgram != 0) { // if passed, create sub shaders
this.vertexShader = ShaderUtilities
.createVertexShader("screen.vert");
this.fragmentShader = ShaderUtilities
.createFragmentShader("screen.frag");
if (this.vertexShader != 0 && this.fragmentShader != 0) {
// add in shaders to main program
- glAttachObjectARB(this.shader, this.vertexShader);
- glAttachObjectARB(this.shader, this.fragmentShader);
- glLinkProgramARB(this.shader);
- glValidateProgramARB(this.shader);
+ glAttachObjectARB(this.shaderProgram, this.vertexShader);
+ glAttachObjectARB(this.shaderProgram, this.fragmentShader);
+ glLinkProgramARB(this.shaderProgram);
+ glValidateProgramARB(this.shaderProgram);
this.useShaders = true;
}
}
}
public void draw() {
if (useShaders) { // use the shader linked
- glUseProgramObjectARB(this.shader);
+ glUseProgramObjectARB(this.shaderProgram);
}
glLoadIdentity();
drawVertices();
@@ -149,6 +150,11 @@ protected void drawVertices() { //drawing code through buffers
glBindBufferARB(GL_ARRAY_BUFFER_ARB, this.vboVertexAttributes);
glVertexPointer(this.numAxisPerVertex, GL_FLOAT, 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);
+
//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);
@@ -158,6 +164,8 @@ protected void drawVertices() { //drawing code through buffers
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
glDisableClientState(GL_VERTEX_ARRAY);
+
+ glDisableVertexAttribArray(this.attributeVColor);
}
}
View
7 LessonThree/shaders/screen.frag
@@ -1,6 +1,7 @@
#version 120
+
+varying vec3 f_color;
+
void main(void) {
- gl_FragColor[0] = 0.0;
- gl_FragColor[1] = 0.0;
- gl_FragColor[2] = 1.0;
+ gl_FragColor = vec4(f_color.x, f_color.y, f_color.z, 1.0);
}
View
5 LessonThree/shaders/screen.vert
@@ -1,5 +1,10 @@
#version 120
+
attribute vec2 coord2d;
+attribute vec3 v_color;
+varying vec3 f_color;
+
void main(void) {
gl_Position = vec4(coord2d, 0.0, 1.0);
+ f_color = v_color;
}

0 comments on commit d149ae3

Please sign in to comment.
Something went wrong with that request. Please try again.