Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added in uniform fade and its sin wave loop

  • Loading branch information...
commit 5dfc10d1c629f75ac31a9491762c14993f904c54 1 parent cf71c58
@alihelmy authored
View
4 LessonThree/com/onehandstand/lwjgltutorial/lessonthree/Main.java
@@ -25,6 +25,7 @@
import static org.lwjgl.opengl.GL11.glViewport;
import org.lwjgl.LWJGLException;
+import org.lwjgl.Sys;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.util.glu.GLU;
@@ -49,8 +50,11 @@ public void start() {
// main loop
while (!Display.isCloseRequested()) {
+ long timeElapsed=Sys.getTime();
+ triangle.update(timeElapsed);
this.render();
Display.update();
+ Display.sync(60);
}
// close down
View
91 LessonThree/com/onehandstand/lwjgltutorial/lessonthree/ShaderTriangle.java
@@ -21,6 +21,8 @@
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.glGetUniformLocation;
+import static org.lwjgl.opengl.GL20.glUniform1f;
import static org.lwjgl.opengl.GL20.glVertexAttribPointer;
import java.nio.ByteBuffer;
@@ -29,13 +31,14 @@
private int shaderProgram, vertexShader, fragmentShader;
private boolean useShaders;
- private int vertexPositionsBuffer, vertexIndicesBuffer,
- vertexColoursBuffer;
- private int vertexPositionsBufferSize, vertexIndicesBufferSize,
- vertexColoursBufferSize;
+ private int vertexAttributesBuffer, vertexIndicesBuffer;
+ private int vertexAttributesBufferSize, vertexIndicesBufferSize;
private final int numVertices=3, numAxisPerVertex=2, numColoursPerVertex=3;
- private int vColorAttributeIndex, coord2DAttributeIndex;
+ private int vColorAttributeIndex, coord2DAttributeIndex,
+ fadeAttributeIndex;
private final int bytesPerFloat=Float.SIZE/8, bytesPerInt=Integer.SIZE/8;
+ private int stride;
+ private float currentFade;
public ShaderTriangle() {
shaderProgram=0;
@@ -46,6 +49,10 @@ public ShaderTriangle() {
initShaderAttributes();
initBuffers();
}
+
+ public void update(long timeElapsed) {
+ currentFade=(float)(Math.sin(timeElapsed / 1000.0 * (2*3.14) / 5) / 2 + 0.5);
+ }
private void initShaderAttributes() {
String coord2dAttributeName="coord2D";
@@ -54,11 +61,12 @@ private void initShaderAttributes() {
String vColorAttributeName="vColor";
vColorAttributeIndex=glGetAttribLocation(shaderProgram,
vColorAttributeName);
+ String fadeAttributeName="fade";
+ fadeAttributeIndex=glGetUniformLocation(shaderProgram, fadeAttributeName);
}
private void initBuffers() {
- setupVertexPositionBuffer();
- setupVertexColoursBuffer();
+ setupVertexAttributesBuffer();
setupVertexIndecesBuffer();
}
@@ -82,49 +90,28 @@ private void setupVertexIndecesBuffer() {
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
}
- private void setupVertexColoursBuffer() {
- vertexColoursBuffer=glGenBuffersARB();
- 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
- 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
- };
- vertexColours.asFloatBuffer().put(vertexColoursData);
- // flip buffer, unmap and unbind
- vertexColours.flip();
- glUnmapBufferARB(GL_ARRAY_BUFFER_ARB);
- glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
- }
-
- private void setupVertexPositionBuffer() {
- vertexPositionsBuffer=glGenBuffersARB();
- vertexPositionsBufferSize=bytesPerFloat*numVertices*numAxisPerVertex;
+ private void setupVertexAttributesBuffer() {
+ vertexAttributesBuffer=glGenBuffersARB();
+ vertexAttributesBufferSize=bytesPerFloat*numVertices
+ *(numAxisPerVertex+numColoursPerVertex);
// set size of vertex attributes buffer
- glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexPositionsBuffer);
- glBufferDataARB(GL_ARRAY_BUFFER_ARB, vertexPositionsBufferSize,
+ glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexAttributesBuffer);
+ glBufferDataARB(GL_ARRAY_BUFFER_ARB, vertexAttributesBufferSize,
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
+ GL_WRITE_ONLY_ARB, vertexAttributesBufferSize, null);
+ // put int vertex position & color data
+ float[] vertexAttributesData=new float[] {
+ 0.0f, 0.8f, 1.0f, 1.0f, 0.0f, 0.8f, -0.8f, 0.0f, 0.0f, 1.0f,
+ -0.8f, -0.8f, 1.0f, 0.0f, 0.0f
};
- vertexPositionAttributes.asFloatBuffer().put(vertexPositionsData);
+ vertexPositionAttributes.asFloatBuffer().put(vertexAttributesData);
// flip buffer, unmap and unbind
vertexPositionAttributes.flip();
glUnmapBufferARB(GL_ARRAY_BUFFER_ARB);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
+ // setup the stride and color offset
+ stride=(numAxisPerVertex+numColoursPerVertex)*bytesPerFloat;
}
private void initShaders() {
@@ -148,18 +135,22 @@ public void draw() {
if (useShaders) { // use the shader linked
glUseProgramObjectARB(shaderProgram);
}
- // bind vertex array
- glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexPositionsBuffer);
- // pass in coords info to vertex shader
+ // bind vertex data array
+ glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexAttributesBuffer);
+
+ //uniform shader attributes
+ glUniform1f(fadeAttributeIndex, currentFade);
+
+ // pass in info to vertex shader
+ int dataOffset=0;
glEnableVertexAttribArray(coord2DAttributeIndex);
glVertexAttribPointer(coord2DAttributeIndex, numAxisPerVertex,
- GL_FLOAT, false, 0, 0);
-
- // bind the colour attribute
+ GL_FLOAT, false, stride, 0);
+ dataOffset+=numAxisPerVertex*bytesPerFloat;
glEnableVertexAttribArray(vColorAttributeIndex);
- glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexColoursBuffer);
glVertexAttribPointer(vColorAttributeIndex, numColoursPerVertex,
- GL_FLOAT, false, 0, 0);
+ GL_FLOAT, false, stride, dataOffset);
+ dataOffset+=numColoursPerVertex*bytesPerFloat;
// draw the vertices using the indices
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vertexIndicesBuffer);
View
3  LessonThree/shaders/screen.frag
@@ -1,7 +1,8 @@
#version 120
varying vec3 fColor;
+uniform float fade;
void main(void) {
- gl_FragColor = vec4(fColor.x, fColor.y, fColor.z, 1.0);
+ gl_FragColor = vec4(fColor.x, fColor.y, fColor.z, fade);
}
View
1  LessonThree/shaders/screen.vert
@@ -3,6 +3,7 @@
attribute vec2 coord2D;
attribute vec3 vColor;
varying vec3 fColor;
+uniform float fade;
void main(void) {
gl_Position = vec4(coord2D, 0.0, 1.0);
Please sign in to comment.
Something went wrong with that request. Please try again.