OpenGL greedy meshing using VBO - opengl

I am trying to reduce the number of faces that are rendered in my voxel engine by implementing a greedy meshing algorithm similar to Mikola's (http://0fps.net/2012/06/30/meshing-in-a-minecraft-game/)
Since I am using a VBO to draw each triangle for every cube I am wondering how I will be able to merge the triangles of adjacent cubes?
For reference here is what my cube vertex array looks like (note that it is interleaved to allow for texture coordinates to be included):
GLfloat cubeInterleaved[] =
{
// Front face u v
-0.5f, -0.5f, 0.5f, 0, 0,
0.5f, -0.5f, 0.5f, 1, 0,
0.5f, 0.5f, 0.5f, 1, 1,
-0.5f, 0.5f, 0.5f, 0, 1,
// Right face
0.5f, -0.5f, 0.5f, 0, 0,
0.5f, -0.5f, -0.5f, 1, 0,
0.5f, 0.5f, -0.5f, 1, 1,
0.5f, 0.5f, 0.5f, 0, 1,
// Back face
0.5f, -0.5f, -0.5f, 0, 0,
-0.5f, -0.5f, -0.5f, 1, 0,
-0.5f, 0.5f, -0.5f, 1, 1,
0.5f, 0.5f, -0.5f, 0, 1,
// Left face
-0.5f, -0.5f, -0.5f, 0, 0,
-0.5f, -0.5f, 0.5f, 1, 0,
-0.5f, 0.5f, 0.5f, 1, 1,
-0.5f, 0.5f, -0.5f, 0, 1,
// Top Face
-0.5f, 0.5f, 0.5f, 0, 0,
0.5f, 0.5f, 0.5f, 1, 0,
0.5f, 0.5f, -0.5f, 1, 1,
-0.5f, 0.5f, -0.5f, 0, 1,
// Bottom Face
0.5f, -0.5f, 0.5f, 0, 0,
-0.5f, -0.5f, 0.5f, 1, 0,
-0.5f, -0.5f, -0.5f, 1, 1,
0.5f, -0.5f, -0.5f, 0, 1
};

Since I am using a VBO to draw each triangle for every cube I am confused as to how I will be able to merge the triangles of adjacent cubes.
By using only a subset of the vertices, which is done by properly populating the index buffer for drawing.
Take this set of 4 triangles for example
5
/ \
3 --- 4
/ \ / \
0 --- 1 --- 2
You can either draw
GL_TRIANGLES: 0 1 3 1 2 4 1 4 3 3 4 5
or if all the vertices are coplanar you can just use the outer edges
GL_TRIANGLES: 0 2 5
And you can do the same with the vertices of blocks forming your world by appropriately populating the index buffer passed to glDrawElements

Related

OpenGL Converting from busy vertex buffer to element buffer

If you look at my history you can see some more context for what I'm trying to achieve.
I'm trying to draw multiple cubes with an element buffer without using instancing but for the sake of simplicity i'll use quads drawn with triangles.
If I have a VBO my array would look like below:
float cubeVertices[] = {
//FRONT
-0.5f, 0.5f, 0.5f, //front top left
-0.5f, -0.5f, 0.5f, //front bottom left
0.5f, 0.5f, 0.5f, //front top right
0.5f, 0.5f, 0.5f, //front top right
-0.5f, -0.5f, 0.5f, //front bottom left
0.5f, -0.5f, 0.5f //front bottom right
};
And converted to an element buffer it looks like:
float cubeVertices[] = {
-0.5f, 0.5f, 0.5f, //front top left
0.5f, 0.5f, 0.5f, //front top right
-0.5f, -0.5f, 0.5f, //front bottom left
0.5f, -0.5f, 0.5f //front bottom right
};
unsigned int cubeIndices[] = {
0, 2, 1, //FRONT
1, 2, 3
};
But what do I do if I want to draw multiple? (WITHOUT INSTANCING)
For a VBO it seems simple enough
float cubeVertices[] = {
//FRONT translate(x, y, z)
-0.5f, 0.5f, 0.5f, 0, 0, 0,//front top left
-0.5f, -0.5f, 0.5f, 0, 0, 0,//front bottom left
0.5f, 0.5f, 0.5f, 0, 0, 0,//front top right
0.5f, 0.5f, 0.5f, 0, 0, 0,//front top right
-0.5f, -0.5f, 0.5f, 0, 0, 0,//front bottom left
0.5f, -0.5f, 0.5f, 0, 0, 0,//front bottom right
//FRONT translate(x, y, z)
-0.5f, 0.5f, 0.5f, 1, 1, 1,//front top left
-0.5f, -0.5f, 0.5f, 1, 1, 1,//front bottom left
0.5f, 0.5f, 0.5f, 1, 1, 1,//front top right
0.5f, 0.5f, 0.5f, 1, 1, 1,//front top right
-0.5f, -0.5f, 0.5f, 1, 1, 1,//front bottom left
0.5f, -0.5f, 0.5f, 1, 1, 1,//front bottom right
};
Where each face is moved by the translate of (x y z) like model = glm::translate(model, glm::vec3(x, y, z))
But this has a lot of duplicate data like repeating the translation 6 times per quad and writing the vertex drawn locations twice per quad and an extra 2 duplicate vertices per quad.
Now imagine that for more advanced models and it seems like the waste of memory would get quite large.
So how do I convert the above to an element buffer? Ideally without duplicating data obviously.
I'll write some code/pseudo code to help explain what I'm after.
float cubeVertices[] = {
-0.5f, 0.5f, 0.5f, //front top left
0.5f, 0.5f, 0.5f, //front top right
-0.5f, -0.5f, 0.5f, //front bottom left
0.5f, -0.5f, 0.5f //front bottom right
};
unsigned int cubeIndices[] = {
0, 2, 1, //FRONT
1, 2, 3
};
unsigned int cubeLocations[] = {
0, 0, 0,
1, 1, 1
};
for (int loc = 0; loc < 2; ++loc)
{
for (int i = 0; i < 6; ++i)
{
//VERTEX COORDS //MODEL LOCATION
Draw(cubeVertices[cubeIndices[i]], cubeLocations[loc]);
}
}
**But done in one draw call, e.g. a simple mesh (Not instancing)**
The above would generates a memory efficient version of the VBO as an element array
Not sure how well I've articulated what I'm after is but I've tried my best to provide an explanation of my goal.
Think of a basic minecraft clone where all the unneeded drawn faces really starts to add up and instancing no longer works so meshing is now required.

OpenGL Render Half Polygon

I am getting half polygon rendered while using Indices in OpenGL. Any idea what's going wrong?
float vertices[] = {
-0.5f, 0.5f, 0,
-0.5f, -0.5f, 0,
0.5f, -0.5f, 0,
0.5f, -0.5f, 0,
0.5f, 0.5f, 0,
-0.5f, 0.5f, 0
};
uint32_t Indices[] = {
0,1,3,
3,1,2
};
When you draw with indices you just need 4 vertices not 6. The index is the "address" of the vertex:
float vertices[] = {
-0.5f, -0.5f, 0, // vertex 0
0.5f, -0.5f, 0, // vertex 1
0.5f, 0.5f, 0, // vertex 2
-0.5f, 0.5f, 0 // vertex 3
};
uint32_t Indices[] = {
0, 1, 3, // 1. triangle, vertices 0, 1, 2
0, 2, 3 // 2. triangle, vertices 0, 2, 3
};
Are you drawing with indices ? If that's the case you just need 4 vertices
If you're drawing with indices then the data is incorrect index 2 and 3 have the same value supplying the same two positions won't do anything, You're supplying two vertices to draw a triangle which requires 3 vertices

In opengl, how to use the mouse to click to get the pixel information of the graphic drawn in the frame buffer

I recently needed to implement a function in the QT+OPENGL environment, that is, I drew a cube (presumably in the center of the view) in my own defined framebuffer (I defined it as fbo) and colored it in red. And nothing is drawn in the default buffer, so I can't see my cube in the view (but it is drawn in the framebuffer fbo)
I hope that when I click in the middle of the view (the position of the cube), I can display the color of the cube----red instead of the color of the background of the view.
Here are the problems I have encountered:
I can only get the color of the background of the view with the mouse, and the cube drawn in the frame buffer is not picked up (the cube is red).
In the initializeGL() function, I initialized and configured the framebuffer fbo and some of the configuration required to draw the cube.
In the paintGL() function, I bind my defined framebuffer to GL_FRAMEBUFFER and then do the drawing.
In the mousePressEvent(QMouseEvent *e) function, I did the picking work. This is the response function of the QT mouse click. I put the information read by glReadPixels into the array pix and print it out.
void OpenglWidget::initializeGL()
{
core1 = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_4_0_Core>();
core = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>();
core->glGenFramebuffers(1, &fbo);
core->glBindFramebuffer(GL_FRAMEBUFFER, fbo);
// Create the texture object for the primitive information buffer
core->glGenTextures(1, &m_pickingTexture);
glBindTexture(GL_TEXTURE_2D, m_pickingTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32UI, SCR_WIDTH, SCR_HEIGHT,
0, GL_RGB_INTEGER, GL_UNSIGNED_INT, NULL);
core1->glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D,
m_pickingTexture, 0);
// Create the texture object for the depth buffer
core->glGenTextures(1, &m_depthTexture);
core->glBindTexture(GL_TEXTURE_2D, m_depthTexture);
core->glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, SCR_WIDTH, SCR_HEIGHT,
0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
core1->glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D,
m_depthTexture, 0);
// Disable reading to avoid problems with older GPUs
core->glReadBuffer(GL_NONE);
// Verify that the FBO is correct
GLenum Status = core->glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (Status == GL_FRAMEBUFFER_COMPLETE) {
qDebug() << "ok";
}
// Restore the default framebuffer
core->glBindTexture(GL_TEXTURE_2D, 0);
core->glBindFramebuffer(GL_FRAMEBUFFER, 0);
GLfloat verticPosition[] = {
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
};
GLint beamIndices[] = {
0,1,2,3,4,5,6,7
};
core->glGenVertexArrays(1, &VAOBeam);
core->glGenBuffers(1, &VBOBeam);
core->glGenBuffers(1, &EBOBeam);
core->glBindVertexArray(VAOBeam);
core->glBindBuffer(GL_ARRAY_BUFFER, VBOBeam);
core->glBufferData(GL_ARRAY_BUFFER,sizeof(float) * 3 * 36, verticPosition, GL_STATIC_DRAW);
core->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBOBeam);
core->glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLint) * 8, beamIndices, GL_STATIC_DRAW);
core->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
core->glEnableVertexAttribArray(0);
core->glBindBuffer(GL_ARRAY_BUFFER, 0);
core->glBindVertexArray(0);
}
void OpenglWidget::paintGL()
{
core->glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glClearColor(0,0,1,1);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
float frametime =GetFrameTime();
mCamera.Update(frametime);
Shader GLprogram = Shader("E:\\QT\\OpenglTest\\vs.vert","E:\\QT\\OpenglTest\\fs.frag");
GLprogram.use();
core->glBindVertexArray(VAOBeam);
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, mCamera.TempTranslateVrc);
glm::mat4 view = glm::mat4(1.0f);
//view = glm::lookAt(glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.5f,0.5f,0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
view = glm::lookAt(mCamera.mPos,mCamera.mViewCenter,mCamera.mUp);
glm::mat4 projection = glm::mat4(1.0f);
projection = glm::perspective(glm::radians(45.0f), (float)1.0, 0.1f, 100.0f);
GLprogram.setMatrix4f("model",model);
GLprogram.setMatrix4f("view",view);
GLprogram.setMatrix4f("projection",projection);
core->glDrawArrays(GL_TRIANGLES,0,36);
core->glUseProgram(0);
update();
}
void OpenglWidget::mousePressEvent(QMouseEvent *e)
{
core1 = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_4_0_Core>();
float pix[4] = {0.0};
mCamera.getInitPos(e->x(),e->y());//This is the camera's setup function, no need to worry about it.
core1->glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);
core1->glReadBuffer(GL_COLOR_ATTACHMENT1);
core1->glReadPixels(e->x() , 500 - e->y() + 65, 1, 1, GL_RGBA, GL_FLOAT, pix);
qDebug() << e->x()<< 500 - e->y() <<pix[0] << pix[1] << pix[2] << pix[3];
}
I can only get the color of the background of the view with the mouse, and the cube drawn in the frame buffer is not picked up (the cube is red).
The issue is caused, because in the color buffer attached to GLCOLOR ATTACHMENT1, never is rendered to.
The name of the enumerator constant for the 1st color buffer, which can be attached by glFramebufferTexture2D to a framebuffer is GL_COLOR_ATTACHMENT0, rather than not GL_COLOR_ATTACHMENT1:
void OpenglWidget::initializeGL()
{
// [...]
core1->glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0, // <---- GL_COLOR_ATTACHMENT0 instead of GL_COLOR_ATTACHMENT1
GL_TEXTURE_2D,
m_pickingTexture, 0);
// [...]
}
void OpenglWidget::mousePressEvent(QMouseEvent *e)
{
// [...]
core1->glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);
core1->glReadBuffer(GL_COLOR_ATTACHMENT0); // <---- GL_COLOR_ATTACHMENT0
// [ ...]
}
Note, by default it is rendered to the first color buffer (GL_COLOR_ATTACHMENT0) of a framebuffer. If you want to render to an other color buffer, than the 1st one, then you would have to specify a color buffer (list) explicitly by glDrawBuffers.
See OpenGL 4.6 API Core Profile Specification; 17.4.1 Selecting Buffers for Writing; page 513:
17.4.1 Selecting Buffers for Writing
[...] The set of buffers of a framebuffer object to which all fragment colors are written is controlled with the commands
void DrawBuffers( sizei n, const enum *bufs );
void NamedFramebufferDrawBuffers( uint framebuffer, sizei n, const enum *bufs );
[...] For framebuffer objects, in the initial state the draw buffer for
fragment color zero is COLOR_ATTACHMENT0. For both the default framebuffer
and framebuffer objects, the initial state of draw buffers for fragment colors other then zero is NONE.

How can I instantiate an array inside the base class from a derived class?

I have a base class that contains an unassigned pointer _vertices, and I want to populate it from the derived class constructor:
class GameRenderable {
bool _indexed;
int _vertSize;
int _idxSize;
unsigned int VAO, VBO, EBO;
unsigned int _texture0;
protected:
float *_vertices;
unsigned int *_indices;
public:
GameRenderable(GameRenderableMode grm);
~GameRenderable();
void Render();
protected:
void SetupBuffer(int, int);
};
void GameRenderable::SetupBuffer(int vs, int is) {
_indexed = false;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vs, _vertices, GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// texture coord attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
_idxSize = is;
_vertSize = vs;
}
void GameRenderable::Render() {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, _texture0);
glBindVertexArray(VAO);
glm::mat4 model = glm::mat4(1.0f);
GetShader()->setMat4("model", model);
glDrawArrays(GL_TRIANGLES, 0, _vertSize);
}
The derived classes:
class Cube : public GameRenderable {
public:
Cube();
~Cube();
};
Cube::Cube():GameRenderable(GameRenderableMode::_3D) {
/* Cube ASCII
E--------F A -0.5f, 0.5f, -0.5f
/| /| B 0.5f, 0.5f, -0.5f
A--------B | C -0.5f, -0.5f, -0.5f
| | | | D 0.5f, -0.5f, -0.5f
| G------|-H E -0.5f, 0.5f, 0.5f
|/ |/ F 0.5f, 0.5f, 0.5f
C--------D G -0.5f, -0.5f, 0.5f
H 0.5f, -0.5f, 0.5f
*/
float vertices[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f
};
_vertices = new float[sizeof(vertices)];
memcpy_s(_vertices, sizeof(vertices), vertices, sizeof(float));
SetupBuffer(36, 0);
SetupTexture("container.jpg");
}
Cube::~Cube() {
}
When I call SetupBuffer() the array passed to openGL is always only 1 element, and nothing is drawn. If I put the code of SetupBuffer() directly inside the constructor, it works.
GL Buffer Issues
The sizeof( type ) operator returns the size in bytes, so for example the code:
std::cout << sizeof(float) << std::endl;
will print the output: 4
so on the following line:
_vertices = new float[sizeof(vertices)];
you are allocating 4 times the amount of data that you need to store the array vertices. It should be something like:
_vertices = new float[sizeof(vertices) / sizeof(float)];
//this is more ugly but could also be:
_vertices = (float*)(new char[sizeof(vertices)]);
//that works since an ascii 'char' is 1 byte
the function memcpy_s from MSDN:
Parameters
dest
New buffer.
destSize
Size of the destination buffer, in bytes for memcpy_s and wide characters (wchar_t) for wmemcpy_s.
src
Buffer to copy from.
count
Number of characters to copy.
so the call should be something like:
memcpy_s(_vertices, sizeof(vertices), vertices, sizeof(vertices));
//although, I would just use the regular 'memcpy':
memcpy(_vertices, vertices, sizeof(vertices));
Your SetupBuffer function takes the parameter vs which is input directly into glBufferData, however glBufferData also takes the size in bytes:
size
Specifies the size in bytes of the buffer object's new data store.
so entering the value 36 will not suffice. You want to find a way to pass the value of sizeof(vertices), although since you know the stride of each vertex (5 floats), you could change the call to:
glBufferData(GL_ARRAY_BUFFER, vs * 5 * sizeof(float), _vertices, GL_STATIC_DRAW);
As many of the comments mentioned, you could also use the std::vector class to simplify some of the code, however I think it is much more important to address the misunderstandings in the code you have already written.
OOP Issues
I am fairly sure the above will fix your problem. However, just to make sure the constructor:
Cube::Cube() : GameRenderable(GameRenderableMode::_3D) {
doesn't overwrite the GameRenderable constructor, the GameRenderable constructor will be called first (with the parameter _3D) so ensure that the code within the GameRenderable constructor doesn't break anything (as I can't see it).

C++ Remove invisible faces VBO

I made a VBO cube a while ago, It work's great but it has a lack of performance.
How can I remove invisible faces in a VBO?
I also have a problem with the textures, they are seemed to be messed up :/
If anyone knows something to fix that, it would be great!
My code:
#include <windows.h>
#include <iostream>
#include <glew.h>
#include <GL/gl.h>
#include <GL/glu.h>
#define OFFSET_BUFFER(bytes) ((GLfloat *)NULL + bytes)
PFNGLGENBUFFERSARBPROC glGenBuffers = NULL;
PFNGLBINDBUFFERPROC glBindBuffer = NULL;
PFNGLBUFFERDATAPROC glBufferData = NULL;
GLfloat vertex[];
GLuint m_vertexBuffer;
GLuint m_textureBuffer;
void VBOinit()
{
#ifdef _WIN32
glGenBuffers = (PFNGLGENBUFFERSARBPROC)wglGetProcAddress("glGenBuffers");
glBindBuffer = (PFNGLBINDBUFFERPROC)wglGetProcAddress("glBindBuffer");
glBufferData = (PFNGLBUFFERDATAPROC)wglGetProcAddress("glBufferData");
#else
glGenBuffers = (PFNGLGENBUFFERSARBPROC)glXGetProcAddress((const GLubyte*)"glGenBuffers");
glBindBuffer = (PFNGLBINDBUFFERPROC)glXGetProcAddress((const GLubyte*)"glBindBuffer");
glBufferData = (PFNGLBUFFERDATAPROC)glXGetProcAddress((const GLubyte*)"glBufferData");
#endif
if (!glGenBuffers || !glBindBuffer || !glBufferData)
{
std::cerr << "VBOs are not supported by your graphics card" << std::endl;
return;
}
// TEXTURE VBO
GLfloat texture[] =
{
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,
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
};
glGenBuffers(1, &m_textureBuffer);
glBindBuffer(GL_ARRAY_BUFFER, m_textureBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(texture), &texture, GL_STATIC_DRAW);
// GEOMETRIC VBO
GLfloat vertex[] =
{
0.5f, 0.5f, 0.5f, -0.5f, 0.5f, 0.5f, -0.5f, -0.5f, 0.5f, // v0-v0.5f-v2 (front)
-0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f, 0.5f, 0.5f, 0.5f, // v2-v3-v0
0.5f, 0.5f, 0.5f, 0.5f, -0.5f, 0.5f, 0.5f, -0.5f, -0.5f, // v0-v3-v4 (right)
0.5f, -0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f, 0.5f, 0.5f, // v4-v5-v0
0.5f, 0.5f, 0.5f, 0.5f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f, // v0-v5-v6 (top)
-0.5f, 0.5f, -0.5f, -0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, // v6-v0.5f-v0
-0.5f, 0.5f, 0.5f, -0.5f, 0.5f, -0.5f, -0.5f, -0.5f, -0.5f, // v0.5f-v6-v7 (left)
-0.5f, -0.5f, -0.5f, -0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f, // v7-v2-v0.5f
-0.5f, -0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f, 0.5f, // v7-v4-v3 (bottom)
0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f, -0.5f, -0.5f, // v3-v2-v7
0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, 0.5f, -0.5f, // v4-v7-v6 (back)
-0.5f, 0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f, -0.5f, -0.5f // v6-v5-v4
};
glGenBuffers(1, &m_vertexBuffer); //Generate a buffer for the vertices
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer); //Bind the vertex buffer
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex), &vertex[0], GL_STATIC_DRAW); //Send the data to OpenGL
}
void VBOrender(int x, int y, int z)
{
glBindTexture(GL_TEXTURE_2D, Texture::textures[0]);
glTranslatef(x, y, z);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);
glVertexPointer(3, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, m_textureBuffer);
glTexCoordPointer(2, GL_FLOAT, 0, OFFSET_BUFFER(0));
//glPolygonMode(GL_FRONT, GL_LINE);
glDrawArrays(GL_TRIANGLES, 0, 36);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glTranslatef(-x, -y, -z);
}
Thank you for reading :)
Adding detail on what we started in the comments above, plus a couple more things.
As was previously suggested, add glEnable(GL_CULL_FACE). I doubt that it addresses your bottleneck, but it can't hurt.
One other thing you should generally do is store your positions and texture coordinates interleaved in a single buffer, instead of storing them in separate buffers. Again, I think you're limited elsewhere right now, just a general recommendation.
To illustrate what I suggested in the comment about avoiding to set redundant state inside your loop. Right now your structure in pseudo code looks like this:
loop over x, y, z
VBOrender(x, y, z)
end loop
Instead, I would structure it like this:
VBObind()
loop over x, y, z
VBOrender(x, y, z)
end loop
VBOunbind()
And split up the code in your current VBOrender function like this:
void VBObind()
{
glBindTexture(GL_TEXTURE_2D, Texture::textures[0]);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);
glVertexPointer(3, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, m_textureBuffer);
glTexCoordPointer(2, GL_FLOAT, 0, OFFSET_BUFFER(0));
}
void VBOunbind()
{
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
void VBOrender(int x, int y, int z)
{
glTranslatef(x, y, z);
glDrawArrays(GL_TRIANGLES, 0, 36);
glTranslatef(-x, -y, -z);
}
I expect that this will give you a significant performance improvement. To get massively better performance, you would need something more dramatic, like packing all the cubes in a single draw call. This looks slightly tricky, though, because from what I can see in your code on pastebin, the rendering of each cube is conditional.
If you're willing to write your own shaders, you can make the translation an attribute, which would be much faster to update than the fixed function transformation matrix.
You can turn on back-face culling by calling:
glFrontFace(GL_CCW); // <- says faces are defined in counter-clockwise order. Change if yours are in clockwise order
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
As for your textures, it looks like you've defined texture coordinates for 11 cube faces, but you you have 36 triangles you're drawing. 36 triangles would be 18 quads, so I think you need more texture coordinates.