Hello Guest

Vertex Buffer Woe [FIXED]

  • 1 Replies
  • 4981 Views
Vertex Buffer Woe [FIXED]
« on: December 28, 2010, 13:37:22 »
Hello,

I'm currently porting this tutorial to LWJGL : http://www.arcsynthesis.org/gltut/Basics/Tutorial%2001.html

Everything seems to be going fine, i can create and load the Shaders without issue, but when i go to draw the triangle on the screen, it remains black with no visible triangle.

Some code fragments :

Create the VBO :
Code: [Select]
package com.maoni.shaders.util;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import static org.lwjgl.opengl.GL15.*;

public enum InitializeVertexBuffer {
INSTANCE,
;

public int createPositionBufferObject(float vertices[]) {
int positionBufferObject = glGenBuffers();
FloatBuffer fb = BufferUtils.createFloatBuffer(vertices.length);
fb.put(vertices);
fb.flip();

glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
glBufferData(positionBufferObject, fb, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

return positionBufferObject;
}
}

Initialise the Shaders etc :
Code: [Select]

private static void init() {
try {
int width = 800;
int height = 600;

Display.setDisplayMode(new DisplayMode(width, height));
Display.setVSyncEnabled(true);
Display.setTitle("Maoni V0.01");
Display.create();

// Setup the shader program
shaderList.add(CreateShader.VERTEX.load(_VERTEX_SHADER_LOCATION));
shaderList.add(CreateShader.FRAGMENT.load(_FRAGMENT_SHADER_LOCATION));
_PROGRAM = CreateProgram.INSTANCE.create(shaderList);

// Setup the PositionBufferObject
_PBO = InitializeVertexBuffer.INSTANCE.createPositionBufferObject(vertexPositions);

glViewport(0, 0, width, height);
} catch (LWJGLException e) {
e.printStackTrace();
}
}

And then try and Draw it :
Code: [Select]

private static void render() {
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glUseProgram(_PROGRAM);

glBindBuffer(GL_ARRAY_BUFFER, _PBO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, false, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 3);

glDisableVertexAttribArray(0);
glUseProgram(0);

Display.update();
}


The whole code is listed here :

https://github.com/Almclean/Maoni

If anyone could point me in the right direction i would be very grateful.

Many thanks,
M
« Last Edit: January 01, 2011, 22:30:56 by Moridin »

Re: Vertex Buffer Woe [FIXED]
« Reply #1 on: January 01, 2011, 22:43:40 »
Ok so a lot of reading the opengl superbible later and i've finally got my first lwjgl triangle on the screen.  Hardly Crysis 3 but here's what i did so that's its documented for future opengl newbies.

So first off, I swapped the lwjgl.jar for lwjgl-debug.jar.  This threw up an exception in the Vertex buffer init code.  The main problem was here :

Code: [Select]
glBufferData(positionBufferObject, fb, GL_STATIC_DRAW);
//should be
glBufferData(GL_ARRAY_BUFFER, fb, GL_STATIC_DRAW);

So i wasn't actually putting data into the array buffer properly...oops.

The second main bit was the fact that i didn't create and initialise a Vertex Array Object, so i added :

Code: [Select]
// Create the Vao
vao = glGenVertexArrays();
glBindVertexArray(vao);

To create one.  Without this, the superbible informs me, all VBO-specific calls will fail and nothing will get drawn.

So i've checked the code into Github, take a look and i hope you find it useful.

Onward with OpenGL 3.x !

M