Guys, i really stuck with this…(( Want to draw two simple triangles as a quad through VBO, but can see only triangle rendered through immediate mode((
App class:
package vbo;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.input.Keyboard;
import lwjgl.VBOHelper;
import app.IKeyListener;
import app.LWJGLApp;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
public class VBOApp extends LWJGLApp implements IKeyListener {
boolean end = false;
VBOApp(String title, int width, int height) {
super(title, width, height);
}
/**
* @param args
*/
public static void main(String[] args) {
VBOApp app = new VBOApp("VBO example", 800, 600);
app.start();
}
int vertBuf = 0;
int colBuf = 0;
int indBuf = 0;
@Override
public boolean onInit() {
addKeyListener(this);
if(!VBOHelper.checkHardwareVBOSupport()){
return false;
}
// Buffers
FloatBuffer vBuf = BufferUtils.createFloatBuffer(12);
FloatBuffer cBuf = BufferUtils.createFloatBuffer(16);
IntBuffer iBuf = BufferUtils.createIntBuffer(6);
float color[] = {1, 0, 1, 0};
// First
float first[] = {-1, -1, 0};
vBuf.put(first);
cBuf.put(color);
// Second
float second[] = {-1, 1, 0};
vBuf.put(second);
cBuf.put(color);
// Third
float third[] = {1, 1, 0};
vBuf.put(third);
cBuf.put(color);
// Fourth
float fourth[] = {1, -1, 0};
vBuf.put(fourth);
cBuf.put(color);
// Indices
int indices[] = {0, 1, 2, 0, 2, 3};
iBuf.put(indices);
// Creating buffers and filling data
vertBuf = VBOHelper.createVBOID();
VBOHelper.bufferData(vertBuf, vBuf, GL_STATIC_DRAW);
colBuf = VBOHelper.createVBOID();
VBOHelper.bufferData(colBuf, cBuf, GL_STATIC_DRAW);
indBuf = VBOHelper.createVBOID();
VBOHelper.bufferElementData(indBuf, iBuf, GL_STATIC_DRAW);
return true;
}
@Override
public boolean prepareFrame(long msecElapsed) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, -6.0f);
glBegin(GL_TRIANGLES); // Drawing Using Triangles
glVertex3f( 0.0f, 1.0f, 0.0f); // Top
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glEnd();
VBOHelper.bindVBO(vertBuf, indBuf, colBuf);
VBOHelper.drawTriangles(6);
return !end;
}
@Override
public void onDestroy() {
// Destroying buffers
VBOHelper.destroyBuffer(vertBuf);
VBOHelper.destroyBuffer(colBuf);
VBOHelper.destroyBuffer(indBuf);
}
@Override
public void keyboardEvent(int key, boolean pressed) {
if(key == Keyboard.KEY_ESCAPE){
end = true;
}
}
}
VBO helper class:
package lwjgl;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import org.lwjgl.opengl.GLContext;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import utils.Log;
public class VBOHelper {
/**
* Checking CAPs
*/
public static boolean checkHardwareVBOSupport(){
if(!GLContext.getCapabilities().GL_ARB_vertex_buffer_object){
Log.hardwareNotSupport("GL_ARB_vertex_buffer_object");
}
return GLContext.getCapabilities().GL_ARB_vertex_buffer_object;
}
/**
* Creating, filling, destroying
*/
// Creating id for our VBO
public static int createVBOID() {
return glGenBuffers();
}
// Float data for our VBO (vertex, color, normal)(default usage = GL_STATIC_DRAW)
public static void bufferData(int id, FloatBuffer data, int usage) {
glBindBuffer(GL_ARRAY_BUFFER, id);
glBufferData(GL_ARRAY_BUFFER, data, usage);
}
// Int data for our VBO (indices)(default usage = GL_STATIC_DRAW)
public static void bufferElementData(int id, IntBuffer data, int usage) {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, data, usage);
}
public static void destroyBuffer(int id){
glDeleteBuffers(id);
}
/**
* Binding, drawing
*/
// Preparing OpenGL for drawing and binding buffers
public static void bindVBO(int vertexBufferID, int indexBufferID, int colourBufferID) {
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
glVertexPointer(3, GL_FLOAT, 0, 0);
glEnableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, colourBufferID);
glColorPointer(4, GL_FLOAT, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
}
public static void drawTriangles(int indexBufferSize){
glDrawElements(GL_TRIANGLES, indexBufferSize, GL_UNSIGNED_INT, 0);
}
}