Results 1 to 7 of 7

Thread: Shader lighting and see-through faces

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2015
    Posts
    4

    Shader lighting and see-through faces

    Hi!

    I'm trying to write my first shader, with a fixed directional light. The vertex and fragment shaders are as below:

    Vertex Shader:
    Code :
    #version 330
     
    uniform mat4 mvp;
    uniform mat3 nmat;
    uniform vec3 lightDir;
    uniform vec4 ambient;
     
    in vec3 coord3d;     
    in vec3 v_color;  
    in vec3 v_normal;    
     
    out vec4 f_color;
     
    void main(void) 
    { 
            float NdotL;
            gl_Position = mvp * vec4(coord3d, 1.0);
     
    	f_normal = normalize(nmat * v_normal);
     
    	NdotL = max(dot(f_normal, lightDir), 0.0);
     
    	f_color = ambient * vec4(v_color,1.0);
     
    	if (NdotL > 0.0)
    	{
    		f_color += vec4(v_color, 1.0) * NdotL;
    	}
     
    	f_color.a = 1.0;
    };
    Fragment Shader:
    Code :
    #version 330
     
    in vec4 f_color;
     
    void main(void) {
    	gl_FragColor = f_color;
    };

    For nmat, I pass the model transform (converted to a mat3) so that normals and eye are all in world coordinates. With the above shaders, I'm trying to render a scene with two boxes but what I get is this (the light is directed in order to illuminate the top and right faces):

    647.jpg

    So you see what the problem is: the right face of the box is visible even though it's behind the front face. Do you know why this happens and a possible way out of this problem?

    Thanks!

  2. #2
    Member Contributor
    Join Date
    Oct 2014
    Posts
    55
    Seems like you didn't enable the depth test.

    Code :
    glEnable(GL_DEPTH_TEST);

  3. #3
    Junior Member Newbie
    Join Date
    Mar 2015
    Posts
    4
    Hi Betrayal,

    I've checked and the depth test is enabled.

  4. #4
    Member Contributor
    Join Date
    Oct 2014
    Posts
    55
    Hmm, are you sure?

    What library are you using to setup an OpenGL context?
    Don't forget to setup a number of bits != 0 for the depth buffer in context creation.


    If this doesn't help and nobody else is going to answer, just post the full code an i'll try to search the error tomorrow.

  5. #5
    Junior Member Newbie
    Join Date
    Mar 2015
    Posts
    4
    Here is the code I use for initialization
    Code :
    int glWin::init(int argc, char* argv[], const char* title, int width, int height, glm::vec4& bgColor)
    {
    	m_bgColor = bgColor;
    	glutInit(&argc, argv);
    	glutInitContextVersion(2, 0);
    	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
    	glutInitWindowSize(width, height);
    	//glutCreateWindow("My first triangle");
    	glutCreateWindow(title);
    	GLenum glew_status = glewInit();
    	if (glew_status != GLEW_OK)
    	{
    		fprintf(stderr, "Error: %s\n", glewGetErrorString(glew_status));
    		return EXIT_FAILURE;
    	}
    	if (!GLEW_VERSION_2_0)
    	{
    		fprintf(stderr, "Error: your graphic card does not support OpenGL 2.0\n");
    		return 1;
    	}
     
    	glEnable(GL_DEPTH_TEST);								
    	glDepthFunc(GL_LEQUAL);
    	return 1;
    }

  6. #6
    Member Contributor
    Join Date
    Oct 2014
    Posts
    55
    Well, can you please post the full code?

  7. #7
    Junior Member Newbie
    Join Date
    Mar 2015
    Posts
    4
    I fixed the problem. Actually the shader and the GL initialization was fine, I've just set up the vertices and indices incorrectly!

    Thanks!

Similar Threads

  1. How does a shader itterate though lists of faces?
    By MrUNOwen in forum OpenGL: Basic Coding
    Replies: 9
    Last Post: 08-04-2017, 07:11 PM
  2. Front faces, back faces and edge-on faces
    By sam_hawker in forum OpenGL: Advanced Coding
    Replies: 4
    Last Post: 02-23-2017, 07:24 AM
  3. Replies: 7
    Last Post: 09-15-2015, 04:00 AM
  4. cg lighting shader
    By Alessandro_dup1 in forum OpenGL: GLSL
    Replies: 3
    Last Post: 12-11-2004, 08:51 AM
  5. Lighting problem (opengl + shader lighting)
    By ox1d_47 in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 08-19-2004, 09:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean