I need to update my mesh with new vertices. I create the VBO as such (initially it gets created with only one vertex in it):
public Mesh(float[] vertex, int size)
{
texture = null;
meshType = 1; //will draw lines
FloatBuffer verticesBuffer = null;
IntBuffer indicesBuffer = null;
int vboID;
try
{
vertexCount = size;
vaoID = glGenVertexArrays();
glBindVertexArray(vaoID);
vboIDList = new ArrayList<>();
// Vertices VBO generation
vboID = glGenBuffers();
vboIDList.add(vboID);
verticesBuffer = MemoryUtil.memAllocFloat(size * 3); // !!! Must Be manually freed!
verticesBuffer.put(vertex).flip();
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, verticesBuffer, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
vertexAttrArrCount += 1;
// Indices VBO generation
vboID = glGenBuffers();
vboIDList.add(vboID);
indicesBuffer = MemoryUtil.memAllocInt(size); // !!! Must be manually freed!
indicesBuffer.put(new int[]{0}).flip();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW);
// unbinding
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
finally
{
if (verticesBuffer != null)
{
MemoryUtil.memFree(verticesBuffer); // Freeing vertex buffer
}
if (indicesBuffer != null)
{
MemoryUtil.memFree(indicesBuffer); // Freeing indices buffer
}
}
}
then I want to update the VBO buffer and write new vertices into it. Note that I do create VBO to have enough space for my new vertices, and I do control that it doesn't get overfilled. I also control how many elements I draw with each render call, so I don't draw the empty 0/0/0 vertices.
My problem is, this code WORKS:
public void updateVBO(float[] vertices, int[] indices, int size)
{
if (meshType == 1)
{
lineCount = size;
FloatBuffer subDataF = null;
IntBuffer subDataI = null;
int vboID;
try
{
//System.out.printf("Adding vertex (%f, %f, %f) to position %d\n",vertex.x,vertex.y,vertex.z,position);
vboID = vboIDList.get(0);
//float[] vert = new float[]{vertex.x, vertex.y, vertex.z};
subDataF = MemoryUtil.memAllocFloat(vertices.length); // !!! Must Be manually freed!
subDataF.put(vertices).flip();
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, subDataF, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
vboID = vboIDList.get(1);
//int[] index = new int[]{ position };
subDataI = MemoryUtil.memAllocInt(indices.length); // !!! Must Be manually freed!
subDataI.put(indices).flip();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, subDataI, GL_STATIC_DRAW);
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
finally
{
if (subDataF != null)
{
MemoryUtil.memFree(subDataF);
}
if (subDataI != null)
{
MemoryUtil.memFree(subDataI);
}
}
}
}
so when I pass the entire vertices array, and re-allocate VBO memory from scratch - it draws exactly what I need it to.
However I would like to use glBufferSubData, so that I don't re-allocate the momory each time I add new vertex.
And this code DOESN'T WORK:
public void addVertex(Vector3f vertex, int position)
{
if (meshType == 1)
{
FloatBuffer subDataF = null;
IntBuffer subDataI = null;
int vboID;
lineCount = position+1;
try
{
System.out.printf("Adding vertex (%f, %f, %f) to position %d\n",vertex.x,vertex.y,vertex.z,position);
vboID = vboIDList.get(0);
float[] vert = new float[]{vertex.x, vertex.y, vertex.z};
subDataF = MemoryUtil.memAllocFloat(3); // !!! Must Be manually freed!
subDataF.put(vert).flip();
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferSubData(GL_ARRAY_BUFFER, position * 3 * 4, subDataF);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
vboID = vboIDList.get(1);
int[] index = new int[]{ position };
subDataI = MemoryUtil.memAllocInt(1); // !!! Must Be manually freed!
subDataI.put(index).flip();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboID);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, position * 4, subDataI);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
finally
{
if (subDataF != null)
{
MemoryUtil.memFree(subDataF);
}
if (subDataI != null)
{
MemoryUtil.memFree(subDataI);
}
}
}
}
Also I'm aware it's not optimized the way I create the floatbuffer and intbuffer, I just want to get it to work before I clean this up. I was trying a bunch of things, so the last piece of code is weird because of that.
Still, I don't understand what I'm doing wrong. I did check that I pass all the data correctly, and that the position (and offset) seem to be calculated how they should be. And it just doesn't draw anything, while when I use glBufferData it does.
Could someone explain where I'm making a mistake?
After all suggestions, here's what I end up with, but it still doesn't work at all:
public void addVertex(Vector3f vertex, int position)
{
if (meshType == 1)
{
FloatBuffer subDataF = null;
IntBuffer subDataI = null;
int vboID;
lineCount = position+1;
try
{
System.out.printf("Adding vertex (%f, %f, %f) to position %d\n",vertex.x,vertex.y,vertex.z,position);
vboID = vboIDList.get(0);
float[] vert = new float[]{vertex.x, vertex.y, vertex.z};
subDataF = MemoryUtil.memAllocFloat(3); // !!! Must Be manually freed!
subDataF.put(vert).flip();
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferSubData(GL_ARRAY_BUFFER, (long)(position * 3 * 4), (FloatBuffer)subDataF);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
vboID = vboIDList.get(1);
int[] index = new int[]{ position };
subDataI = MemoryUtil.memAllocInt(1); // !!! Must Be manually freed!
subDataI.put(index).flip();
glBindVertexArray(vaoID);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, (long)(position * 4), (IntBuffer)subDataI);
}
finally
{
if (subDataF != null)
{
MemoryUtil.memFree(subDataF);
}
if (subDataI != null)
{
MemoryUtil.memFree(subDataI);
}
}
}
}
I did check that VAO ID is correct.
Just as I thought, it was something stupid, and not at all connected to VAO binding and such.
The thing is, when I create VBO initially, I do it like this:
// Vertices VBO generation
vboID = glGenBuffers();
vboIDList.add(vboID);
verticesBuffer = MemoryUtil.memAllocFloat(size * 3); // !!! Must Be manually freed!
verticesBuffer.put(vertex).flip();
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, verticesBuffer, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
vertexAttrArrCount += 1;
I assumed that because I allocate buffer for size*3 floats, it will be of that size, and when I put it in the VBO - it will allocate size*3*4 bytes, i.e. enough for size*3 floats.
Turns out Nope! Because I put only one vertex (3 floats) into the buffer - it will allocate only that amount of space. So when I later try to use glBufferSubData - it only has spaces for 3 floats on the GPU, and naturally doesn't put the values where I need them. I'm actually surprised it doesn't flat-out crash on me.
To fix this, at the moment I did this instead:
// Vertices VBO generation
...
verticesBuffer.put(vertex).put(new float[size*3 - 3]).flip();
...
So basically I'm manually putting an empty array into the FloatBuffer, and that ensures that the buffer is the right size.
Here's the result:
Constructor:
public Mesh(float[] vertex, int size)
{
texture = null;
meshType = 1; //will draw lines
FloatBuffer verticesBuffer = null;
IntBuffer indicesBuffer = null;
int vboID;
try
{
vertexCount = size;
vaoID = glGenVertexArrays();
glBindVertexArray(vaoID);
vboIDList = new ArrayList<>();
// Vertices VBO generation
vboID = glGenBuffers();
vboIDList.add(vboID);
verticesBuffer = MemoryUtil.memAllocFloat(size * 3); // !!! Must Be manually freed!
verticesBuffer.put(vertex).put(new float[size*3 - 3]).flip();
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, verticesBuffer, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
vertexAttrArrCount += 1;
// Indices VBO generation
vboID = glGenBuffers();
vboIDList.add(vboID);
indicesBuffer = MemoryUtil.memAllocInt(size); // !!! Must be manually freed!
indicesBuffer.put(new int[size]).flip(); // I need the first element 0 anyway
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL_STATIC_DRAW);
// unbinding
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
finally
{
if (verticesBuffer != null)
{
MemoryUtil.memFree(verticesBuffer); // Freeing vertex buffer
}
if (indicesBuffer != null)
{
MemoryUtil.memFree(indicesBuffer); // Freeing indices buffer
}
}
}
And then updating:
public void addVertex(Vector3f vertex, int position)
{
if (meshType == 1)
{
FloatBuffer subDataF = null;
IntBuffer subDataI = null;
int vboID;
lineCount = position+1;
try
{
vboID = vboIDList.get(0);
float[] vert = new float[]{vertex.x, vertex.y, vertex.z};
subDataF = MemoryUtil.memAllocFloat(vert.length); // !!! Must Be manually freed!
subDataF.put(vert).flip();
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferSubData(GL_ARRAY_BUFFER, (long)(position * 3 * 4), (FloatBuffer)subDataF);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
vboID = vboIDList.get(1);
int[] index = new int[]{ position };
subDataI = MemoryUtil.memAllocInt(index.length); // !!! Must Be manually freed!
subDataI.put(index).flip();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboID);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, (long)(position * 4), (IntBuffer)subDataI);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
finally
{
if (subDataF != null)
{
MemoryUtil.memFree(subDataF);
}
if (subDataI != null)
{
MemoryUtil.memFree(subDataI);
}
}
}
}
And it works. Do note that the code is a bit dirty, I didn't clean it up before posting an answer.
Related
I am trying to render two triangles on the screen at once, using two different VBOs so that I can add textures later (to my understanding, if you want to add different textures, you must make two VBOs?).
I have tried using a combination of VAOs (both examples) and VAOs and IBOs (1st example).
Edit: As Stackoverflow doesn't recognise the tag, I'd like to clarify that IBO stands for Index Buffer Object
In both cases, I get a blank red screen.
I have previously managed to draw four triangles onto the screen using only one VBO and one IBO, but no VAO, putting all of the vertices into the one VBO, however I feel I need to learn how to draw multiple VBOs, as having everything in one VBO will become cumbersome and inefficient as greater numbers of objects are added.
I have already consulted other related questions as well as several tutorials, but have failed to find the information I am looking for in them (the tutorials tending to only describe drawing one item)
https://learnopengl.com/Getting-started/OpenGL
https://www.lwjgl.org/guide
OpenGL - VAO, VBO, IBO, glDrawElements not displaying
What is the role of glBindVertexArrays vs glBindBuffer and what is their relationship?
Textured triangles with OpenGL using VBO/IBO
Here is my source code:
Render loop
private void setupLoop() {
GL.createCapabilities();
debugProc = GLUtil.setupDebugMessageCallback();
// Set the clear color
glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
while (!glfwWindowShouldClose(window)) {
Engine.makeAndDrawBuffersForTwoTriangles();
renderLoop();
}
}
private void renderLoop() {
// Run the rendering loop until the user has attempted to close
// the window or has pressed the ESCAPE key.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
float aspect = (float) width / height;
glLoadIdentity();
glOrtho(-aspect, aspect, -1, 1, -1, 1);
glfwSwapBuffers(window); // swap the color buffers
// Poll for window events.
glfwPollEvents();
}
Defining my vertices and declaring my ids
static int vbo_left;
static int vao_left;
static int ibo_left;
static float left_vertices[] = { -0.5f, 0f, -0.25f, 0.5f, 0f, 0f };
static int left_indices[] = { 0, 1, 2, 3, 4, 5 };
static int vbo_right;
static int vao_right;
static int ibo_right;
static float right_vertices[] = { 0f, 0f, 0.25f, -0.5f, 0.5f, 0f };
static int right_indices[] = { 0, 1, 2, 3, 4, 5 };
1st Engine class
public static void makeAndDrawBuffersForTwoTriangles() {
// VBO
vbo_left = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_left);
glBufferData(GL_ARRAY_BUFFER, left_vertices, GL_STATIC_DRAW);
// IBO
ibo_left = glGenBuffers();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_left);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,
(IntBuffer) BufferUtils.createIntBuffer(left_indices.length).put(left_indices).flip(), GL_STATIC_DRAW);
glVertexPointer(2, GL_FLOAT, 0, 0L);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
// VAO
vao_left = glGenVertexArrays();
glBindVertexArray(vao_left);
glBindBuffer(GL_ARRAY_BUFFER, vbo_left);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, false, 2, 0);
glBindVertexArray(0);
// VBO
vbo_right = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_right);
glBufferData(GL_ARRAY_BUFFER, right_vertices, GL_STATIC_DRAW);
// IBO
ibo_right = glGenBuffers();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_right);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,
(IntBuffer) BufferUtils.createIntBuffer(right_indices.length).put(right_indices).flip(),
GL_STATIC_DRAW);
glVertexPointer(2, GL_FLOAT, 0, 0L);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
// VAO
vao_right = glGenVertexArrays();
glBindVertexArray(vao_right);
glBindBuffer(GL_ARRAY_BUFFER, vbo_right);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, false, 2, 0);
glBindVertexArray(0);
// Unbind all
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
// Draw elements
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo_left);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_left);
glVertexAttribPointer(0, 2, GL_FLOAT, false, 2, 0);
glDrawElements(GL_TRIANGLES, left_indices.length, GL_UNSIGNED_INT, 0L);
glBindBuffer(GL_ARRAY_BUFFER, vbo_right);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_right);
glVertexAttribPointer(0, 2, GL_FLOAT, false, 2, 0);
glDrawElements(GL_TRIANGLES, right_indices.length, GL_UNSIGNED_INT, 0L);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
/*
// Draw Arrays
glBindVertexArray(vao_left);
glDrawArrays(GL_TRIANGLES, 0, left_indices.length);
glBindVertexArray(vao_right);
glDrawArrays(GL_TRIANGLES, 0, right_indices.length);
glBindVertexArray(0);
*/
2nd Engine class
public static void makeAndDrawBuffersForTwoTriangles() {
vbo_left = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_left);
glBufferData(GL_ARRAY_BUFFER, left_vertices, GL_STATIC_DRAW);
vao_left = glGenVertexArrays();
glBindVertexArray(vao_left);
glBindBuffer(GL_ARRAY_BUFFER, vao_left);
glVertexAttribPointer(vao_left, 2, GL_FLOAT, false, 2, 0);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
vbo_left = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_left);
glBufferData(GL_ARRAY_BUFFER, left_vertices, GL_STATIC_DRAW);
vao_left = glGenVertexArrays();
glBindVertexArray(vao_left);
glBindBuffer(GL_ARRAY_BUFFER, vao_left);
glVertexAttribPointer(vao_left, 2, GL_FLOAT, false, 2, 0);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(vao_left);
glDrawArrays(GL_TRIANGLES, 0, left_vertices.length);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(vao_right);
glDrawArrays(GL_TRIANGLES, 0, right_vertices.length);
}
The Index buffers binding is stated in the Vertex Array Object. Invoking glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); breaks the binding of the index buffer to the VAO.
You have to delete glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);.
Furthermore, the vertex specification is stored in the VAO, so the VAO has to be bound, before the array of generic vertex attribute data is specified and the index buffer is bound:
// VBO
vbo_left = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_left);
glBufferData(GL_ARRAY_BUFFER, left_vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// IBO
ibo_left = glGenBuffers();
// VAO
vao_left = glGenVertexArrays();
glBindVertexArray(vao_left);
// IBO
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_left);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,
(IntBuffer) BufferUtils.createIntBuffer(left_indices.length).put(left_indices).flip(), GL_STATIC_DRAW);
// vertex specification
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo_left);
glVertexAttribPointer(0, 2, GL_FLOAT, false, 2, 0);
Note, in compare to the index buffer, the array buffer binding is a global state.
Each attribute which is stated in the VAOs state vector may refer to a different ARRAY_BUFFER. This reference is stored when glVertexAttribPointer (respectively glVertexPointer) is called. Then the buffer which is currently bound to the target ARRAY_BUFFER is associated to the attribute and the name (value) of the object is stored in the state vector of the VAO.
But the index buffer is a state of the VAO. When a buffer is bound to the target ELEMENT_ARRAY_BUFFER, then this buffer is associated to the vertex array object which is currently bound.
so i want to draw a shape using VAO and VBOs and i think im doing everything right but whenever i run my code i just get the window with the clear color. I had an issue before when i tried to initialize the triangles before i called create capabilities, am i missing some function to start drawing?
here is my code:
int vaoId, vboId, vertexCount;
float[] vertices = {
// Left bottom triangle
-0.5f, 0.5f,
-0.5f, -0.5f,
0.5f, -0.5f,};
private void init() {
if (!glfwInit()) {
throw new IllegalStateException("Failed to Initialize GLFW!");
}
int width = 1000;
int height = 1000;
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
window = glfwCreateWindow(width, height, "App", NULL, NULL);
if (window == 0) {
throw new IllegalStateException("Failed to create Window!");
}
GLFWVidMode videoMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(window, (videoMode.width() - width) / 2, (videoMode.height() - height) / 2);
// Make the OpenGL context current
glfwMakeContextCurrent(window);
// Enable v-sync
glfwSwapInterval(1);
glfwShowWindow(window);
}
private void loop() {
// This line is critical for LWJGL's interoperation with GLFW's
// OpenGL context, or any context that is managed externally.
// LWJGL detects the context that is current in the current thread,
// creates the GLCapabilities instance and makes the OpenGL
// bindings available for use.
GL.createCapabilities();
initTriangle();
// Run the rendering loop until the user has attempted to close
// the window or has pressed the ESCAPE key.
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT); // clear the framebuffer
glBindVertexArray(vaoId);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES, 0, vertexCount);
glDisableVertexAttribArray(0);
glBindVertexArray(0);
glfwSwapBuffers(window); // swap the color buffers
// Poll for window events. The key callback above will only be
// invoked during this call.
glfwPollEvents();
}
}
private void initTriangle() {
FloatBuffer vertBuf = MemoryUtil.memAllocFloat(vertices.length);
vertBuf.put(vertices);
vertBuf.flip();
vaoId = glGenVertexArrays();
glBindVertexArray(vaoId);
vboId = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, vertBuf, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, false, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
I hope you guys can help, thanks a lot.
I am not using shaders. Is that my problem? And is it a necessity
The state of the art way of rendering in OpenGL, would be to use a Shader.
If you don't use a shader, than you have to define the array of vertex data by glVertexPointer
vaoId = glGenVertexArrays();
glBindVertexArray(vaoId);
vboId = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, vertBuf, GL_STATIC_DRAW);
glVertexPointer( 2, GL_FLOAT, 0, 0 ); // <---------------
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
and you have to enable the client-side capability for vertex coordinates by glEnableClientState( GL_VERTEX_ARRAY ):
glBindVertexArray(vaoId);
glEnableClientState( GL_VERTEX_ARRAY ); // <---------------
glDrawArrays(GL_TRIANGLES, 0, vertexCount);
glDisableClientState( GL_VERTEX_ARRAY ); // <---------------
glBindVertexArray(0);
Note, this state of the client-side capability (or vertex attribute array) is stored in the Vertex Array Object.
So it is sufficient to enable the vertex coordinates, when the vertex array object is specified. The enabling and disabling of the vertex coordinates, when drawing the geometry, can be omitted:
vaoId = glGenVertexArrays();
glBindVertexArray(vaoId);
vboId = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, vertBuf, GL_STATIC_DRAW);
glVertexPointer( 2, GL_FLOAT, 0, 0 );
glEnableClientState( GL_VERTEX_ARRAY ); // <---------------
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glBindVertexArray(vaoId);
glDrawArrays(GL_TRIANGLES, 0, vertexCount);
glBindVertexArray(0);
I started watching these tutorials for creating a 2d top-down game using LWJGL and I read that VBO's should be fast but for rendering 48*48 tiles per frame I get only about 100FPS which is pretty slow because I will add a lot more stuff to the game than just some static, not moving or changing, tiles.
What can I do to make this faster? Keep in mind that I just started learning lwjgl and opengl so I probably won't know many things.
Anyways, here are some parts of my code (I removed some parts from the code that were kinda meaningless and replaced them with some descriptions):
The main loop
double targetFPS = 240.0;
double targetUPS = 60.0;
long initialTime = System.nanoTime();
final double timeU = 1000000000 / targetUPS;
final double timeF = 1000000000 / targetFPS;
double deltaU = 0, deltaF = 0;
int frames = 0, updates = 0;
long timer = System.currentTimeMillis();
while (!window.shouldClose()) {
long currentTime = System.nanoTime();
deltaU += (currentTime - initialTime) / timeU;
deltaF += (currentTime - initialTime) / timeF;
initialTime = currentTime;
if (deltaU >= 1) {
// --- [ update ] ---
--INPUT HANDLING FOR BASIC MOVEMENT, CLOSING THE GAME AND TURNING VSYNC ON AND OFF USING A METHOD FROM THE INPUT HANDLER CLASS--
world.correctCamera(camera, window);
window.update();
updates++;
deltaU--;
}
if (deltaF >= 1) {
// --- [ render ] ---
glClear(GL_COLOR_BUFFER_BIT);
world.render(tileRenderer, shader, camera, window);
window.swapBuffers();
frames++;
deltaF--;
}
--PRINTING THE FPS AND UPS EVERY SECOND--
}
The input handler methods used:
I have this in my constructor:
this.keys = new boolean[GLFW_KEY_LAST];
for(int i = 0; i < GLFW_KEY_LAST; i++)
keys[i] = false;
And here are the methods:
public boolean isKeyDown(int key) {
return glfwGetKey(window, key) == 1;
}
public boolean isKeyPressed(int key) {
return (isKeyDown(key) && !keys[key]);
}
public void update() {
for(int i = 32; i < GLFW_KEY_LAST; i++)
keys[i] = isKeyDown(i);
}
This is the render method from the World class:
public void render(TileRenderer renderer, Shader shader, Camera camera, Window window) {
int posX = ((int) camera.getPosition().x + (window.getWidth() / 2)) / (scale * 2);
int posY = ((int) camera.getPosition().y - (window.getHeight() / 2)) / (scale * 2);
for (int i = 0; i < view; i++) {
for (int j = 0; j < view; j++) {
Tile t = getTile(i - posX, j + posY);
if (t != null)
renderer.renderTile(t, i - posX, -j - posY, shader, world, camera);
}
}
}
This is the renderTile() method from TileRenderer:
public void renderTile(Tile tile, int x, int y, Shader shader, Matrix4f world, Camera camera) {
shader.bind();
if (tileTextures.containsKey(tile.getTexture()))
tileTextures.get(tile.getTexture()).bind(0);
Matrix4f tilePosition = new Matrix4f().translate(new Vector3f(x * 2, y * 2, 0));
Matrix4f target = new Matrix4f();
camera.getProjection().mul(world, target);
target.mul(tilePosition);
shader.setUniform("sampler", 0);
shader.setUniform("projection", target);
model.render();
}
This is the constructor and render method from Model class:
public Model(float[] vertices, float[] texture_coords, int[] indices) {
draw_count = indices.length;
v_id = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, v_id);
glBufferData(GL_ARRAY_BUFFER, createBuffer(vertices), GL_STATIC_DRAW);
t_id = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, t_id);
glBufferData(GL_ARRAY_BUFFER, createBuffer(texture_coords), GL_STATIC_DRAW);
i_id = glGenBuffers();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, i_id);
IntBuffer buffer = BufferUtils.createIntBuffer(indices.length);
buffer.put(indices);
buffer.flip();
glBufferData(GL_ELEMENT_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
public void render() {
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, v_id);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, t_id);
glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, i_id);
glDrawElements(GL_TRIANGLES, draw_count, GL_UNSIGNED_INT, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
}
I store the vertices, texture coords and indices in the tile renderer:
float[] vertices = new float[]{
-1f, 1f, 0, //top left 0
1f, 1f, 0, //top right 1
1f, -1f, 0, //bottom right 2
-1f, -1f, 0, //bottom left 3
};
float[] texture = new float[]{
0, 0,
1, 0,
1, 1,
0, 1,
};
int[] indices = new int[]{
0, 1, 2,
2, 3, 0
};
I don't know what else to put here but the full source code and resources + shader files are available on github here.
With your current system, what I would recommend doing is grouping your tiles based on texture. Create something like this:
Map<Texture, List<Tile>> tiles = new HashMap<Texture, List<Tile>>()
Then when you go to render your map of tiles, you will only need to set the texture once per group of tiles, rather than once per tile. This saves PCI-E bandwidth for pushing textures/texture ids to the GPU. You would achieve that like this (pseudo code):
for (Texture tex : tile.keySet())
{
BIND TEXTURE
for (Tile tile : tiles.get(tex))
{
SET UNIFORMS
RENDER
}
}
Something else I see along these lines is that you are pushing the projection matrix to each tile individually. When you are running a shader program, the value of a given uniform stays the same until you change it or until the program ends. Set the projection matrix uniform once.
It also appears that you are calling this every renderTile(...). Given the value does not change, calculate it once before the render pass, then pass it in as a variable in the renderTile(...) method rather than passing in camera and world.
a friend and I are currently working on some LWJGL 2D related things. I was working on VBOs and was able to get it to work. Now when using this to draw tiles, they work. however, my FPS drops significantly. Perhaps I'm doing something wrong.
Here's my code.
public void drawTextureRect(float x, float y, float width, float height,
String textureName) {
// Bind the texture to draw
texture = loadTexture(textureName);
texture.bind();
// Set triangle vertex data
FloatBuffer vertices = BufferUtils.createFloatBuffer(6);
vertices.put(new float[] { x, y, x + width, y, x, height + y });
// Set texture coordinates
FloatBuffer textcoord = BufferUtils.createFloatBuffer(6);
textcoord.put(new float[] { 0, 0, 1, 0, 0, 1 });
// Allow OpenGL to interpret the Text Coordinates & Vertex Data
textcoord.flip();
vertices.flip();
int VertexHandle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, VertexHandle);
glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
int TextureHandle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, TextureHandle);
glBufferData(GL_ARRAY_BUFFER, textcoord, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, VertexHandle);
glVertexPointer(2, GL_FLOAT, 0, 0L);
glBindBuffer(GL_ARRAY_BUFFER, TextureHandle);
glTexCoordPointer(2, GL_FLOAT, 0, 0L);
// Enable Vertex and Texture Arrays
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
// Draw Triangle
glDrawArrays(GL_TRIANGLES, 0, 3);
// Unable Vertex and Textures
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
/* SECOND TRIANGLE */
// Set Second Triangle Coordinates
FloatBuffer vertices2 = BufferUtils.createFloatBuffer(6);
vertices2.put(new float[] { x + width, y + height, x, height + y,
x + width, y });
// Set Second Texture Coordinates
FloatBuffer textcoord2 = BufferUtils.createFloatBuffer(6);
textcoord2.put(new float[] { 1, 1, 0, 1, 1, 0 });
// Allow OpenGl to interpret the Text Coordinates & Vertex Data
textcoord2.flip();
vertices2.flip();
int VertexHandle2 = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, VertexHandle2);
glBufferData(GL_ARRAY_BUFFER, vertices2, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
int TexCord = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, TexCord);
glBufferData(GL_ARRAY_BUFFER, textcoord2, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, VertexHandle2);
glVertexPointer(2, GL_FLOAT, 0, 0L);
glBindBuffer(GL_ARRAY_BUFFER, TexCord);
glTexCoordPointer(2, GL_FLOAT, 0, 0L);
// Enable Vertex and Texture Arrays
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
// Draw Triangle
glDrawArrays(GL_TRIANGLES, 0, 3);
// Unable Vertex and Textures
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glDeleteBuffers(TextureHandle);
glDeleteBuffers(TexCord);
glDeleteBuffers(VertexHandle);
glDeleteBuffers(VertexHandle2);
}`
I started learning OpenGL. I can load a .obj model and draw it with an elementBuffer. But I'm stuck at trying to two different models at a time. The model I want to draw is in an Entity class.
Most tutorials I can find about this only shows how to load and draw a sinlge model. None explains (In a way I can find/understand at least) how to handle multiple models.
Here is all my code:
public static void main(String[] args) throws LWJGLException, IOException
{
PixelFormat pixelFormat = new PixelFormat();
ContextAttribs contextAtrributes = new ContextAttribs(3, 2);
contextAtrributes.withForwardCompatible(true);
contextAtrributes.withProfileCore(true);
Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
Display.setTitle("Textured quad!");
Display.create(pixelFormat, contextAtrributes);
Mouse.create();
Mouse.setGrabbed(true);
Keyboard.create();
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
entity = new Entity("planeTex.obj");
entity2 = new Entity("modelTex2.obj");
Shaders.load();
Textures.load();
Camera.create(new Vector3f(0, 1, -0.75f), new Vector3f(-50, 0, 20), HEIGHT, WIDTH);
while (!Display.isCloseRequested())
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
entity.draw();
entity2.draw();
Display.update();
Display.sync(60);
}
}
public class Entity
{
private int vao, vbo, ebo;
private int elementSize;
public Entity(String name)
{
vao = glGenVertexArrays();
glBindVertexArray(vao);
vbo = glGenBuffers();
*Load vertex data into buffer*
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
ebo = glGenBuffers();
*load data into elementBuffer*
*Set elementSize to the element count*
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, elementBuffer, GL_STATIC_DRAW);
}
public void draw()
{
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glDrawElements(GL_TRIANGLES, elementSize, GL_UNSIGNED_INT, 0);
}
}
public class Shaders
{
public static int vertexShader, fragmentShader;
public static int shaderProgram;
public static int uniTrans;
public static void load()
{
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, loadFile("vertex.shader"));
glCompileShader(vertexShader);
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, loadFile("fragment.shader"));
glCompileShader(fragmentShader);
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glBindFragDataLocation(shaderProgram, 0, "outColor");
glLinkProgram(shaderProgram);
glUseProgram(shaderProgram);
// Specify the layout of the vertex data
int posAttrib = glGetAttribLocation(shaderProgram, "position");
glEnableVertexAttribArray(posAttrib);
glVertexAttribPointer(posAttrib, 3, GL_FLOAT, false, (Float.SIZE / 8) * 8, 0);
int colAttrib = glGetAttribLocation(shaderProgram, "color");
glEnableVertexAttribArray(colAttrib);
glVertexAttribPointer(colAttrib, 3, GL_FLOAT, false, (Float.SIZE / 8) * 8, (Float.SIZE / 8) * 3);
int texAttrib = glGetAttribLocation(shaderProgram, "texcoord");
glEnableVertexAttribArray(texAttrib);
glVertexAttribPointer(texAttrib, 2, GL_FLOAT, false, (Float.SIZE / 8) * 8, (Float.SIZE / 8) * 6);
uniTrans = glGetUniformLocation(Shaders.shaderProgram, "model");
}
}
The result is that only the Entity object created last will be drawn. No matter the draw order.
Well, I fixed it by placing this chunk
// Specify the layout of the vertex data
int posAttrib = glGetAttribLocation(Shaders.shaderProgram, "position");
glEnableVertexAttribArray(posAttrib);
glVertexAttribPointer(posAttrib, 3, GL_FLOAT, false, (Float.SIZE / 8) * 8, 0);
int colAttrib = glGetAttribLocation(Shaders.shaderProgram, "color");
glEnableVertexAttribArray(colAttrib);
glVertexAttribPointer(colAttrib, 3, GL_FLOAT, false, (Float.SIZE / 8) * 8, (Float.SIZE / 8) * 3);
int texAttrib = glGetAttribLocation(Shaders.shaderProgram, "texcoord");
glEnableVertexAttribArray(texAttrib);
glVertexAttribPointer(texAttrib, 2, GL_FLOAT, false, (Float.SIZE / 8) * 8, (Float.SIZE / 8) * 6);
In the entity class draw loop just before the glDrawElements method. Placing the chunk anywhere else would result in a crashed program:
"Cannot use offsets when Array Buffer Object is disabled"
Otherwise it would simply draw nothing at all. I got the feeling from NicolBolas that I should put it in the Entity constructor, but as I said, it didn't work anywhere.