Jay_PC
Senior Newbie 
|
 |
«
Posted
2012-07-26 03:39:32 » |
|
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
|
|
|
|
sproingie
|
 |
«
Reply #1 - Posted
2012-07-26 03:58:28 » |
|
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.
|
|
|
|
Jay_PC
Senior Newbie 
|
 |
«
Reply #2 - Posted
2012-07-26 04:18:34 » |
|
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. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| 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(); |
|
|
|
|
Games published by our own members! Check 'em out!
|
|
sproingie
|
 |
«
Reply #3 - Posted
2012-07-26 04:28:29 » |
|
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.
|
|
|
|
Jay_PC
Senior Newbie 
|
 |
«
Reply #4 - Posted
2012-07-26 04:58:03 » |
|
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. 
|
|
|
|
UprightPath
|
 |
«
Reply #5 - Posted
2012-07-26 05:00:53 » |
|
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. 1 2
| Gdx.gl.glEnable(GL10.GL_DEPTH_TEST); Gdx.gl10.glDepthFunc(GL10.GL_LESS); |
|
|
|
|
Jay_PC
Senior Newbie 
|
 |
«
Reply #6 - Posted
2012-07-26 05:16:41 » |
|
PERFECT! now It doesn't show through, and Yeah that's the best part about OGL. its almost IDENTICAL regardless of language or library 1 2
| 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.
|
|
|
|
UprightPath
|
 |
«
Reply #7 - Posted
2012-07-26 05:36:18 » |
|
 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. What you need is to look for a "UV Mapper" in your favorite modeling program, such as Blender or Milkshape.
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
|
|
|
|
Jay_PC
Senior Newbie 
|
 |
«
Reply #8 - Posted
2012-07-26 15:30:15 » |
|
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...
|
|
|
|
ra4king
|
 |
«
Reply #9 - Posted
2012-08-02 01:12:04 » |
|
PERFECT! now It doesn't show through, and Yeah that's the best part about OGL. its almost IDENTICAL regardless of language or library 1 2
| 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. Isn't GL_LEQUAL more preferable?
|
|
|
|
Games published by our own members! Check 'em out!
|
|
UprightPath
|
 |
«
Reply #10 - Posted
2012-08-02 01:17:59 » |
|
Isn't GL_LEQUAL more preferable?
No clue, ra4king, is it? 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...
What program are you using? Blender?
|
|
|
|
sproingie
|
 |
«
Reply #11 - Posted
2012-08-02 01:59:50 » |
|
GL_LEQUAL is important for multitexturing. Don't think it matters much otherwise.
|
|
|
|
Jay_PC
Senior Newbie 
|
 |
«
Reply #12 - Posted
2012-08-02 02:07:19 » |
|
What program are you using? Blender?
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:  I think I understand this part, on the top triangle the coordinates are normal 1 2 3 4 5 6 7 8 9 10
| 1____________2 |.|..........| |..|.........| |...|........| |....|.......| |.....|......| |......|.....| |.......|... | |........|...| 4____________3 |
but on the Second one its: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| 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  Aslo this is my Blender View, showing the texture Mapped properly but How do I export to obj with the texture coordinates? 
|
|
|
|
UprightPath
|
 |
«
Reply #13 - Posted
2012-08-02 03:12:01 » |
|
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
|
|
|
|
Jay_PC
Senior Newbie 
|
 |
«
Reply #14 - Posted
2012-08-02 04:06:43 » |
|
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. 
|
|
|
|
UprightPath
|
 |
«
Reply #15 - Posted
2012-08-02 04:15:44 » |
|
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.
|
|
|
|
Jay_PC
Senior Newbie 
|
 |
«
Reply #16 - Posted
2012-08-02 04:40:20 » |
|
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.
|
|
|
|
|
|