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

Started by
3 comments, last by Lord_Evil 15 years, 3 months ago
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]
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).
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.
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?
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).

If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!

This topic is closed to new replies.

Advertisement