Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Texturing OpenGL Faces  (Read 2548 times)
0 Members and 1 Guest are viewing this topic.
Offline 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
Offline sproingie

JGO Kernel


Medals: 202



« 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.
Offline 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.  Cry

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!
Legends of Yore - The Casual Retro Roguelike
Offline sproingie

JGO Kernel


Medals: 202



« 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.
Offline 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.


Offline 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);

Offline 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.
Offline UprightPath
« Reply #7 - Posted 2012-07-26 05:36:18 »

Cheesy

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

Offline 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...
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« 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!
Legends of Yore - The Casual Retro Roguelike
Offline 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?

Offline sproingie

JGO Kernel


Medals: 202



« Reply #11 - Posted 2012-08-02 01:59:50 »

GL_LEQUAL is important for multitexturing.  Don't think it matters much otherwise.
Offline 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?
Offline 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

Offline 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. Cheesy
Offline 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.

Offline 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.
Offline Jay_PC

Senior Newbie





« Reply #17 - Posted 2012-08-02 21:43:44 »

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
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (43 views)
2014-10-16 15:22:06

Norakomi (33 views)
2014-10-16 15:20:20

lcass (38 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (68 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (86 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!