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