Hi all, since this is my first post, I am not really expecting any answers, though i’ll post anyway. I just learned VBO 2 days ago and I have a basic knowlege of array implementation. I can accomplish texture loading in imediate mode, but I am having trouble with Vertex Buffer Objects. Here is my code, and my output.
public void create() {
setup();
render();
}
int cube;
int texture;
private void makeCube(float x, float y, float z) {
texture = glGenBuffers();
cube = glGenBuffers();
FloatBuffer cubeBuffer;
FloatBuffer textureBuffer;
float highX = x + tileSize;
float highY = y + tileSize;
float highZ = z + tileSize;
float[] textureData = new float[]{
0, 0,
1, 0,
1, 1,
0, 1,
0, 0,
1, 0,
1, 1,
0, 1,
0, 0,
1, 0,
1, 1,
0, 1,
0, 0,
1, 0,
1, 1,
0, 1,
0, 0,
1, 0,
1, 1,
0, 1,
0, 0,
1, 0,
1, 1,
0, 1};
textureBuffer = asFloatBuffer(textureData);
glBindBuffer(GL_ARRAY_BUFFER, texture);
glBufferData(GL_ARRAY_BUFFER, textureBuffer, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
float[] cubeData = new float[]{
/*Front Face*/
x, y, z,
highX, y, z,
highX, highY, z,
x, highY, z,
/*Back Face*/
x, y, highZ,
highX, y, highZ,
highX, highY, highZ,
x, highY, highZ,
/*Left Face*/
x, y, z,
x, y, highZ,
x, highY, highZ,
x, highY, z,
/*Right Face*/
highX, y, z,
highX, y, highZ,
highX, highY, highZ,
highX, highY, z,
/*Bottom Face*/
x, y, z,
x, y, highZ,
highX, y, highZ,
highX, y, z,
/*Top Face*/
x, highY, z,
x, highY, highZ,
highX, highY, highZ,
highX, highY, z};
cubeBuffer = asFloatBuffer(cubeData);
glBindBuffer(GL_ARRAY_BUFFER, cube);
glBufferData(GL_ARRAY_BUFFER, cubeBuffer, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
private void renderCube() {
textures.get(1).bind();
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, cube);
glVertexPointer(3, GL_FLOAT, 0, 0);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, texture);
glTexCoordPointer(2, GL_FLOAT, 0, 0);
glDrawArrays(GL_QUADS, 0, 24);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
private void render() {
while (!Display.isCloseRequested()) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
camera();
renderCube();
Display.update();
Display.sync(30);
}
Display.destroy();
System.exit(0);
}
private void setup() {
try {
Display.setDisplayMode(new DisplayMode(frameWidth, frameHeight));
Display.setTitle("3D Project");
Display.setVSyncEnabled(vSync);
Display.create();
} catch (LWJGLException ex) {
Logger.getLogger(Camera.class.getName()).log(Level.SEVERE, null, ex);
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, (float) Display.getWidth() / (float) Display.getHeight(), zNear, zFar);
//glOrtho(0, Display.getWidth(), Display.getHeight(), 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glLoadIdentity();
loadTextures();
makeCube(0, 0, -1);
}
The output that I am getting is a solid color that takes the primary color of the texture. (If I load a water texture, the cube is blue, if I load grass, its green) This, to me, indicates that the texture is rendering, just not correctly.
EDIT: Apparently I overlooked the back side of the cube, which loads the texture, however the rest of the cube is a solid color.
EDIT again: Solved my own issue. Posted solved code.
/Thread