Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (517)
Games in Android Showcase (123)
games submitted by our members
Games in WIP (578)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  Phong Shading giving a black line  (Read 2076 times)
0 Members and 1 Guest are viewing this topic.
Offline Spacebeans
« Posted 2014-06-25 18:26:39 »

I've had this problem before, I think its because of normals. If anyone knows how to fix this, or add normals to my existing block batch, let me know.

Block.java
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  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
package passage.games;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.util.vector.Vector3f;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;

public class Block {
   public Vector3f location;
   public Vector3f rotation;
   
   private int vertexPointer;
   private int colorPointer;
   
   private FloatBuffer vertexBuffer;
   private FloatBuffer colorBuffer;
   
   private Shader boxShader;
     
   private float[] vertexData = {
       -1.0f,-1.0f,-1.0f,
       -1.0f,-1.0f, 1.0f,
       -1.0f, 1.0f, 1.0f,
       
       1.0f, 1.0f,-1.0f,
       -1.0f,-1.0f,-1.0f,
       -1.0f, 1.0f,-1.0f,
       
       1.0f,-1.0f, 1.0f,
       -1.0f,-1.0f,-1.0f,
       1.0f,-1.0f,-1.0f,
       
       1.0f, 1.0f,-1.0f,
       1.0f,-1.0f,-1.0f,
       -1.0f,-1.0f,-1.0f,
       
       -1.0f,-1.0f,-1.0f,
       -1.0f, 1.0f, 1.0f,
       -1.0f, 1.0f,-1.0f,
       
       1.0f,-1.0f, 1.0f,
       -1.0f,-1.0f, 1.0f,
       -1.0f,-1.0f,-1.0f,
       
       -1.0f, 1.0f, 1.0f,
       -1.0f,-1.0f, 1.0f,
       1.0f,-1.0f, 1.0f,
       
       1.0f, 1.0f, 1.0f,
       1.0f,-1.0f,-1.0f,
       1.0f, 1.0f,-1.0f,
       
       1.0f,-1.0f,-1.0f,
       1.0f, 1.0f, 1.0f,
       1.0f,-1.0f, 1.0f,
       
       1.0f, 1.0f, 1.0f,
       1.0f, 1.0f,-1.0f,
       -1.0f, 1.0f,-1.0f,
       
       1.0f, 1.0f, 1.0f,
       -1.0f, 1.0f,-1.0f,
       -1.0f, 1.0f, 1.0f,
       
       1.0f,  1.0f, 1.0f,
       -1.0f, 1.0f, 1.0f,
       1.0f, -1.0f, 1.0f
   };
   
   public float[] colorData = {
      0.583f,  0.771f,  0.014f,
       0.609f,  0.115f,  0.436f,
       0.327f,  0.483f,  0.844f,
       
       0.822f,  0.569f,  0.201f,
       0.435f,  0.602f,  0.223f,
       0.310f,  0.747f,  0.185f,
       
       0.597f,  0.770f,  0.761f,
       0.559f,  0.436f,  0.730f,
       0.359f,  0.583f,  0.152f,
       
       0.483f,  0.596f,  0.789f,
       0.559f,  0.861f,  0.639f,
       0.195f,  0.548f,  0.859f,
       
       0.014f,  0.184f,  0.576f,
       0.771f,  0.328f,  0.970f,
       0.406f,  0.615f,  0.116f,
       
       0.676f,  0.977f,  0.133f,
       0.971f,  0.572f,  0.833f,
       0.140f,  0.616f,  0.489f,
       
       0.997f,  0.513f,  0.064f,
       0.945f,  0.719f,  0.592f,
       0.543f,  0.021f,  0.978f,
       
       0.279f,  0.317f,  0.505f,
       0.167f,  0.620f,  0.077f,
       0.347f,  0.857f,  0.137f,
       
       0.055f,  0.953f,  0.042f,
       0.714f,  0.505f,  0.345f,
       0.783f,  0.290f,  0.734f,
       
       0.722f,  0.645f,  0.174f,
       0.302f,  0.455f,  0.848f,
       0.225f,  0.587f,  0.040f,
       
       0.517f,  0.713f,  0.338f,
       0.053f,  0.959f,  0.120f,
       0.393f,  0.621f,  0.362f,
       
       0.673f,  0.211f,  0.457f,
       0.820f,  0.883f,  0.371f,
       0.982f,  0.099f,  0.879f,
   };
   
   public Block(Vector3f location, Vector3f rotation){
      this.location = location;
      this.rotation = rotation;
   }
   
   public Block(Vector3f location){
      this.location = location;
      this.rotation = new Vector3f(0, 0, 0);
   }
   
   public Block(){
      this.location = new Vector3f(0, 0, 0);
      this.rotation = new Vector3f(0, 0, 0);
   }
   
   public void init(){
      vertexBuffer = BufferUtils.createFloatBuffer(108);
      vertexBuffer.put(vertexData);
      vertexBuffer.flip();
     
      colorBuffer = BufferUtils.createFloatBuffer(108);
      colorBuffer.put(colorData);
      colorBuffer.flip();
     
      vertexPointer = glGenBuffers();
      glBindBuffer(GL_ARRAY_BUFFER, vertexPointer);
      glBufferData(GL_ARRAY_BUFFER, vertexBuffer, GL_STATIC_DRAW);
      glBindBuffer(GL_ARRAY_BUFFER, 0);

      colorPointer = glGenBuffers();
      glBindBuffer(GL_ARRAY_BUFFER, colorPointer);
      glBufferData(GL_ARRAY_BUFFER, colorBuffer, GL_STATIC_DRAW);
      glBindBuffer(GL_ARRAY_BUFFER, 0);
     
      boxShader = new Shader();
      boxShader.attachVertexShader("./assets/box.vert");
      boxShader.attachFragmentShader("./assets/box.frag");
      boxShader.link();
   }
   
   public void render(){
      boxShader.bind();
         glPushMatrix();
           
            glTranslatef(location.x, location.y, location.z);
           
            glRotatef(rotation.x, 1, 0, 0);
            glRotatef(rotation.y, 0, 1, 0);
            glRotatef(rotation.z, 0, 0, 1);
           
            glEnableClientState(GL_VERTEX_ARRAY);
            glEnableClientState(GL_COLOR_ARRAY);
           
            glBindBuffer(GL_ARRAY_BUFFER, vertexPointer);
            glVertexPointer(3, GL_FLOAT, 0, 0);
           
            glBindBuffer(GL_ARRAY_BUFFER, colorPointer);
            glColorPointer(3, GL_FLOAT, 0, 0);
           
            glDrawArrays(GL_TRIANGLES, 0, 12 * 3);
           
            glDisableClientState(GL_VERTEX_ARRAY);
            glDisableClientState(GL_COLOR_ARRAY);
         glPopMatrix();
      boxShader.unbind();
   }
   
   public void update(){
     
   }
   
   public void destroy(){
      glDeleteBuffers(vertexPointer);
      glDeleteBuffers(colorPointer);
   }
}


Vertex shader
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
varying vec3 normal; //Normal data (not setup through batch yet)
varying vec3 vertex; //Vertex location for next stage

void main(){
   vertex = vec3(gl_ModelViewMatrix * gl_Vertex); //Vertex location init
    normal = normalize(gl_NormalMatrix * gl_Normal); //Normal data init

    gl_FrontColor = gl_Color; //Vertex color for next stage
   gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex; //Final position using built-in matrices because I'm lazy :P
}


Fragment shader
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
uniform vec3 lightLocation; //Just set to 10, 10, 10 in the game class
vec3 lightColor = vec3(100, 100, 100); //The lights color

varying vec3 normal; //Normal (not setup through batch yet)
varying vec3 vertex; //Vertex from last stage

void main(){
   vec3 light = normalize(lightLocation.xyz - vertex); //Normalize length between the light location, and vertex location
    vec4 diffuse = vec4(lightColor, 1.0) * max(dot(normal, light), 0.0); //Diffuse color (light color)
    diffuse = clamp(diffuse, 0.0, 1.0); //Final light color

    gl_FragColor = gl_Color * diffuse; //Vertex color + light color
}


My end result:
Offline pitbuller
« Reply #1 - Posted 2014-06-25 18:40:06 »

You need to renormalize interpolated vertex normal at fragment shader but I don't think this only will fix your problems.
Offline Spacebeans
« Reply #2 - Posted 2014-06-25 18:43:13 »

Like I said, I'm not getting normals from anywhere. I don't know how to create those in VBOs. If you have any idea, please tell me!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online trollwarrior1
« Reply #3 - Posted 2014-06-25 18:46:56 »

To create draw normals, first create a buffer object for those normals and store them like you would vertex position. When its time to render, bind the normal buffer and call glNormalPointer
Offline Spacebeans
« Reply #4 - Posted 2014-06-25 18:55:18 »

To create draw normals, first create a buffer object for those normals and store them like you would vertex position. When its time to render, bind the normal buffer and call glNormalPointer

Okay, I think its working, I put the normals on the same data as the colors.


The current block 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  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
package passage.games;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.util.vector.Vector3f;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;

public class Block {
   public Vector3f location;
   public Vector3f rotation;
   
   private int vertexPointer;
   private int colorPointer;
   private int normalPointer;
   
   private FloatBuffer vertexBuffer;
   private FloatBuffer colorBuffer;
   private FloatBuffer normalBuffer;
   
   private Shader boxShader;
     
   private float[] vertexData = {
       -1.0f,-1.0f,-1.0f,
       -1.0f,-1.0f, 1.0f,
       -1.0f, 1.0f, 1.0f,
       
       1.0f, 1.0f,-1.0f,
       -1.0f,-1.0f,-1.0f,
       -1.0f, 1.0f,-1.0f,
       
       1.0f,-1.0f, 1.0f,
       -1.0f,-1.0f,-1.0f,
       1.0f,-1.0f,-1.0f,
       
       1.0f, 1.0f,-1.0f,
       1.0f,-1.0f,-1.0f,
       -1.0f,-1.0f,-1.0f,
       
       -1.0f,-1.0f,-1.0f,
       -1.0f, 1.0f, 1.0f,
       -1.0f, 1.0f,-1.0f,
       
       1.0f,-1.0f, 1.0f,
       -1.0f,-1.0f, 1.0f,
       -1.0f,-1.0f,-1.0f,
       
       -1.0f, 1.0f, 1.0f,
       -1.0f,-1.0f, 1.0f,
       1.0f,-1.0f, 1.0f,
       
       1.0f, 1.0f, 1.0f,
       1.0f,-1.0f,-1.0f,
       1.0f, 1.0f,-1.0f,
       
       1.0f,-1.0f,-1.0f,
       1.0f, 1.0f, 1.0f,
       1.0f,-1.0f, 1.0f,
       
       1.0f, 1.0f, 1.0f,
       1.0f, 1.0f,-1.0f,
       -1.0f, 1.0f,-1.0f,
       
       1.0f, 1.0f, 1.0f,
       -1.0f, 1.0f,-1.0f,
       -1.0f, 1.0f, 1.0f,
       
       1.0f,  1.0f, 1.0f,
       -1.0f, 1.0f, 1.0f,
       1.0f, -1.0f, 1.0f
   };
   
   public float[] colorData = {
      0.583f,  0.771f,  0.014f,
       0.609f,  0.115f,  0.436f,
       0.327f,  0.483f,  0.844f,
       
       0.822f,  0.569f,  0.201f,
       0.435f,  0.602f,  0.223f,
       0.310f,  0.747f,  0.185f,
       
       0.597f,  0.770f,  0.761f,
       0.559f,  0.436f,  0.730f,
       0.359f,  0.583f,  0.152f,
       
       0.483f,  0.596f,  0.789f,
       0.559f,  0.861f,  0.639f,
       0.195f,  0.548f,  0.859f,
       
       0.014f,  0.184f,  0.576f,
       0.771f,  0.328f,  0.970f,
       0.406f,  0.615f,  0.116f,
       
       0.676f,  0.977f,  0.133f,
       0.971f,  0.572f,  0.833f,
       0.140f,  0.616f,  0.489f,
       
       0.997f,  0.513f,  0.064f,
       0.945f,  0.719f,  0.592f,
       0.543f,  0.021f,  0.978f,
       
       0.279f,  0.317f,  0.505f,
       0.167f,  0.620f,  0.077f,
       0.347f,  0.857f,  0.137f,
       
       0.055f,  0.953f,  0.042f,
       0.714f,  0.505f,  0.345f,
       0.783f,  0.290f,  0.734f,
       
       0.722f,  0.645f,  0.174f,
       0.302f,  0.455f,  0.848f,
       0.225f,  0.587f,  0.040f,
       
       0.517f,  0.713f,  0.338f,
       0.053f,  0.959f,  0.120f,
       0.393f,  0.621f,  0.362f,
       
       0.673f,  0.211f,  0.457f,
       0.820f,  0.883f,  0.371f,
       0.982f,  0.099f,  0.879f,
   };
   
   public float[] normalData = {
      0.583f,  0.771f,  0.014f,
       0.609f,  0.115f,  0.436f,
       0.327f,  0.483f,  0.844f,
       
       0.822f,  0.569f,  0.201f,
       0.435f,  0.602f,  0.223f,
       0.310f,  0.747f,  0.185f,
       
       0.597f,  0.770f,  0.761f,
       0.559f,  0.436f,  0.730f,
       0.359f,  0.583f,  0.152f,
       
       0.483f,  0.596f,  0.789f,
       0.559f,  0.861f,  0.639f,
       0.195f,  0.548f,  0.859f,
       
       0.014f,  0.184f,  0.576f,
       0.771f,  0.328f,  0.970f,
       0.406f,  0.615f,  0.116f,
       
       0.676f,  0.977f,  0.133f,
       0.971f,  0.572f,  0.833f,
       0.140f,  0.616f,  0.489f,
       
       0.997f,  0.513f,  0.064f,
       0.945f,  0.719f,  0.592f,
       0.543f,  0.021f,  0.978f,
       
       0.279f,  0.317f,  0.505f,
       0.167f,  0.620f,  0.077f,
       0.347f,  0.857f,  0.137f,
       
       0.055f,  0.953f,  0.042f,
       0.714f,  0.505f,  0.345f,
       0.783f,  0.290f,  0.734f,
       
       0.722f,  0.645f,  0.174f,
       0.302f,  0.455f,  0.848f,
       0.225f,  0.587f,  0.040f,
       
       0.517f,  0.713f,  0.338f,
       0.053f,  0.959f,  0.120f,
       0.393f,  0.621f,  0.362f,
       
       0.673f,  0.211f,  0.457f,
       0.820f,  0.883f,  0.371f,
       0.982f,  0.099f,  0.879f,
   };
   
   public Block(Vector3f location, Vector3f rotation){
      this.location = location;
      this.rotation = rotation;
   }
   
   public Block(Vector3f location){
      this.location = location;
      this.rotation = new Vector3f(0, 0, 0);
   }
   
   public Block(){
      this.location = new Vector3f(0, 0, 0);
      this.rotation = new Vector3f(0, 0, 0);
   }
   
   public void init(){
      vertexBuffer = BufferUtils.createFloatBuffer(108);
      vertexBuffer.put(vertexData);
      vertexBuffer.flip();
     
      colorBuffer = BufferUtils.createFloatBuffer(108);
      colorBuffer.put(colorData);
      colorBuffer.flip();
     
      normalBuffer = BufferUtils.createFloatBuffer(108);
      normalBuffer.put(normalData);
      normalBuffer.flip();
     
      vertexPointer = glGenBuffers();
      glBindBuffer(GL_ARRAY_BUFFER, vertexPointer);
      glBufferData(GL_ARRAY_BUFFER, vertexBuffer, GL_STATIC_DRAW);
      glBindBuffer(GL_ARRAY_BUFFER, 0);

      colorPointer = glGenBuffers();
      glBindBuffer(GL_ARRAY_BUFFER, colorPointer);
      glBufferData(GL_ARRAY_BUFFER, colorBuffer, GL_STATIC_DRAW);
      glBindBuffer(GL_ARRAY_BUFFER, 0);
     
      normalPointer = glGenBuffers();
      glBindBuffer(GL_ARRAY_BUFFER, normalPointer);
      glBufferData(GL_ARRAY_BUFFER, normalBuffer, GL_STATIC_DRAW);
      glBindBuffer(GL_ARRAY_BUFFER, 0);
   }
   
   public void render() {
      glPushMatrix();

      glTranslatef(location.x, location.y, location.z);

      glRotatef(rotation.x, 1, 0, 0);
      glRotatef(rotation.y, 0, 1, 0);
      glRotatef(rotation.z, 0, 0, 1);

      glEnableClientState(GL_VERTEX_ARRAY);
      glEnableClientState(GL_COLOR_ARRAY);
      glEnableClientState(GL_NORMAL_ARRAY);

      glBindBuffer(GL_ARRAY_BUFFER, vertexPointer);
      glVertexPointer(3, GL_FLOAT, 0, 0);

      glBindBuffer(GL_ARRAY_BUFFER, colorPointer);
      glColorPointer(3, GL_FLOAT, 0, 0);
     
      glBindBuffer(GL_ARRAY_BUFFER, normalPointer);
      glNormalPointer(GL_FLOAT, 0, 0);
     
      glDrawArrays(GL_TRIANGLES, 0, 12 * 3);

      glDisableClientState(GL_VERTEX_ARRAY);
      glDisableClientState(GL_COLOR_ARRAY);
      glPopMatrix();
   }

   public void update(){
     
   }
   
   public void destroy(){
      glDeleteBuffers(vertexPointer);
      glDeleteBuffers(colorPointer);
   }
}
Offline Spacebeans
« Reply #5 - Posted 2014-06-29 20:38:14 »

Does anyone know how to correctly find the normals for this cube? I'm not really sure how to do that.
Offline Slyth2727
« Reply #6 - Posted 2014-06-29 21:32:22 »

This is a segment of the per vertex normal generation code I use.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
        for(int i = 0; i < indices.size(); i += 3) {
            int i0 = indices.get(i);
            int i1 = indices.get(i + 1);
            int i2 = indices.get(i + 2);

            Vector3f v1 = positions.get(i1).sub(positions.get(i0));
            Vector3f v2 = positions.get(i2).sub(positions.get(i0));

            Vector3f normal = v1.cross(v2).normalized();
        }


Where the positions List are the positions of the vertices.

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline Spacebeans
« Reply #7 - Posted 2014-06-30 01:04:06 »

Thanks for the reply, but I don't have indices, are there any tutorials on them? I was planning on just getting basic lighting working in 3D, so I can then build upon it with secularity, and different mappings.
Offline orange451

JGO Coder


Medals: 13
Projects: 2
Exp: 4 years


Your face. Your ass. What's the difference?


« Reply #8 - Posted 2014-06-30 01:40:07 »

but I don't have indices
psst.
(Your vertices)

Working on a nice FPS in lwjgl Smiley http://i.imgur.com/q4uFqBS.png
Feel free to message me if you're interested!
Offline Spacebeans
« Reply #9 - Posted 2014-06-30 01:46:05 »

psst.
(Your vertices)

I see what you mean, but me being the terrible thinker I am... How do I do this with Slyth2727's code?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline orange451

JGO Coder


Medals: 13
Projects: 2
Exp: 4 years


Your face. Your ass. What's the difference?


« Reply #10 - Posted 2014-06-30 02:05:31 »

I see what you mean, but me being the terrible thinker I am... How do I do this with Slyth2727's 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  
for (int i = 0; i < vertexData.length; i += 9) {
   float[] v1 = {vertexData[i],     vertexData[i + 1], vertexData[i + 2]}
   float[] v2 = {vertexData[i + 3], vertexData[i + 4], vertexData[i + 5]}
   float[] v3 = {vertexData[i + 6], vertexData[i + 7], vertexData[i + 8]}
   
   float[] normal = getTriangleNormal(v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], v3[0], v3[1], v3[2]
}

public static float[] getTriangleNormal(float argument0, float argument1, float argument2, float argument3, float argument4, float argument5, float argument6, float argument7, float argument8) {
   float ax,ay,az,bx,by,bz,m,rx,ry,rz;

   //point0 -> point1
   ax = argument3-argument0;
   ay = argument4-argument1;
   az = argument5-argument2;

   //point0 -> point2
   bx = argument6-argument0;
   by = argument7-argument1;
   bz = argument8-argument2;

   //cross product
   rx = ay*bz-by*az;
   ry = az*bx-bz*ax;
   rz = ax*by-bx*ay;

   //magnitude
   m = (float) Math.sqrt(rx*rx+ry*ry+rz*rz);

   //normalize
   rx /= m;
   ry /= m;
   rz /= m;

   return new float[] {rx, ry, rz};
}


Not tested, just wrote it in notepad++

Working on a nice FPS in lwjgl Smiley http://i.imgur.com/q4uFqBS.png
Feel free to message me if you're interested!
Offline Spacebeans
« Reply #11 - Posted 2014-06-30 02:17:36 »

Okay, the cubes are completely black now... Is this what you meant?

1  
2  
3  
4  
5  
6  
7  
      for (int i = 0; i < vertexData.length; i += 9) {
         float[] v1 = { vertexData[i],      vertexData[i + 1], vertexData[i + 2] };
         float[] v2 = { vertexData[i + 3], vertexData[i + 4], vertexData[i + 5] };
         float[] v3 = { vertexData[i + 6], vertexData[i + 7], vertexData[i + 8] };

         normalData = getTriangleNormal(v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], v3[0], v3[1], v3[2]);
      }
Offline orange451

JGO Coder


Medals: 13
Projects: 2
Exp: 4 years


Your face. Your ass. What's the difference?


« Reply #12 - Posted 2014-06-30 03:02:03 »

What I gave you would give you a float array containing the normal data for each triangle. You should be able to figure out the rest Smiley

[hint]
simply setting the normal array I provided to the "normalData" array wont work. You need to figure out how to fill the normalData array with the data from "normal"
[/hint]

Working on a nice FPS in lwjgl Smiley http://i.imgur.com/q4uFqBS.png
Feel free to message me if you're interested!
Offline Spacebeans
« Reply #13 - Posted 2014-06-30 05:30:26 »

Alright... I'm starting to get this...

1  
2  
3  
4  
5  
6  
7  
8  
9  
    for (int i = 0; i < vertexData.length; i += 9) {
         float[] v1 = { vertexData[i],     vertexData[i + 1], vertexData[i + 2] };
         float[] v2 = { vertexData[i + 3], vertexData[i + 4], vertexData[i + 5] };
         float[] v3 = { vertexData[i + 6], vertexData[i + 7], vertexData[i + 8] };
         
         normalData[i] = getTriangleNormal(v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], v3[0], v3[1], v3[2])[0];
         normalData[i+1] = getTriangleNormal(v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], v3[0], v3[1], v3[2])[1];
         normalData[i+2] = getTriangleNormal(v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], v3[0], v3[1], v3[2])[2];
      }


Still gives me incorrect normals.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 823
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #14 - Posted 2014-06-30 17:45:03 »

1  
2  
3  
4  
5  
6  
7  
    for (int i = 0; i < vertexData.length; i += 9) {
         ...
         
         normalData[i] = getTriangleNormal(v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], v3[0], v3[1], v3[2])[0];
         normalData[i+1] = getTriangleNormal(v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], v3[0], v3[1], v3[2])[1];
         normalData[i+2] = getTriangleNormal(v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], v3[0], v3[1], v3[2])[2];
      }


Notice how you increment i by 9 with
i+=9
, and fill only the slots
i+0, i+1, i+2
. Therefore the
normalData
array will contain zeroed normals for every 2nd and 3rd vertex of each triangle.

You probably meant:
1  
2  
3  
normalData[i/3+0]  = ...
normalData[i/3+1]  = ...
normalData[i/3+2]  = ...

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Spacebeans
« Reply #15 - Posted 2014-06-30 18:05:25 »

Notice how you increment i by 9 with
i+=9
, and fill only the slots
i+0, i+1, i+2
. Therefore the
normalData
array will contain zeroed normals for every 2nd and 3rd vertex of each triangle.

You probably meant:
1  
2  
3  
normalData[i/3+0]  = ...
normalData[i/3+1]  = ...
normalData[i/3+2]  = ...


Okay, it still gives me this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
      for (int i = 0; i < vertexData.length; i += 9) {
         float[] v1 = { vertexData[i],     vertexData[i + 1], vertexData[i + 2] };
         float[] v2 = { vertexData[i + 3], vertexData[i + 4], vertexData[i + 5] };
         float[] v3 = { vertexData[i + 6], vertexData[i + 7], vertexData[i + 8] };
         
         normalData[i/3+0] = getTriangleNormal(v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], v3[0], v3[1], v3[2]) [0];
         normalData[i/3+1] = getTriangleNormal(v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], v3[0], v3[1], v3[2]) [1];
         normalData[i/3+2] = getTriangleNormal(v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], v3[0], v3[1], v3[2]) [2];
      }
     
      normalBuffer = BufferUtils.createFloatBuffer(108);
      normalBuffer.put(normalData);
      normalBuffer.flip();

                normalPointer = glGenBuffers();
      glBindBuffer(GL_ARRAY_BUFFER, normalPointer);
      glBufferData(GL_ARRAY_BUFFER, normalBuffer, GL_STATIC_DRAW);
      glBindBuffer(GL_ARRAY_BUFFER, 0);


Offline Riven
« League of Dukes »

JGO Overlord


Medals: 823
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #16 - Posted 2014-06-30 18:26:12 »

For every vec3 in the vertexData array, there must be a vec3 in the normalData array. You currently generate 3x less data than you should for the normals.

So, for every i+=9, you have to stuff 9 values in normalData. In this case you replicate the calculated normal for each vertex of the triangle.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Spacebeans
« Reply #17 - Posted 2014-06-30 18:32:24 »

You currently generate 3x less data than you should for the normals.
So, for every i+=9, you have to stuff 9 values in normalData. In this case you replicate the calculated normal for each vertex of the triangle.

I really suck at this normal stuff, is this what you meant by 'replicate'?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
      for (int i = 0; i < vertexData.length; i += 9) {
         float[] v1 = { vertexData[i],     vertexData[i + 1], vertexData[i + 2] };
         float[] v2 = { vertexData[i + 3], vertexData[i + 4], vertexData[i + 5] };
         float[] v3 = { vertexData[i + 6], vertexData[i + 7], vertexData[i + 8] };
         
         normalData[i/3+0] = getTriangleNormal(v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], v3[0], v3[1], v3[2]) [0];
         normalData[i/3+1] = getTriangleNormal(v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], v3[0], v3[1], v3[2]) [1];
         normalData[i/3+2] = getTriangleNormal(v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], v3[0], v3[1], v3[2]) [2];
         
         normalData[i/3+3] = getTriangleNormal(v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], v3[0], v3[1], v3[2]) [0];
         normalData[i/3+4] = getTriangleNormal(v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], v3[0], v3[1], v3[2]) [1];
         normalData[i/3+5] = getTriangleNormal(v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], v3[0], v3[1], v3[2]) [2];
         
         normalData[i/3+6] = getTriangleNormal(v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], v3[0], v3[1], v3[2]) [0];
         normalData[i/3+7] = getTriangleNormal(v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], v3[0], v3[1], v3[2]) [1];
         normalData[i/3+8] = getTriangleNormal(v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], v3[0], v3[1], v3[2]) [2];
         
         System.out.println(normalData[i/3+0] + " " + normalData[i/3+1] + " " +normalData[i/3+2]);
      }
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 823
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #18 - Posted 2014-06-30 18:38:33 »

i+0...i+8

and try not to calculate the same normal 9 times

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Spacebeans
« Reply #19 - Posted 2014-06-30 18:41:21 »

i+0...i+8

I don't understand?
Offline CHmSID

Senior Newbie


Exp: 7-9 months



« Reply #20 - Posted 2014-06-30 20:14:42 »

I'm not sure I'm understanding the problem here, but for a simple cube, wouldn't the normals be just:

1,0,0 for the right face
-1,0,0 for the left face
0,1,0 for the top face
etc?

Am I missing something?
Offline orange451

JGO Coder


Medals: 13
Projects: 2
Exp: 4 years


Your face. Your ass. What's the difference?


« Reply #21 - Posted 2014-06-30 20:47:34 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
for (int i = 0; i < vertexData.length; i += 9) {
   float[] v1 = {vertexData[i],     vertexData[i + 1], vertexData[i + 2]}
   float[] v2 = {vertexData[i + 3], vertexData[i + 4], vertexData[i + 5]}
   float[] v3 = {vertexData[i + 6], vertexData[i + 7], vertexData[i + 8]}
   
   float[] triangleNormal = getTriangleNormal(v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], v3[0], v3[1], v3[2]);

   normalData[i] =     triangleNormal[0];
   normalData[i + 1] = triangleNormal[1];
   normalData[i + 2] = triangleNormal[2];
   normalData[i + 3] = triangleNormal[0];
   normalData[i + 4] = triangleNormal[1];
   normalData[i + 5] = triangleNormal[2];
   normalData[i + 6] = triangleNormal[0];
   normalData[i + 7] = triangleNormal[1];
   normalData[i + 8] = triangleNormal[2];
}


Not the cleanest way. It should work; but I haven't tested it.

Working on a nice FPS in lwjgl Smiley http://i.imgur.com/q4uFqBS.png
Feel free to message me if you're interested!
Offline Spacebeans
« Reply #22 - Posted 2014-06-30 22:55:49 »

Alright, I think its working, but I still see this;

Click to Play

Offline orange451

JGO Coder


Medals: 13
Projects: 2
Exp: 4 years


Your face. Your ass. What's the difference?


« Reply #23 - Posted 2014-06-30 23:17:13 »

If you make your shader just a normal passthrough, what does it look like? (without the phong).

Working on a nice FPS in lwjgl Smiley http://i.imgur.com/q4uFqBS.png
Feel free to message me if you're interested!
Offline Spacebeans
« Reply #24 - Posted 2014-06-30 23:23:33 »

If you make your shader just a normal passthrough, what does it look like? (without the phong).

Without colors:
Click to Play


With color, and without phong:
Click to Play
Offline orange451

JGO Coder


Medals: 13
Projects: 2
Exp: 4 years


Your face. Your ass. What's the difference?


« Reply #25 - Posted 2014-06-30 23:24:37 »

Now that is just odd o:

May I see your complete source? I'll see if my machine yields the same results.

Working on a nice FPS in lwjgl Smiley http://i.imgur.com/q4uFqBS.png
Feel free to message me if you're interested!
Offline Spacebeans
« Reply #26 - Posted 2014-06-30 23:30:38 »

I kinda made this over the course of 30 minuets, using other peoples code, so it might be a bit sloppy.

/libraries/
lwjgl.jar
lwjgl-util.jar
jinput.jar

/libraries/natives/

/passage/games/
Game.java: http://pastebin.com/EcMfCdVL
Camera.java: http://pastebin.com/d4Wv0FEL
Block.java: http://pastebin.com/e4WvSniL
Shader.java: http://pastebin.com/36p6jrup

/assets/
Box.frag: http://pastebin.com/6FVWdJ3N
Box.vert: http://pastebin.com/DkyFNBGi
Offline orange451

JGO Coder


Medals: 13
Projects: 2
Exp: 4 years


Your face. Your ass. What's the difference?


« Reply #27 - Posted 2014-06-30 23:48:38 »

It's something to do with how you calculate your diffuse variable in the fragment shader. I commented that part out of the shader, and the black spots no longer appeared.

However, I've never written a phong shader, so if someone else knows more than me, feel free to jump in Smiley

Working on a nice FPS in lwjgl Smiley http://i.imgur.com/q4uFqBS.png
Feel free to message me if you're interested!
Offline Spacebeans
« Reply #28 - Posted 2014-06-30 23:52:44 »

Removing color from the diffuse makes the black part less intense.

1  
vec4 diffuse = max(dot(normal, light), 0.0);


EDIT: Also, I forgot to remove this;
1  
glUniform3f(glGetUniformLocation(shader.programID, "lightLocation"), 10, 10, 10);
Offline Spacebeans
« Reply #29 - Posted 2014-07-02 06:55:46 »

I kinda made this over the course of 30 minuets, using other peoples code, so it might be a bit sloppy.

/libraries/
lwjgl.jar
lwjgl-util.jar
jinput.jar

/libraries/natives/

/passage/games/
Game.java: http://pastebin.com/EcMfCdVL
Camera.java: http://pastebin.com/d4Wv0FEL
Block.java: http://pastebin.com/e4WvSniL
Shader.java: http://pastebin.com/36p6jrup

/assets/
Box.frag: http://pastebin.com/6FVWdJ3N
Box.vert: http://pastebin.com/DkyFNBGi

I really can't get this to work, I've been working on it for 2 days now. Does anyone know how to fix this?

Click to Play
Pages: [1] 2
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

DarkCart (16 views)
2014-10-31 21:44:48

DarkCart (21 views)
2014-10-31 21:43:57

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

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

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

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

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

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

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

Norakomi (62 views)
2014-10-16 15:22:06
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!