Sign in to follow this  
BeginGamer

Drawing multiple quads for 2d game

Recommended Posts

I figured out how to print two quads using vertex arrays, but I think there maybe an easier way to render multiple quads... any suggestions?


[CODE]
#include "SDL/SDL.h"
#include "SDL_image.h"
#include "SDL/SDL_opengl.h"
GLuint get_texture(void)
{
SDL_Surface * surface = IMG_Load("image1.png");
SDL_Surface *tempt = SDL_CreateRGBSurface(SDL_SWSURFACE, surface->w, surface->h, 32,
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff
#else
0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000
#endif
);
SDL_BlitSurface(surface, NULL,tempt, NULL);
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tempt->w, tempt->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, tempt->pixels);
SDL_FreeSurface(tempt);
SDL_FreeSurface(surface);
return texture;
}
int main( int argc , char ** argv )
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_SetVideoMode(640, 480, 32, SDL_OPENGL);
glClearColor(0, 0, 0, 0);
glViewport(0, 0, 640, 480);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 640, 480, 0, 1, -1);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GLuint id = get_texture();
glBindTexture(GL_TEXTURE_2D,id);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
GLfloat vertices [] = {
//first quad
0,16,0,
0,0,0,
16,0,0,
16,16,0,
//second quad
50,66,0,
50,50,0,
66,50,0,
66,66,0
};
GLubyte indices [] = {
//first indice
0,1,2,
0,2,3,
//second
4,5,6,
4,6,7
};
GLfloat texvertices [] = {
//first quad
0,.5,
0,0,
.5,0,
.5,.5,
//second quad
.5,1,
.5,.5,
1,.5,
1,1
};
glTexCoordPointer(2, GL_FLOAT,0, texvertices);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_BYTE, indices);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
SDL_GL_SwapBuffers();
SDL_Delay(9000);
return 0;
}
[/CODE] Edited by BeginGamer

Share this post


Link to post
Share on other sites
I can't see anything technically wrong with your code - you could possibly use VAO/VBO objects, which are newer.

How are you looking to improve this?

Share this post


Link to post
Share on other sites
Well, I wanted to write a 2d rendering class around glDrawelements. I was just scratching my head, how Icould render N number of quads without rewriting the array of indices

Share this post


Link to post
Share on other sites
Just keep drawing the same, single, quad, but translated/rotated to a different position, and optionally adding a scaling value. Edited by mark ds

Share this post


Link to post
Share on other sites
Add glLoadIdentity() after glMatrixMode(GL_MODELVIEW).

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

Then

glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_BYTE, indices);
glTranslatef( 50.0f, 50.0f, 0.0f );
glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_BYTE, indices);
glTranslatef( 100.0f, 100.0f, 0.0f );
glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_BYTE, indices);

Be aware though that this is the old way of doing things, and you should really be looking at moving away from immediate mode. (Although immediate mode can actually be quite useful for visualising what's going on behind the scenes)

Share this post


Link to post
Share on other sites
ahh... Well if I wanted to do it the VBO way.. would it be along the lines of this
[CODE]
//assume Opengl 2.1
GLuint VBOid;
glGenBuffers( 1 , &VBOid);
glBindBuffer(GL_ARRAY_BUFFER, VBOid);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*6, NULL, GL_DYNAMIC_DRAW);
glVertexPointer(3, GL_FLOAT, 0, NULL);

glBindTexture(GL_TEXTURE_2D, spritesheetID);
// for each sprite in sprites do something like this?
glPushMatrix()
glTranslatedf(posx , posy, layer);
glScalef(sizeX, sizeY, 0 );
glTexCoordPointer(2, GL_FLOAT, 0 , texvertices);

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDrawElements(GL_TRIANGLES, 6 , GL_UNSIGNED_BYTE, indices);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glPopMatrix();
[/CODE]

not sure if my idea is correct

Share this post


Link to post
Share on other sites
Have a look [url="https://github.com/progschj/OpenGL-Examples"]here[/url] at the second and third examples. It'll also get you in the habit of using modern opengl.

Here's another good reference [url="http://antongerdelan.net/opengl"]http://antongerdelan.net/opengl[/url] Look under Basics-> Vertex Buffer Objects (VBOs) and Vertex Array Objects (VAOs) or "[i]How to send geometry to the GPU[/i]"

Share this post


Link to post
Share on other sites
The array of indexes can be completely static. It's always going to be 0/1/2, 0/2/3, 4/5/6, 4/6/7, 8/9/10, 8/10/11, etc. So pick an arbitrary max number of quads that you're going to draw, pre-populate the array up to this number during startup (a simple loop can handle this), (optionally) put it in a GL_ELEMENT_ARRAY_BUFFER, and you're done.

The code to pre-populate the array will look something like this (assuming a selected max of 1024 quads):
[code] unsigned short quadindexes[1024 * 6];
unsigned short *ndx = quadindexes;

for (int i = 0, fv = 0; i < 1024; i++, fv += 4, ndx += 6)
{
ndx[0] = fv;
ndx[1] = fv + 1;
ndx[2] = fv + 2;
ndx[3] = fv;
ndx[4] = fv + 2;
ndx[5] = fv + 3;
}[/code]

The other option - if you're not averse to using deprecated functionality - is to use glDrawArrays with GL_QUADS. That will enable you to do away with indexes altogether. I assume that you don't really want to do this though as you explicitly mentioned that you want to build this around glDrawElements.

Either way will give you the [i]real[/i] advantage of drawing with arrays which is to be able to draw multiple quads in a single draw call. You won't get that from glTranslate for each quad.

By the way - you [i]don't[/i] need to go all the way to VBO/VAO with this code if you're not feeling ready for it yet. In particular, since you have dynamic data, you need to build up a good understanding of pipeline stalls and how the CPU and GPU sync up before doing so, otherwise you risk running substantially slower (you should consider getting this understanding and making the switch eventually though). I [i]really[/i] wish that people would stop recommending "use a VBO" for every single snippet of OpenGL problem code without analysing the problem properly first......... Edited by mhagain

Share this post


Link to post
Share on other sites
VBOs are suggested for every snippet because they are literally the only way to send data to the GPU from OpenGL 3.3 (every other buffer type is just syntactic sugger).

Dynamic data needn't be dynamic data in most cases these days, since you can do all sorts of nifty tricks with shaders.

As to a 2D map of quads - just generate the data for all of them, or use instancing, and render them all with one call.
Drawing them one by one will probably be as slow as software rendering.

Drawing any count of quads for a uniform tile map (where every quad is the same size) is incredibally fast and memory conversative with instancing, so it's preferable if you have the hardware.
I can post some relevant code later on when I have access to my computer, if you want.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this