Re: mvEngine Development Thread
Posted: Fri Aug 06, 2010 11:49 pm
I'm very sorry. That should read "pixmapSize"
The Next Generation of 2D Roleplaying Games
http://elysianshadows.com/phpBB3/
First, i tried writing my own code based on yours (for learning purposes) and it didnt work, so then i just copied and pasted the code and it still doesnt work. I know that the file opens and it gets the 19778 correct because i have the program exit() if it doesnt. However the image doesnt display. I think its probably just an error in my openGL code:qpHalcy0n wrote:I'm very sorry. That should read "pixmapSize"
Code: Select all
//qpHalcy0n's code +
GLuint Tex;
glGenTextures(1,&Tex);
glBindTexture(GL_TEXTURE_2D,Tex);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,imgWidth,imgHeight,0,GL_RGB,GL_UNSIGNED_BYTE,pix);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glFinish();
delete[]pix;
return Tex;
Code: Select all
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
height=texture_height;
width=texture_width;
std::vector<GLfloat>Pos,TPos;
Pos.push_back(0);
Pos.push_back(0);
Pos.push_back(15);
Pos.push_back(0);
Pos.push_back(15);
Pos.push_back(15);
Pos.push_back(0);
Pos.push_back(15);
TPos.push_back(0); //top left
TPos.push_back(1);
TPos.push_back(15/width); //top-right
TPos.push_back(1);
TPos.push_back(15/width);
TPos.push_back(1-(15/height));
TPos.push_back(0);
TPos.push_back(1-(15/height));
//Now to the VBO stuff
glGenBuffers(1,&VBO);
glBindBuffer(GL_ARRAY_BUFFER,VBO);
glBufferData(GL_ARRAY_BUFFER,sizeof(GLfloat)*Pos.size(),&Pos[0],GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER,sizeof(GLfloat)*TPos.size(),&TPos[0],GL_STATIC_DRAW);
//and now rendering
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,Texture);
glColor3f(1.0f,1.0f,1.0f);
glBindBuffer(GL_ARRAY_BUFFER,VBO);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2,GL_FLOAT,NULL,BUFFER_OFFSET(0));
glTexCoordPointer(2,GL_FLOAT,NULL,BUFFER_OFFSET()); //what do i use here?
glDrawArrays(GL_QUADS,NULL,1);
glFinish();
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glBindTexture(GL_TEXTURE_2D,NULL);
glDisable(GL_TEXTURE_2D);
If you're worried about performance I'd replace that seemingly fixed-size vector with an array before I bothered implementing VBOs.mv2112 wrote:I've got vertex arrays to work but i want to try my hand at VBO's. I have some questions:
Here is my code so far, im storing vertices's and texture coordinates in the same VBO.How do i figure out the offset for the TexCoord data in the VBO? Would getting the offset wrong cause a segfault because that's what happens so far. How does this code look?Code: Select all
#define BUFFER_OFFSET(i) ((char *)NULL + (i)) height=texture_height; width=texture_width; std::vector<GLfloat>Pos,TPos; Pos.push_back(0); Pos.push_back(0); Pos.push_back(15); Pos.push_back(0); Pos.push_back(15); Pos.push_back(15); Pos.push_back(0); Pos.push_back(15); TPos.push_back(0); //top left TPos.push_back(1); TPos.push_back(15/width); //top-right TPos.push_back(1); TPos.push_back(15/width); TPos.push_back(1-(15/height)); TPos.push_back(0); TPos.push_back(1-(15/height)); //Now to the VBO stuff glGenBuffers(1,&VBO); glBindBuffer(GL_ARRAY_BUFFER,VBO); glBufferData(GL_ARRAY_BUFFER,sizeof(GLfloat)*Pos.size(),&Pos[0],GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER,sizeof(GLfloat)*TPos.size(),&TPos[0],GL_STATIC_DRAW); //and now rendering glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D,Texture); glColor3f(1.0f,1.0f,1.0f); glBindBuffer(GL_ARRAY_BUFFER,VBO); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(2,GL_FLOAT,NULL,BUFFER_OFFSET(0)); glTexCoordPointer(2,GL_FLOAT,NULL,BUFFER_OFFSET()); //what do i use here? glDrawArrays(GL_QUADS,NULL,1); glFinish(); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); glBindTexture(GL_TEXTURE_2D,NULL); glDisable(GL_TEXTURE_2D);
Thanks.
mv2112
Same tut I used, it's very gooddandymcgee wrote: Checking out this article and the accompanying example code (download at bottom of page) might benefit you. http://www.songho.ca/opengl/gl_vbo.html
Code: Select all
struct Vector3f {
GLFloat x, y, z;
};
struct Vector2f {
GLFloat x, y;
};
struct Vertex {
Vector3f position;
Vector2f texCoord;
};
unsigned int numVertices = 200;
Vertex* vertices = new Vertex[numVertices];
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * numVertices, vertices, GL_STATIC_DRAW);
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), 0);
glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), (void*)sizeof(Vector3f));
Code: Select all
struct mvVector
{
GLfloat x;
GLfloat y;
};
struct mvVector3f
{
GLfloat x;
GLfloat y;
GLfloat z;
};
struct mvVertex
{
mvVector3f Vertex;
mvVector TexCoord;
};
Code: Select all
mvVertex*vertex=new mvVertex[4];
vertex[0].Vertex.x=0.0f;
vertex[0].Vertex.y=0.0f;
vertex[0].Vertex.z=0.0f;
vertex[1].Vertex.x=32.0f;
vertex[1].Vertex.y=0.0f;
vertex[1].Vertex.z=0.0f;
vertex[2].Vertex.x=32.0f;
vertex[2].Vertex.y=32.0f;
vertex[2].Vertex.z=0.0f;
vertex[3].Vertex.x=0.0f;
vertex[3].Vertex.y=32.0f;
vertex[3].Vertex.z=0.0f;
vertex[0].TexCoord.x=0/512; //image is 512x512
vertex[0].TexCoord.y=1-(0/512);
vertex[1].TexCoord.x=32/512;
vertex[1].TexCoord.y=1-(0/512);
vertex[2].TexCoord.x=32/512;
vertex[2].TexCoord.y=1-(32/512);
vertex[3].TexCoord.x=0/512;
vertex[3].TexCoord.y=1-(32/512);
GLuint VBO;
glGenBuffers(1,&VBO);
glBindBuffer(GL_ARRAY_BUFFER,VBO);
glBufferData(GL_ARRAY_BUFFER,sizeof(mvVertex)*4,vertex,GL_STATIC_DRAW);
glFinish();
glBindBuffer(GL_ARRAY_BUFFER,NULL);
Code: Select all
void GFX::DrawTiles(GLuint&VBO,GLuint&tex,int vertices,float alpha)
{
glPushMatrix();
glLoadIdentity();
glColor4f(1.0f,1.0f,1.0f,alpha);
glEnable(GL_TEXTURE_2D);
glBindBuffer(GL_ARRAY_BUFFER,VBO);
glBindTexture(GL_TEXTURE_2D,tex);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(3, GL_FLOAT,sizeof(mvVertex), 0);
glTexCoordPointer(2, GL_FLOAT,sizeof(mvVertex), (void*)sizeof(mvVector3f));
glDrawArrays(GL_QUADS,0,vertices/4);
glFinish();
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glBindTexture(GL_TEXTURE_2D,NULL);
glBindBuffer(GL_ARRAY_BUFFER,NULL);
glDisable(GL_TEXTURE_2D);
glPopMatrix();
}
Amen to that bro!eatcomics wrote:YAY for internet people finding problems when you're too tired to because you've been up coding till 3 or 4 in the morning :D