Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

Advanced search

1141956 Posts in 48108 Topics- by 39377 Members - Latest Member: mixin

July 29, 2015, 03:31:30 am

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)OpenGL Textured Rectangle with Tris!
Pages: [1]
Author Topic: OpenGL Textured Rectangle with Tris!  (Read 1354 times)
Level 10

All day, every other day

View Profile WWW
« on: February 17, 2013, 07:47:39 am »

What I have now is this but it feels terribly wrong, does anyone have a better one?

public static void render3DTexture(String texture, double pX, double pY,
double pZ, double tX, double tY, double w, double h, double dW,
double dH, double rX, double rY, double rZ) {
glBindTexture(GL_TEXTURE_2D, textures.get(texture));


glTranslated(pX, pY, pZ);

glRotated(rX, 1, 0, 0);
glRotated(rY, 0, 1, 0);
glRotated(rZ, 0, 0, 1);

glScale((float) dW, (float) dH);


glTexCoord2d(tX / w, (tY + 1) / h);
glVertex2f(-0.5f, -0.5f);

glTexCoord2d((tX + 1) / w, (tY + 1) / h);
glVertex2f(0.5f, -0.5f);

glTexCoord2d((tX + 1) / w, tY / h);
glVertex2f(0.5f, 0.5f);

glTexCoord2d(tX / w, tY / h);
glVertex2f(-0.5f, 0.5f);

glTexCoord2d(tX / w, (tY + 1) / h);
glVertex2f(-0.5f, -0.5f);



glBindTexture(GL_TEXTURE_2D, 0);

Level 8


View Profile WWW
« Reply #1 on: February 17, 2013, 08:46:09 am »

In GL (and with 3D in general) what you want to do is try and batch a large-ish amount of geometry together at once, and then use matrices to transform it all.

So typically what you will want to do is make a class (or function) that can render an object or complete mesh. Using immediate mode GL (what you are doing) you'll end up with a few calls to set things up (like glBindTexture, glEnable (GL_TEXTURE_2D), glPushMatrix, glTranslate/etc. for the object's rotation and position) and then following that a glBegin (GL_TRIANGLES) and a loop to go through and draw tons of triangles by calling glNormal3f and glVertex3f repeatedly.

This can be actually quite fast for many purposes; however eventually you may want to move to vertex arrays or display lists. But as long as your code is structured more or less this way it's not a huge deal to migrate it.

You generally don't want to draw a single quad, unless for e.g. user interface purposes (rendering a button, icon) or else perhaps impostors. Even in the case of impostors, however, you'd typically want to render all of them at the same time if you can manage.

Hope this helps!
Level 5

View Profile WWW Email
« Reply #2 on: February 17, 2013, 09:14:04 am »

If you were making a next AAA title I would say what you wrote is a moral sin. But since we are on an indie dev forum I would say it looks good enough (in terms of speed of development/convenience being > than every single tiny bit of performance). Depends how important rendering speed is for your game...

One thing I would change is the way you handle texture binding. It's actually surprisingly expensive so I would get rid of:
glBindTexture(GL_TEXTURE_2D, 0);
glBindTexture(GL_TEXTURE_2D, textures.get(texture));
if(texture!=LASTBINDTEXTURE) glBindTexture(GL_TEXTURE_2D, textures.get(texture));

Pocket Space Empire [PC game] - fast paced, no micromanagement, turn based 4X space empire builder (devlog)
Level 1

View Profile Email
« Reply #3 on: February 17, 2013, 09:17:38 am »

You should find/write vector, quaternion and matrix classes. For any serious usage it gets really tedious to use three variables instead of one. Functions with 13 arguments are rarely a good idea. You should also write a class for textures to quickly get texture size and to avoid redundant opengl calls.

A much cleaner function signature would then look something like that:
public static void render3DTexture(Texture texture, Matrix44 transform, Rect2 texcoords)

Level 0

View Profile WWW
« Reply #4 on: February 18, 2013, 11:50:53 am »

I would not change it just because it 'feels' wrong Smiley

This is something I used when I was prototyping Paddlenoid:

#define TRIANGLE_QUAD_ARRAY(x, y, w, h) \
{                 \
    x, y,         \
    x,   y + h, \
    x + w, y + h, \
    x + w, y + h, \
    x + w, y,     \
    x,     y      \

void DrawTextureInPlace(GLuint gluiTexture, GLshort iWidht, GLshort iHeight)
  static const GLshort texCoords[] = TRIANGLE_QUAD_ARRAY(0, 0, 1, 1);

  const GLshort ihw = iWidht/2;
  const GLshort ihh = iHeight/2;
  const GLshort boxCoords[] = TRIANGLE_QUAD_ARRAY(-ihw, -ihh, iWidht, iHeight);

  glBindTexture(GL_TEXTURE_2D, gluiTexture);

  glTexCoordPointer(2, GL_SHORT, 0, texCoords);
  glVertexPointer(2, GL_SHORT, 0, boxCoords);
  glDrawArrays(GL_TRIANGLES, 0, 6);


It uses vertex arrays. It's not the 'right' way either. But since it has less 'gl' calls it's going to be a lot faster on some devices.

The idea is to
'glPushMatrix' and 'glRotate' / 'glTranslate' into position, call this function and 'glPopMatrix' again .. if you're working with a fixed pipeline that is...

But like 'Archibald' and 'PsySal' already mentioned. For really great performance, try to work with larger batches of triangles. And when you do, use VBO's instead of vertex array (like in my example) and don't create them when you need to draw them but create them just once before you draw them Smiley

But more importantly, only optimize when you have a reason to. If this is fast / practical enough, don't change a thing, focus on the game !
Level 2

View Profile WWW Email
« Reply #5 on: February 18, 2013, 12:59:06 pm »

Just an addition to what's already said, if you're learning opengl now, you should probably learn the more recent "GL3" style method of copying the vertices into a buffer and drawing the buffer (instead of the glBegin/End calls), otherwise if you intend to continue delving into 3d you'll find you eventually need to forget everything you learned about the old Gl1.5 calls like this and learn the newer way.
Pages: [1]
Jump to:  

Theme orange-lt created by panic