Help me using buffer objects

Started by
2 comments, last by sjaakiejj 12 years, 1 month ago
Greetings,

I'm having a bit of trouble grasping the concept of drawing things with buffer objects, and a lot of trouble getting anything to be displayed on screen with one. Up until now I have been using gl_begin/gl_end for drawing.

So I basically have everything set up more or less how I want it, only I need to know what function calls to make and in what order. Here's what I've tried:

//Initialization

trimesh_rendering* t = get_world_trias();

glGenBuffers(1, &vboId);
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, sizeof(t->vertices)+sizeof(t->normals)+sizeof(t->colors), 0, GL_STREAM_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(t->vertices), t->vertices); // copy vertices starting from 0 offest
glBufferSubData(GL_ARRAY_BUFFER, sizeof(t->vertices), sizeof(t->normals), t->normals); // copy normals after vertices
glBufferSubData(GL_ARRAY_BUFFER, sizeof(t->vertices)+sizeof(t->normals), sizeof(t->colors), t->colors); // copy colours after normals
glBindBuffer(GL_ARRAY_BUFFER, 0);

delete_world_trias(t);


//get_world_trias()

trimesh_rendering* TriMeshRenderer::get_world_trias() {
trimesh_rendering* t = new trimesh_rendering;
int num = 0;
node *q;
int col, row;
//Count num trias
for( int r=0; r<world->get_rows()+world->get_cols()-1; r++ ) {
for( int c=maximum(0, r-world->get_rows()+1); c<=r && c<world->get_cols(); c++ ) {
col = c;
row = r-c;
for( q = world->get(col,row,0); q != 0; q = q->next ) {
TriMesh *mesh = q->shape;
if(mesh!=0) {
num += mesh->get_numTrias()*9;
}
}
}
}
triasize = num/9;
worldsize = num*3;
t->len = num;
t->vertices = new GLfloat[t->len];
t->normals = new GLfloat[t->len];
t->colors = new GLfloat[t->len];
int m = 0;
for( int r=0; r<world->get_rows()+world->get_cols()-1; r++ ) {
for( int c=maximum(0, r-world->get_rows()+1); c<=r && c<world->get_cols(); c++ ) {
col = c;
row = r-c;
for( q = world->get(col,row,0); q != 0; q = q->next ) {
TriMesh *mesh = q->shape;
if(mesh!=0) {
int n = mesh->get_numTrias();
for( int i=0; i<n; i++ ) {
Triangle3D tria = mesh->get_tria(i);
t->vertices[m+0] = tria.p1.x;
t->vertices[m+1] = tria.p1.y-tria.p1.z;
t->vertices[m+2] = 0;
t->vertices[m+3] = tria.p2.x;
t->vertices[m+4] = tria.p2.y-tria.p2.z;
t->vertices[m+5] = 0;
t->vertices[m+6] = tria.p3.x;
t->vertices[m+7] = tria.p3.y-tria.p3.z;
t->vertices[m+8] = 0;

t->normals[m+0] = tria.normal.x;
t->normals[m+1] = tria.normal.y;
t->normals[m+2] = tria.normal.z;
t->normals[m+3] = tria.normal.x;
t->normals[m+4] = tria.normal.y;
t->normals[m+5] = tria.normal.z;
t->normals[m+6] = tria.normal.x;
t->normals[m+7] = tria.normal.y;
t->normals[m+8] = tria.normal.z;

t->colors[m+0] = 1.0;
t->colors[m+1] = 1.0;
t->colors[m+2] = 1.0;
t->colors[m+3] = 1.0;
t->colors[m+4] = 1.0;
t->colors[m+5] = 1.0;
t->colors[m+6] = 1.0;
t->colors[m+7] = 1.0;
t->colors[m+8] = 1.0;
m+=9;
}
}
}
}
}
return t;
}


//Update and draw

glBindBuffer(GL_ARRAY_BUFFER, vboId);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, 0);
trimesh_rendering* t = get_world_trias();
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(t->vertices), t->vertices); // copy vertices starting from 0 offest
glBufferSubData(GL_ARRAY_BUFFER, sizeof(t->vertices), sizeof(t->normals), t->normals); // copy normals after vertices
glBufferSubData(GL_ARRAY_BUFFER, sizeof(t->vertices)+sizeof(t->normals), sizeof(t->colors), t->colors); // copy colours after normals
delete_world_trias(t);

glDrawArrays(GL_TRIANGLES, 0, triasize);
glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glUseProgram(0);



My goal is to use a vertex shader to transform primitives from a square to an isometric view.
Advertisement
You've got a few problems in your code that I can spot straight off. I haven't used GL_STREAM_DRAW myself, so I'd try using GL_STATIC_DRAW to make sure your code is working correctly.

So your initialisation should look a bit like this:



GLuint pVertexBufferObj; //You should remember this one, as it specifies the "location" of the data in GPU memory

/* Generate a buffer object, and bind it with the data */
glGenBuffers(1, &pVertexBufferObj);
glBindBuffer(GL_ARRAY_BUFFER, pVertexBufferObj);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * t->NumberVertices, t->vertices, GL_STATIC_DRAW);

/* Tell OpenGL we're now done providing it our data */
glBindBuffer(GL_ARRAY_BUFFER, 0);



Next in the draw call you would specify the following:

glEnableClientState(GL_VERTEX_ARRAY);
// glEnableClientState(GL_NORMAL_ARRAY); //If we're using normals
// glEnableClientState(GL_TEXTURE_COORD_ARRAY); //If we're using textures

glBindBuffer( GL_ARRAY_BUFFER, pVertexBufferObject);
//3 = Number of elements (xyz), GL_FLOAT = Type of elements, sizeof(Vertex) = size of your elements in bytes, 0 = offset from the start of your vertex buffer object array
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), 0 );
//Same for Normal & Texture pointers

glPushMatrix();
glDrawArrays(GL_TRIANGLES, 0, t->NumberVertices);
glPopMatrix();

glBindBuffer( GL_ARRAY_BUFFER, 0 );
//glDisableClientState( GL_TEXTURE_COORD_ARRAY );
//glDisableClientState( GL_NORMAL_ARRAY );
glDisableClientState( GL_VERTEX_ARRAY );


Important thing to note here is that Vertex Buffer Objects do not have their coordinates stored in the CPU memory, but in the GPU memory. Sending the vertices off to a buffer object at every draw call defeats the purpose of using them, as they exist to reduce the bandwidth usage between CPU and GPU.
Thanks, I now have visual output!
It covers the whole screen in an orange color though. Could have something to do with my camera matrix. In pursuit of my goal of having the shader make things isometric, I set it up with the GLM math library ( http://glm.g-truc.net/ ) to look at the screen like this:


glm::vec3 eye = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 center = glm::vec3(0.0f, 0.0f, 1.0f);
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);
glm::mat4 viewMatrix = glm::lookAt(eye, center, up);
glUniformMatrix4fv(glGetUniformLocation(p, "vMatrix"), 1, GL_FALSE, glm::value_ptr(viewMatrix));


So I have a few more questions before I can draw things properly.
When you create an orthographic projection, any non zero Z value isn't drawn as it is outside znear/zfar range. So if you don't want perspective yet want to have your geometry stored with all three axis, any z coordinate must be set to zero eg. inside a vertex shader. Is this a correct assumption?
What range are coordinates inside a vertex shader in? 0.0-1.0?
In the following shader, what function do I need in order to set "vertex" to the same value as that inside "t->vertices"? (so I don't need ftransform())


#version 330
in vec4 vertex;
uniform float ox;
uniform float oy;
uniform mat4 vMatrix;
uniform mat4 mMatrix;
out float odepth;
void main()
{
vec4 vert = ftransform() * vMatrix;

float sqrX = vert.x;
float sqrY = 1.0-vert.y;
float sqrZ = vert.z;

vert.x = ox + sqrX-sqrY;
vert.y = oy + (sqrY+sqrX)/2.0 - sqrZ;
vert.z = 0.0;

odepth = sqrZ;

gl_Position = vert;
}


Again, thanks very much.

Thanks, I now have visual output!
It covers the whole screen in an orange color though. Could have something to do with my camera matrix. In pursuit of my goal of having the shader make things isometric, I set it up with the GLM math library ( http://glm.g-truc.net/ ) to look at the screen like this:


glm::vec3 eye = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 center = glm::vec3(0.0f, 0.0f, 1.0f);
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);
glm::mat4 viewMatrix = glm::lookAt(eye, center, up);
glUniformMatrix4fv(glGetUniformLocation(p, "vMatrix"), 1, GL_FALSE, glm::value_ptr(viewMatrix));


So I have a few more questions before I can draw things properly.
When you create an orthographic projection, any non zero Z value isn't drawn as it is outside znear/zfar range. So if you don't want perspective yet want to have your geometry stored with all three axis, any z coordinate must be set to zero eg. inside a vertex shader. Is this a correct assumption?

Orthographic view is not zeroing the Z value. Orthographic is instead setting the angle between your near and far plane to be zero, or in OpenGL just by using glOrtho which does it for you. The objects are still 3D, and so your z-values are not lost.


What range are coordinates inside a vertex shader in? 0.0-1.0?
In the following shader, what function do I need in order to set "vertex" to the same value as that inside "t->vertices"? (so I don't need ftransform())


#version 330
in vec4 vertex;
uniform float ox;
uniform float oy;
uniform mat4 vMatrix;
uniform mat4 mMatrix;
out float odepth;
void main()
{
vec4 vert = ftransform() * vMatrix;

float sqrX = vert.x;
float sqrY = 1.0-vert.y;
float sqrZ = vert.z;

vert.x = ox + sqrX-sqrY;
vert.y = oy + (sqrY+sqrX)/2.0 - sqrZ;
vert.z = 0.0;

odepth = sqrZ;

gl_Position = vert;
}


Again, thanks very much.
[/quote]

Use the following:

vec4 vertex = gl_Vertex;


that'll put your vertex coordinate into the vec4 vertex. Remember that a vertex shader is called once for every vertex, and so instead of ftransform(), you would multiply your vertex by your projection matrix, e.g.


gl_Position = gl_ModelViewProjectionMatrix * vertex;

This topic is closed to new replies.

Advertisement