glDrawArrays doesn't draw - c++

I use vectors to store vertex and normal data
vector<float> vertex;
vector<float> normal;
For example:
normal.push_back(-1);
normal.push_back(0);
normal.push_back(0);
vertex.push_back(BARRIER_RADIUS);
vertex.push_back(POLE_HEIGHT);
vertex.push_back(-POLE_RADIUS);
for (int i = POLE_POINTS, i >= 0; i--)
{
//add more vertex
}
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, &vertex[0]);
glNormalPointer(GL_FLOAT, 0, &normal[0]);
glDrawArrays(GL_TRIANGLE_FAN, 0, vertex.size());
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
//Add some more + Draw some thing else the same way
What I've got from this is nothing. it doesn't draw anything onto the screen.
The previous version, which works, is as follow:
glNormal3f(-1, 0, 0);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(BARRIER_RADIUS, POLE_HEIGHT, -POLE_RADIUS);
for ()
{
//add more vertex
glVertex3f(........);
}
glEnd();
//draw more the same way
Point me to where I've gone wrong.

You have defined vertex as a vector of float, but a geometric vertex consists of 3 floats, so that's a recipe for confusion. The number of vertices is not vertex.size(), but rather one third of that. I'm not sure if that's your only problem, but it's a problem.
You should have the same number of normals as vertices.

Related

OpenGL vertex array not rendering

I'm trying to draw some basic triangles using opengl, but it's not rendering on screen. These are the relevant functions:
glewInit();
glClearColor(0.0, 0.0, 0.0, 1.0);
glFrontFace(GL_CW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
Vertex vertices[] = {Vertex(Vector3f(0.0, 1.0, 0.0)),
Vertex(Vector3f(-1.0, -1.0, 0.0)),
Vertex(Vector3f(1.0, -1.0, 0.0))};
mesh.addVertices(vertices, 3);
Pastebin links to Vertex.hpp and Vector3f.hpp:
Vertex.hpp
Vector3f.hpp
/*
* Mesh.cpp:
*/
Mesh::Mesh()
{
glGenBuffers(1, &m_vbo); // unsigned int Mesh::m_vbo
}
void Mesh::addVertices(Vertex vertices[4], int indexSize)
{
m_size = indexSize * 3;
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBufferData(GL_ARRAY_BUFFER, m_size, vertices, GL_STATIC_DRAW);
}
void Mesh::draw()
{
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 4 * sizeof(Vertex), 0);
glDrawArrays(GL_TRIANGLES, 0, m_size);
glDisableVertexAttribArray(0);
}
It's just black if I call glClear otherwise just the random noise of a default window. I can make it draw a triangle by using the most primitive method:
glBegin(GL_TRIANGLES);
glColor3f(0.4, 0.0, 0.0);
glVertex2d(0.0, 0.5);
glVertex2d(-0.5, -0.5);
glVertex2d(0.5, -0.5);
glEnd();
That works and displays what it should do correctly, so I guess that at least says my application is not 100% busted. The tutorial I'm following is in Java, and I'm translating it to C++ SFML as I go along, so I guess it's possible that something got lost in translation so to speak, unless I'm just missing something really basic (more likely.)
How do we fix this so it uses the Vertex list to draw the triangle like it's supposed to?
So many mistakes. There are truly a lot of examples, in any language, so why?
const float pi = 3.141592653589793; is member field of Vector3f. Do you realise this is non-static member and it is included in each and every Vector3f you use, so your vectors actually have four elements - x, y, z, and pi? Did you informed GL about it, so it could skip this garbage data? I don't think so.
You using glVertexAttribPointer, but don't have active shader. There is no guarantee that position is in slot 0. Either use glVertexPointer, or use shader with position attribute bound to 0.
void Mesh::addVertices(Vertex vertices[4], int indexSize) what [4] supposed to mean here? While it is not an error, it is at least misguiding.
glBufferData(GL_ARRAY_BUFFER, m_size, vertices, GL_STATIC_DRAW); m_size is 3*3 in your example, while documentation says it should be array size in bytes - which is sizeof(Vertex) * indexSize.
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 4 * sizeof(Vertex), 0); why stride parameter is 4*sizeof(Vertex)? Either set it to 0 or write correct stride - which is sizeof(Vertex).
glDrawArrays(GL_TRIANGLES, 0, m_size); m_size is already [incorrectly] set as "vertex buffer size", while DrawArrays expects number of vertices to draw - which is m_size / sizeof(Vertex) (given m_size is calculated correctly).

OpenGL: glDrawElements doesn't draw

I'm trying out the Vertex Arrays stuff but for some reason the glDrawElements command doesn't draw anything for me. I can draw using glBegin/glEnd and glDrawElements in between, but glDrawElements doesn't work. Here's a code snippet:
These arrays get set up in the constructor:
double points[100];
GLint indices[50];
for (int i=0; i < 50; i++){
points[2*i] = radius * cos(i*2*PI/50);
points[2*i + 1] = radius * sin(i*2*PI/50);
indices[i] = i;
}
Working code using only the points array with glArrayElement:
void GLCircle::draw()
{
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_DOUBLE, 0, points);
glBegin(GL_POLYGON);
for (int i=0; i < 50; i++){
glArrayElement(i);
}
glEnd();
}
Also working code, using points array, specific indices accessed via indices array:
void GLCircle::draw()
{
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_DOUBLE, 0, points);
glBegin(GL_POLYGON);
for (int i=0; i < 50; i++){
glArrayElement(indices[i]);
}
glEnd();
}
NON-working code, attempting to use glDrawElements:
void GLCircle::draw()
{
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_DOUBLE, 0, points);
glDrawElements(GL_POLYGON, 4, GL_INT, indices);
}
Any advice? It's not entirely necessary for me to use it at this point, but it's disturbing that it doesn't work...
The 2nd parameter of glDrawElements is count, so shouldn't 4 be the numbers of indices (50)?
The parameters for
glDrawElements()
are as follows.. :
1st [mode] parameter is what kind of primitive to render.
2nd [count] parameter should be the number of elements to render. ie. the number of vertices
3rd [type] parameter should be the type of the value in the 4th parameter.. can ONLY be either
GL_UNSIGNED_BYTE or GL_UNSIGNED_SHORT or GL_UNSIGNED_INT
4th [indices] parameter is a pointer to where the indices are stored.
You can read more on this here..
I just faced the same problem.
Try GL_UNSIGNED_INT
glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_INT, indices);

Using C++ std::Vector in glVertexPointer OpenGL function

I have a vector of floats, this vector describes a set of triangles each triangle is described using 18 floats, first 3 are the first vertex, next 3 decribes the normal of the last vertex, and so on 3 times until describe each triangle.
i am using
std::vector< GLfloat >
to store all numbers, and this is my code to render the triangles
void Visualizer::draw3dModel()
{
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glNormalPointer(GL_FLOAT, 6 * sizeof(GLfloat), this->vertexes->data() + 3);
glVertexPointer(3, GL_FLOAT, 6 * sizeof(GLfloat), this->vertexes->data());
glPushMatrix();
glScalef(0.05f, 0.05f, 0.05f);
glColor3f(1,1,1);
glDrawArrays(GL_TRIANGLES, 0, this->vertexes->size());
glPopMatrix();
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
}
this code actually works, but in certain moments I recreate the vector of floats with new triangles, it can be more or less triangles
before creating new triangles I clear the vector of vertexes, using this functions
std::vector< GLfloat >* MarchingCubesThread::getTriangles(float minvalue_scale)
{
this->generateTriangles(minvalue_scale);
for(unsigned int i=0; i < this->num_triangles; i++)
{
for(int j=0; j < 3; j++)
{
this->vertexes.push_back(this->triangles[i].p[j].x);
this->vertexes.push_back(this->triangles[i].p[j].y);
this->vertexes.push_back(this->triangles[i].p[j].z);
this->vertexes.push_back(this->triangles[i].norm.x);
this->vertexes.push_back(this->triangles[i].norm.y);
this->vertexes.push_back(this->triangles[i].norm.z);
}
}
return &(this->vertexes);
}
void MarchingCubesThread::generateTriangles(float minvalue_scale)
{
this->vertexes.clear();
this->triangles.clear();
this->triangles = MarchingCubesDataset(this->dataset->getMaxVal() * minvalue_scale, *(this->dataset), LinearInterp, this->num_triangles);
}
After playing around creating a new set of new triangles, the OpenGL render updates the mesh well, but in certain moments, I get some garbage triangles, and/or triangles that are from last iteration, and they should be cleared by calling this:
this->vertexes.clear();
this->triangles.clear();
here some screenshots taken in consecutive times:
any clues about what is happening here?, thank you
PD: for a complete source code, this is the public git repository on github:
https://github.com/joecabezas/MemoriaJoeCabezasCode/tree/visualizer
glDrawArrays expects not count of floats in your buffer, but count of vertices.
So correct call should be:
glDrawArrays(GL_TRIANGLES, 0, this->vertexes->size() / 6);
By letting glDrawArrays to draw 6 times more vertices you were displaying garbage after end of valid vector data, and sometimes it was data from previous iteration.

Rendering mesh polygons in OpenGL - very slow

I recently switched from intermediate mode and have a new rendering process. There must be something I am not understanding. I think it has something to do with the indices.
Here is my diagram: Region->Mesh->Polygon Array->3 vertex indices which references the master list of vertices.
Here my render code:
// Render the mesh
void WLD::render(GLuint* textures, long curRegion, CFrustum cfrustum)
{
int num = 0;
// Set up rendering states
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
// Set up my indices
GLuint indices[3];
// Cycle through the PVS
while(num < regions[curRegion].visibility.size())
{
int i = regions[curRegion].visibility[num];
// Make sure the region is not "dead"
if(!regions[i].dead && regions[i].meshptr != NULL)
{
// Check to see if the mesh is in the frustum
if(cfrustum.BoxInFrustum(regions[i].meshptr->min[0], regions[i].meshptr->min[2], regions[i].meshptr->min[1], regions[i].meshptr->max[0], regions[i].meshptr->max[2], regions[i].meshptr->max[1]))
{
// Cycle through every polygon in the mesh and render it
for(int j = 0; j < regions[i].meshptr->polygonCount; j++)
{
// Assign the index for the polygon to the index in the huge vertex array
// This I think, is redundant
indices[0] = regions[i].meshptr->poly[j].vertIndex[0];
indices[1] = regions[i].meshptr->poly[j].vertIndex[1];
indices[2] = regions[i].meshptr->poly[j].vertIndex[2];
// Enable texturing and bind the appropriate texture
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textures[regions[i].meshptr->poly[j].tex]);
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &vertices[0].x);
glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &vertices[0].u);
// Draw
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, indices);
}
}
}
num++;
}
// End of rendering - disable states
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
Sorry if I left anything out. And I really appreciate feedback and help with this. I would even consider paying someone who is good with OpenGL and optimization to help me with this.
There is no point in using array rendering if you're only rendering 3 vertices at a time. The idea is to send thousands through with a single call. That is, you render a single "Polygon Array" or "Mesh" with one call.

How to use the stride parameter in VBO?

I have problems understanding how to use one buffer with 3 different data in it, I have the following structure for my vert,text,color data:
struct xyzf {
float x, y, z;
};
struct xyf {
float x, y;
};
struct vertype {
xyzf points[4];
};
struct textype {
xyf points[4];
};
struct coltype {
GLuint points[4];
};
struct buftype {
vertype vertex;
textype texcoord;
coltype color;
};
Then I (try to) use it in the following way (which makes most sense to me):
glBindBufferARB(GL_ARRAY_BUFFER, mybufid);
glVertexPointer(3, GL_FLOAT, sizeof(buftype), 0);
glTexCoordPointer(2, GL_FLOAT, sizeof(buftype), (GLvoid *)sizeof(vertype));
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(buftype), (GLvoid *)(sizeof(vertype)+sizeof(textype)));
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glDrawArrays(GL_QUADS, 0, indices);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glBindBufferARB(GL_ARRAY_BUFFER, 0); // unbind
But it fails, and its rendering something else than what I got with setting stride and pointers to zero all. When I do that, I get my vertex data correctly rendered, but text/color still incorrect.
I can't use &data[0].vertex for the last param in glVertexPointer() etc. because I don't have the data anymore since I am using a VBO, that method works only with vertex arrays.
Also I'm not sure how the count value for glDrawArrays() works, I read docs which say it is the indices, so a quad would have 4, right? But when I multiply the count of quads by 4, it only renders HALF of my vertices, whats going on there? (it will render them all if I multiply by 8...)
It seems you are interleaving your data per quad, but you should rather interleave it per vertex (at least that's what usually done).
Your code should be right if you change the vertex layout to this:
xyz
uv
rgba
xyz
uv
rgba
xyz
uv
rgba
xyz
uv
rgba
Rather than
xyzxyzxyzxyz
uvuvuvuv
rgbargbargbargba