I've been having trouble texturing a cube using vertex buffer objects. I got the cube up and running, and my program compiles, but the texture does not appear. I think my problem may be my GLSL in my shaders.
I got a vertex shader and a fragment shader up and running. The fragment shader was working when it was just making it a color, but I can't get it to texture an image.
EDIT2EDIT2: I simplified the shaders so I could figure out setting uniforms first. The program runs, but the VBO doesn't have any texture on it. Many examples I've seen do basically the same thing I am (I think). So I think the problem now could be something small or obvious that I'm not seeing.
EDIT3: Now I've been trying to convert from drawElements to drawArrays. I simplifiedcan't get the shaders so I could figure out setting uniforms firstcube to render let alone render with textures. The program runsWhen I run this, there are clearly triangles being rendered, but I can't relate their position to the VBO doesn't have any texture on itvertex array. Many examples I've seen do basicallyFor example, some of the same thing I am (I think)triangles have points at 0,0,0 and there are no points 0,0,0 in vertexArray. So I think the problem now couldI may be something smallmissing some glDrawArrays syntax or obviousI left something from glDrawElements that I'm not seeingshouldn't be there.
public void initVBO(GL2 gl, String textureFileName, String textureFileType) {
// cube ///////////////////////////////////////////////////////////////////////
// v6----- v5
// /| /|
// v1------v0|
// | | | |
// | |v7---|-|v4
// |/ |/
// v2------v3
// vertex coords array for glDrawArrays() =====================================
// A cube has 6 sides and each side has 2 triangles, therefore, a cube consists
// of 36 vertices (6 sides * 2 tris * 3 vertices = 36 vertices). And, each
// vertex is 3 components (x,y,z) of floats, therefore, the size of vertex
// array is 108 floats (36 * 3 = 108).
float[] vertexArray = { 1, 1, 1, -1, 1, 1, -1,-1, 1, // v0-v1-v2 (front)
0, 0 -1,-1, 01,
1,-1, 1, 1, 1, 1, // v2-v3-v0
1, 1, 1, 1,-x1, x1, x 1,-1,-1, // v0-v3-v4 (right)
x 1,-1,-1, x 1, x1,-1, 1, 1, 1, // v4-v5-v0
1, 1, 1, 1, 1,-x1, -x1, x1,-1, // v0-v5-v6 (top)
x -1, 1,-x1, x -1, 1, 1, 1, 1, 1, // v6-v1-v0
-1, 1, 1, -1, 1,-1, -1,-1,-1, // v1-v6-v7 (left)
-x1,-1,-1, x -1,-x1, 1, -1, 1, 1, // v7-v2-v1
x -1,-1,-1, x 1,-x1,-1, 1,-1, 1, // v7-v4-v3 (bottom)
1,-x1, 1, -x1,-x1, 1, -1,-1,-1, // v3-v2-v7
x 1,-x1,-x1, -1,-1,-1, -1, 1,-1, // v4-v7-v6 (back)
-1, 1,-1, 1, 1,-1, 1,-1,-1 }; // v6-v5-v4
vertices = Buffers.newDirectFloatBuffer(vertexArray.length);
vertices.put(vertexArray);
vertices.flip();
short[] indexArray = {
1, 2, 4,
1, 4, 3,
1, 7, 5,
1, 3, 7,
1, 5, 2,
5, 6, 2,
7, 8, 5,
5, 8, 6,
2, 6, 8,
2, 8, 4,
4, 8, 7,
4, 7, 3,
};
indices = Buffers.newDirectShortBuffer(indexArray.length);
indices.put(indexArray);
indices.flip();
FloatBuffer textureData = Buffers.newDirectFloatBuffer(12);
textureData.put(textureLeft);
textureData.put(textureRight);
textureData.put(textureBottom);
textureData.put(textureTop);
textureData.flip();
int[] temp = new int[3];
gl.glGenBuffers(3, temp, 0);
VBOVertices = temp[0];
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, VBOVertices);
gl.glBufferData(GL.GL_ARRAY_BUFFER, vertices.capacity()
* Buffers.SIZEOF_FLOAT, vertices, GL.GL_STATIC_DRAW);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
VBOIndices = temp[1];
gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, VBOIndices);
gl.glBufferData(GL.GL_ELEMENT_ARRAY_BUFFER, indices.capacity()
* Buffers.SIZEOF_SHORT, indices, GL.GL_STATIC_DRAW);
gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0);
vboTextureCoordHandle = temp[2];
gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFERGL_ARRAY_BUFFER, vboTextureCoordHandle);
gl.glBufferData(GL.GL_ELEMENT_ARRAY_BUFFERGL_ARRAY_BUFFER, vboTextureCoordHandle, textureData, GL.GL_STATIC_DRAW);
gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0);GL_STAT
IC_DRAW);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
shader = new Shader(gl);
shader.bind(gl);
shader.attachVertexShader(gl);
shader.attachFragmentShader(gl);
shader.link(gl);
//shader.unbind(gl);
}
public void drawCubeVBO(GL2 gl, String textureFileName, String textureFileType) {
//shader.bind(gl);
//initializing textures
this.textureFileName = textureFileName;
this.textureFileType = textureFileType;
loadTexture(gl);
texture.enable(gl);
shadergl.bindglEnable(glGL.GL_TEXTURE_2D);
//setting the uniform
this shader.textureFileNamesetUniform(gl, ="tex", textureFileName;0);
//activating textures
this gl.textureFileTypeglActiveTexture(GL2.GL_TEXTURE0 =+ textureFileType;0);
texture.bind(gl); loadTexture
//drawing
gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
texture/* Setup Position Pointer */
gl.enableglBindBuffer (GL.GL_ARRAY_BUFFER, VBOVertices);
gl.glVertexPointer (3, GL.GL_FLOAT, 0, 0);
texture/* Setup Texture Coordinate Pointer */
gl.bindglBindBuffer (GL.GL_ARRAY_BUFFER, vboTextureCoordHandle);
gl.glTexCoordPointer (2, GL.GL_FLOAT, 0, 0);
//actual drawing
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, VBOVertices);
gl.glDrawArrays(GL.GL_TRIANGLES, (int) vertices.get(0), vertices.capacity());
gl.glActiveTextureglDisableClientState(GLGL2.GL_TEXTURE0GL_TEXTURE_COORD_ARRAY);
shadergl.setUniformglDisableClientState(gl, "tex", 0GL2.GL_VERTEX_ARRAY);
gl.glEnable(GL.GL_TEXTURE_2D);
gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
/* Setup Position Pointer */
gl.glBindBuffer (GL.GL_ARRAY_BUFFER, VBOVertices);
gl.glVertexPointer (3, GL.GL_FLOAT, 0, 0);
/* Setup Texture Coordinate Pointer */
gl.glBindBuffer (GL.GL_ARRAY_BUFFER, vboTextureCoordHandle);
gl.glTexCoordPointer (2, GL.GL_FLOAT, 0, 0);
gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, VBOIndices);
gl.glDrawElements(GL.GL_TRIANGLES, indices.capacity(), GL.GL_UNSIGNED_SHORT, 0);
gl.glDisableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
shader.unbind(gl);
}
public void loadTexture(GL2 gl) {
// Load texture from image
try {
// Create a OpenGL Texture object from (URL, mipmap, file suffix)
// Use URL so that can read from JAR and disk file.
texture = TextureIO.newTexture(getClass().getClassLoader()
.getResource(textureFileName), // relative to project root
false, textureFileType);
// Use linear filter for texture if image is larger than the
// original texture
gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Use linear filter for texture if image is smaller than the
// original texture
gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// Texture image flips vertically. Shall use TextureCoords class to
// retrieve
// the top, bottom, left and right coordinates, instead of using
// 0.0f and 1.0f.
TextureCoords textureCoords = texture.getImageTexCoords();
textureTop = textureCoords.top();
textureBottom = textureCoords.bottom();
textureLeft = textureCoords.left();
textureRight = textureCoords.right();
} catch (GLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}