diff options
| author | talha <talha@talhaamir.xyz> | 2024-04-06 03:26:09 +0500 | 
|---|---|---|
| committer | talha <talha@talhaamir.xyz> | 2024-04-06 03:26:09 +0500 | 
| commit | 21439a3eeb0a91e1f0f3f653492574b6a79e867d (patch) | |
| tree | 05cab1476dadd663ba0c249bf2dfa9ebd3a0e0c5 /source | |
| parent | 02077305a5b63fcf7242ac909d8caed5cc3cf18f (diff) | |
Completed Depth Testing
Diffstat (limited to 'source')
| -rw-r--r-- | source/main.cpp | 255 | ||||
| -rw-r--r-- | source/shaders/depth_test.fs.glsl | 30 | ||||
| -rw-r--r-- | source/shaders/depth_test.vs.glsl (renamed from source/shaders/model/model.vs.glsl) | 8 | ||||
| -rw-r--r-- | source/shaders/model/model.fs.glsl | 152 | 
4 files changed, 231 insertions, 214 deletions
| diff --git a/source/main.cpp b/source/main.cpp index 18a816b..80eb2d6 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -36,7 +36,6 @@ typedef double   r64;  typedef u8        b8; -  #include "math.h"  // =========== Shader Loading ============= @@ -129,6 +128,40 @@ Vec3 camera_look_around(r32 angle_pitch, r32 angle_yaw)      return camera_look;  } +s32 gl_load_texture(u32 texture_id, const char* path) +{ +  s32 width, height, nrChannels; +  unsigned char *data = stbi_load(path, &width, &height, &nrChannels, 0); +  if (data) +  { +    GLenum format; +    if (nrChannels == 1) +      format = GL_RED; +    else if (nrChannels == 3) +      format = GL_RGB; +    else if (nrChannels == 4) +      format = GL_RGBA; + +    glBindTexture(GL_TEXTURE_2D, texture_id); +    glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data); +    glGenerateMipmap(GL_TEXTURE_2D); + +    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); +    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); +    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); +    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + +    stbi_image_free(data); +  } +  else +  { +    printf("failed to load image texture at path: %s", path); +    stbi_image_free(data); +  } + +  return texture_id; +} +  // =================== Model Loading ========================  // This section contains a whole host of things:  // 1. classes @@ -439,7 +472,7 @@ int main(int argc, char* argv[])                                            SDL_WINDOW_OPENGL);      SDL_SetRelativeMouseMode(SDL_TRUE); -     +     	// create an opengl context  	SDL_GLContext context = SDL_GL_CreateContext(window);  	if (!context) @@ -457,20 +490,109 @@ int main(int argc, char* argv[])    // filesystem playground stuff    size_t read_count; -  char* vertex_source = (char*)SDL_LoadFile("./source/shaders/model/model.vs.glsl", &read_count); -  char* fragment_source = (char*)SDL_LoadFile("./source/shaders/model/model.fs.glsl", &read_count); +  char* vertex_source = (char*)SDL_LoadFile("./source/shaders/depth_test.vs.glsl", &read_count); +  char* fragment_source = (char*)SDL_LoadFile("./source/shaders/depth_test.fs.glsl", &read_count);  	GLuint vertex_shader = gl_create_vertex_shader(vertex_source);  	GLuint fragment_shader = gl_create_fragment_shader(fragment_source);  	GLuint shader_program = gl_create_shader_program(vertex_shader, fragment_shader);  	printf("Successfully compiled shaders.\n"); -  glUseProgram(shader_program); +  float cubeVertices[] = { +				// positions          // texture Coords +        -0.5f, -0.5f, -0.5f,  0.0f, 0.0f, +         0.5f, -0.5f, -0.5f,  1.0f, 0.0f, +         0.5f,  0.5f, -0.5f,  1.0f, 1.0f, +         0.5f,  0.5f, -0.5f,  1.0f, 1.0f, +        -0.5f,  0.5f, -0.5f,  0.0f, 1.0f, +        -0.5f, -0.5f, -0.5f,  0.0f, 0.0f, + +        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f, +         0.5f, -0.5f,  0.5f,  1.0f, 0.0f, +         0.5f,  0.5f,  0.5f,  1.0f, 1.0f, +         0.5f,  0.5f,  0.5f,  1.0f, 1.0f, +        -0.5f,  0.5f,  0.5f,  0.0f, 1.0f, +        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f, + +        -0.5f,  0.5f,  0.5f,  1.0f, 0.0f, +        -0.5f,  0.5f, -0.5f,  1.0f, 1.0f, +        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f, +        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f, +        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f, +        -0.5f,  0.5f,  0.5f,  1.0f, 0.0f, + +         0.5f,  0.5f,  0.5f,  1.0f, 0.0f, +         0.5f,  0.5f, -0.5f,  1.0f, 1.0f, +         0.5f, -0.5f, -0.5f,  0.0f, 1.0f, +         0.5f, -0.5f, -0.5f,  0.0f, 1.0f, +         0.5f, -0.5f,  0.5f,  0.0f, 0.0f, +         0.5f,  0.5f,  0.5f,  1.0f, 0.0f, + +        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f, +         0.5f, -0.5f, -0.5f,  1.0f, 1.0f, +         0.5f, -0.5f,  0.5f,  1.0f, 0.0f, +         0.5f, -0.5f,  0.5f,  1.0f, 0.0f, +        -0.5f, -0.5f,  0.5f,  0.0f, 0.0f, +        -0.5f, -0.5f, -0.5f,  0.0f, 1.0f, + +        -0.5f,  0.5f, -0.5f,  0.0f, 1.0f, +         0.5f,  0.5f, -0.5f,  1.0f, 1.0f, +         0.5f,  0.5f,  0.5f,  1.0f, 0.0f, +         0.5f,  0.5f,  0.5f,  1.0f, 0.0f, +        -0.5f,  0.5f,  0.5f,  0.0f, 0.0f, +        -0.5f,  0.5f, -0.5f,  0.0f, 1.0f +    }; +    float planeVertices[] = { +        // positions          // texture Coords (note we set these higher than 1 (together with GL_REPEAT as texture wrapping mode). this will cause the floor texture to repeat) +         5.0f, -0.5f,  5.0f,  2.0f, 0.0f, +        -5.0f, -0.5f,  5.0f,  0.0f, 0.0f, +        -5.0f, -0.5f, -5.0f,  0.0f, 2.0f, + +         5.0f, -0.5f,  5.0f,  2.0f, 0.0f, +        -5.0f, -0.5f, -5.0f,  0.0f, 2.0f, +         5.0f, -0.5f, -5.0f,  2.0f, 2.0f								 +    };    stbi_set_flip_vertically_on_load(1); -  // ============ Start Model handling using Assimp ============ -  // loading a 3d model using assimp -  Model test_model = Model(std::string("assets/Survival_Backpack/backpack.obj")); +   +	u32 cube_vao, cube_vbo, plane_vao, plane_vbo; +	 +	glGenVertexArrays(1, &cube_vao); +	glGenBuffers(1, &cube_vbo); + +	glBindVertexArray(cube_vao); +  glBindBuffer(GL_ARRAY_BUFFER, cube_vbo); +  glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertices), &cubeVertices, GL_STATIC_DRAW); +  glEnableVertexAttribArray(0); +  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0); +  glEnableVertexAttribArray(1); +  glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3*sizeof(float))); +  glBindVertexArray(0); + +	glGenVertexArrays(1, &plane_vao); +	glGenBuffers(1, &plane_vbo); + +	glBindVertexArray(plane_vao); +  glBindBuffer(GL_ARRAY_BUFFER, plane_vbo); +  glBufferData(GL_ARRAY_BUFFER, sizeof(planeVertices), &planeVertices, GL_STATIC_DRAW); +  glEnableVertexAttribArray(0); +  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0); +  glEnableVertexAttribArray(1); +  glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3*sizeof(float))); +  glBindVertexArray(0); + +  u32 cube_tex_id; +  glGenTextures(1, &cube_tex_id); +  glActiveTexture(GL_TEXTURE0); +  gl_load_texture(cube_tex_id, "assets/container.jpg"); + +  u32 plane_tex_id; +  glGenTextures(1, &plane_tex_id); +  glActiveTexture(GL_TEXTURE1); +  gl_load_texture(plane_tex_id, "assets/smiling.png"); + +	glUseProgram(shader_program); +    // directional light things    // - directional light params @@ -479,15 +601,15 @@ int main(int argc, char* argv[])    Vec3 DL_diffuse   =   Vec3{ 0.5f, 0.5f, 0.5f };    Vec3 DL_specular  =   Vec3{ 1.0f, 1.0f, 1.0f }; -  int DL_ambient_loc  = glGetUniformLocation(shader_program, "dirLight.ambient"); -  int DL_diffuse_loc  = glGetUniformLocation(shader_program, "dirLight.diffuse"); -  int DL_specular_loc = glGetUniformLocation(shader_program, "dirLight.specular"); -  int DL_dir_loc      = glGetUniformLocation(shader_program, "dirLight.direction"); +  //int DL_ambient_loc  = glGetUniformLocation(shader_program, "dirLight.ambient"); +  //int DL_diffuse_loc  = glGetUniformLocation(shader_program, "dirLight.diffuse"); +  //int DL_specular_loc = glGetUniformLocation(shader_program, "dirLight.specular"); +  //int DL_dir_loc      = glGetUniformLocation(shader_program, "dirLight.direction"); -  glUniform3fv(DL_dir_loc, 1, DL_direction.data); -  glUniform3fv(DL_ambient_loc, 1, DL_ambient.data); -  glUniform3fv(DL_diffuse_loc, 1, DL_diffuse.data); -  glUniform3fv(DL_specular_loc, 1, DL_specular.data); +  //glUniform3fv(DL_dir_loc, 1, DL_direction.data); +  //glUniform3fv(DL_ambient_loc, 1, DL_ambient.data); +  //glUniform3fv(DL_diffuse_loc, 1, DL_diffuse.data); +  //glUniform3fv(DL_specular_loc, 1, DL_specular.data);    // load point light    Vec3 PL_position = Vec3{ 0.0f, 0.0f, 3.0f }; @@ -495,24 +617,24 @@ int main(int argc, char* argv[])    Vec3 PL_diffuse  = Vec3{ 0.5f, 0.5f, 0.5f };    Vec3 PL_specular = Vec3{ 1.0f, 1.0f, 1.0f }; -  s32 PL_pos_loc      = glGetUniformLocation(shader_program, "pointLight.position"); -  s32 PL_ambient_loc  = glGetUniformLocation(shader_program, "pointLight.ambient"); -  s32 PL_diffuse_loc  = glGetUniformLocation(shader_program, "pointLight.diffuse"); -  s32 PL_specular_loc = glGetUniformLocation(shader_program, "pointLight.specular"); -  s32 PL_kc_loc       = glGetUniformLocation(shader_program, "pointLight.kC"); -  s32 PL_kl_loc       = glGetUniformLocation(shader_program, "pointLight.kL"); -  s32 PL_kq_loc       = glGetUniformLocation(shader_program, "pointLight.kQ"); - -  glUniform3fv(PL_pos_loc,      1, PL_position.data); -  glUniform3fv(PL_ambient_loc,  1, PL_ambient.data); -  glUniform3fv(PL_diffuse_loc,  1, PL_diffuse.data); -  glUniform3fv(PL_specular_loc, 1, PL_specular.data); -  // attenuation factors -  glUniform1f(PL_kc_loc, 1.0f); -  glUniform1f(PL_kl_loc, 0.09f); -  glUniform1f(PL_kq_loc, 0.032f); - -	int camera_pos_loc = glGetUniformLocation(shader_program, "cameraPosition"); +  //s32 PL_pos_loc      = glGetUniformLocation(shader_program, "pointLight.position"); +  //s32 PL_ambient_loc  = glGetUniformLocation(shader_program, "pointLight.ambient"); +  //s32 PL_diffuse_loc  = glGetUniformLocation(shader_program, "pointLight.diffuse"); +  //s32 PL_specular_loc = glGetUniformLocation(shader_program, "pointLight.specular"); +  //s32 PL_kc_loc       = glGetUniformLocation(shader_program, "pointLight.kC"); +  //s32 PL_kl_loc       = glGetUniformLocation(shader_program, "pointLight.kL"); +  //s32 PL_kq_loc       = glGetUniformLocation(shader_program, "pointLight.kQ"); + +  //glUniform3fv(PL_pos_loc,      1, PL_position.data); +  //glUniform3fv(PL_ambient_loc,  1, PL_ambient.data); +  //glUniform3fv(PL_diffuse_loc,  1, PL_diffuse.data); +  //glUniform3fv(PL_specular_loc, 1, PL_specular.data); +  //// attenuation factors +  //glUniform1f(PL_kc_loc, 1.0f); +  //glUniform1f(PL_kl_loc, 0.09f); +  //glUniform1f(PL_kq_loc, 0.032f); + +	//int camera_pos_loc = glGetUniformLocation(shader_program, "cameraPosition");  	// objects  	Vec3 model_translations[] = { @@ -529,7 +651,7 @@ int main(int argc, char* argv[])  	uint32_t model_loc = glGetUniformLocation(shader_program, "Model");  	// camera stuff -	Vec3 camera_pos  = Vec3{ 0.0, 5.0, 10.0f}; +	Vec3 camera_pos  = Vec3{ 0.0, 0.0, 10.0f};  	Vec3 preset_up_dir = Vec3{ 0.0, 1.0, 0.0 };    r32 angle_yaw, angle_pitch, angle_roll; @@ -552,6 +674,7 @@ int main(int argc, char* argv[])  	glUniformMatrix4fv(proj_loc, 1, GL_TRUE, proj.buffer);  	glEnable(GL_DEPTH_TEST); +  glDepthFunc(GL_LESS);  	u8 game_running = true; @@ -607,10 +730,10 @@ int main(int argc, char* argv[])  				} break;  				case (SDL_KEYUP):  				{ -                    if (ev.key.keysym.sym == SDLK_LSHIFT) -                    { -                        hold_lshift = false; -                    } +          if (ev.key.keysym.sym == SDLK_LSHIFT) +          { +              hold_lshift = false; +          }  					if (ev.key.keysym.sym == SDLK_w)  					{  						move_w = false; @@ -628,11 +751,11 @@ int main(int argc, char* argv[])  						move_d = false;  					}  				} break; -                case (SDL_MOUSEMOTION): -                { -                    SDL_MouseMotionEvent mouse_event = ev.motion; -                    r32 x_motion = (r32)mouse_event.xrel; -                    r32 y_motion = (r32)mouse_event.yrel; +        case (SDL_MOUSEMOTION): +        { +          SDL_MouseMotionEvent mouse_event = ev.motion; +          r32 x_motion = (r32)mouse_event.xrel; +          r32 y_motion = (r32)mouse_event.yrel;  					if (x_motion != 0.0 || y_motion != 0.0)  					{  						angle_yaw = angle_yaw + To_Radian(x_motion * 0.1f); @@ -640,7 +763,7 @@ int main(int argc, char* argv[])  						camera_look = camera_look_around(angle_pitch, angle_yaw);  					} -                } break; +        } break;  				default:  				{  					break; @@ -675,7 +798,7 @@ int main(int argc, char* argv[])  		glUseProgram(shader_program);  		glUniformMatrix4fv(view_loc, 1, GL_TRUE, view.buffer); -    glUniform3fv(camera_pos_loc, 1, camera_pos.data); +    //glUniform3fv(camera_pos_loc, 1, camera_pos.data);  		time_prev = time_curr; @@ -684,15 +807,37 @@ int main(int argc, char* argv[])  		//glClearColor(0.0f, 0.0f, 0.0f, 1.0f);  		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); -		for (int i = 0; i < 1; i++) -		{ -			Vec3 translation_iter = model_translations[i]; -			Mat4 model = init_value4m(1.0); -			Mat4 model_translation = translation_matrix4m(translation_iter.x, translation_iter.y, translation_iter.z); -			model = multiply4m(model_translation, model); -			glUniformMatrix4fv(model_loc, 1, GL_TRUE, model.buffer); -      test_model.draw(shader_program); -		} +    { +      s32 tex_id_loc = glGetUniformLocation(shader_program, "TexId"); +      glUniform1i(tex_id_loc, 0); +      Vec3 translation_iter = model_translations[0]; +      Mat4 model = init_value4m(1.0); +      Mat4 model_translation = translation_matrix4m(translation_iter.x, translation_iter.y, translation_iter.z); +      model = multiply4m(model_translation, model); +      glUniformMatrix4fv(model_loc, 1, GL_TRUE, model.buffer); +      glBindVertexArray(cube_vao); +      glDrawArrays(GL_TRIANGLES, 0, 36); +    } +    { +      s32 tex_id_loc = glGetUniformLocation(shader_program, "TexId"); +      glUniform1i(tex_id_loc, 1); +      Vec3 translation_iter = model_translations[1]; +      Mat4 model = init_value4m(1.0); +      Mat4 model_translation = translation_matrix4m(translation_iter.x, translation_iter.y, translation_iter.z); +      model = multiply4m(model_translation, model); +      glUniformMatrix4fv(model_loc, 1, GL_TRUE, model.buffer); +      glBindVertexArray(plane_vao); +      glDrawArrays(GL_TRIANGLES, 0, 6); +    } + +		//for (int i = 0; i < 2; i++) +		//{ +		//	Vec3 translation_iter = model_translations[i]; +		//	Mat4 model = init_value4m(1.0); +		//	Mat4 model_translation = translation_matrix4m(translation_iter.x, translation_iter.y, translation_iter.z); +		//	model = multiply4m(model_translation, model); +		//	glUniformMatrix4fv(model_loc, 1, GL_TRUE, model.buffer); +		//}  		SDL_GL_SwapWindow(window);  	} diff --git a/source/shaders/depth_test.fs.glsl b/source/shaders/depth_test.fs.glsl new file mode 100644 index 0000000..796d849 --- /dev/null +++ b/source/shaders/depth_test.fs.glsl @@ -0,0 +1,30 @@ +#version 330 core + + +in vec2 TexCoords; +in vec3 VertexWorldPos; +uniform sampler2D TexId; +out vec4 FragColor; + +uniform float near = 0.1f; +uniform float far = 100.0f; + +/* @note +float linear_fragment_depth = MakeDepthLinear(non_linear_fragment_depth); +float scaled_lfd = linear_fragment_depth/far; + +gives us the z value in eye space. +This is purely for learning purposes. +The equation used in MakeDepthLinear is derived from the PerspectiveProjectionMatrix. +Take a look at the equation for that in the codebase  +or here: https://www.songho.ca/opengl/gl_projectionmatrix.html +*/ +float MakeDepthLinear(float depth) { +  float ndc = 2.0f*depth - 1; +  float linear_depth = (2.0 * far * near)/(far + near - ndc*(far - near)); +  return linear_depth; +} + +void main() { +    FragColor = texture(TexId, TexCoords); +} diff --git a/source/shaders/model/model.vs.glsl b/source/shaders/depth_test.vs.glsl index da394cf..b3b81cc 100644 --- a/source/shaders/model/model.vs.glsl +++ b/source/shaders/depth_test.vs.glsl @@ -1,7 +1,6 @@  #version 330 core  layout(location=0) in vec3 aPos; -layout(location=1) in vec3 aNormal; -layout(location=2) in vec2 aTex; +layout(location=1) in vec2 aTex;  uniform mat4 Model;  uniform mat4 View; @@ -9,17 +8,12 @@ uniform mat4 Projection;  out vec2 TexCoords;  out vec3 VertexWorldPos; -out vec3 FragNormal;  // @note: I still do not fully understand how the FragNormal calculation works. Need to make sure I intuitively   // get that  void main() {    gl_Position = Projection*View*Model*vec4(aPos, 1.0); -    VertexWorldPos = vec3(Model * vec4(aPos, 1.0)); -  FragNormal = mat3(transpose(inverse(Model))) * aNormal; -  FragNormal = normalize(FragNormal);    TexCoords = aTex;  }; - diff --git a/source/shaders/model/model.fs.glsl b/source/shaders/model/model.fs.glsl deleted file mode 100644 index 87da0b6..0000000 --- a/source/shaders/model/model.fs.glsl +++ /dev/null @@ -1,152 +0,0 @@ -#version 330 core - -#define MAX_TEXTURES 32 -struct Material { -  sampler2D diffuse[MAX_TEXTURES]; -  sampler2D specular[MAX_TEXTURES]; -  float shininess; -}; - -struct DirectionalLight { -  vec3 direction; - -  vec3 ambient; -  vec3 diffuse; -  vec3 specular; -}; - -struct PointLight { -  vec3 position; - -  vec3 ambient; -  vec3 diffuse; -  vec3 specular; - -  // attentuation factors -  float kC; -  float kL; -  float kQ; -}; - -struct SpotLight { -  vec3 position; - -  vec3 ambient; -  vec3 diffuse; -  vec3 specular; - -  // attenuation factors -  float kC; -  float kL; -  float kQ; - -  // vector for the direction directly in front of the spotlight -  vec3 front; - -  // spot radius -  float radius_inner; -  float radius_outer; // to smooth out the light - -}; - -// this is the result of a light creation. This contains the multipliers for each kind of a light we want -// to have. -struct LightFactor { -  vec3 ambient; -  vec3 diffuse; -  vec3 specular; -}; - -in vec2 TexCoords; -in vec3 FragNormal; -in vec3 VertexWorldPos; -uniform Material material; -uniform PointLight pointLight; -uniform DirectionalLight dirLight; -uniform vec3 cameraPosition; - -out vec4 FragColor; - -LightFactor make_directional_light(DirectionalLight light, vec3 CONST_viewDir) { -  LightFactor res; - -  vec3 DL_lightDir = normalize(-light.direction); -  res.ambient = light.ambient; - -  float DL_diffuseStrength = max(dot(DL_lightDir, FragNormal), 0.0); -  res.diffuse = light.diffuse * DL_diffuseStrength; - -  vec3  DL_reflectDir	    = reflect(-DL_lightDir, FragNormal); -  float DL_specularity		= max(dot(CONST_viewDir, DL_reflectDir), 0.0); -  float DL_shinePower			= pow(DL_specularity, material.shininess); -  res.specular	= light.specular * DL_shinePower; - -  return res; -}; - -LightFactor make_point_light(PointLight light, vec3 CONST_viewDir) { -  LightFactor res; - -  float PL_lightDistance = length(light.position - VertexWorldPos); -  float PL_attenuationFactor = 1.0 /  -  (light.kC + (light.kL * PL_lightDistance) + (light.kQ * PL_lightDistance * PL_lightDistance)); -  res.ambient = PL_attenuationFactor * light.ambient; - -  vec3 PL_lightDir = normalize(light.position - VertexWorldPos); -  float PL_diffuseStrength = max(dot(PL_lightDir, FragNormal), 0.0); -  res.diffuse = PL_attenuationFactor * light.diffuse * PL_diffuseStrength; - -  vec3  PL_reflectDir		= reflect(-PL_lightDir, FragNormal); -  float PL_specularity	= max(dot(CONST_viewDir, PL_reflectDir), 0.0); -  float PL_shinePower		= pow(PL_specularity, material.shininess); -  res.specular		      = PL_attenuationFactor * PL_shinePower * light.specular; - -  return res; -} - -LightFactor make_spot_light(SpotLight light, vec3 CONST_viewDir) { -  LightFactor res; - -  float SL_lightDistance = length(light.position - VertexWorldPos); -  float SL_attenuationFactor = 1.0 /  -  (light.kC + (light.kL * SL_lightDistance) + (light.kQ * SL_lightDistance * SL_lightDistance)); -  vec3 SL_lightDir = normalize(light.position - VertexWorldPos); - -  res.ambient = SL_attenuationFactor * light.ambient; - -  float SL_diffAmount = dot(SL_lightDir, normalize(-light.front)); -  float SL_spotLightFadeFactor = clamp((SL_diffAmount - light.radius_outer)/(light.radius_inner - light.radius_outer), 0.0f, 1.0f); -  float SL_diffuseStrength = max(dot(SL_lightDir, FragNormal), 0.0); -  res.diffuse = SL_spotLightFadeFactor * SL_attenuationFactor * light.diffuse * SL_diffuseStrength; - -  vec3  SL_reflectDir		= reflect(-SL_lightDir, FragNormal); -  float SL_specularity	= max(dot(CONST_viewDir, SL_reflectDir), 0.0); -  float SL_shinePower		= pow(SL_specularity, material.shininess); -  res.specular		      = SL_spotLightFadeFactor * SL_attenuationFactor * SL_shinePower * light.specular; - -  return res; -} - -void main() { -  vec3 CONST_viewDir		= normalize(cameraPosition - VertexWorldPos); -  vec3 combinedAmbience = vec3(0.0); -  vec3 combinedDiffuse  = vec3(0.0); -  vec3 combinedSpecular = vec3(0.0); - -  LightFactor DL_factors = make_directional_light(dirLight, CONST_viewDir); -  combinedAmbience += DL_factors.ambient; -  combinedDiffuse += DL_factors.diffuse; -  combinedSpecular += DL_factors.specular; - -  //LightFactor PL_factors = make_point_light(pointLight, CONST_viewDir); -  //combinedAmbience += PL_factors.ambient; -  //combinedDiffuse += PL_factors.diffuse; -  //combinedSpecular += PL_factors.specular; - -  vec3 ambientLight  = combinedAmbience * vec3(texture(material.diffuse[0],  TexCoords)); -  vec3 diffuseLight  = combinedDiffuse  * vec3(texture(material.diffuse[0],  TexCoords)); -  vec3 specularLight = combinedSpecular * vec3(texture(material.specular[0], TexCoords)); - -  vec3 color = ambientLight + diffuseLight + specularLight; -  FragColor = vec4(color, 1.0); -}; | 
