Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (121)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Lighting  (Read 437 times)
0 Members and 1 Guest are viewing this topic.
Offline zFollette

Junior Duke


Exp: 2 years


I like jokes


« Posted 2014-01-12 06:17:40 »

I have added normals to my project, I am pretty sure they are added right, but my lighting is weird.



The raised block is 0, 0, 0 and my light position is -1, 0, -1

Here is some code:

Normals:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
 float[] normalData = new float[]{
                            /*Front Normal*/
                            0, 0, 1,
                           
                            /*Back Normal*/
                            0, 0, -1,
                           
                            /*Left Normal*/
                            -1, 0, 0,
                           
                            /*Right Normal*/
                            1, 0, 0,
                           
                            /*Bottom Normal*/
                            0, -1, 0,
                           
                            /*Top Normal*/
                            0, 1, 0
                        };


They are in that order to correspond to the order in which I generate my faces.

Lighting:

1  
2  
3  
4  
5  
glEnable(GL_LIGHTING);
            glEnable(GL_LIGHT0);
            glLight(GL_LIGHT0, GL_AMBIENT, compileBuffer(new float[]{1.5f, 1.5f, 1.5f, 1f}));
            glLight(GL_LIGHT0, GL_DIFFUSE, compileBuffer(new float[]{1.5f, 1.5f, 1.5f, 1f}));
            glLight(GL_LIGHT0, GL_POSITION, compileBuffer(new float[]{light.x, light.y, light.z, 1}));


Normals Rendering:
1  
2  
glBindBuffer(GL_ARRAY_BUFFER, normal);
        glNormalPointer(GL_FLOAT, 0, 0);


Normals Buffering:

1  
2  
3  
glBindBuffer(GL_ARRAY_BUFFER, normal);
        glBufferData(GL_ARRAY_BUFFER, normalBuffer, GL_STATIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);


I hope that somewhere along this chain of code is my problem.
(This thread way modified, the following 6 questions have nothing to do with my current question).

Humor will keep you alive.
Offline trollwarrior1
« Reply #1 - Posted 2014-01-12 06:33:24 »

Normal is basically a vec4 of length 1 if i'm not mistaking. It is pointing to some kind of direction. Normal tells how direction certain triangle is facing, so you can do lighting operations.
Most modeling programs should be able to generate normals for you.
Offline Longarmx
« Reply #2 - Posted 2014-01-12 06:41:33 »

This basically calculates the normal for all vertices using surrounding vertices positions. This current code requires you to be storing your data in a VBO and rendering using an IBO with glDrawElements(...).

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
private void calcNormals(Vertex[] vertices, int[] indices)
   {
      for(int i = 0; i < indices.length; i += 3)
      {
         int i0 = indices[i];
         int i1 = indices[i + 1];
         int i2 = indices[i + 2];
         
         Vector3f v1 = vertices[i1].getPos().sub(vertices[i0].getPos());
         Vector3f v2 = vertices[i2].getPos().sub(vertices[i0].getPos());
         
         Vector3f normal = v1.cross(v2).normalized();
         
         vertices[i0].setNormal(vertices[i0].getNormal().add(normal));
         vertices[i1].setNormal(vertices[i1].getNormal().add(normal));
         vertices[i2].setNormal(vertices[i2].getNormal().add(normal));
      }
     
      for(int i = 0; i < vertices.length; i++)
         vertices[i].setNormal(vertices[i].getNormal().normalized());
   }


Code taken from: https://github.com/BennyQBD/3DGameEngine/blob/master/src/com/base/engine/Mesh.java

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

Junior Duke


Exp: 2 years


I like jokes


« Reply #3 - Posted 2014-01-12 06:56:10 »

This basically calculates the normal for all vertices using surrounding vertices positions. This current code requires you to be storing your data in a VBO and rendering using an IBO with glDrawElements(...).

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
private void calcNormals(Vertex[] vertices, int[] indices)
   {
      for(int i = 0; i < indices.length; i += 3)
      {
         int i0 = indices[i];
         int i1 = indices[i + 1];
         int i2 = indices[i + 2];
         
         Vector3f v1 = vertices[i1].getPos().sub(vertices[i0].getPos());
         Vector3f v2 = vertices[i2].getPos().sub(vertices[i0].getPos());
         
         Vector3f normal = v1.cross(v2).normalized();
         
         vertices[i0].setNormal(vertices[i0].getNormal().add(normal));
         vertices[i1].setNormal(vertices[i1].getNormal().add(normal));
         vertices[i2].setNormal(vertices[i2].getNormal().add(normal));
      }
     
      for(int i = 0; i < vertices.length; i++)
         vertices[i].setNormal(vertices[i].getNormal().normalized());
   }


Code taken from: https://github.com/BennyQBD/3DGameEngine/blob/master/src/com/base/engine/Mesh.java

Maybe it would be worthy to note that I do not know what indices are. I realize that it is the plural of index though. I am am using VBO's to store, but I am not using IBM's to render

Humor will keep you alive.
Offline zFollette

Junior Duke


Exp: 2 years


I like jokes


« Reply #4 - Posted 2014-01-12 06:57:10 »

Normal is basically a vec4 of length 1 if i'm not mistaking. It is pointing to some kind of direction. Normal tells how direction certain triangle is facing, so you can do lighting operations.
Most modeling programs should be able to generate normals for you.

I want to be able to make normals for cubes made in openGL.

Humor will keep you alive.
Offline trollwarrior1
« Reply #5 - Posted 2014-01-12 07:24:12 »

Well you just have to hard code them as you would hard code the vertices.

if you have a cube with length 1 edges centered around 0,0,0 coordinate, lets take a bottom face. Bottom face normal would be facing down. So the normal for bottom face is (0, -1, 0). The normal for up face would be (0, 1, 0). The normal for right face would be (1, 0, 0). The normal for front face would be (0, 0, -1). The normal for back face would be (0,0,1). This is all assuming that positive X is to the right, positive Y is up, positive Z is inside the screen(forwards through the screen).

Also, if you would change where the cube is located, the normals wouldn't change. They would always be same same no matter where the cube is located.
Offline zFollette

Junior Duke


Exp: 2 years


I like jokes


« Reply #6 - Posted 2014-01-12 16:02:46 »

I have updated the thread with another question.

Humor will keep you alive.
Offline theagentd
« Reply #7 - Posted 2014-01-13 19:17:14 »

Built-in OpenGL lighting sucks hard. It's ancient. If you want to do anything decent you'll want to use shaders. You'll have no use at all for the stuff you're doing now.

Myomyomyo.
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.

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

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

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

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

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

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

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

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

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

BurntPizza (47 views)
2014-10-11 23:10:45
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!