Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added in vertex buffer code to draw triangle using vbo

  • Loading branch information...
commit 1624586c3df0811b9eca60a6e7d5cdbfa007894a 1 parent 4a0f42d
@alihelmy authored
View
132 LessonTwo/com/onehandstand/lwjgltutorial/lessontwo/ShaderTriangle.java
@@ -1,69 +1,137 @@
package com.onehandstand.lwjgltutorial.lessontwo;
+import static org.lwjgl.opengl.ARBBufferObject.GL_STATIC_DRAW_ARB;
+import static org.lwjgl.opengl.ARBBufferObject.GL_WRITE_ONLY_ARB;
+import static org.lwjgl.opengl.ARBBufferObject.glBindBufferARB;
+import static org.lwjgl.opengl.ARBBufferObject.glBufferDataARB;
+import static org.lwjgl.opengl.ARBBufferObject.glGenBuffersARB;
+import static org.lwjgl.opengl.ARBBufferObject.glMapBufferARB;
+import static org.lwjgl.opengl.ARBBufferObject.glUnmapBufferARB;
import static org.lwjgl.opengl.ARBShaderObjects.glAttachObjectARB;
import static org.lwjgl.opengl.ARBShaderObjects.glCreateProgramObjectARB;
import static org.lwjgl.opengl.ARBShaderObjects.glLinkProgramARB;
import static org.lwjgl.opengl.ARBShaderObjects.glUseProgramObjectARB;
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.glBegin;
-import static org.lwjgl.opengl.GL11.glColor3f;
-import static org.lwjgl.opengl.GL11.glEnd;
+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.glDrawElements;
+import static org.lwjgl.opengl.GL11.glEnableClientState;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
-import static org.lwjgl.opengl.GL11.glVertex2f;
+import static org.lwjgl.opengl.GL11.glVertexPointer;
+
+import java.nio.ByteBuffer;
public class ShaderTriangle {
private int shader, vertexShader, fragmentShader;
private boolean useShaders;
-
+ private int vboVertexAttributes, vboVertexIndices;
+ private int vboVertexAttributesSize, vboVertexIndicesSize;
+ private final int numVertices=3, numAxisPerVertex=2;
+
public ShaderTriangle() {
- this.shader=0;
- this.vertexShader=0;
- this.fragmentShader=0;
- this.useShaders=false;
+ this.shader = 0;
+ this.vertexShader = 0;
+ this.fragmentShader = 0;
+ this.useShaders = false;
this.initShaders();
+ this.initBuffers();
+ }
+
+ private void initBuffers() {
+ this.vboVertexAttributes=glGenBuffersARB();
+ this.vboVertexIndices=glGenBuffersARB();
+ int bytesPerFloat=Float.SIZE/8;
+ int bytesPerInt=Integer.SIZE/8;
+ vboVertexAttributesSize=bytesPerFloat*numVertices*numAxisPerVertex;
+ vboVertexIndicesSize=bytesPerInt*numVertices;
+
+ // set size of vertex attributes buffer
+ glBindBufferARB(GL_ARRAY_BUFFER_ARB, this.vboVertexAttributes);
+ glBufferDataARB(GL_ARRAY_BUFFER_ARB, vboVertexAttributesSize, GL_STATIC_DRAW_ARB);
+ ByteBuffer vertexPositionAttributes = glMapBufferARB(
+ GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB,
+ vboVertexAttributesSize, null);
+ //put int vertex position data
+ vertexPositionAttributes.putFloat(0.0f);
+ vertexPositionAttributes.putFloat(0.8f);
+ vertexPositionAttributes.putFloat(0.8f);
+ vertexPositionAttributes.putFloat(-0.8f);
+ vertexPositionAttributes.putFloat(-0.8f);
+ vertexPositionAttributes.putFloat(-0.8f);
+ //flip buffer, unmap and unbind
+ vertexPositionAttributes.flip();
+ glUnmapBufferARB(GL_ARRAY_BUFFER_ARB);
+ glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
+
+ // 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);
+ ByteBuffer vertexIndexes = glMapBufferARB(
+ GL_ELEMENT_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB,
+ vboVertexIndicesSize, null);
+ //put in vertex index data
+ vertexIndexes.putInt(0);
+ vertexIndexes.putInt(1);
+ vertexIndexes.putInt(2);
+ //flip buffer, unmap and unbind
+ vertexIndexes.flip();
+ glUnmapBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB);
+ glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
}
private void initShaders() {
- //create the main shader program
- this.shader=glCreateProgramObjectARB();
- if(this.shader!=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
+ // create the main shader program
+ this.shader = glCreateProgramObjectARB();
+ if (this.shader != 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);
- this.useShaders=true;
+ this.useShaders = true;
}
}
}
-
+
public void draw() {
- if(useShaders) { //use the shader linked
+ if (useShaders) { // use the shader linked
glUseProgramObjectARB(this.shader);
}
-
glLoadIdentity();
- //red, which will show if our fragment shader doesn't work
- glColor3f(1.0f, 0.0f, 0.0f);
-
drawVertices();
-
- //free up use of shader program
glUseProgramObjectARB(0);
}
protected void drawVertices() {
- //drawn at 0.0 in z axis, so wont show if vertex shader doesn't work
- glBegin(GL_TRIANGLES);
- glVertex2f(0.0f, 0.8f);
- glVertex2f(-0.8f, -0.8f);
- glVertex2f(0.8f, -0.8f);
- glEnd();
+ //drawing code through buffers
+ glEnableClientState(GL_VERTEX_ARRAY);
+
+ //bind the buffers
+ glBindBufferARB(GL_ARRAY_BUFFER_ARB, this.vboVertexAttributes);
+ glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, this.vboVertexIndices);
+
+ //get vertex position data
+ glVertexPointer(this.numAxisPerVertex, GL_FLOAT, this.vboVertexAttributesSize, 0);
+
+ //draw the vertices using the indices
+ glDrawElements(GL_TRIANGLES, 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);
}
-
+
}
View
2  LessonTwo/com/onehandstand/lwjgltutorial/lessontwo/ShaderUtilities.java
@@ -37,7 +37,7 @@ public static int createShader(String shaderFileName, int shaderType) {
String shaderFilePath=SHADERS_ROOT_PATH+File.separator+shaderFileName;
int newShader=glCreateShaderObjectARB(shaderType);
if(newShader!=0) {
- String shaderCode=readInShaderCodeFromShaderFile(shaderFileName);
+ String shaderCode=readInShaderCodeFromShaderFile(shaderFilePath);
if(shaderCode!=null && !shaderCode.isEmpty()) {
glShaderSourceARB(newShader, shaderCode);
glCompileShaderARB(newShader);
Please sign in to comment.
Something went wrong with that request. Please try again.