• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
BeginGamer

Drawing multiple quads for 2d game

9 posts in this topic

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
0

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?
0

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
0

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
0

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)
0

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
0

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]"
0

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
0

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.
0

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  
Followers 0