Jump to content
  • Advertisement
Sign in to follow this  
wacaine

OpenGL How do I draw a different texture on each side of a Cube?

This topic is 3468 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

I am pretty new to OpenGL I have a cube and have a texture on it. The problem is i want a different texture per side. Can this be done. if so how? I only need a snippet of code applying 2 textures to two sides, i can expand it from there. this sounds like my issue but I would like sample program to go off of. http://www.gamedev.net/community/forums/topic.asp?topic_id=519151 here is the code that does it for all sides int[] tmp_tex = new int[1]; gl.glGenTextures(1, tmp_tex, 0); int tex = tmp_tex[0]; gl.glBindTexture(GL10.GL_TEXTURE_2D, tex); gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA, bmp.getWidth(), bmp.getHeight(), 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, bb); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); gl.glColor4f(1.0f, 1, 1, 1.0f); gl.glNormal3f(0,0,1); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4); gl.glNormal3f(0,0,-1); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 4, 4); gl.glColor4f(1, 1.0f, 1, 1.0f); gl.glNormal3f(-1,0,0); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 8, 4); gl.glNormal3f(1,0,0); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 12, 4); gl.glColor4f(1, 1, 1.0f, 1.0f); gl.glNormal3f(0,1,0); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 16, 4); gl.glNormal3f(0,-1,0); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 20, 4); [Edited by - wacaine on January 21, 2009 10:32:25 AM]

Share this post


Link to post
Share on other sites
Advertisement

// say we have an array of bmps: "bmps[6]"
// and their data "bbs[6]"
int[] tmp_tex = new int[6];

gl.glGenTextures(6, tmp_tex);
for(unsigned int i = 0; i < 6; ++i)
{
gl.glBindTexture(GL10.GL_TEXTURE_2D, tmp_tex);
gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA, bmps.getWidth(), bmps.getHeight(), 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, bbs);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
}


gl.glColor4f(1.0f, 1, 1, 1.0f);
gl.glNormal3f(0,0,1);
gl.glBindTexture(GL10.GL_TEXTURE_2D, tmp_tex[0]);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
gl.glNormal3f(0,0,-1);
gl.glBindTexture(GL10.GL_TEXTURE_2D, tmp_tex[1]);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 4, 4);

gl.glColor4f(1, 1.0f, 1, 1.0f);
gl.glNormal3f(-1,0,0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, tmp_tex[2]);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 8, 4);
gl.glNormal3f(1,0,0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, tmp_tex[3]);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 12, 4);

gl.glColor4f(1, 1, 1.0f, 1.0f);
gl.glNormal3f(0,1,0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, tmp_tex[4]);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 16, 4);
gl.glNormal3f(0,-1,0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, tmp_tex[5]);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 20, 4);




Basically you just bind a different texture before drawing each face. This isn't the most efficient way of doing it though, you would be better off using a vertex format that included the normal directions, and uvs which map into an "atlas" texture. Thats just a single texture that contains all the textures you want to use on the object. But that is more tricky to do.

/edit Just noticed the glGenTextures usage was weird. According to docs it only takes 2 parameters (assuming you are using TAO framework).

Share this post


Link to post
Share on other sites
You definitely want to make one big texture with various sub-textures inside of it for each face of the cube. Use texture coordinates to get the right sub-texture to display per face.

Share this post


Link to post
Share on other sites
thanks. thats very helpfull. i have it working now. i do not see too much slow down but I am interested if you have an example of how to make one big texture and how you grab parts of it out per face?

what is the efficiency difference? i mean the bmp's in one big texture vs, 6 smaller ones, its all in memory anyway?

Share this post


Link to post
Share on other sites
Quote:
Original post by wacaine
what is the efficiency difference? i mean the bmp's in one big texture vs, 6 smaller ones, its all in memory anyway?

Well you have 6 texture binds instead of one. However, with immediate mode and only a cube being drawn, you won't notice much difference. With a more complex scene (draw 100+ cubes ;) ) you'll get a bigger boost.

Using subtextures quite easy. Just put your 6 images into one bin image, preferably leaving a small border/gap in between (to prevent color bleeding). Then adjust your texture coordinates from [0,1] to the range each respective texture uses. If, for example all 6 images are next to each other on the x-axis you'd have [0, 1/6], [1/6,2/6] etc. for x (you should calculate those, e.g. subimage.left/image.width, in order to take gaps into account).

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!