Skip to content
Browse files

prelim work for getting textures in

  • Loading branch information...
1 parent 15eff27 commit a3805769a7a9c68f0b2ef0366bf8b05770af2ef6 @alihelmy committed
View
119 LessonSix/com/onehandstand/lwjgltutorial/lessonsix/Cube.java
@@ -17,24 +17,34 @@
import static org.lwjgl.opengl.GL20.glGetAttribLocation;
import static org.lwjgl.opengl.GL20.glVertexAttribPointer;
+import java.io.File;
+import java.io.IOException;
import java.nio.ByteBuffer;
+import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.ARBShaderObjects;
import org.lwjgl.opengl.GL11;
+import org.lwjgl.opengl.GL13;
import org.lwjgl.util.vector.Vector3f;
+import org.newdawn.slick.opengl.Texture;
+import org.newdawn.slick.opengl.TextureLoader;
+import org.newdawn.slick.util.ResourceLoader;
public class Cube {
- private int vertexAttributesBuffer, vertexIndicesBuffer;
+ private int vertexAttributesBufferID, vertexIndicesBufferID;
private int vertexAttributesBufferSize, vertexIndicesBufferSize;
- private final int numVertices=8, numAxisPerVertex=3, numColoursPerVertex=3;
+ private final int numVertices=8, numAxisPerVertex=3, numColoursPerVertex=3,
+ numTexCoordsPerVertex=2;
private int numIndeces;
private int colorAttributeIndex, coordsAttributeIndex,
- modelPositionAttributeIndex, modelRotationAttributeIndex;
+ modelPositionAttributeIndex, modelRotationAttributeIndex,
+ texCoordsAttributeIndex, textureSamplerIndex;
private final int bytesPerFloat=Float.SIZE/8, bytesPerInt=Integer.SIZE/8;
private int stride;
private Vector3f position, rotation;
private static int shader;
+ private static final String TEXTURES_ROOT_PATH="textures"+File.separator;
public Cube() {
position=new Vector3f(0f, 0f, -4f);
@@ -42,6 +52,17 @@ public Cube() {
initShaderAttributes();
initBuffers();
}
+
+ private Texture initTextures() {
+ Texture texture=null;
+ try {
+ texture=TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream(TEXTURES_ROOT_PATH+"crate.png"));
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ return texture;
+ }
public void update(long timeElapsed) {
rotation.y-=(45.0/6000.0);
@@ -50,21 +71,56 @@ public void update(long timeElapsed) {
private void initShaderAttributes() {
String coordsAttributeName="coords";
coordsAttributeIndex=glGetAttribLocation(shader, coordsAttributeName);
+
String colorAttributeName="color";
colorAttributeIndex=glGetAttribLocation(shader, colorAttributeName);
+
+ String texCoordsAttributeName="texCoords";
+ texCoordsAttributeIndex=glGetAttribLocation(shader, texCoordsAttributeName);
+
String modelPositionAttributeName="modelPosition";
modelPositionAttributeIndex=ARBShaderObjects.glGetUniformLocationARB(shader, modelPositionAttributeName);
+
String modelRotationAttributeName="modelRotation";
modelRotationAttributeIndex=ARBShaderObjects.glGetUniformLocationARB(shader, modelRotationAttributeName);
+
+ String textureSamplerIndexName="textureSampler";
+ textureSamplerIndex=ARBShaderObjects.glGetUniformLocationARB(shader, textureSamplerIndexName);
}
private void initBuffers() {
setupVertexAttributesBuffer();
+ setupTextureBuffer();
setupVertexIndecesBuffer();
}
+ private void setupTextureBuffer() {
+ Texture texture=initTextures();
+ if (texture!=null) {
+ GL13.glActiveTexture(GL13.GL_TEXTURE0);
+
+ texture.bind();
+ ByteBuffer textureData=getTextureByteData(texture);
+
+ GL11.glTexParameteri(GL11.GL_TEXTURE_2D,
+ GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
+
+ GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA,
+ (int)texture.getWidth(), (int)texture.getHeight(), 0,
+ GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, textureData);
+ }
+ }
+
+ private ByteBuffer getTextureByteData(Texture texture) {
+ byte[] textureDataArray=texture.getTextureData();
+ ByteBuffer textureData=BufferUtils.createByteBuffer(textureDataArray.length);
+ textureData.put(textureDataArray);
+ textureData.flip();
+ return textureData;
+ }
+
private void setupVertexIndecesBuffer() {
- vertexIndicesBuffer=glGenBuffersARB();
+ vertexIndicesBufferID=glGenBuffersARB();
// put in vertex index data
int[] indecesData=new int[] {
// front
@@ -84,7 +140,7 @@ private void setupVertexIndecesBuffer() {
numIndeces=indecesData.length;
vertexIndicesBufferSize=bytesPerInt*numIndeces;
// set size of vertex index buffer
- glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vertexIndicesBuffer);
+ glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vertexIndicesBufferID);
glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vertexIndicesBufferSize,
GL_STATIC_DRAW_ARB);
ByteBuffer vertexIndeces=glMapBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB,
@@ -97,11 +153,12 @@ private void setupVertexIndecesBuffer() {
}
private void setupVertexAttributesBuffer() {
- vertexAttributesBuffer=glGenBuffersARB();
- vertexAttributesBufferSize=bytesPerFloat*numVertices
- *(numAxisPerVertex+numColoursPerVertex);
+ vertexAttributesBufferID=glGenBuffersARB();
+ // setup the stride (data size per vertex)
+ stride=(numAxisPerVertex+numColoursPerVertex+numTexCoordsPerVertex)*bytesPerFloat;
+ vertexAttributesBufferSize=numVertices*stride;
// set size of vertex attributes buffer
- glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexAttributesBuffer);
+ glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexAttributesBufferID);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, vertexAttributesBufferSize,
GL_STATIC_DRAW_ARB);
ByteBuffer vertexPositionAttributes=glMapBufferARB(GL_ARRAY_BUFFER_ARB,
@@ -109,53 +166,55 @@ private void setupVertexAttributesBuffer() {
// put int vertex position & color data
float[] vertexAttributesData=new float[] {
// front
- -1f, -1f, 1f, 1f, 0f, 0f,
- 1f, -1f, 1f, 0f, 1f, 0f,
- 1f, 1f, 1f, 0f, 0f, 1f,
- -1f, 1f, 1f, 1f, 1f, 1f,
+ -1f, -1f, 1f, 1f, 0f, 0f, 0f, 0f,
+ 1f, -1f, 1f, 0f, 1f, 0f, 1f, 0f,
+ 1f, 1f, 1f, 0f, 0f, 1f, 1f, 1f,
+ -1f, 1f, 1f, 1f, 1f, 1f, 0f, 1f,
// back
- -1f, -1f, -1f, 1f, 0f, 0f,
- 1f, -1f, -1f, 0f, 1f, 0f,
- 1f, 1f, -1f, 0f, 0f, 1f,
- -1f, 1f, -1f, 1f, 1f, 1f,
+ -1f, -1f, -1f, 1f, 0f, 0f, 0f, 0f,
+ 1f, -1f, -1f, 0f, 1f, 0f, 1f, 0f,
+ 1f, 1f, -1f, 0f, 0f, 1f, 1f, 1f,
+ -1f, 1f, -1f, 1f, 1f, 1f, 0f, 1f,
};
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;
}
+ protected int passInDrawDataAndUpdateDataOffset(int attributeIndex, int numElements, int bytesPerElement, int dataOffset) {
+ glEnableVertexAttribArray(attributeIndex);
+ glVertexAttribPointer(attributeIndex, numElements,
+ GL_FLOAT, false, stride, dataOffset);
+ int newDataOffset=dataOffset+(numElements*bytesPerElement);
+ return newDataOffset;
+ }
+
public void draw() {
// bind vertex data array
- glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexAttributesBuffer);
+ glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexAttributesBufferID);
//bind the uniform attributes
ARBShaderObjects.glUniform3fARB(modelPositionAttributeIndex, position.x, position.y, position.z);
ARBShaderObjects.glUniform3fARB(modelRotationAttributeIndex, rotation.x, rotation.y, rotation.z);
+ ARBShaderObjects.glUniform1iARB(textureSamplerIndex, GL13.GL_TEXTURE0);
// pass in info to vertex shader
int dataOffset=0;
- glEnableVertexAttribArray(coordsAttributeIndex);
- glVertexAttribPointer(coordsAttributeIndex, numAxisPerVertex,
- GL_FLOAT, false, stride, 0);
- dataOffset+=numAxisPerVertex*bytesPerFloat;
- glEnableVertexAttribArray(colorAttributeIndex);
- glVertexAttribPointer(colorAttributeIndex, numColoursPerVertex,
- GL_FLOAT, false, stride, dataOffset);
- dataOffset+=numColoursPerVertex*bytesPerFloat;
+ dataOffset=passInDrawDataAndUpdateDataOffset(coordsAttributeIndex, numAxisPerVertex, bytesPerFloat, dataOffset);
+ dataOffset=passInDrawDataAndUpdateDataOffset(colorAttributeIndex, numColoursPerVertex, bytesPerFloat, dataOffset);
+ dataOffset=passInDrawDataAndUpdateDataOffset(texCoordsAttributeIndex, numTexCoordsPerVertex, bytesPerFloat, dataOffset);
// draw the vertices using the indices
- glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vertexIndicesBuffer);
+ glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vertexIndicesBufferID);
GL11.glDrawElements(GL_TRIANGLES, numIndeces, GL_UNSIGNED_INT, 0);
// unbind the buffers
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
- glDisableVertexAttribArray(colorAttributeIndex);
+ glDisableVertexAttribArray(texCoordsAttributeIndex);
}
public static void setShaderProgram(int shaderProgram) {
View
3 LessonSix/com/onehandstand/lwjgltutorial/lessonsix/Main.java
@@ -31,6 +31,7 @@
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
+import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.Vector2f;
import org.lwjgl.util.vector.Vector3f;
@@ -140,6 +141,8 @@ private void initOpenGL() {
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
+
+ glEnable(GL11.GL_TEXTURE_2D);
// setup the lens params [projection matrix]
glMatrixMode(GL_PROJECTION);
View
4 LessonSix/com/onehandstand/lwjgltutorial/lessonsix/ShaderUtilities.java
@@ -21,7 +21,7 @@
public class ShaderUtilities {
private static boolean verboseValidityCheck=false;
- private static final String SHADERS_ROOT_PATH="shaders";
+ private static final String SHADERS_ROOT_PATH="shaders"+File.separator;
public static int createFragmentShader(String pathToShaderFile) {
int fragmentShader=createShader(pathToShaderFile,
@@ -36,7 +36,7 @@ public static int createVertexShader(String pathToShaderFile) {
}
public static int createShader(String shaderFileName, int shaderType) {
- String shaderFilePath=SHADERS_ROOT_PATH+File.separator+shaderFileName;
+ String shaderFilePath=SHADERS_ROOT_PATH+shaderFileName;
int newShader=glCreateShaderObjectARB(shaderType);
if (newShader!=0) {
String shaderCode=readInShaderCodeFromShaderFile(shaderFilePath);
View
5 LessonSix/shaders/screen.frag
@@ -2,8 +2,11 @@
uniform vec3 modelPosition;
uniform vec3 modelRotation;
+uniform sampler2D textureSampler;
+
varying vec3 fragmentColor;
+varying vec2 fragmentTexCoords;
void main(void) {
- gl_FragColor=vec4(fragmentColor.x,fragmentColor.y,fragmentColor.z,1.0);
+ gl_FragColor=texture2D(textureSampler, fragmentTexCoords);
}
View
5 LessonSix/shaders/screen.vert
@@ -2,9 +2,13 @@
attribute vec3 coords;
attribute vec3 color;
+attribute vec2 texCoords;
+
uniform vec3 modelPosition;
uniform vec3 modelRotation;
+
varying vec3 fragmentColor;
+varying vec2 fragmentTexCoords;
void main(void) {
mat4x4 positionMatrix=mat4x4(1.0);
@@ -33,4 +37,5 @@ void main(void) {
gl_Position=gl_ModelViewProjectionMatrix*positionMatrix*rotationMatrix*vec4(coords, 1.0);
fragmentColor=color;
+ fragmentTexCoords=texCoords;
}
View
BIN LessonSix/textures/crate.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a380576

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