Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (516)
Games in Android Showcase (122)
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   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Screenspace to Worldspace Coordinates [SOLVED] on: 2014-09-22 05:01:28
SOLVED EDIT:
Embarrassingly, I solved the problem soon after creating this post. What was originally:
1  
2  
3  
4  
5  
6  
7  
8  
public Matrix translate(Vector v)
{
   Matrix t = new Matrix().loadIdentity();
   t.setElement(2, 0, v.getX());
   t.setElement(2, 1, v.getY());
   mul(t);
   return this;
}

Should have been:
1  
2  
3  
4  
5  
6  
7  
8  
public Matrix translate(Vector v)
{
   Matrix t = new Matrix().loadIdentity();
   t.setElement(0, 2, v.getX());
   t.setElement(1, 2, v.getY());
   mul(t);
   return this;
}

I had the row/column addresses mixed up Tongue. Anyway, thanks to those who read the post even though I answered it shortly after making it!

ORIGINAL POST:
So I'm making a tool to edit worlds for my game and I've run into the problem of converting screenspace coordinates into worldspace coordinates. I need this so I can guide the cell-selection cursor with my mouse no matter how zoomed in or panned around the view is. Upon some reading I decided to use a view matrix to compute all the transformations going on with the view, then apply it's inverse to the mouse coordinates. Unfortunately this doesn't seem to be working, and I've no idea why.

The more I pan in one direction the more the cursor moves in the other. The more zoomed in the view is the closer the cursor gets to the origin. Just to be clear, I'm not using the view matrix to apply transformations on the renderings, I'm just using it to compute cursor coordinates.

Cursor code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
//reset view matrix
view = new Matrix();
//add translate transformation (to center of display so zooming appears to happen from center)
view.translate(GLDisplay.center());
//add scale transformation (scale x & y by zoom)
view.scale(new Vector(zoom, zoom));
//add translate transformation (to position of view)
view.translate(camera.get());
//set view matrix to inverse of it's self
view.invert();
//apply view matrix to mouse screenspace coordinates (supposed to give cursor coordinates in worldspace)
cursor = view.transform(new Vector(Mouse.getX(), Mouse.getY()));
//round cursor coordinates down to whole numbers
cursor.floor();


Matrix code:
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  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
//Multiply this matrix by matrix "a"
public Matrix mul(Matrix a)
{
   for(int r = 0; r < 3; r++)
   {
      for(int c = 0; c < 3; c++)
      {
         setElement
         (r, c,
            m[r][0] * a.getElement(0, c) +
            m[r][1] * a.getElement(1, c) +
            m[r][2] * a.getElement(2, c)
         );
      }
   }
   return this;
}

//Creates a translation matrix from a vector and applies it to this matrix
public Matrix translate(Vector v)
{
   Matrix t = new Matrix().loadIdentity();
   t.setElement(2, 0, v.getX());
   t.setElement(2, 1, v.getY());
   mul(t);
   return this;
}

//Creates a scaling matrix from a vector and applies it to this matrix
public Matrix scale(Vector v)
{
   Matrix s = new Matrix().loadIdentity();
   s.setElement(0, 0, v.getX());
   s.setElement(1, 1, v.getY());
   mul(s);
   return this;
}

//Returns the determinate of a minor matrix (used in inverting a matrix)
public float minorDeterminant(float[][] minor)
{
   return (minor[0][0] * minor[1][1]) - (minor[1][0] * minor[0][1]);
}

//Inverts this matrix
public Matrix invert()
{
   //matrix of minor determinates used to create the cofactor matrix
   Matrix minors = new Matrix();
   //generates matrix of minor determinants (minors)
   {
      //minor matrix to be recycled in making the matrix of minor determinates
      float[][] minor = new float[2][2];
      minor[0][0] = m[1][1];    minor[0][1] = m[1][2];
      minor[1][0] = m[2][1];    minor[1][1] = m[2][2];
      minors.setElement(0, 0, minorDeterminant(minor));
     
      minor[0][0] = m[1][0];    minor[0][1] = m[1][2];
      minor[1][0] = m[2][0];    minor[1][1] = m[2][2];
      minors.setElement(0, 1, minorDeterminant(minor));
     
      minor[0][0] = m[1][0];    minor[0][1] = m[1][1];
      minor[1][0] = m[2][0];    minor[1][1] = m[2][1];
      minors.setElement(0, 2, minorDeterminant(minor));
     
      minor[0][0] = m[0][1];    minor[0][1] = m[0][2];
      minor[1][0] = m[2][1];    minor[1][1] = m[2][2];
      minors.setElement(1, 0, minorDeterminant(minor));
     
      minor[0][0] = m[0][0];    minor[0][1] = m[0][2];
      minor[1][0] = m[2][0];    minor[1][1] = m[2][2];
      minors.setElement(1, 1, minorDeterminant(minor));
     
      minor[0][0] = m[0][0];    minor[0][1] = m[0][1];
      minor[1][0] = m[2][0];    minor[1][1] = m[2][1];
      minors.setElement(1, 2, minorDeterminant(minor));
     
      minor[0][0] = m[0][1];    minor[0][1] = m[0][2];
      minor[1][0] = m[1][1];    minor[1][1] = m[1][2];
      minors.setElement(2, 0, minorDeterminant(minor));
     
      minor[0][0] = m[0][0];    minor[0][1] = m[0][2];
      minor[1][0] = m[1][0];    minor[1][1] = m[1][2];
      minors.setElement(2, 1, minorDeterminant(minor));
     
      minor[0][0] = m[0][0];    minor[0][1] = m[0][1];
      minor[1][0] = m[1][0];    minor[1][1] = m[1][1];
      minors.setElement(2, 2, minorDeterminant(minor));
   }
   
   //Cofactor matrix to be transposed to create the adjugate matrix
   Matrix cofactor = new Matrix();
   //applies "checkerboard" signing of cofactor elements
   {
      cofactor.setElement(0, 0, minors.getElement(0, 0));
      cofactor.setElement(0, 1, -minors.getElement(0, 1));
      cofactor.setElement(0, 2, minors.getElement(0, 2));
     
      cofactor.setElement(1, 0, -minors.getElement(1, 0));
      cofactor.setElement(1, 1, minors.getElement(1, 1));
      cofactor.setElement(1, 2, -minors.getElement(1, 2));
     
      cofactor.setElement(2, 0, minors.getElement(2, 0));
      cofactor.setElement(2, 1, -minors.getElement(2, 1));
      cofactor.setElement(2, 2, minors.getElement(2, 2));
   }

   //Adjugate matrix to be divided by the determinant to invert this matrix
   Matrix adjugate = new Matrix();
   //loads cofactor elements into adjugate and transposes them
   {
      adjugate.setElement(0, 0, cofactor.getElement(0, 0));
      adjugate.setElement(0, 1, cofactor.getElement(1, 0));
      adjugate.setElement(0, 2, cofactor.getElement(2, 0));

      adjugate.setElement(1, 0, cofactor.getElement(0, 1));
      adjugate.setElement(1, 1, cofactor.getElement(1, 1));
      adjugate.setElement(1, 2, cofactor.getElement(2, 1));

      adjugate.setElement(2, 0, cofactor.getElement(0, 2));
      adjugate.setElement(2, 1, cofactor.getElement(1, 2));
      adjugate.setElement(2, 2, cofactor.getElement(2, 2));
   }
   
   //finds the determinate of this matrix
   float determinant =
   (
      (m[0][0] * m[1][1] * m[2][2]) +
      (m[0][1] * m[1][2] * m[2][0]) +
      (m[0][2] * m[1][0] * m[2][1]) +
      (m[0][1] * m[1][0] * m[2][2]) -
      (m[0][0] * m[1][2] * m[2][1]) -
      (m[0][2] * m[1][1] * m[2][0])
   );
   //sets each element of this matrix to the corresponding element of the adjugate divided by the determinant
   for(int r = 0; r < 3; r++)
   {
      for(int c = 0; c < 3; c++)
      {
         m[r][c] = adjugate.getElement(r, c) / determinant;
      }
   }
   
   return this;
}

//applies this matrix to a given vector
public Vector transform(Vector v)
{
   Vector vt = v.get();
   v.setX(m[0][0] * vt.getX() + m[0][1] * vt.getY() + m[0][2]);
   v.setY(m[1][0] * vt.getX() + m[1][1] * vt.getY() + m[1][2]);
   return v;
}


I've combed this code over for half the day, and spent the other half learning about matrices and writing it in the first place. I know my code mustn't be perfect, but I don't have the know-how to see what I've done wrong. If someone can spot the error in my ways or simply point me to a better way to go about this, I'd be really appreciative. I know it's quite a bit of code, but I'll take any help I can get. Thanks for taking time to read this and share your input, it means a lot.
2  Game Development / Newbie & Debugging Questions / Re: [lwjgl] [glsl] Blending & depth buffer problems on: 2014-05-15 15:10:18
The shader is a possible elegant solution to the problem that would also allow for shadow mapping that includes the alpha in textures. Also, if I could avoid the heavy process of manually sorting with a fast program that runs on the GPU it'd save a lot time and processing power.
3  Game Development / Newbie & Debugging Questions / Re: [lwjgl] [glsl] Blending & depth buffer problems on: 2014-05-15 06:28:45
In case this is an issue with the shader loading it's self, here is the class I made for handling shaders:

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  
package core;

import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL11.*;
import java.io.BufferedReader;
import java.io.FileReader;

public class Shader
{
   private int programHandle;
   private int vertHandle;
   private int fragHandle;
   
   public Shader(String res)
   {
      programHandle = glCreateProgram();
      vertHandle = glCreateShader(GL_VERTEX_SHADER);
      fragHandle = glCreateShader(GL_FRAGMENT_SHADER);
     
      StringBuilder vertSource = new StringBuilder();
      StringBuilder fragSource = new StringBuilder();
      try
      {
         BufferedReader vertReader = new BufferedReader(new FileReader("src/res/" + res + ".vs"));
         BufferedReader fragReader = new BufferedReader(new FileReader("src/res/" + res + ".fs"));
         String line;
         while((line = vertReader.readLine()) != null)
         {
            vertSource.append(line).append('\n');
         }
         while((line = fragReader.readLine()) != null)
         {
            fragSource.append(line).append('\n');
         }
         vertReader.close();
         fragReader.close();
      }
      catch(Exception e)
      {
         System.err.println(e);
      }
     
      glShaderSource(vertHandle, vertSource);
      glShaderSource(fragHandle, fragSource);
      glCompileShader(vertHandle);
      glCompileShader(fragHandle);
      if(glGetShaderi(vertHandle, GL_COMPILE_STATUS) == GL_FALSE)
      {
         System.err.println("Vertex shader failed to compile");
      }
      if(glGetShaderi(fragHandle, GL_COMPILE_STATUS) == GL_FALSE)
      {
         System.err.println("Fragment shader failed to compile");
      }
      glAttachShader(programHandle, vertHandle);
      glAttachShader(programHandle, fragHandle);
      glLinkProgram(programHandle);
      glValidateProgram(programHandle);
   }
   
   public void bind()
   {
      glUseProgram(programHandle);
   }
   
   public void release()
   {
      glUseProgram(0);
   }
   
   public void destroy()
   {
      glDeleteProgram(programHandle);
      glDeleteShader(vertHandle);
      glDeleteShader(fragHandle);
   }
}


EDIT: It's getting late here, I'm going in a few minutes, I'll check back tomorrow, and thanks for all the quick replies! I'll have to ask my programming teacher about this, it appears as though "discard" should be working properly, so I'm unsure as to what I should do next.
4  Game Development / Newbie & Debugging Questions / Re: [lwjgl] [glsl] Blending & depth buffer problems on: 2014-05-15 05:55:24
Yeah, there are no errors.

If I apply textures in my Java program do fragments take on the texel's colour, or do I have to re-map textures in the shader? Because maybe it's determining colours before the textures have been applied, as if they were on a solid white plane?
5  Game Development / Newbie & Debugging Questions / Re: [lwjgl] [glsl] Blending & depth buffer problems on: 2014-05-15 05:47:52
Yes, does it make a difference if I write the number one as 1 or 1.0?
6  Game Development / Newbie & Debugging Questions / Re: [lwjgl] [glsl] Blending & depth buffer problems on: 2014-05-15 05:36:06
The fragment shader works, if I change it to the following everything becomes red.

1  
2  
3  
4  
void main()
{
   gl_FragColor = vec4(1, 0, 0, 1);
}


EDIT: Using glGetShaderi(GL_COMPILE_STATUS) returns a clean compile too Sad
7  Game Development / Newbie & Debugging Questions / Re: [lwjgl] [glsl] Blending & depth buffer problems on: 2014-05-15 05:31:14
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
in vec2 coord;
out vec4 color;

uniform sampler2D tex;

void main()
{
   vec4 texel = texture(tex, coord);
   if(texel.a < 1)
   {
      discard;
   }
   color = texel;
}


This is the same one as on the OpenGL wiki, but every other variant I've tried fails just like this one.
8  Game Development / Newbie & Debugging Questions / [solved] [lwjgl] [glsl] Blending & depth buffer problems on: 2014-05-15 05:15:01
SOLVED EDIT:


My suspicions about textures not being properly mapped to fragments were correct! Using shaders provided by this (http://lwjgl.org/wiki/index.php?title=GLSL_Tutorial:_Texturing) page and then discarding fragments with alpha worked!

Vertex shader:
1  
2  
3  
4  
5  
void main()
{
   gl_Position = ftransform();
   gl_TexCoord[0] = gl_MultiTexCoord0;
}


Fragment shader:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
uniform sampler2D tex;

void main()
{
   gl_FragColor = texture2D(tex, gl_TexCoord[0].st);
   if(gl_FragColor.a < 1.0)
   {
      discard;
   }
}


ORIGINAL PROBLEM:

Hello everyone!

Yesterday I came across an issue that I first thought would be an easy work-around. When OpenGL uses blending it mixes fragments already written to the colour buffer, and rewrites fragments written to the depth buffer. Unfortunately this causes really annoying artefacts where there is plain alpha in a texture, and upon research I found out a few solutions. The first thing is to use blending like I already am and simply order rendering of transparent objects from back to front, but this seems over-complex for a problem so mundane. The second option, is to use a fragment shader that simply discards the fragment all together if it contains alpha. As I have read, the second option seems to be very simple and eliminates the need to order back-to-front as well as the need to use blending.

However...

It's not working. I used the shader provided by this (http://www.opengl.org/wiki/Transparency_Sorting) page and many variants constructed from other tutorials, even some blatant copy & pastes. Still, discard doesn't seem to work at all. As I understand, discard should literally cut out holes in the quads where there's alpha on the textures, but it doesn't. I even tried to discard any fragment that has a green value greater than 0, the "discard" command does nothing. I'm really tearing my hair out over this because not only would it solve my problem with blending, it would remove alpha fragments from the depth buffer and make shadow mapping possible.

Here is the problem:


And here's what it should look like (accept with depth testing):


My questions are: is there ultimately a better approach to transparency sorting that will allow for shadow mapping in the future? Am I misinterpreting the use of the "discard" command? If the latter, how can I properly use it to achieve transparency sorting with out the obvious front-to-back procedure?

Thanks for reading, and sorry for the long post! I just don't understand what to do, and tutorials don't actually come out and explain the steps to solving this.
9  Game Development / Newbie & Debugging Questions / Re: 2D label on objects in 3D (LWJGL) on: 2014-01-26 09:12:04
Ah! I forgot vertices can be used in matrix math Tongue

Thanks for clearing things up, now it's just up to me to work out the grimy details. Thanks to everyone who replied so quickly! I'll post how I did it when I do Cheesy
10  Game Development / Newbie & Debugging Questions / Re: 2D label on objects in 3D (LWJGL) on: 2014-01-26 08:37:20
to zFollette: thanks for the help, at first I though it was that simple, but then I realized If I'm rendering the labels on a 2D plane (the screen) how am I to tell where to render the label so that it's over top of the player in 3D?

to nerb: That was my guess too (pretty much reverse ray tracing), and now that I know I just have to find out how I could use the perspective matrix to cast the 3D coordinate to the a 2D coordinate on the screen.
11  Game Development / Newbie & Debugging Questions / Re: 2D label on objects in 3D on: 2014-01-26 07:58:05
LWJGL, sorry, I'll put that in the title.
12  Game Development / Newbie & Debugging Questions / 2D label on objects in 3D (LWJGL) on: 2014-01-26 07:55:08
In the end I want to be able to create labels like the player names in the below image,

however, I want them to remain the same size no matter how far away or close up the camera is. My first thought is that the labels would have to be rendered orthographically, but then how can I tell where to render them (in order to be above their player)? I figured maybe some kind of backwards ray-tracing, but I'm not sure how this could be done, let alone if this is even the answer.

My question is how can I render labels orthographically over objects rendered with perspective?
13  Game Development / Newbie & Debugging Questions / Re: Where are the texture coordinates going wrong? (It's broken again) on: 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
14  Game Development / Newbie & Debugging Questions / Re: Where are the texture coordinates going wrong? (It's broken again) on: 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?
15  Game Development / Newbie & Debugging Questions / Re: Where are the texture coordinates going wrong? on: 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?
16  Game Development / Newbie & Debugging Questions / Re: Where are the texture coordinates going wrong? on: 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;
      }
   }
}
17  Game Development / Newbie & Debugging Questions / Re: Where are the texture coordinates going wrong? on: 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!
18  Game Development / Newbie & Debugging Questions / Re: Where are the texture coordinates going wrong? on: 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?
19  Game Development / Newbie & Debugging Questions / [SOLVED AGAIN] Where are the texture coordinates going wrong? on: 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
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

TehJavaDev (31 views)
2014-10-27 03:28:38

TehJavaDev (26 views)
2014-10-27 03:27:51

DarkCart (40 views)
2014-10-26 19:37:11

Luminem (21 views)
2014-10-26 10:17:50

Luminem (26 views)
2014-10-26 10:14:04

theagentd (32 views)
2014-10-25 15:46:29

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

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

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

lcass (43 views)
2014-10-15 16:18:58
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!