Page 7 of 8

Re: mvEngine Development Thread

Posted: Fri Aug 06, 2010 11:49 pm
by qpHalcy0n
I'm very sorry. That should read "pixmapSize"

Re: mvEngine Development Thread

Posted: Sat Aug 07, 2010 12:23 am
by mv2112
qpHalcy0n wrote:I'm very sorry. That should read "pixmapSize"
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:

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;
Does anything look wrong here?

Re: mvEngine Development Thread

Posted: Sat Aug 07, 2010 1:04 am
by qpHalcy0n
You need to make a habit of checking the GL error string occasionally or after functions that are most likely to cause problems....especially when loading assets, like textures.

In this case, the 3rd parameter to glTexImage2D is invalid. It won't fail, but if you were to query the error string, you'd see that it did not succeed. A valid parameter is GL_RGB8, not GL_RGB. Give that a whirl.

Rendering Tiles in OpenGL

Posted: Sun Aug 15, 2010 10:00 pm
by mv2112
I'm in need of some advice. I'm creating a map class and i don't know how to go about rendering the map. I want the rendering to be as fast and efficient as possible. Will regular intermediate mode work for rendering a large number of textured quads, or will it slow down? Is it possible to use a VBO or Vertex array to make the rendering go faster? What would be the most efficient approach?

Thanks ahead of time!
mv2112

Re: mvEngine Development Thread

Posted: Sun Aug 15, 2010 10:05 pm
by Ginto8
using a vertex array is recommended, especially if it's a large map and you zoom out quite a bit, but for most purposes immediate mode should work perfectly fine.

Re: mvEngine Development Thread

Posted: Sun Aug 15, 2010 10:15 pm
by qpHalcy0n
Under the presumption that the world doesn't change, then the absolute fastest course of action would be a hardware buffered resource. In a tiled world, the index buffer is ultra-predictable and can be procedurally generated. The vertex buffer can be uploaded at one time and you only really need one. You would order the vertices in order of texture access. Eg: All quads that require texture X (some number) will go in first, all those which require textureY (again, some number) will go in after that, etc, etc...

When it comes time to render, you split the draw calls into however many unique textures are visible. The flow of control goes as follows:


1) Bind index buffer and vertex buffer.
2) Set up vertex assembly state. (all that lovely glVertexPointer, glTexCoordPointer, etc etc...b/s)
3) Loop through visible textures
4) Render indexed triangles from the offset in which the vertices that use textureX (some number) begin, to the number of quads that require said texture. Rinse and repeat for textureY, and so forth.
5) Evict textures, unbind index and vertex buffers.

VBO's with verticies and texcoords

Posted: Mon Aug 16, 2010 5:16 pm
by mv2112
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.

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

Thanks.
mv2112

Re: VBO's with verticies and texcoords

Posted: Mon Aug 16, 2010 5:32 pm
by dandymcgee
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.

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

Thanks.
mv2112
If you're worried about performance I'd replace that seemingly fixed-size vector with an array before I bothered implementing VBOs. ;)

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

Re: VBO's with verticies and texcoords

Posted: Mon Aug 16, 2010 7:01 pm
by GroundUpEngine
dandymcgee 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
Same tut I used, it's very good ;)

Re: mvEngine Development Thread

Posted: Mon Aug 16, 2010 7:59 pm
by X Abstract X
I would try to do things a bit differently. I've always used glVertexAttribPointer so I might be off on my glVertexPointer and glTexCoordPointer arguments.

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));
I left out the code for things you seemed to have done properly.

Re: mvEngine Development Thread

Posted: Mon Aug 16, 2010 10:37 pm
by mv2112
Ok, i tried what X Abstract X said and i might be doing something wrong because it doesn't render.
Here are my structs:

Code: Select all

struct mvVector
{
	GLfloat x;
	GLfloat y;
};

struct mvVector3f
{
	GLfloat x;
	GLfloat y;
	GLfloat z;
};

struct mvVertex
{
	mvVector3f Vertex;
	mvVector TexCoord;
};
My VBO init and Array:

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);
And lastly the render function:

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();
}
I have absolutely no idea what im doing wrong. I've checked the gl error string after every line of code i've listed above and nothing comes up :x . Anyone know whats wrong?

Re: mvEngine Development Thread

Posted: Mon Aug 16, 2010 10:52 pm
by X Abstract X
What value are you passing as vertices? I've never used glDrawArrays so excuse my ignorrance but, why are you diving vertices by 4? Shouldn't the 3rd argument of glDrawArrays() be 4?

Re: mvEngine Development Thread

Posted: Tue Aug 17, 2010 12:17 am
by mv2112
Oops, it should be 4. I though you had to specify how many quads instead of vertices's. Now it works!

Re: mvEngine Development Thread

Posted: Tue Aug 17, 2010 9:37 am
by eatcomics
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

Re: mvEngine Development Thread

Posted: Tue Aug 17, 2010 6:05 pm
by GroundUpEngine
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
Amen to that bro! ;)