[?] Leaving Immediate Mode (drawing a VBO)

Started by
14 comments, last by _Pickle_ 12 years, 7 months ago
OpenGL Questions
Using some OpenGL tutorials I was able to make the following program.
I understand how it works but not how to fully implement it to do what I want it to. I made some generic functions within a fv namespace, it should be obvious what they do.
The source is at the end of this post.

I basically have a few questions:

What version of OpenGL am I using?

How could I draw just the vertex array?
(For a game I am writing each vertex is unique and creating a long list just numbering them would be redundant.
So basically I want to just use an Array Buffer and not an Element Buffer.)

Is an Array Buffer a VBO ?

Is this the best method to do all this? (I am trying to stop using immediate mode)

Will this attain better speed than Immediate mode? (I'm hoping x4 at least)

Why does the primitive draw flat? (I specify different Z coords)


Thanks
It seems making the switch is almost possible!
However I'm willing to work.

Thanks in advance! I appreciate any help!
Walker

fvDraw.v.glsl
#version 110

attribute vec2 position;

varying vec2 texcoord;

void main()
{
gl_Position = vec4(position, 0.0, 1.0);
texcoord = position * vec2(-0.5) + vec2(0.5);
}

fvDraw.f.glsl
#version 110

uniform float fade_factor;
uniform sampler2D textures[2];

varying vec2 texcoord;

void main()
{
gl_FragColor = mix(
texture2D(textures[0], texcoord),
texture2D(textures[1], texcoord),
fade_factor
);
}

Main.cpp


#include "fv.h"
#include "math.h"

//Draw Data
static const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f, 10.0f,
1.0f, -1.0f, -10.0f,
-1.0f, 1.0f, 10.0f,
1.0f, 1.0f, -10.0f
};
static const GLushort g_element_buffer_data[] = { 0, 1, 2, 3 };
//Resources
static struct
{
GLuint program;
struct
{
GLint fade_factor;
GLint textures[2];
} uniforms;
struct
{
GLint position;
} attributes;
GLfloat fade_factor;
GLuint vBuffer, eBuffer;
GLuint textures[2];
} resources;
void render()
{
glUseProgram(resources.program);
glUniform1f(resources.uniforms.fade_factor, resources.fade_factor);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, resources.textures[0]);
glUniform1i(resources.uniforms.textures[0], 0);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, resources.textures[1]);
glUniform1i(resources.uniforms.textures[1], 1);

glBindBuffer(GL_ARRAY_BUFFER, resources.vBuffer);

// Specify that it's data is vertex data.
glVertexAttribPointer(
resources.attributes.position, // attribute //
3, // size //
GL_FLOAT, // type //
GL_FALSE, // normalized? //
sizeof(GLfloat)*3, // stride //
(void*)0 // array buffer offset //
);
glEnableVertexAttribArray(resources.attributes.position);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, resources.eBuffer);
glDrawElements(
GL_TRIANGLE_STRIP, // mode //
4, // count //
GL_UNSIGNED_SHORT, // type //
(void*)0 // element array buffer offset //
);
glDisableVertexAttribArray(resources.attributes.position);
SDL_GL_SwapBuffers();
}
static void update()
{
int milliseconds = SDL_GetTicks();
resources.fade_factor = sin((float)milliseconds * 0.001f) * 0.5f + 0.5f;
//glutPostRedisplay();
}
int main( int argc, char *argv[] )
{
fv::init();
fv::createWindow(640,480,32,"OpenGL 2.0 drawing");
fv::gl::initAdvanced();

resources.vBuffer = fv::gl::makeBuffer( GL_ARRAY_BUFFER, g_vertex_buffer_data, sizeof(g_vertex_buffer_data) );
resources.eBuffer = fv::gl::makeBuffer( GL_ELEMENT_ARRAY_BUFFER, g_element_buffer_data, sizeof(g_element_buffer_data) );
resources.textures[0] = fv::file::loadTexture("opengl.png");
resources.textures[1] = fv::file::loadTexture("sonic.png");
resources.program = fv::gl::loadShaders("fvDraw.f.glsl","shader.v.glsl");

resources.uniforms.fade_factor = glGetUniformLocation(resources.program, "fade_factor");
resources.uniforms.textures[0] = glGetUniformLocation(resources.program, "textures[0]");
resources.uniforms.textures[1] = glGetUniformLocation(resources.program, "textures[1]");
resources.attributes.position = glGetAttribLocation(resources.program, "position");
while (1)
{
render();
update();
}
return 0;
}
If this post was helpful please +1 or like it !

Webstrand
Advertisement
What version of OpenGL am I using? Use glGetString to find out what the driver supports.

VBO require GL 1.5
Also, you seem to be using GLSL 1.10 which requires GL 2.0

How could I draw just the vertex array? glDrawArrays

Is an Array Buffer a VBO ? Yes, it is a VBO for vertices.

Is this the best method to do all this? Yes

Will this attain better speed than Immediate mode? That depends on where the bottleneck is. Were you CPU limited by all the immediate calls? Is your game the next generation of 3d shooter like Doom 4 or Half-Life 3 or Crysis or Batman or Ghostbusters?

Why does the primitive draw flat? (I specify different Z coords)
gl_Position = vec4(position, 0.0, 1.0);
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Drawing using just a VBO
I am trying to draw just the Vertex buffer, however I just cant get it to work.
I have the new attempt and the old working vertex+ element buffer attached.
What am I doing incorrectly?

Draw attempt using just the vertex buffer (does not work) :
void render()
{
glUseProgram(resources.program);
glUniform1f(resources.uniforms.fade_factor, resources.fade_factor);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, resources.textures[0]);
glUniform1i(resources.uniforms.textures[0], 0);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, resources.textures[1]);
glUniform1i(resources.uniforms.textures[1], 1);

glBindBuffer(GL_ARRAY_BUFFER, resources.vBuffer);
glVertexPointer( 3, GL_FLOAT, 0, (char *) NULL );
glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );

SDL_GL_SwapBuffers();
}

Draw routine using a vertex and element buffer:
void render()
{
glUseProgram(resources.program);
glUniform1f(resources.uniforms.fade_factor, resources.fade_factor);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, resources.textures[0]);
glUniform1i(resources.uniforms.textures[0], 0);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, resources.textures[1]);
glUniform1i(resources.uniforms.textures[1], 1);

glBindBuffer(GL_ARRAY_BUFFER, resources.vBuffer);

// Specify that it's data is vertex data.
glVertexAttribPointer(
resources.attributes.position, // attribute //
3, // size //
GL_FLOAT, // type //
GL_FALSE, // normalized? //
sizeof(GLfloat)*3, // stride //
(void*)0 // array buffer offset //
);
glEnableVertexAttribArray(resources.attributes.position);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, resources.eBuffer);
glDrawElements(
GL_TRIANGLE_STRIP, // mode //
4, // count //
GL_UNSIGNED_SHORT, // type //
(void*)0 // element array buffer offset //
);
glDisableVertexAttribArray(resources.attributes.position);
SDL_GL_SwapBuffers();
}
If this post was helpful please +1 or like it !

Webstrand
It could be that you didn't enable the vertex array because you are calling
glVertexPointer but you didn't enable it.

Also, any reason that you don't use glVertexAttribPointer?
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

It could be that you didn't enable the vertex array because you are calling
glVertexPointer but you didn't enable it.

Also, any reason that you don't use glVertexAttribPointer?


The following line enables it right?
glBindBuffer(GL_ARRAY_BUFFER, resources.vBuffer);
If this post was helpful please +1 or like it !

Webstrand

The following line enables it right?
glBindBuffer(GL_ARRAY_BUFFER, resources.vBuffer);



glEnableClientState

[quote name='coderWalker' timestamp='1315445463' post='4858876']
The following line enables it right?
glBindBuffer(GL_ARRAY_BUFFER, resources.vBuffer);



glEnableClientState
[/quote]


glEnableClientState ? nooooo. That has been Deprecated. I'm trying to draw using up to date OpenGL.
Also these are Server side aka on the GPU.

So I don't want that command right?
If this post was helpful please +1 or like it !

Webstrand
So don't use glVertexPointer
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
[source=cpp]

GLuint vao;

glGenVertexArrays(1, &vao);

glBindVertexArray(vao);[/source]






add this code right after you create your OpenGL context. Forward compatibility requires you to create and use a custom vertex array.
OpenGL fanboy.

[source=cpp] GLuint vao; glGenVertexArrays(1, &vao); glBindVertexArray(vao);[/source]






add this code right after you create your OpenGL context. Forward compatibility requires you to create and use a custom vertex array.


It depends on which context you made. GL 3.1 and above context's require VAO to be used.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

This topic is closed to new replies.

Advertisement