• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
rafciok_6

GLSL Lighting

6 posts in this topic

I have a problem with my per vertex diffuse lighting implementation. I cannot find the problem. tried everything. Maybe someone can help.

Here is the code.

 

Here is how I calculate normals for every point and store it in vbo 

 

 

 
//Normals
glGenBuffers(1, &m_vbo_normals); 
glBindBuffer(GL_ARRAY_BUFFER, m_vbo_normals); 
for (int i = 0; i < 24; i += 3)
{
 
glm::vec3 normal = glm::normalize(glm::cross(glm::vec3(corners[indices[i + 1]]) -
glm::vec3(corners[indices[i]]), glm::vec3(corners[indices[i + 2]]) -
glm::vec3(corners[indices[i]])));
 
normals[i] = normal.x; 
normals[i + 1] = normal.y; 
normals[i + 2] = normal.z;
}
glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(float), normals, GL_STATIC_DRAW); 
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(2);

 

Vertex.vert

 

#version 150 core
 

uniform mat4 modelview, projection;

uniform mat3 normalMatrix;
uniform vec3 lightPos;
uniform vec3 lambient;
uniform vec3 ldiffuse;
 
in vec4 position;
in vec3 normal;
in vec3 inColour; 
 
out vec3 outColour;
 
 
void main()
{
gl_Position = projection*modelview*position;
vec3 ambient = inColour * lambient;
 
//current position of the vertex 
vec3 pos =  vec3(modelview * position);
vec3 n = normalize(normalMatrix * normal);
 
//vector to pointing to the light pos 
vec3 I = normalize(lightPos - pos);
 
float dcont = max(0.0, dot(n, I));
vec3 diffuse = dcont * inColour * ldiffuse;
 
 
 
outColour = diffuse;
}
 

 

fragmet.frag

 

#version 150 core
 

in vec3 outColour;

  
out vec3 outFrag;
 
void main()
{
outFrag =  outColour;
}
 

and here is how I calculate normalMatrix and send uniform variables

 

glTranslatef( 0.0, 0.0, -2.0 );

glRotatef( m_euler[0], 1.0, 0.0, 0.0 );
glRotatef( m_euler[1], 0.0, 1.0, 0.0 );
glRotatef( m_euler[2], 0.0, 0.0, 1.0 );
 
glGetFloatv( GL_MODELVIEW_MATRIX, modelview );
glGetFloatv( GL_PROJECTION_MATRIX, projection);
 
glUniformMatrix4fv( m_uniform_modelview, 1, GL_FALSE, modelview );
glUniformMatrix4fv( m_uniform_projection, 1, GL_FALSE, projection );
glm::mat3* MV = new glm::mat3 (modelview[16]);
setUniform(m_shader, "normalMatrix", -glm::transpose(glm::inverse(*MV)));
 
 
glUniform3f(glGetUniformLocation(m_shader->GetProgramHandle(), "lambient"), 1.0, 1.0, 1.0);
glUniform3f(glGetUniformLocation(m_shader->GetProgramHandle(), "ldiffuse"), 1.0, 1.0, 1.0);
glUniform3f(glGetUniformLocation(m_shader->GetProgramHandle(), "lightPos"), 1.0, 1.0, 1.0);
glUniform3f(glGetUniformLocation(m_shader->GetProgramHandle(), "direction"), 1.0, 1.0, 0.0);

 

Thank you for help. 

0

Share this post


Link to post
Share on other sites

glm::mat3* MV = new glm::mat3 (modelview[16]);
setUniform(m_shader, "normalMatrix", -glm::transpose(glm::inverse(*MV)));

 

Is there a reason you are using new there? You are also passing just one float to the mat3's constructor, and it's out of bounds.

I would suggest doing something like glm::transpose(glm::inverse(glm::mat3(modelview))). Also, I think you should normalize your normals after the loop where you calculate them.

 

You can use &matrix[0][0] or glm::value_ptr(matrix) to access a glm matrix, you don't have to use your own float arrays. But if you really want to, you can convert a float array to mat3 or mat4 with make_mat3 and make_mat4. Just include glm/gtc/type_ptr.hpp to get access to those functions.

 

Edit. I meant that modelview is glm::mat4. Also, maybe a better example code:

glm::mat4 modelview_matrix; 
glGetFloatv(GL_MODELVIEW_MATRIX, &modelview_matrix[0][0]);
glm::mat3 normal_matrix = glm::transpose(glm::inverse(glm::mat3(modelview_matrix)));
const float *pointer = glm::value_ptr(normal_matrix);
Edited by Sponji
1

Share this post


Link to post
Share on other sites


I have a problem with my per vertex diffuse lighting implementation.

Can you tell us, what the problem is ? Blank screen ? Rendered cube,  but with wrong lighting ? A screenshot would help.

0

Share this post


Link to post
Share on other sites

 


I have a problem with my per vertex diffuse lighting implementation.

Can you tell us, what the problem is ? Blank screen ? Rendered cube,  but with wrong lighting ? A screenshot would help.

 

I have a black screen with nothing displayed on it. Is looks like I did something wrong with the matrices. 

 

I think one of the problem was with my normal matrix which I calculate. Sponij thanks for the answer really help. 

I changed my code for normal matrix according with code example above. But still is something wrong. 

0

Share this post


Link to post
Share on other sites
I can give you some advices:
 
Things to check:
 
Uniform Locations;
Lighting computations in just one space;
Normals (you can test with glEnable(GL_CULL_FACE));
Creation order of the shaders;
 
Is very good to check opengl.org instead of trying to do a lot of crazy changes. It has the latest documentation too.
0

Share this post


Link to post
Share on other sites

Not sure if this is the problem, but I don't think you're calculating the normals on a per vertex basis.  Rather it looks like you're calculating it on a per triangle basis.

for (int i = 0; i < 24; i += 3)//Using i += 3
{
//Calculating the normal for triangle starting at indices[i]
//When do you calculate the normal for the vertex at indices[i+1]?
//Maybe you wanted to store this normal for all the vertices in this triangle?
//At i == 3, you're calculating the normal for another triangle (I think)
glm::vec3 normal = glm::normalize(glm::cross(glm::vec3(corners[indices[i + 1]]) -
glm::vec3(corners[indices[i]]), glm::vec3(corners[indices[i + 2]]) -
glm::vec3(corners[indices[i]])));
 
//At i=3, you calculated a normal for the first vertex of the second triangle (I think)
//but storing it as the normal of the second vertex of the first triangle (I think)
//Is normals[i] the normal for corners[i]?  You're calculating the normal for corners[indices[i]].
//Since this is per vertex normals, you have 8 corners to go with your 8 normals?
//And the 24 indices make 8 triangles out of those 8 corners?
//Just want to make sure the numbers are right.  You're reusing i to itererate indices and normals.
normals[i] = normal.x; 
normals[i + 1] = normal.y; 
normals[i + 2] = normal.z;
} 

You appear to be calculating the normal of a triangle.  You should store that normal for all 3 vertices of the triangle.

 

Be careful, some vertices may be used for more than one triangle.  Zero the normals out at the start, in the loop add the new normal to what's already there.  Re-normalize the normals in a loop afterwards.

Edited by Soxory
2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0