Texturing OpenGL Faces

I just recently started Doing 3D Game Development in Java with LWJGL Currently Ive Managed to create a Basic Model, and Load it and Render it on screen, but I cant Figure out how to Take a Texture and Apply it to the Model.

I know it has something to do with glBind() or glBindTexture(). and I can figure out how to load a PNG using inputStream, but I cant figure out what to do with the texture AFTER I have it loaded all the tutorials I see Just do 2D with Quads and I’m using 3D and Triangles

Texturing a 3d model involves using texture coordinates to apply a region of a texture to each of the faces on the model. In the end, it’s the same idea as texturing a bunch of flat polygons, just more complicated.

This is normally something you’d never do by hand for any complex model. What you need is to look for a “UV Mapper” in your favorite modeling program, such as Blender or Milkshape. This will add texture coordinates to the model so it will be textured when you use a model loader and renderer that understands the model format. UV mapping can be a really tedious, fiddly business that takes a while to get used to (truthfully I never did) so be patient when learning it.

How Did I know someone was going to say that I had to go back to Blender. :’(

But thank you, Right now I’m only doing a simple cube. My Issue is more along the lines that I apply the texture to a Face and it dosn’t show. Below is what I currently have, the only thing I can think of would be I need to add glTexCoord3f(); But I dont know what to put as the texture coordinates.

            glColor3f(0.0f, 0.7f, 1.0f);
            modelTex.texture.bind();
            glBegin(GL_TRIANGLES);
            for (Face face : faces) {
            	
                Vector3f n1 = normals.get((int) face.normal.x - 1);
                glNormal3f(n1.x, n1.y, n1.z);
    			
                Vector3f v1 = vertices.get((int) face.vertex.x - 1);
                glVertex3f(v1.x, v1.y, v1.z);
    			
                Vector3f n2 = normals.get((int) face.normal.y - 1);
                glNormal3f(n2.x, n2.y, n2.z);

                Vector3f v2 = vertices.get((int) face.vertex.y - 1);
                glVertex3f(v2.x, v2.y, v2.z);

                Vector3f n3 = normals.get((int) face.normal.z - 1);
                glNormal3f(n3.x, n3.y, n3.z);

                Vector3f v3 = vertices.get((int) face.vertex.z - 1);
                glVertex3f(v3.x, v3.y, v3.z);
            }
            glEnd();

You only need glTexCoord2f, since you’re only using 2d textures (3d textures are very uncommon). The tutorials should cover how to texture one square, so just apply the same idea to all the faces of the cube.

Well then I managed to make the texture appear… but I ran into a few issues, First:

Seccond, I noticed the Faces are drawing in a specific Order… for instance one of the Faces is showing, Even though its on the rear face of the Cube.

http://dl.dropbox.com/u/21736488/Wut.png

Oh! You have to change a setting in your GL to get it to show up right (Took me hours to find that out.)

These are the snippets for when using LibGDX, however I’m fairly sure you can find the correct ones for LWJGL without having to look too hard.


		Gdx.gl.glEnable(GL10.GL_DEPTH_TEST);
		Gdx.gl10.glDepthFunc(GL10.GL_LESS);

PERFECT! now It doesn’t show through, and Yeah that’s the best part about OGL. its almost IDENTICAL regardless of language or library

glEnable(GL11.GL_DEPTH_TEST);
GL11.glDepthFunc(GL11.GL_LESS);

Now all I have to do is figure out how to un-stretch the textures.

:smiley:

Glad to be of help. I spent hours dicking around with things, trying to figure out why it mattered what order I rendered my triangles in. And how would I manage to handle it when I rotate around the damnable cube.

As for your current issue? My guess would be that it’s an issue with the order that your triangles are getting formed. However, I don’t know how you’d solve it with the way you’re doing things currently.

I’ve heard that term so many times. However, it only just snapped when you said it, heh. I always thought it was an ‘Ultra Violet Mapper’ or something, not a mapper that helps you select the correct U (& U2) and V (& V2) for your textures when you’re doing 3D stuff. xD

Well right now I just use a for loop and map the texture to EVERY face the same way. I have a hard time understanding Exporting to obj WITH textures. I know when you export it to obj thats been triangulated, the faces are in the format f v1/vt1/vn1 v2/vt2/vn2 v3/vt1/vn3 But Every time I export the obj, it never adds vt…

Isn’t GL_LEQUAL more preferable?

No clue, ra4king, is it?

What program are you using? Blender?

GL_LEQUAL is important for multitexturing. Don’t think it matters much otherwise.

Yes, I’m just using the default cube right now. My Problem is that whenever I try to add a texture in blender the texture only shows up in render and not in the model view. and when I export it to .obj I cant find the option to export with texture coordinates

also Ive got this working so far but the textures are seemingly mapped weird on every other triangle:

https://dl.dropbox.com/u/21736488/ModelLoader2.png

I think I understand this part, on the top triangle the coordinates are normal


1____________2
|.|..........|
|..|.........|
|...|........|
|....|.......|
|.....|......|
|......|.....|
|.......|... |
|........|...|
4____________3

but on the Second one its:


1_____________
|.|..........|
|..|.........|
|...|........|
|....|.......|
|.....|......|
|......|.....|
|.......|... |
|........|...|
4____________2





_____________3

Its also Creating Faces on the Inside for some reason… though in the obj file it only says there’s 12 Faces. none being the inside one

https://dl.dropbox.com/u/21736488/insidefaces.png

Aslo this is my Blender View, showing the texture Mapped properly but How do I export to obj with the texture coordinates?

https://dl.dropbox.com/u/21736488/BlenderView.png

To get it to work in Blender.

  1. Change one of your display panes to UV/Image Editor (I do it with the top right pane most of the time).
  2. Create a new Image of whatever dimensions you’d like to use for all of the object’s textures.
  3. On your main screen go into Edit Mode (Select the mesh and press Tab), select all (A).
  4. Press ‘U’ then select UV Unwrap or Smart UV Unwrap (Not sure what the difference is, honestly).
  5. Check whether the unwrap (Which will appear on your Image Editor pane) fits your dimensions. If it doesn’t, you might need to select ‘Constraint to Image Bounds’ in the UVs Menu on that display.
  6. Go to the properties window (Tends to be lower right) select ‘Render’ tab, then scroll down to ‘Bake’. Expand that, and select ‘Texture’ from the Bake Mode list. Then click ‘Bake’. It will output your current texture (Stretched as needed) to the image you created in step 2 and aligned to your UVs.
  7. Save that image.

If you need to make sure that certain things will be a specific size, you can mess around with the layout of the UV produced in step 5. This will both output a corrected texture and provide you with your face texture UVs. :3

BEAUTIFUL! The Obj is now showing the texture coordinates and v/vt/vn for the faces. now I just have to modify EVERYTHING(ModelLoader, Model, Face, Render) hahahaha but its an amazing start, Now all I need to learn is Animation. :smiley:

Check out the LibGDX ObjLoader. It should be able to help you figure out just how to modify it. :3

As for Animation? That’s where I’m having trouble myself. I’m torn between doing several .obj files, loading them into a program and then outputting a custom format or something. I mean, there are other options like the MD5 system, however those tend to be expensive (Based on bone movements, it recomputes a lot of stuff each frame or something) to use.

Definitely will check that out

As for animation, I know what you mean, I cant seem to find any other way besides MD5 or coming up with your own system.

Uhh ohh! CRAP! now the Model doesn’t show! … Ill pastebin the Code

Using Debug Statements I’ve determined that the program loads properly and Opens a window, but the object will no longer render at all…

GLTexturedModel:
http://pastebin.java-gaming.org/f5a7d260228

GLTexturedFace:
http://pastebin.java-gaming.org/5a7d6320828

GLModelLoader:
http://pastebin.java-gaming.org/d6288680623

Main:
http://pastebin.java-gaming.org/62888760326

EDIT: I think I figured it out. I’m only storing one of the texture coordinate indexes in the Face therefore its not mapping at all.
Second edit: in the ModelLoader I got the x coordinate every time… Now it shows again but the UV Map isnt working