Jump to content
  • Advertisement
Sign in to follow this  
rakoon2

OpenGL Texture questions and more.

This topic is 5192 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello guys! I want to port my 2D sprite tile game to OpenGl + SDL from "SDL only"! My game has 16*16 tiles so I should use a large texture. I am going to use a texture manager that returns a texture ID and texture coordinates.
1:  How large should I make the textures? 256*256?  512*512?

2:  Texture coordinates? How? Tutorial? Help please! :)

3:  Should I draw a quad or two triangles?

4:  Transparenc:  Masking?  Alpha-channel? Hm..!? :3


Thank you very much! :)

Share this post


Link to post
Share on other sites
Advertisement
1) Depends. 256x256 for maximum compatibility with legacy hardware.
512x512 and more for modern hardware (TNT2 class and above).
2) NeHe is a good start if you are looking for OGL tutorials.
Generally, your tile coords are in the range [0..1] and your tile size in texture-space is 1.0/TextureWidth * TileWidth and 1.0/TextureHeight * TileHeight.

Texture coords in OGL:

1 ^
|
y |
|
|
+-------------->
0 x 1


3) Use two triangles. It's more hardware-friendly.
You can, however, start with quads because it's simpler.

4) I'd go for alpha channel transparency because it provides a
smooth transparency range and avoids possible artifacts.
NeHe has tutorials for both, so just try out both and see what
you like most [smile].


Hope this helps,
Pat.

Share this post


Link to post
Share on other sites
Original post by darookie
1) Ok! :)
2)
Not:

0
+----------->
| x 1
y |
|
|
1 ~

Why 0 and 1?

Ea I have a texture 32*32. And I want to draw [0,0]-[16,16]. How?





Can you give me a simple example?


const int t_w = 16; // texture width
const int t_h = 16; // texture height
const int x = 300; // screen x
const int y = 300; // screen y

glBegin( GL_QUADS );
glTexCoord2f( 0, 0 ); glVertex2i( x, y );
glTexCoord2f( 1, 0 ); glVertex2i( x, y + t_h);
glTexCoord2f( 1, 1 ); glVertex2i( x + t_w, y + t_h );
glTexCoord2f( 0, 1 ); glVertex2i( x + t_w, y );
glEnd();








3) Ok! ;)

4) You mean Tutorial 32, right?! :)
I have some problems with sdl and the SDL_Image lib. :|

5)Hope this helps, Pat.
Yes, thank you! ;)

Share this post


Link to post
Share on other sites
Quote:
Original post by rakoon2

Not:

0
+----------->
| x 1
y |
|
|
1 ~

Why 0 and 1?

No. The OGL coord system has its origin set to the lower left corner.
An illustration:

Quote:

Can you give me a simple example?

This article (image taken from there) might help you with that.

If you need more information (preferable after reading and trying some tuts[wink]), feel free to ask.

Pat

Share this post


Link to post
Share on other sites

const int t_w = 16;
const int t_h = 16;
const int new_texture_id = tiles[x][y]->getTextureId();

glEnable( GL_TEXTURE_2D );
if( current_texture_id != new_texture_id )
{
glBindTexture( GL_TEXTURE_2D, texture_manager->getTexture( new_texture_id, tile->getTextureFileName()) ); // or so
current_texture = new_texture;
}

glBegin( GL_QUADS );
glTexCoord2f( 0, 0 ); glVertex2i( x, y ); // x -- y
glTexCoord2f( 1, 0 ); glVertex2i( x, y + t_h ); // x -- y + h
glTexCoord2f( 1, 1 ); glVertex2i( x + t_w, y + t_h ); // x+w -- y+h
glTexCoord2f( 0, 1 ); glVertex2i( x + t_w, y ); // x+w-- y
glEnd();




Okay! How can I set the TexCoords now?( not the coord on the quad(glTexCoord2f( 0, 1 )..)- the coords on the texture! )

Thank you! :)

Share this post


Link to post
Share on other sites
hmmm i'm not sure what you mean. The glTexCoord2f() calls attach the texture at the point specified to the vertex you specify afterwards, thats all you need todo.

You'll have to explain better I think if thats not what you mean

Share this post


Link to post
Share on other sites
16*16 tiles
a 256*256 texture that contains 16*16 tiles

I heard I should have one big texture to avoid state changes!
So how can I change the part of the texture I want to "blit" on the quad?



t // texture 256*256

+---+
| t1| quad with the first tile in the texture
+---+

+---+
|t17| quad with the 17th tile in the texture
+---+




How can I set the coordinates?
ea:
glSetCoordOnTex( 16, 16 ); // ??

Share this post


Link to post
Share on other sites
Read the tutorials on textures, all of this is covered there.

That said you entire texture is in the range 0-1. If you want to break it up into 16x16 chunks then the relevant coordinates (for a 256x256 texture) are 1/16 = .0625, so a quad with the first tile would be

glTexCoord2f(0,0);glVertex3f(...);
glTexCoord2f(0,.0625);glVertex3f(...);
glTexCoord2f(.0625,.0625);glVertex3f(...);
glTexCoord2f(.0625,0);glVertex3f(...);



but again, read nehe tutorial 6, all of this is explained in great detail there.

Share this post


Link to post
Share on other sites
Oh! Thank you! Weired! :p


for the 13.13 texture:

glTexCoord2f( 0.8125, 0.8125 );
glTexCoord2f( 0.875, 0.8125 );
glTexCoord2f( 0.875, 0.875 );
glTexCoord2f( 0.8125, 0.875 );

0.875 = 0.0625 * 14
0.8125 = 0.875 - 0.0625
?




*nehe,tut*

Share this post


Link to post
Share on other sites
Just to clear things up, OpenGL only has a standard option to make 0,0 reaside in the lower left corner of the screen.
If you set up the viewport by yourself via glOrtho (which I don't know since I've never used SDL), you can just alter the coordinates from:
glOrtho(0, 640, 0, 480, -1, 1);
to:
glOrtho(0, 640, 480, 0, -1, 1);

Then you would have a coordinate system with 0,0 as beeing the upper left corner of the screen.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!