Opengl 2.0 with multiple VAO - opengl

I was trying to setup a 2d screen with two layers of drawing
1) a grid made of white solid lines
2) tiles made of colored square
here are my opengl codes:
setup grid
//glGenBuffers(1, &gridVAO);
glGenVertexArrays(1, &gridVAO); // **updated**
glBindVertexArray(gridVAO);
glGenBuffers(2, gridVBO);
glBindBuffer(GL_ARRAY_BUFFER, gridVBO[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(gridPos), gridPos, GL_STATIC_DRAW);
glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, gridVBO[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(gridColors), gridColors, GL_STATIC_DRAW);
glVertexAttribPointer(vColor, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
setup tile
// glGenBuffers(1, &tileVAO);
glGenVertexArrays(1, &tileVAO); //**updated**
glBindVertexArray(tileVAO);
glGenBuffers(2, tileVBO);
glBindBuffer(GL_ARRAY_BUFFER, tileVBO[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(tilePos), tilePos, GL_STATIC_DRAW);
glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, tileVBO[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(tileColors), tileColors, GL_STATIC_DRAW);
glVertexAttribPointer(vColor, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
draw tiles
glBindVertexArray(tileVAO);
glDrawArrays(GL_TRIANGLES, 0, TILE_COUNT_X * TILE_COUNT_Y * 6);
glBindVertexArray(0);
draw grid
glBindVertexArray(gridVAO);
glDrawArrays(GL_LINES, 0, girdVertexCount);
glBindVertexArray(0);
by keeping only setup+draw grid produces this:
by keeping only setup+draw tiles produces this:
The VAOs are working properly by themselves.
However, magic begins here:
If I do the combination of
setup grid
setup tiles
// draw tiles
draw grid
It produces this:
Looks like the draw grid codes pick up buffers from tile's VAO. Help Please :D
update:
after corrected to glGenVertexArrays the same problem still happens
but i found both VAOs are 0
cout<<"gridVAO "<<gridVAO<<endl;
cout<<"tileVAO "<<tileVAO<<endl;
output:
gridVAO 0
tileVAO 0

Related

OpenGL catmull rom spline loop coloring

I have a catmull rom spline loop in c++. I able to make the spline and i have the curve points, but when I tried to fill the bordered area with GL_TRIANGLE_FAN I got too much straight line. And my question is, how can I fill the spline loop area with colour efficiently?
This is how I draw the spline:
glBindVertexArray(vao);
unsigned int vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(lines) * sizeof(float), lines, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_LINE_STRIP, 0, vecLinePoints.size());
EDIT:
This is how I tried to fill my concave polygone:
glClear(GL_DEPTH_BUFFER_BIT);
glDisable(GL_STENCIL_TEST);
glStencilMask(0xFF);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glStencilOp(GL_INVERT, GL_INVERT, GL_INVERT);
glClear(GL_STENCIL_BUFFER_BIT);
glBindVertexArray(vao);
unsigned int vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(polyPoints) * sizeof(float), polyPoints, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLE_FAN, 0, vecPoints.size());
glStencilMask(0x00);
glStencilFunc(GL_NOTEQUAL, 0, 0xFF);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDisable(GL_STENCIL_TEST);

glDrawElements not drawing

I create my buffers with the following code:
//generate buffers
glGenVertexArrays(1, &VAO);
//glGenBuffers(1, &EBO);
glGenBuffers(1, &VBO_vertices);
glGenBuffers(1, &VBO_colors);
glGenBuffers(1, &VBO_normals);
// Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s).
glBindVertexArray(VAO);
// Copy our vertices array in a buffer for OpenGL to use
glBindBuffer(GL_ARRAY_BUFFER, VBO_vertices);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*vertices.size(), &vertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, vTable.size() * sizeof(int), &vTable[0], GL_STATIC_DRAW);
// Position attribute
glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0); //size = 3 (X,Y,Z)
glEnableVertexAttribArray(0);
//Buffer for color
glBindBuffer(GL_ARRAY_BUFFER, VBO_colors);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*vertices.size(), &v_color[0], GL_STATIC_DRAW);
// Color attribute
glVertexAttribPointer((GLuint)1, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0); //size = 3 (R,G,B)
glEnableVertexAttribArray(1);
//Buffer for normals
glBindBuffer(GL_ARRAY_BUFFER, VBO_normals);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*vertices.size(), &v_normals[0], GL_STATIC_DRAW);
//normal attribute
glVertexAttribPointer((GLuint)2, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0); //size = 3 (R,G,B)
glEnableVertexAttribArray(2);
// Unbind the VAO
glBindVertexArray(0);
My data are :
vector<vec3> vertices, v_normals,v_color;
vector<int> vTable;
I have vertices, normals and colors per vertex and an index table with the index vertices of each triangle.
When I try to render this, nothing appears on the window.
glBindVertexArray(VAO); //Bind VAO
glDrawElements(GL_TRIANGLES, vTable.size(), GL_UNSIGNED_INT, &vTable[0]);
glBindVertexArray(0); //Unbind VAO
If I used this:
glDrawArrays(GL_TRIANGLES,0,vTable.size());
It draws something but an incomplete object, like in the link image.
image
Anybody knows what happens? Thanks in advance
Your glDrawElements call is wrong, the last parameter should be a byte offset into your GL_ELEMENT_ARRAY_BUFFER that holds the indices, not pointer to system memory.
glDrawElements(GL_TRIANGLES, vTable.size(), GL_UNSIGNED_INT, 0);

Drawing multiple objects with glDrawArrays

UPDATE: Upon further investigation, it seems to be switching the program that causes the 2nd object to not be drawn. I don't know why. Both objects use essentially the same GLSL.
I have a hunch I'm not using buffers right. I have a cube and a prism defined. If I comment out the prism's draw call, the cube draws. otherwise only the prism draws. What am I missing here
Draw Code:
glUseProgram(cubeProgram);
glBindBuffer(GL_ARRAY_BUFFER, vertexArrayBuffers[0]);
glVertexAttribPointer(cVPos, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(cVPos);
glBindBuffer(GL_ARRAY_BUFFER, vertexArrayBuffers[1]);
glVertexAttribPointer(cNormID, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(cNormID);
glDrawArrays(GL_TRIANGLES, 0, 36);
glUseProgram(priProgram);
glBindBuffer(GL_ARRAY_BUFFER, vertexArrayBuffers[2]);
glVertexAttribPointer(pVPos, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(pVPos);
glBindBuffer(GL_ARRAY_BUFFER, vertexArrayBuffers[3]);
glVertexAttribPointer(pNormID, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(pNormID);
glDrawArrays(GL_TRIANGLES, 0, 64);
VBO Creation:
glGenBuffers(4, vertexArrayBuffers);
glBindBuffer(GL_ARRAY_BUFFER, vertexArrayBuffers[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVerts), cubeVerts, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vertexArrayBuffers[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(cubeNorms), cubeNorms, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vertexArrayBuffers[2]);
glBufferData(GL_ARRAY_BUFFER, sizeof(priVerts), priVerts, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vertexArrayBuffers[3]);
glBufferData(GL_ARRAY_BUFFER, sizeof(priNorms), priNorms, GL_STATIC_DRAW);
Let me know if there's more code needed.
So it was my GLSL causing the problem. I was calling a function to set variables for the shader and one of them seems to have unset. I recalled the function after drawing the first object and they both draw. Hooray!

Proper way to use indexed VBO

I am working on a simple 3D Engine. I currently have a working setup with multiple VAO's which I can switch between during the render loop, but they all are not using index buffers.
I'm now trying to add a new VAO composed of 4 VBO's: vert position, color, normal and indices.
Everything compiles and runs but the drawing calls to the second VAO (with indexed vertices) do not render. I'm sure there is a problem with my setup somewhere, so I've added this code which includes all the VAO and VBO generations, calls, and uses. Does anything in this code seem wrong, and is this the correct way to set it all up?
VAO1 has 3 buffers: position, color, normals
VAO2 has 3 buffers: position, color, normals and vertex indices
//Initalize vaos and vbos
GLuint vao1, vbo1[3];
GLuint vao2, vbo2[4];
//Generate Vertex arrays:
glGenVertexArrays(1, &vao1);
glGenVertexArrays(1, &vao2);
//Generate Buffers:
glGenBuffers(3, vbo1);
glGenBuffers(4, vbo2);
//Initalize Bufferdata vectors:
vector<GLfloat> VertPosBuffer1Vector;
vector<GLfloat> VertNormalBuffer1Vector;
vector<GLfloat> VertColorBuffer1Vector;
vector<GLfloat> VertPosBuffer2Vector;
vector<GLfloat> VertNormalBuffer2Vector;
vector<GLfloat> VertColorBuffer2Vector;
vector<GLuint> VertIndexBuffer2Vector;
//Fill Buffers:
//(not included but all vectors are filled with data)
//VAO 1
glBindVertexArray(vao1);
//Vertex position buffer:
glBindBuffer(GL_ARRAY_BUFFER, vbo1[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*VertPosBuffer1Vector.size(), &VertPosBuffer1Vector[0], GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(0);
//Vertex color buffer:
glBindBuffer(GL_ARRAY_BUFFER, vbo1[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*VertColorBuffer1Vector.size(), &VertColorBuffer1Vector[0], GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(1);
//Vertex normal buffer:
glBindBuffer(GL_ARRAY_BUFFER, vbo1[2]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*VertNormalBuffer1Vector.size(), &VertNormalBuffer1Vector[0], GL_STATIC_DRAW);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(2);
//VAO 2
glBindVertexArray(vao2);
//Vertex position buffer:
glBindBuffer(GL_ARRAY_BUFFER, vbo2[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*VertPosBuffer2Vector.size(), &VertPosBuffer2Vector[0], GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(0);
//Vertex color buffer:
glBindBuffer(GL_ARRAY_BUFFER, vbo2[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*VertColorBuffer2Vector.size(), &VertColorBuffer2Vector[0], GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(1);
//Vertex normal buffer:
glBindBuffer(GL_ARRAY_BUFFER, vbo2[2]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*VertNormalBuffer2Vector.size(), &VertNormalBuffer2Vector[0], GL_STATIC_DRAW);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(2);
//Vertex index buffer:
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo2[3]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint)*VertIndexBuffer2Vector.size(), &VertIndexBuffer2Vector[0], GL_STATIC_DRAW);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(3);
//unbind vao
glBindVertexArray(0);
//bind first vao
glBindVertexArray(vao1);
and
//RENDERLOOP
//render objects from vao1 using:
glDrawArrays(GL_TRIANGLES, start, size);
//switch vao
glBindVertexArray(0);
glBindVertexArray(vao2);
//render objects from vao2 using:
glDrawElements(
GL_TRIANGLES,
start,
GL_UNSIGNED_INT,
(void*)0
);
I have checked that the data in my buffers are correct.
Is it correct that the shader doesn't take in any information of indices? The shader will be the same as if I didn't use an index buffer?
Thank you
The indices are not a vertex attribute. So what you need to do is remove these two lines:
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(3);
I also noticed that you are using the variable "start" as the count argument for glDrawElements. I don't know the values of start and size, but I assume you should use "size" as the second argument in glDrawElements.

OpenGL VBO, VAO, Cubes, and Colors

I'm currently trying to get my head around VBOs and I'm running into some problems.
I'm using an interleaved array with position, colors, and normals. However, when I go to draw, the display is just white.
This is the structure of my array:
GLfloat position[3];
GLfloat normal[3];
GLfloat color[4];
Here's the code:
Initialization:
glGenBuffers(1, &arrays[0]);
glBindBuffer(GL_ARRAY_BUFFER, arrays[0]);
glBufferData(GL_ARRAY_BUFFER, 125*10*36*sizeof(GLfloat), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glGenVertexArrays(1, &vaoID[1]);
glBindVertexArray(vaoID[1]);
glBindBuffer(GL_ARRAY_BUFFER, vaoID[1]);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 40, ((void*)0));
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 40, ((void*)12));
glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, GL_TRUE, 40, ((void*)24));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glBindVertexArray(0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Draw:
glPushMatrix();
glBindVertexArray(vaoID[1]);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 125*36);
glPopMatrix();
I'm making a 5x5 cube of cubes that spectrum in color from black to white. However, on draw, this is all I'm getting:
I see a problem with what you've posted above. You've defined your colors as being floats in the struct, but you're telling glVertexAttribPointer() that your colors are unsigned bytes. It should probably be something like this:
glVertexAttribPointer(2, 4, GL_FLOAT, GL_TRUE, 40, ((void*)24));
And that assumes that you're actually putting floats into those memory locations. How are you setting them?