Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  [SOLVED AGAIN] Where are the texture coordinates going wrong?  (Read 527 times)
0 Members and 1 Guest are viewing this topic.
Offline Kwauhn

Senior Newbie


Exp: 3 years


I like cats


« Posted 2014-01-14 00:41:26 »

So I am making a game engine, and fairly early on I came across a problem that is really boggling me. I have a test sphere implemented in-game by a class called "StaticMesh", which contains a model parsed from an obj file and a texture. This sphere was created and exported in blender (where it looked fine), but when I attach a texture to the static mesh the texture coordinates just break.

EDIT: With help form StrideColossus I fixed the previous problem, and I thought I'd fixed the problem after that, but now it appears as though I haven't. I used slick's "flip" option on one of my school computers (a mac) and it solved the problem, but when I took my project back home to my computer (windows) the "flip" option ceased to work. I tried it again yesterday on a mac at school and the code is still good, but sure enough, when I take it home we're back to "flip" not working. I'd assume this is a system related issue, but maybe there's a fix?

This is what it looked like in blender (the black lines mark the seams)


And this is what it looks like in my engine


I've combed over the code, and can't find any reason as to why it would act like this, but maybe some fresh eyes can spot a problem. Here's the "StaticMesh" class
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
package neatEngine.game;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import neatEngine.Console;
import neatEngine.graphics.Polygon;
import neatEngine.graphics.TextureImage;
import neatEngine.math.Vector2f;
import neatEngine.math.Vector3f;
import static org.lwjgl.opengl.GL11.*;

/*
 * An environment component that has static geometry.
 * <p>
 * The static mesh should be used for models who's geometry and textures don't
 * change.
 *
 * @author Kwauhn (Quinn Desforge-Major)
 * @version 0.01
 */

public class StaticMesh {
   private Vector3f p;
   private List<Vector3f> v;
   private List<Vector3f> n;
   private List<Vector2f> t;
   private List<Polygon> f;
   private TextureImage ti;
   
   /*
    * Creates a new static mesh from the specified resource at the specified
    * position.
    *
    * @param r the resource file containing the mesh information
    * @param tir the resource file containing the texture image
    * @param p the vector representing the position of this static mesh
    */

   public StaticMesh(String r, String tir, Vector3f p){
      this.p = p;
      v = new ArrayList<Vector3f>();
      n = new ArrayList<Vector3f>();
      t = new ArrayList<Vector2f>();
      f = new ArrayList<Polygon>();
      ti = new TextureImage(tir);
     
      int lineNumber = 0;
      try{
         BufferedReader reader = new BufferedReader(new FileReader(new File(r)));
         String line;
         while((line = reader.readLine()) != null){
            if(line.startsWith("v ")){
               String[] indices = line.split(" ");
               v.add(new Vector3f(Float.valueOf(indices[1]), Float.valueOf(indices[2]), Float.valueOf(indices[3])));
            }else if(line.startsWith("vn ")){
               String[] indices = line.split(" ");
               n.add(new Vector3f(Float.valueOf(indices[1]), Float.valueOf(indices[2]), Float.valueOf(indices[3])));
            }else if(line.startsWith("vt ")){
               String[] indices = line.split(" ");
               t.add(new Vector2f(Float.valueOf(indices[1]), Float.valueOf(indices[2])));
            }else if(line.startsWith("f ")){
               String[] points = line.split(" ");
               String[] point1 = points[1].split("/");
               String[] point2 = points[2].split("/");
               String[] point3 = points[3].split("/");
               Vector3f vertexIndices = new Vector3f(Integer.valueOf(point1[0]), Integer.valueOf(point2[0]), Integer.valueOf(point3[0]));
               Vector3f normalIndices = new Vector3f(Integer.valueOf(point1[2]), Integer.valueOf(point2[2]), Integer.valueOf(point3[2]));
               Vector3f textureIndices = new Vector3f(Integer.valueOf(point1[1]), Integer.valueOf(point2[1]), Integer.valueOf(point3[1]));
               f.add(new Polygon(vertexIndices, normalIndices, textureIndices));
            }
            lineNumber++;
         }
         reader.close();
      }catch(Exception e){
         Console.logError(e, "Could not load static mesh file: Error at " + lineNumber);
         e.printStackTrace();
      }
   }
   
   /*
    * Renders this static mesh.
    */

   public void render(){
      ti.bind();
      glPushMatrix();
      glTranslatef(p.getX(), p.getY(), p.getZ());
      glBegin(GL_TRIANGLES);
      {
         for(int i = 0; i < f.size(); i++){
            Vector3f v1 = v.get((int)(f.get(i).getVertexIndices().getX()) - 1);
            Vector3f v2 = v.get((int)(f.get(i).getVertexIndices().getY()) - 1);
            Vector3f v3 = v.get((int)(f.get(i).getVertexIndices().getZ()) - 1);
            Vector3f n1 = n.get((int)(f.get(i).getNormalIndices().getX()) - 1);
            Vector3f n2 = n.get((int)(f.get(i).getNormalIndices().getY()) - 1);
            Vector3f n3 = n.get((int)(f.get(i).getNormalIndices().getZ()) - 1);
            Vector2f t1 = t.get((int)(f.get(i).getTextureIndices().getX()) - 1);
            Vector2f t2 = t.get((int)(f.get(i).getTextureIndices().getY()) - 1);
            Vector2f t3 = t.get((int)(f.get(i).getTextureIndices().getZ()) - 1);
           
             glNormal3f(n1.getX(), n1.getY(), n1.getZ());   glTexCoord2f(t1.getX(), t1.getY());      glVertex3f(v1.getX(), v1.getY(), v1.getZ());
             glNormal3f(n2.getX(), n2.getY(), n2.getZ());   glTexCoord2f(t2.getX(), t2.getY());      glVertex3f(v2.getX(), v2.getY(), v2.getZ());
             glNormal3f(n3.getX(), n3.getY(), n3.getZ());   glTexCoord2f(t3.getX(), t3.getY());    glVertex3f(v3.getX(), v3.getY(), v3.getZ());
         }
      }
      glEnd();
      glPopMatrix();
      glDisable(GL_TEXTURE_2D);
   }
   
   /*
    * Returns the vector representing the position of this static mesh.
    *
    * @return the vector representing the position of this static mesh
    */

   public Vector3f getPosition(){
      return p;
   }
   
   /*
    * Sets the current position vector to the one specified.
    *
    * @param p the new position vector
    */

   public void setPosition(Vector3f p){
      this.p = p;
   }
}


And here's the "TextureImage" class
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
package neatEngine.graphics;

import java.io.File;
import java.io.FileInputStream;
import neatEngine.Console;
import org.newdawn.slick.opengl.*;

/*
 * This class makes texture handling easy and simple.
 * <p>
 * Texture Image uses the Slick Utility library to handle textures, and contains
 * some simple fail-safes to avoid fatal errors.
 *
 * @author Kwauhn (Quinn Desforge-Major)
 * @version 0.01
 */

public class TextureImage {
   private Texture t;
   private String r;
   
   /*
    * Creates a new Texture Image from the file with the provided path.
    *
    * @param r the path of the image resource
    */

   public TextureImage(String r){
      this.r = r;
      try{
         t = TextureLoader.getTexture("PNG", new FileInputStream(new File(r)), true);
      }catch(Exception e){
         Console.logError(e, "Could not load texture from \"" + r + "\"");
         t = null;
      }
   }
   
   /*
    * Binds the texture for drawing with OpenGL.
    */

   public void bind(){
      if(t != null){
         t.bind();
      }
   }
   
   /*
    * Reloads the texture file (for runtime editing).
    */

   public void reload(){
      try{
         t = TextureLoader.getTexture("PNG", new FileInputStream(new File(r)), true);
      }catch(Exception e){
         Console.logError(e, "Could not load texture from \"" + r + "\"");
         t = null;
      }
   }
}


Any help is appreciated. Staring at this code isn't really helping the project get along, and there's a lot to do! Thanks for reading Smiley
Offline Kwauhn

Senior Newbie


Exp: 3 years


I like cats


« Reply #1 - Posted 2014-01-14 01:55:41 »

Upon further investigation of the obj file, it appears as though blender set every UV index any given face to the same texture coordinate. Does anyone know how to fix this?
Offline Stranger

Senior Member


Medals: 6



« Reply #2 - Posted 2014-01-14 06:20:04 »

Are you sure everything it's ok in these lines?

96            Vector2f t1 = t.get((int)(f.get(i).getTextureIndices().getX()) - 1);
97            Vector2f t2 = t.get((int)(f.get(i).getTextureIndices().getX()) - 1);
98            Vector2f t3 = t.get((int)(f.get(i).getTextureIndices().getX()) - 1);

Anton
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Kwauhn

Senior Newbie


Exp: 3 years


I like cats


« Reply #3 - Posted 2014-01-14 16:18:10 »

Thank you! You saved me a lot of trouble looking in the wrong place, however, now the sphere looks like this


A can see the texture is actually being applied to the surface, it's just in the wrong places Sad I'll keep looking into it. Thanks so much for pointing that out!
Offline StrideColossus
« Reply #4 - Posted 2014-01-14 16:44:13 »

The 'seams' look to be there now but are way fatter than the one in Blender, maybe it's something to do with how you're loading the texture image?  Can you post that code as well.

As an aside that's a lot of line.splits there Wink, suggest use some intermediate variables: your code will be a lot cleaner and therefore easier to read and maintain, plus a little faster to load too.
Offline Kwauhn

Senior Newbie


Exp: 3 years


I like cats


« Reply #5 - Posted 2014-01-14 17:12:46 »

Thanks for the tips on performance Smiley I'll keep that in mind when I'm cleaning up code.

As for the texture loader, I'm using slick-util, so I wouldn't think it would be anything with that. I guess you never know though, this is the "TextureImage" class
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
package neatEngine.graphics;

import java.io.File;
import java.io.FileInputStream;

import neatEngine.Console;

import org.newdawn.slick.opengl.*;

/*
 * This class makes texture handling easy and simple.
 * <p>
 * Texture Image uses the Slick Utility library to handle textures, and contains
 * some simple fail-safes to avoid fatal errors.
 *
 * @author Kwauhn (Quinn Desforge-Major)
 * @version 0.01
 */

public class TextureImage {
   private Texture t;
   private String r;
   
   /*
    * Creates a new Texture Image from the file with the provided path.
    *
    * @param r the path of the image resource
    */

   public TextureImage(String r){
      this.r = r;
      try{
         t = TextureLoader.getTexture("PNG", new FileInputStream(new File(r)));
      }catch(Exception e){
         Console.logError(e, "Could not load texture from \"" + r + "\"");
         t = null;
      }
   }
   
   /*
    * Binds the texture for drawing with OpenGL.
    */

   public void bind(){
      if(t != null){
         t.bind();
      }
   }
   
   /*
    * Reloads the texture file (for runtime editing).
    */

   public void reload(){
      try{
         t = TextureLoader.getTexture("PNG", new FileInputStream(new File(r)));
      }catch(Exception e){
         Console.logError(e, "Could not load texture from \"" + r + "\"");
         t = null;
      }
   }
}
Offline StrideColossus
« Reply #6 - Posted 2014-01-14 17:23:18 »

Thanks for the tips on performance Smiley I'll keep that in mind when I'm cleaning up code.

The performance is a bonus, I was thinking more about maintainability - be a lot easier to change and fix the code if it's neater.

Quote
As for the texture loader, I'm using slick-util, so I wouldn't think it would be anything with that. I guess you never know though, this is the "TextureImage" class

Never used Slick but it's unlikely to be a problem with that as you say.  Maybe try using the 'flip' parameter in getTexture()?  (though it doesn't look like that's the problem)
Offline Kwauhn

Senior Newbie


Exp: 3 years


I like cats


« Reply #7 - Posted 2014-01-14 17:43:17 »



SUCCESS! Thank you so much Cheesy now I can continue with the shader stuff.

I never would have guessed that was the problem, how did you know?
Offline StrideColossus
« Reply #8 - Posted 2014-01-15 09:40:15 »

OpenGL generally needs to have images inverted, you'll see that 'flip' option in most engines or libraries (including my own custom stuff that does it by default).
Glad you got it sorted.
- stride
Offline Kwauhn

Senior Newbie


Exp: 3 years


I like cats


« Reply #9 - Posted 2014-01-17 19:41:41 »

Slick's flip option doesn't seem to work on my windows computer. I added an edit above and updated the code, does anyone know why it refuses to work on my machine?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Kwauhn

Senior Newbie


Exp: 3 years


I like cats


« Reply #10 - Posted 2014-01-17 20:16:42 »

I have NO IDEA why, but if the extension of the image is anything but "PNG" slick loads the image properly (even though it's a png file).

Seriously, what the hell  Tongue
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.

Dwinin (28 views)
2014-09-12 09:08:26

Norakomi (57 views)
2014-09-10 13:57:51

TehJavaDev (75 views)
2014-09-10 06:39:09

Tekkerue (38 views)
2014-09-09 02:24:56

mitcheeb (57 views)
2014-09-08 06:06:29

BurntPizza (45 views)
2014-09-07 01:13:42

Longarmx (28 views)
2014-09-07 01:12:14

Longarmx (34 views)
2014-09-07 01:11:22

Longarmx (35 views)
2014-09-07 01:10:19

mitcheeb (40 views)
2014-09-04 23:08:59
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!