PDA

View Full Version : VBO and glDrawElements, textures, glfw, and glew.



genusis
05-28-2013, 04:19 PM
Well i have been trying to upgrade my current primitive system to use vertex buffer objects, however when i finally got it done it would not display the texture on the screen or load anything in particular.




struct vertex_t {
float x, y, z;
float u, v;
float r, g, b, a;
};


struct openext{
GLuint vertex_buffer; //texture vertex buffer.
GLuint index_buffer; //index buffer.
vertex_t buffer[4];
};


void create_vertex_buffer(widget *control)
{
float x2,x1;
float y2,y1;
//GLfloat vertex[36];
GLuint index[4] = {0,1,2,3};
control->actualpos.x = control->pos.x + control->parent->actualpos.x;
control->actualpos.y = control->pos.y + control->parent->actualpos.y;

x1 = (float)control->imgpos.x / control->img->width;
x2 = (float)(control->imgpos.x + control->width) / control->img->width;
y1 = (float)control->imgpos.y / control->img->height;
y2 = (float)(control->imgpos.y + control->height) / control->img->height;

/*index 0*/
//texture coords
control->oglbuf.buffer[0].u = x1; control->oglbuf.buffer[0].v = y2;
//vertex coords
control->oglbuf.buffer[0].x = control->actualpos.x; control->oglbuf.buffer[0].y = control->actualpos.y; control->oglbuf.buffer[0].z = 0;
//color coords
control->oglbuf.buffer[0].r = 1.0; control->oglbuf.buffer[0].b = 1.0; control->oglbuf.buffer[0].g = 1.0; control->oglbuf.buffer[0].a = 1.0;

/*index 1*/
//texture coords
control->oglbuf.buffer[1].u = x2; control->oglbuf.buffer[1].v = y2;
//vertex coords
control->oglbuf.buffer[1].x = control->actualpos.x + control->sizex; control->oglbuf.buffer[1].y = control->actualpos.y; control->oglbuf.buffer[1].z = 0;
//color coords
control->oglbuf.buffer[1].r = 1.0; control->oglbuf.buffer[1].b = 1.0; control->oglbuf.buffer[1].g = 1.0; control->oglbuf.buffer[1].a = 1.0;

/*index 2*/
//texture coords
control->oglbuf.buffer[2].u = x2; control->oglbuf.buffer[2].v = y1;
//vertex coords
control->oglbuf.buffer[2].x = control->actualpos.x + control->sizex; control->oglbuf.buffer[2].y = control->actualpos.y + control->sizey; control->oglbuf.buffer[2].z = 0;
//color coords
control->oglbuf.buffer[2].r = 1.0; control->oglbuf.buffer[2].b = 1.0; control->oglbuf.buffer[2].g = 1.0; control->oglbuf.buffer[2].a = 1.0;

/*index 3*/
//texture coords
control->oglbuf.buffer[3].u = x1; control->oglbuf.buffer[3].v = y1;
//vertex coords
control->oglbuf.buffer[3].x = control->actualpos.x; control->oglbuf.buffer[3].y = control->actualpos.y + control->sizey; control->oglbuf.buffer[3].z = 0;
//color coords
control->oglbuf.buffer[3].r = 1.0; control->oglbuf.buffer[3].b = 1.0; control->oglbuf.buffer[3].g = 1.0; control->oglbuf.buffer[3].a = 1.0;

glGenBuffers(1,&control->oglbuf.vertex_buffer);
glGenBuffers(1,&control->oglbuf.index_buffer);

glBindBuffer(GL_ARRAY_BUFFER,control->oglbuf.vertex_buffer);
glBufferData(GL_ARRAY_BUFFER,sizeof(control->oglbuf.buffer),control->oglbuf.buffer,GL_STATIC_DRAW);//fill up the array with vertex and color-data

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,control->oglbuf.index_buffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,4 * sizeof(GLuint),index,GL_STATIC_DRAW);//this one with indices
}



void draw_widget_test(widget *control) //draws all the image widgets on the canvas.
{
glColor4f(1, 1, 1, 1);// set the image color properties, 1 being highest 0.0000 being lowest

glBindTexture(GL_TEXTURE_2D, control->img->texid);

glBindBufferARB(GL_ARRAY_BUFFER, control->oglbuf.vertex_buffer);
glVertexPointer(3, GL_FLOAT, sizeof(struct vertex_t), (GLvoid *)offsetof(struct vertex_t, x));
glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex_t), (GLvoid *)offsetof(struct vertex_t, u));
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(struct vertex_t), (GLvoid *)offsetof(struct vertex_t, r));
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, control->oglbuf.index_buffer);
glIndexPointer(GL_UNSIGNED_INT,sizeof(GLuint),0);

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);

glDrawElements(GL_TRIANGLE_STRIP,4,GL_UNSIGNED_INT ,0);

glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);

glBindBufferARB(GL_ARRAY_BUFFER, 0);
}



This is the code i am using to try and draw the Texture to the screen. Also i have initialized Glew and GLFW i initialized Glew right after i made the GLFW window.


void init_screen(int swidth, int sheight, int mode)
{
GLenum err = 0;

if( !glfwInit())
render_error(ERROR_GLFWINIT_ERROR);

// Finally we can Open an OpenGL window
if(!glfwOpenWindow ( swidth, sheight, 0,0,0,0,0,0, mode))
render_error(ERROR_GLFWWIN_ERROR);

glfwSetWindowTitle( TITLE);//Sets the Windows Name
the_screen.height = sheight;
the_screen.width = swidth;

err = glewInit();

if(err!=GLEW_OK)
{
//Problem: glewInit failed, something is seriously wrong.
render_error(ERROR_GLEWINIT_ERROR);
}
}


And for the top of each Cell that contains the includes for GLFW and Glew i did this.

#include <GL/glew.h>
#include <glfw.h>

I hope someone can help me pinpoint the problem i am currently faced with as i can not get this to work at all no matter how many different things i try.....