• 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
youpi1

draw multiple 2D textured elements at once

4 posts in this topic

I am working on a n-body code with "glut functions" display. I would like to display each body with a 2D texture from a bmp image (a star).
The coordinates of each body (x, y, z) are in the "pos" array.

I try to use glTexCoordPointer with the following display function :

[CODE]

void drawPoints()
{
glEnable(GL_TEXTURE_2D); // Enable texture mapping.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glBindTexture(GL_TEXTURE_2D, texture[0]); // pick the texture.
glLoadIdentity(); // reset the view before we draw each star.
glTranslatef(0.0f, 0.0f, zoom); // zoom into the screen.

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(4, GL_DOUBLE, 4*sizeof(double), pos);
glTexCoordPointer(4, GL_DOUBLE, 4*sizeof(double), pos);
glColor4ub(30, 100, 120, 255);

glBegin(GL_QUADS); // Begin Drawing The Textured Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);
glEnd(); // Done Drawing The Textured Quad

glDrawArrays(GL_POINTS, 0, numBodies);

glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);

glutSwapBuffers();

}
[/CODE]



before this, I call the following function for loading the 2D texture :

[CODE]
// Load Bitmaps And Convert To Textures
void LoadGLTextures(void)
{
// Load Texture
Image *image1;

// allocate space for texture
image1 = (Image *) malloc(sizeof(Image));
if (image1 == NULL) {
printf("Error allocating space for image");
exit(0);
}

if (!ImageLoad("Data/Star.bmp", image1)) {
exit(1);
}

// Create Textures
glGenTextures(3, &texture[0]);

// linear filtered texture
glBindTexture(GL_TEXTURE_2D, texture[0]); // 2d texture (x and y size)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // scale linearly when image bigger than texture
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // scale linearly when image smalled than texture
glTexImage2D(GL_TEXTURE_2D, 0, 3, image1->sizeX, image1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image1->data);
};
[/CODE]


My main problem is that only one textured element is displayed at the center of the window (x=0, y=0) ( see figure in attachment).

It seems that I don't use correctly the function [b]glTexCoordPointer [/b]. In the above drawPoints() function, I call it with :

[CODE]glTexCoordPointer(4, GL_DOUBLE, 4*sizeof(double), pos);[/CODE]

where pos is the array of coordinates of each body (x, y, z).

I found another solution for plotting all the bodies with glTranslate in a loop on all the elements :


[CODE]

for(int i=0;i<numBodies;i++)
{
glPushMatrix();
glTranslatef(pos[i],pos[i+1],pos[i+2]);
glColor4ub(30, 100, 120, 255);
glBegin(GL_QUADS); // Begin Drawing The Textured Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);
glEnd(); // Done Drawing The Textured Quad
glPopMatrix();
}
[/CODE]

You can see the expected result on the second image in attachment ( N_Bodies.png).

but I would like to avoid this solution and find a way to draw all the bodies at once (with glDrawArrays)

Any idea ? Edited by youpi1
1

Share this post


Link to post
Share on other sites
[quote name='youpi1' timestamp='1351572110' post='4995295']
[CODE]
void drawPoints()
{
...
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(4, GL_DOUBLE, 4*sizeof(double), pos);
glTexCoordPointer(4, GL_DOUBLE, 4*sizeof(double), pos);
[/code]
[/quote]

[code]
glVertexPointer
[/code]
This is given the pos array, which should hold the vertices of all the drawn primitives.

[code]
glTexCoordPointer
[/code]
This should be given an array of uv coordinates (typically described by 2 components rather than 4), 1 for each vertex passed to the GPU.

You're passing the "pos" array to both, I don't think you should be doing that.

Upon fixing that, look into VBOs - That's what it looks like you're trying to use. I use VBOs for drawing multiple polygons at once, it's pretty fast. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] Edited by SuperVGA
0

Share this post


Link to post
Share on other sites
Please don't recommend VBOs for this class of problem. They're not a band-aid solution, and if a VBO needs to be dynamically updated they need careful planning and advance design before implementation. The OP is drawing so little geometry that using a VBO is [i]extremely[/i] unlikely to offer any performance gain. If a VBO needs to be dynamically updated it may even cause significant performance loss. The OP clearly has trouble with setting up basic client-side vertex arrays so grafting a VBO on top of that will only make things worse. Let's get the vertex array code working right first, profile the application, determine if this part of the pipeline is actually a bottleneck, and [i]then[/i] think about VBOs - not before.

Remember that Quake used [i]immediate mode[/i] back in 1996, drew a few thousand polys per frame, and didn't suffer overly much from it. Jumping in with a recommendation of switching to VBOs every time one sees non-VBO code is not being in the least bit helpful.

So, with that said, to the OP:

The code you have posted is a little confusing. You're setting up your client states, your gl*Pointer calls, [i]then[/i] you draw a quad in immediate mode, [i]then[/i] you issue a glDrawArrays call with GL_POINTS (despite the fact that you're drawing quads). As correctly pointed out, your glVertexPointer and glTexCoordPointer calls are using the same array, you're using size 4 for both, and GL_DOUBLE is never a good idea (unless you know that you specifically absolutely need a double here - most gfx hardware is going to be much happier with floats).

Something like this looks more like what you're trying to achieve, but it's difficult to say at present.

[CODE]struct myVertex
{
float position[3];
float texcoord[2];
};

myVertex drawData[WHATEVER_MAXIMUM_I_SET];

void drawPoints (void)
{
// fill in the array we're going to draw from; all that glTranslatef in the immediate mode code
// does is move x, y and z by the translation, so we can accomplish the same result by adding
// the translation to the base coordinates. for extra performance, and if this data doesn't
// need to change at runtime, we could just set this array up once when the program starts.
// we're drawing quads so each quad is 4 vertices. make sure that the value of WHATEVER_MAXIMUM_I_SET
// is at least 4 * numBodies.
for (int i = 0; i < numBodies; i++)
{
drawData[i * 4 + 0].position[0] = -1 + pos[i];
drawData[i * 4 + 0].position[1] = -1 + pos[i + 1];
drawData[i * 4 + 0].position[2] = pos[i + 2];
drawData[i * 4 + 0].texcoord[0] = 0;
drawData[i * 4 + 0].texcoord[1] = 0;

drawData[i * 4 + 1].position[0] = 1 + pos[i];
drawData[i * 4 + 1].position[1] = -1 + pos[i + 1];
drawData[i * 4 + 1].position[2] = pos[i + 2];
drawData[i * 4 + 1].texcoord[0] = 1;
drawData[i * 4 + 1].texcoord[1] = 0;

drawData[i * 4 + 2].position[0] = 1 + pos[i];
drawData[i * 4 + 2].position[1] = 1 + pos[i + 1];
drawData[i * 4 + 2].position[2] = pos[i + 2];
drawData[i * 4 + 2].texcoord[0] = 1;
drawData[i * 4 + 2].texcoord[1] = 1;

drawData[i * 4 + 3].position[0] = -1 + pos[i];
drawData[i * 4 + 3].position[1] = 1 + pos[i + 1];
drawData[i * 4 + 3].position[2] = pos[i + 2];
drawData[i * 4 + 3].texcoord[0] = 0;
drawData[i * 4 + 3].texcoord[1] = 1;
}

// to do - if we got a pixel format with a stencil buffer we should clear stencil as well as depth here.
// this will give us > 20% extra performance.
glEnable (GL_TEXTURE_2D);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture (GL_TEXTURE_2D, texture[0]);
glLoadIdentity ();
glTranslatef (0.0f, 0.0f, zoom);

// vertex array setup - note the difference here. do you understand what is being done?
// in theory we only need to do this setup once for this program, but if we were drawing anything else
// that would change.
glEnableClientState (GL_VERTEX_ARRAY);
glEnableClientState (GL_TEXTURE_COORD_ARRAY);
glVertexPointer (3, GL_FLOAT, sizeof (myVertex), drawData[0].position);
glTexCoordPointer (2, GL_FLOAT, sizeof (myVertex), drawData[0].texcoord);
glColor4ub (30, 100, 120, 255);

// we're drawing quads and the number of vertices is 4 * numBodies as each body is a quad with 4 vertices
glDrawArrays (GL_QUADS, 0, numBodies * 4);

// in theory we don't even need to do this at all as we're just going to enable them again in the next frame
glDisableClientState (GL_TEXTURE_COORD_ARRAY);
glDisableClientState (GL_VERTEX_ARRAY);
glutSwapBuffers ();
}[/CODE]
2

Share this post


Link to post
Share on other sites
[quote name='mhagain' timestamp='1351600358' post='4995374']
Please don't recommend VBOs for this class of problem. They're not a band-aid solution, and if a VBO needs to be dynamically updated they need careful planning and advance design before implementation.
[/quote]
Whoops. Sorry. I missed that he passed along the position on every individual draw...
Thought he was dealing with a bunch of static geometry. And then I also assumed that he meant to draw a lot of it... :-/
0

Share this post


Link to post
Share on other sites
Thanks a lot mhagain, It works ! I understand better now. Actually, I was confusing between array passed to glVertexPointer and glTexCoordPointer. Your code shows that we have to compute the coordinates of each quad vertex and this FOR each body. I thought that we would just need the "raw" positions of each body (containing in the "pos" array) without doing the shift.

So now, I am going to increase the performance of display by using texured objects with VBO.

Maybe I will bore you again on this forum :)
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