Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411497 Posts in 69373 Topics- by 58428 Members - Latest Member: shelton786

April 25, 2024, 07:15:02 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)OpenGL Textured Rectangle with Tris!
Pages: [1]
Print
Author Topic: OpenGL Textured Rectangle with Tris!  (Read 2245 times)
Quarry
Level 10
*****


View Profile
« 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?

Code:
	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));

glPushMatrix();

glTranslated(pX, pY, pZ);

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

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

glBegin(GL_TRIANGLE_STRIP);

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

glEnd();

glPopMatrix();

glBindTexture(GL_TEXTURE_2D, 0);
}
Logged
PsySal
Level 8
***


Yaay!


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!
Logged
Chris Koźmik
Level 5
*****


Silver Lemur Games


View Profile WWW
« 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);
and
replace
glBindTexture(GL_TEXTURE_2D, textures.get(texture));
with
if(texture!=LASTBINDTEXTURE) glBindTexture(GL_TEXTURE_2D, textures.get(texture));
Logged

Stellar Monarch 2 (dev log, IN DEVELOPMENT)
Stellar Monarch (dev log, released)
epcc
Level 1
*


View Profile
« 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:
Code:
 public static void render3DTexture(Texture texture, Matrix44 transform, Rect2 texcoords) 
Logged

blackvanderson
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:

Code:
#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);
  glEnableClientState(GL_VERTEX_ARRAY);
  glEnableClientState(GL_TEXTURE_COORD_ARRAY);

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

  glDisableClientState(GL_VERTEX_ARRAY);
  glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}

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 !
Logged
Raptor85
Level 5
*****



View Profile WWW
« 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.
Logged

-Fuzzy Spider
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic