Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (739)
Games in Android Showcase (224)
games submitted by our members
Games in WIP (820)
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] Lighting and rotation issues  (Read 1579 times)
0 Members and 1 Guest are viewing this topic.
Offline thedanisaur

JGO Knight


Medals: 59



« Posted 2015-03-22 09:47:25 »

Basically what's going on is that when I have objects lit and they are rotated 0 or 180 degrees the lighting is perfect, but when rotated 90 or -90 degrees the lighting is inverted.

Image: models on left are rotated 0 and 180 degrees, models on the right....well you get it.



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  
      //===================================================
      //===========PROJECTION
      //===================================================
      float[] matrix_array = new float[16];
      gl.glGetFloatv(GL2.GL_PROJECTION_MATRIX, matrix_array, 0);
      Matrix4 m4 = new Matrix4();
      m4.setMatrix(matrix_array);

      FloatBuffer matrix_buffer = Buffers.newDirectFloatBuffer(m4.toArray().length);
      matrix_buffer.put(m4.toArray());
      matrix_buffer.flip();
      shader.setAttributeMatrix4f(gl, false, "projection_matrix", matrix_buffer);
     
      //===================================================
      //===========MODELVIEW
      //===================================================
       matrix_array = new float[16];
      gl.glGetFloatv(GL2.GL_MODELVIEW_MATRIX, matrix_array, 0);
      m4.setMatrix(matrix_array);

      matrix_buffer = Buffers.newDirectFloatBuffer(m4.toArray().length);
      matrix_buffer.put(m4.toArray());
      matrix_buffer.flip();
      shader.setAttributeMatrix4f(gl, false, "modelview_matrix", matrix_buffer);

      //===================================================
      //===========NORMAL
      //===================================================
      Matrix3 m3 = new Matrix3();
      m4 = m4.invert();
      m4 = m4.transpose();
      m3.copyMatrix(m4.m_mat);

      matrix_buffer = Buffers.newDirectFloatBuffer(m3.toArray().length);
      matrix_buffer.put(m3.toArray());
      matrix_buffer.flip();
      shader.setAttributeMatrix3f(gl, false, "normal_matrix", matrix_buffer);
     
      //===================================================
      //===========MODEL-TO-WORLD
      //===================================================
      Vector3 rotation = m_transform.getRotation();
      Vector3 scale = m_transform.getScale();
      Vector3 translate = m_transform.getTranslate();
      Matrix4 rot_mat = new Matrix4().initRotate(rotation.m_x, rotation.m_y, rotation.m_z);
      Matrix4 scale_mat = new Matrix4().initScale(scale.m_x, scale.m_y, scale.m_z);
      Matrix4 trans_mat = new Matrix4().initTranslate(translate.m_x, translate.m_y, translate.m_z);
      m4 = trans_mat.multiply(rot_mat.multiply(scale_mat));
      m4.transpose();//make column major

      matrix_buffer = Buffers.newDirectFloatBuffer(m4.toArray().length);
      matrix_buffer.put(m4.toArray());
      matrix_buffer.flip();
      shader.setAttributeMatrix4f(gl, false, "model_to_world_matrix", matrix_buffer);



Vert Shader:


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  
in vec2 tex_coord;
in vec3 vert_pos;
in vec3 normal;

uniform vec3 light_pos;
uniform vec3 view_pos;
uniform mat3 normal_matrix;
uniform mat4 modelview_matrix;
uniform mat4 projection_matrix;
uniform mat4 model_to_world_matrix;

out vec2 pass_tex_coord;
out vec3 pass_normal;
out vec3 pass_light_dir;
out vec3 pass_view_dir;

void main(void)
{
   vec4 pos = vec4(vert_pos.xyz, 1.0);
   gl_Position = projection_matrix * modelview_matrix * pos;
   pass_tex_coord = tex_coord;
   
   vec3 vert_pos_world = vec4(model_to_world_matrix * pos).xyz;
   pass_normal = vec4(model_to_world_matrix * vec4(normal, 0)).xyz;
   pass_light_dir = vert_pos_world - light_pos;
}



Frag Shader:


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
in vec2 pass_tex_coord;
in vec3 pass_normal;
in vec3 pass_light_dir;// L
in vec3 pass_view_dir;//  V

out vec4 out_color;

void main(void)
{
   vec3 n_normal = normalize(pass_normal);
   vec3 n_light_dir = normalize(pass_light_dir);
   float n_dot_l = max(0.0, dot(n_normal, -n_light_dir));
   out_color = vec4(n_dot_l, n_dot_l, n_dot_l, 1.0);
}



Edit: replaced color variable with normal, same values are sent, they are the correct normals. I have verified by outputting them as color

Every village needs an idiot Cool
Offline skys0ldier

Senior Newbie


Exp: 3 years



« Reply #1 - Posted 2015-03-22 12:10:23 »

Quote
in vec2 tex_coord;
in vec3 vert_pos;
in vec3 color;

where are your normals?
You need the normal of every face to get the correct
lighting values..
Offline thedanisaur

JGO Knight


Medals: 59



« Reply #2 - Posted 2015-03-22 22:07:38 »

That's not the issue. Sorry for the confusion the color variable actually contains the normals, I just didn't refactor. I have since updated my code to reflect this so it won't be confusing in the future, my bad.

I can assure you that the normals are being sent to the color variable and they are the correct normals.

Old code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
      for(int i = 0, j = 0; i < interleaved_array.length; i += Vertex.SIZE, j++)
      {
         interleaved_array[i+0] = m_vertices[j].m_position.m_x;
         interleaved_array[i+1] = m_vertices[j].m_position.m_y;
         interleaved_array[i+2] = m_vertices[j].m_position.m_z;
         interleaved_array[i+3] = m_vertices[j].m_normal.m_x;
         interleaved_array[i+4] = m_vertices[j].m_normal.m_y;
         interleaved_array[i+5] = m_vertices[j].m_normal.m_z;
         interleaved_array[i+6] = m_vertices[j].m_uv.m_x;
         interleaved_array[i+7] = m_vertices[j].m_uv.m_y;
      }



      
1  
2  
3  
int vert_pos = gl.glGetAttribLocation(shader.m_program_index, "vert_pos");
      int color = gl.glGetAttribLocation(shader.m_program_index, "color");
      int tex_coord = gl.glGetAttribLocation(shader.m_program_index, "tex_coord");


1  
2  
3  
4  
5  
6  
7  
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, m_vbo_handle[0]);
      gl.glEnableVertexAttribArray(vert_pos);
      gl.glEnableVertexAttribArray(color);
      gl.glEnableVertexAttribArray(tex_coord);
      gl.glVertexAttribPointer(vert_pos, 3, GL.GL_FLOAT, false, Buffers.SIZEOF_FLOAT * (Vertex.SIZE), 0);//start
      gl.glVertexAttribPointer(color, 3, GL.GL_FLOAT, false, Buffers.SIZEOF_FLOAT * (Vertex.SIZE), Buffers.SIZEOF_FLOAT * 3);//12
      gl.glVertexAttribPointer(tex_coord, 2, GL.GL_FLOAT, false, Buffers.SIZEOF_FLOAT * (Vertex.SIZE), Buffers.SIZEOF_FLOAT * 6);//24

Every village needs an idiot Cool
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline thedanisaur

JGO Knight


Medals: 59



« Reply #3 - Posted 2015-03-23 00:54:53 »

After thinking through this I've figured out that the reason it's not working properly is that the rotation around Y is invert -> opposite.

So by changing my model to world matrix as follows:

      
1  
2  
3  
4  
5  
6  
7  
8  
9  
Vector3 rotation = m_transform.getRotation();
      Vector3 scale = m_transform.getScale();
      Vector3 translate = m_transform.getTranslate();
      //Matrix4 rot_mat = new Matrix4().initRotate(rotation.m_x, rotation.m_y, rotation.m_z); <- This line changes to
      Matrix4 rot_mat = new Matrix4().initRotate(rotation.m_x, -rotation.m_y, rotation.m_z);// <-This
      Matrix4 scale_mat = new Matrix4().initScale(scale.m_x, scale.m_y, scale.m_z);
      Matrix4 trans_mat = new Matrix4().initTranslate(translate.m_x, translate.m_y, translate.m_z);
      m4 = trans_mat.multiply(rot_mat.multiply(scale_mat));
      m4.transpose();//make column major


I have fixed the immediate problem, now I'm curious what the root issue could be. My models show up as expected (translated, rotated, scaled), and neither the X or Z components are broken in the lighting model...

Every village needs an idiot Cool
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 
Ecumene (55 views)
2017-09-30 02:57:34

theagentd (78 views)
2017-09-26 18:23:31

cybrmynd (186 views)
2017-08-02 12:28:51

cybrmynd (184 views)
2017-08-02 12:19:43

cybrmynd (191 views)
2017-08-02 12:18:09

Sralse (201 views)
2017-07-25 17:13:48

Archive (757 views)
2017-04-27 17:45:51

buddyBro (891 views)
2017-04-05 03:38:00

CopyableCougar4 (1440 views)
2017-03-24 15:39:42

theagentd (1323 views)
2017-03-24 15:32:08
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
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!