Results 1 to 3 of 3

Thread: Implementing Flat Shading

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

    Implementing Flat Shading

    Hello,

    I'm trying to implement flat shading in my geometry shader (because it is per-primitive).
    I use tessellation to transform a colour cube into a colour sphere.
    Then I want to combine the Phong Lighting Model and Flat Shading Model to shade the sphere.

    This is my geometry shader:

    Code :
    #version 420
     
    layout (triangles) in;
    in vec3 Colour[];
     
    layout(triangle_strip, max_vertices = 3) out;
    out vec3 v_colour;
     
    void main() {
    	vec3 lightPos = vec3(0.0, 2.0, 0.0); // position of light source.
    	vec3 eye = vec3(0.0); // vector the camera is looking along.
     
    	vec3 v0 = gl_in[0].gl_Position.xyz;
    	vec3 v1 = gl_in[1].gl_Position.xyz;
    	vec3 v2 = gl_in[2].gl_Position.xyz;
     
    	vec3 N = normalize(cross(v1 - v0, v2 - v0)); // Primitive normal.
     
    	for (int i = 0; i < 3; i++) {
    		vec3 L = normalize(lightPos - gl_in[i].gl_Position.xyz); // direction from fragment to light source.
    		vec3 E = normalize(eye - gl_in[i].gl_Position.xyz); // view vector.
    		vec3 R = normalize(reflect(-L, N)); // reflection vector of light source.
     
    		vec3 ambient = vec3(0.1);
    		vec3 diffuse = vec3(0.2) * dot(L, N);
    		vec3 specular = vec3(0.3) * pow(max(dot(R, E), 0.0), 0.3 * 60.0);
    		vec3 intensity = ambient + diffuse + specular;
     
    		gl_Position = gl_in[i].gl_Position;
    		v_colour = intensity * Colour[i];
     
    		EmitVertex();
    	}
     
    	EndPrimitive();
    }

    With this code, there is a small spot of light on top of the sphere, but no specular highlights or anything.

    Thanks.

  2. #2
    Senior Member Regular Contributor Agent D's Avatar
    Join Date
    Sep 2011
    Location
    Innsbruck, Austria
    Posts
    281
    I can't tell what your vertex shader does, but I guess the gl_in[i].gl_Position is already after applying perspective (or whatever) projection and the light positon is thus not in camera space.

  3. #3
    Junior Member Newbie
    Join Date
    Mar 2014
    Posts
    4
    My vertex shader just passes through the position.
    This is my tessellation evaluation shader:

    Code :
    #version 420
     
    layout (triangles, equal_spacing) in;
     
    out vec3 Colour;
    out vec4 LightPos;
     
    uniform mat4 projection;
    uniform mat4 world;
    uniform mat4 view;
     
    void main() {
    	vec3 p0 = gl_in[0].gl_Position.xyz * gl_TessCoord.x;
    	vec3 p1 = gl_in[1].gl_Position.xyz * gl_TessCoord.y;
    	vec3 p2 = gl_in[0].gl_Position.xyz * gl_TessCoord.z;
     
    	vec3 position = normalize(p0 + p1 + p2);
    	gl_Position = projection * view * world * vec4(position, 1.0);
     
    	LightPos = projection * view * world * vec4(0.0, 2.0, 0.0, 1.0);
    	Colour = vec3(position.x, position.y, position.z);
    }

    I removed lightPos from the geometry shader and calculated it in the TES.
    I'm not sure whether I should be multiplying LightPos with the matrices.

Similar Threads

  1. glDrawElements with flat shading
    By silikone in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 11-06-2017, 12:18 PM
  2. help with flat shading
    By ale211734 in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 06-25-2015, 12:10 PM
  3. VBO with flat shading
    By tchau83 in forum OpenGL: Advanced Coding
    Replies: 5
    Last Post: 09-27-2010, 08:24 AM
  4. flat shading with VBO
    By thinks in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 07-23-2009, 05:10 AM
  5. flat shading
    By heppe in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 12-04-2003, 05:43 AM

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