• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
SpectreNectar

Help me using buffer objects

3 posts in this topic

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
[CODE]
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);
[/CODE]

//get_world_trias()
[CODE]
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;
}
[/CODE]

//Update and draw
[CODE]
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);
[/CODE]


My goal is to use a vertex shader to transform primitives from a square to an isometric view.
0

Share this post


Link to post
Share on other sites
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:

[code]

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);

[/code]

Next in the draw call you would specify the following:
[code]
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 );
[/code]

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.
1

Share this post


Link to post
Share on other sites
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 ( [url="http://glm.g-truc.net/"]http://glm.g-truc.net/[/url] ) to look at the screen like this:

[CODE]
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));
[/CODE]

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())

[CODE]
#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;
}
[/CODE]

Again, thanks very much.
0

Share this post


Link to post
Share on other sites
[quote name='SpectreNectar' timestamp='1330968050' post='4919505']
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 ( [url="http://glm.g-truc.net/"]http://glm.g-truc.net/[/url] ) to look at the screen like this:

[CODE]
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));
[/CODE]

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?
[/quote]
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.

[quote]
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())

[CODE]
#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;
}
[/CODE]

Again, thanks very much.
[/quote]

Use the following:
[code]
vec4 vertex = gl_Vertex;
[/code]

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.

[code]
gl_Position = gl_ModelViewProjectionMatrix * vertex;
[/code]
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0