mvEngine Development Thread

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: mvEngine Development Thread

Post by qpHalcy0n »

I'm very sorry. That should read "pixmapSize"
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Re: mvEngine Development Thread

Post 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?
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: mvEngine Development Thread

Post 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.
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Rendering Tiles in OpenGL

Post 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
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: mvEngine Development Thread

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: mvEngine Development Thread

Post 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.
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

VBO's with verticies and texcoords

Post 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
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: VBO's with verticies and texcoords

Post 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
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: VBO's with verticies and texcoords

Post 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 ;)
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: mvEngine Development Thread

Post 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.
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Re: mvEngine Development Thread

Post 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?
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: mvEngine Development Thread

Post 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?
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Re: mvEngine Development Thread

Post by mv2112 »

Oops, it should be 4. I though you had to specify how many quads instead of vertices's. Now it works!
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: mvEngine Development Thread

Post 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
Image
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: mvEngine Development Thread

Post 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! ;)
Post Reply