Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

formatting

  • Loading branch information...
commit 8a3ae667b89978408bfb836044e87011625138f3 1 parent ad4cd6a
@alihelmy authored
View
20 LessonThree/com/onehandstand/lwjgltutorial/lessonthree/Main.java
@@ -32,10 +32,10 @@
public class Main {
private ShaderTriangle triangle;
-
+
public void start() {
try {
- DisplayMode displayMode = new DisplayMode(640, 480);
+ DisplayMode displayMode=new DisplayMode(640, 480);
Display.setDisplayMode(displayMode);
Display.setVSyncEnabled(true);
Display.setTitle("Modern OpenGL LWJGL Port Lesson Two");
@@ -68,22 +68,22 @@ private void initWorld() {
}
private void render() {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+ glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
- //world rendering code
+ // world rendering code
this.triangle.draw();
}
private void initOpenGL() {
// setup viewing area
- DisplayMode displayMode = Display.getDisplayMode();
- int width = displayMode.getWidth();
- int height = displayMode.getHeight();
+ DisplayMode displayMode=Display.getDisplayMode();
+ int width=displayMode.getWidth();
+ int height=displayMode.getHeight();
glViewport(0, 0, width, height);
// setup the camera params
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
- GLU.gluPerspective(45.0f, (float)width / (float)height, 1.0f, 100f);
+ GLU.gluPerspective(45.0f, (float)width/(float)height, 1.0f, 100f);
// setup the model (drawing) params
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
@@ -93,13 +93,13 @@ private void initOpenGL() {
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
- //enable blending
+ // enable blending
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
public static void main(String[] args) {
- Main lessonOne = new Main();
+ Main lessonOne=new Main();
lessonOne.start();
}
View
131 LessonThree/com/onehandstand/lwjgltutorial/lessonthree/ShaderTriangle.java
@@ -14,29 +14,34 @@
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 java.nio.ByteBuffer;
-
-import org.lwjgl.opengl.GL20;
+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.GL12.glDrawRangeElements;
+import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray;
+import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray;
+import static org.lwjgl.opengl.GL20.glGetAttribLocation;
+import static org.lwjgl.opengl.GL20.glVertexAttribPointer;
-import static org.lwjgl.opengl.GL11.*;
-import static org.lwjgl.opengl.GL12.*;
-import static org.lwjgl.opengl.GL20.*;
+import java.nio.ByteBuffer;
public class ShaderTriangle {
private int shaderProgram, vertexShader, fragmentShader;
private boolean useShaders;
- private int vertexPositionsBuffer, vertexIndicesBuffer, vertexColoursBuffer;
- private int vertexPositionsBufferSize, vertexIndicesBufferSize, vertexColoursBufferSize;
+ private int vertexPositionsBuffer, vertexIndicesBuffer,
+ vertexColoursBuffer;
+ private int vertexPositionsBufferSize, vertexIndicesBufferSize,
+ vertexColoursBufferSize;
private final int numVertices=3, numAxisPerVertex=2, numColoursPerVertex=3;
private int vColorAttributeIndex, coord2DAttributeIndex;
private final int bytesPerFloat=Float.SIZE/8, bytesPerInt=Integer.SIZE/8;
public ShaderTriangle() {
- shaderProgram = 0;
- vertexShader = 0;
- fragmentShader = 0;
- useShaders = false;
+ shaderProgram=0;
+ vertexShader=0;
+ fragmentShader=0;
+ useShaders=false;
initShaders();
initShaderAttributes();
initBuffers();
@@ -44,9 +49,11 @@ public ShaderTriangle() {
private void initShaderAttributes() {
String coord2dAttributeName="coord2D";
- coord2DAttributeIndex=glGetAttribLocation(shaderProgram, coord2dAttributeName);
+ coord2DAttributeIndex=glGetAttribLocation(shaderProgram,
+ coord2dAttributeName);
String vColorAttributeName="vColor";
- vColorAttributeIndex=glGetAttribLocation(shaderProgram, vColorAttributeName);
+ vColorAttributeIndex=glGetAttribLocation(shaderProgram,
+ vColorAttributeName);
}
private void initBuffers() {
@@ -60,14 +67,16 @@ private void setupVertexIndecesBuffer() {
vertexIndicesBufferSize=bytesPerInt*numVertices;
// set size of vertex index buffer
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,
- vertexIndicesBufferSize, null);
- //put in vertex index data
- int[] indecesData=new int[] {0, 1, 2};
+ glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vertexIndicesBufferSize,
+ GL_STATIC_DRAW_ARB);
+ ByteBuffer vertexIndeces=glMapBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB,
+ GL_WRITE_ONLY_ARB, vertexIndicesBufferSize, null);
+ // put in vertex index data
+ int[] indecesData=new int[] {
+ 0, 1, 2
+ };
vertexIndeces.asIntBuffer().put(indecesData);
- //flip buffer, unmap and unbind
+ // flip buffer, unmap and unbind
vertexIndeces.flip();
glUnmapBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
@@ -78,18 +87,18 @@ private void setupVertexColoursBuffer() {
vertexColoursBufferSize=bytesPerFloat*numVertices*numColoursPerVertex;
// set size of vertex attributes buffer
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,
- vertexColoursBufferSize, null);
- //put int vertex position data
+ glBufferDataARB(GL_ARRAY_BUFFER_ARB, vertexColoursBufferSize,
+ GL_STATIC_DRAW_ARB);
+ ByteBuffer vertexColours=glMapBufferARB(GL_ARRAY_BUFFER_ARB,
+ GL_WRITE_ONLY_ARB, vertexColoursBufferSize, null);
+ // put int vertex position data
float[] vertexColoursData=new float[] {
- 1.0f, 1.0f, 0.0f, //v1
- 0.0f, 0.0f, 1.0f, //v2
- 1.0f, 0.0f, 0.0f, //v3
+ 1.0f, 1.0f, 0.0f, // v1
+ 0.0f, 0.0f, 1.0f, // v2
+ 1.0f, 0.0f, 0.0f, // v3
};
vertexColours.asFloatBuffer().put(vertexColoursData);
- //flip buffer, unmap and unbind
+ // flip buffer, unmap and unbind
vertexColours.flip();
glUnmapBufferARB(GL_ARRAY_BUFFER_ARB);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
@@ -100,18 +109,19 @@ private void setupVertexPositionBuffer() {
vertexPositionsBufferSize=bytesPerFloat*numVertices*numAxisPerVertex;
// set size of vertex attributes buffer
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,
- vertexPositionsBufferSize, null);
- //put int vertex position data
+ glBufferDataARB(GL_ARRAY_BUFFER_ARB, vertexPositionsBufferSize,
+ GL_STATIC_DRAW_ARB);
+ ByteBuffer vertexPositionAttributes=glMapBufferARB(GL_ARRAY_BUFFER_ARB,
+ GL_WRITE_ONLY_ARB, vertexPositionsBufferSize, null);
+ // put int vertex position data
float[] vertexPositionsData=new float[] {
- 0.0f, 0.8f, //v1
- 0.8f, -0.8f, //v2
- -0.8f, -0.8f //v3
+ 0.0f, 0.8f, // v1
+ 0.8f, -0.8f, // v2
+ -0.8f, -0.8f
+ // v3
};
vertexPositionAttributes.asFloatBuffer().put(vertexPositionsData);
- //flip buffer, unmap and unbind
+ // flip buffer, unmap and unbind
vertexPositionAttributes.flip();
glUnmapBufferARB(GL_ARRAY_BUFFER_ARB);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
@@ -119,19 +129,17 @@ private void setupVertexPositionBuffer() {
private void initShaders() {
// create the main shader program
- shaderProgram = glCreateProgramObjectARB();
- if (shaderProgram != 0) { // if passed, create sub shaders
- vertexShader = ShaderUtilities
- .createVertexShader("screen.vert");
- fragmentShader = ShaderUtilities
- .createFragmentShader("screen.frag");
- if (vertexShader != 0 && fragmentShader != 0) {
+ shaderProgram=glCreateProgramObjectARB();
+ if (shaderProgram!=0) { // if passed, create sub shaders
+ vertexShader=ShaderUtilities.createVertexShader("screen.vert");
+ fragmentShader=ShaderUtilities.createFragmentShader("screen.frag");
+ if (vertexShader!=0&&fragmentShader!=0) {
// add in shaders to main program
glAttachObjectARB(shaderProgram, vertexShader);
glAttachObjectARB(shaderProgram, fragmentShader);
glLinkProgramARB(shaderProgram);
glValidateProgramARB(shaderProgram);
- useShaders = true;
+ useShaders=true;
}
}
}
@@ -140,27 +148,30 @@ public void draw() {
if (useShaders) { // use the shader linked
glUseProgramObjectARB(shaderProgram);
}
- //bind vertex array
+ // bind vertex array
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexPositionsBuffer);
- //pass in coords info to vertex shader
+ // pass in coords info to vertex shader
glEnableVertexAttribArray(coord2DAttributeIndex);
- glVertexAttribPointer(coord2DAttributeIndex, numAxisPerVertex, GL_FLOAT, false, 0, 0);
-
- //bind the colour attribute
+ glVertexAttribPointer(coord2DAttributeIndex, numAxisPerVertex,
+ GL_FLOAT, false, 0, 0);
+
+ // bind the colour attribute
glEnableVertexAttribArray(vColorAttributeIndex);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexColoursBuffer);
- glVertexAttribPointer(vColorAttributeIndex, numColoursPerVertex, GL_FLOAT, false, 0, 0);
-
- //draw the vertices using the indices
+ glVertexAttribPointer(vColorAttributeIndex, numColoursPerVertex,
+ GL_FLOAT, false, 0, 0);
+
+ // draw the vertices using the indices
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vertexIndicesBuffer);
- glDrawRangeElements(GL_TRIANGLES, 0, numVertices-1, numVertices, GL_UNSIGNED_INT, 0);
-
- //unbind the buffers
+ 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);
-
+
glDisableVertexAttribArray(vColorAttributeIndex);
-
+
glUseProgramObjectARB(0);
}
}
View
48 LessonThree/com/onehandstand/lwjgltutorial/lessonthree/ShaderUtilities.java
@@ -19,43 +19,46 @@
import org.lwjgl.opengl.ARBVertexShader;
public class ShaderUtilities {
-
+
private static boolean verboseValidityCheck=false;
private static final String SHADERS_ROOT_PATH="shaders";
public static int createFragmentShader(String pathToShaderFile) {
- int fragmentShader=createShader(pathToShaderFile, ARBFragmentShader.GL_FRAGMENT_SHADER_ARB);
+ int fragmentShader=createShader(pathToShaderFile,
+ ARBFragmentShader.GL_FRAGMENT_SHADER_ARB);
return fragmentShader;
}
public static int createVertexShader(String pathToShaderFile) {
- int vertexShader=createShader(pathToShaderFile, ARBVertexShader.GL_VERTEX_SHADER_ARB);
+ int vertexShader=createShader(pathToShaderFile,
+ ARBVertexShader.GL_VERTEX_SHADER_ARB);
return vertexShader;
}
public static int createShader(String shaderFileName, int shaderType) {
String shaderFilePath=SHADERS_ROOT_PATH+File.separator+shaderFileName;
int newShader=glCreateShaderObjectARB(shaderType);
- if(newShader!=0) {
+ if (newShader!=0) {
String shaderCode=readInShaderCodeFromShaderFile(shaderFilePath);
- if(shaderCode!=null && !shaderCode.isEmpty()) {
+ if (shaderCode!=null&&!shaderCode.isEmpty()) {
glShaderSourceARB(newShader, shaderCode);
glCompileShaderARB(newShader);
- if(isShaderValid(newShader)) {
+ if (isShaderValid(newShader)) {
return newShader;
}
}
}
- //if anything fails, return 0 as error code
+ // if anything fails, return 0 as error code
return 0;
}
-
+
private static String readInShaderCodeFromShaderFile(String pathToShaderFile) {
try {
- BufferedReader bufferedReader=new BufferedReader(new FileReader(pathToShaderFile));
+ BufferedReader bufferedReader=new BufferedReader(new FileReader(
+ pathToShaderFile));
StringBuffer shaderCodeBuffer=new StringBuffer();
String codeLine=bufferedReader.readLine();
- while(codeLine!=null) {
+ while (codeLine!=null) {
shaderCodeBuffer.append(codeLine+"\n");
codeLine=bufferedReader.readLine();
}
@@ -66,29 +69,30 @@ private static String readInShaderCodeFromShaderFile(String pathToShaderFile) {
return null;
}
}
-
+
public static boolean isShaderValid(int shaderToCheck) {
- IntBuffer iVal = BufferUtils.createIntBuffer(1);
- glGetObjectParameterARB(shaderToCheck, GL_OBJECT_INFO_LOG_LENGTH_ARB, iVal);
- int length = iVal.get();
- boolean isValid = length>1;
-
- if (isValid && verboseValidityCheck) {
+ IntBuffer iVal=BufferUtils.createIntBuffer(1);
+ glGetObjectParameterARB(shaderToCheck, GL_OBJECT_INFO_LOG_LENGTH_ARB,
+ iVal);
+ int length=iVal.get();
+ boolean isValid=length>1;
+
+ if (isValid&&verboseValidityCheck) {
printShaderLogInfo(shaderToCheck, iVal, length);
}
-
+
return isValid;
}
private static void printShaderLogInfo(int shaderToCheck, IntBuffer iVal,
int length) {
- ByteBuffer infoLog = BufferUtils.createByteBuffer(length);
+ ByteBuffer infoLog=BufferUtils.createByteBuffer(length);
iVal.flip();
glGetInfoLogARB(shaderToCheck, iVal, infoLog);
- byte[] infoBytes = new byte[length];
+ byte[] infoBytes=new byte[length];
infoLog.get(infoBytes);
- String out = new String(infoBytes);
+ String out=new String(infoBytes);
System.out.println("Info log:\n"+out);
}
-
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.