diff options
-rwxr-xr-x | build.sh | 7 | ||||
-rw-r--r-- | source/main.cpp (renamed from main.cpp) | 315 | ||||
-rw-r--r-- | source/shaders/light_source.fs.glsl | 5 | ||||
-rw-r--r-- | source/shaders/light_source.vs.glsl | 10 | ||||
-rw-r--r-- | source/shaders/light_subject.fs.glsl | 46 | ||||
-rw-r--r-- | source/shaders/light_subject.vs.glsl | 18 |
6 files changed, 226 insertions, 175 deletions
@@ -8,7 +8,7 @@ mkdir -p $build_dir include_path=include include_opts="-I $include_path" -files="main.cpp $include_path/glad/glad.c" +files="source/main.cpp $include_path/glad/glad.c" build_opts="$build_dir/main" lib_path="lib/SDL2" @@ -16,7 +16,8 @@ link_opts="-L $lib_path -lSDL2" build_command="clang++ -std=c++11 -g -Og $include_opts $files $link_opts -o $build_opts" -printf "Building your sdl2 project.\n\nThis is the build command for posteritys' sake:\n\n" +printf "This is the build command for posteritys' sake:\n" printf "$build_command\n\n" +printf "Building Project...\n" $build_command -printf "Build Complete!\n" +printf "\nBuild Complete!\n\n" diff --git a/main.cpp b/source/main.cpp index daff7ec..b7597fe 100644 --- a/main.cpp +++ b/source/main.cpp @@ -6,6 +6,7 @@ #include "stb_image.h" /* @lookup: +* - I do not understand how floating point numbers work, so I should probably look into that. * - The normal matrix calculation in the fragment shader for the object affected by light has been mainly copied. * I have tried to understand the formula, and whilst it made some sense, it is not fully clear to me, and I cannot picture it yet. * Revisit the derivation for the normal matrix some time in the future. @@ -20,10 +21,24 @@ * - Switch to using clang on windows (for simplicity) */ +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; + +typedef int8_t s8; +typedef int16_t s16; +typedef int32_t s32; +typedef int64_t s64; + +typedef float r32; +typedef double r64; + +typedef u8 b8; // =========== Shader Loading ============= -unsigned int create_vertex_shader(const char* vertex_shader_source) +unsigned int gl_create_vertex_shader(char* vertex_shader_source) { unsigned int vertex_shader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertex_shader, 1, &vertex_shader_source, NULL); @@ -42,7 +57,7 @@ unsigned int create_vertex_shader(const char* vertex_shader_source) return vertex_shader; } -unsigned int create_fragment_shader(const char* fragment_shader_source) +unsigned int gl_create_fragment_shader(char* fragment_shader_source) { unsigned int fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragment_shader, 1, &fragment_shader_source, NULL); @@ -61,7 +76,7 @@ unsigned int create_fragment_shader(const char* fragment_shader_source) return fragment_shader; } -unsigned int create_shader_program(unsigned int vertex_shader, unsigned int fragment_shader) +unsigned int gl_create_shader_program(unsigned int vertex_shader, unsigned int fragment_shader) { unsigned int shader_program = glCreateProgram(); @@ -85,13 +100,23 @@ unsigned int create_shader_program(unsigned int vertex_shader, unsigned int frag return shader_program; } +int platform_read_file() +{ + return 0; +} + +int platform_write_file() +{ + return 0; +} + // =========================================================== MATH ================================================== #define PI 3.14159265358979323846264338327950288f #define Square(x) ((x)*(x)) #define To_Radian(x) ((x) * PI / 180.0f) #define To_Degree(x) ((x) * 180.0f / PI) -float clampf(float x, float bottom, float top) +r32 clampf(r32 x, r32 bottom, r32 top) { if (x < bottom) { @@ -109,32 +134,32 @@ float clampf(float x, float bottom, float top) union Vec3 { struct { - float x; - float y; - float z; + r32 x; + r32 y; + r32 z; }; - float data[3]; + r32 data[3]; }; union Vec4 { struct { - float x; - float y; - float z; - float w; + r32 x; + r32 y; + r32 z; + r32 w; }; - float data[4]; + r32 data[4]; }; union Mat4 { Vec4 xyzw[4]; - float data[4][4]; - float buffer[16]; + r32 data[4][4]; + r32 buffer[16]; }; // ========================================================== Vec3 ========================================================== -Vec3 init3v(float x, float y, float z) +Vec3 init3v(r32 x, r32 y, r32 z) { Vec3 res; res.x = x; @@ -144,7 +169,7 @@ Vec3 init3v(float x, float y, float z) return res; } -Vec3 scaler_add3v(Vec3 vec, float scaler) +Vec3 scaler_add3v(Vec3 vec, r32 scaler) { Vec3 res; res.x = vec.x + scaler; @@ -154,7 +179,7 @@ Vec3 scaler_add3v(Vec3 vec, float scaler) return res; } -Vec3 scaler_multiply3v(Vec3 vec, float scaler) +Vec3 scaler_multiply3v(Vec3 vec, r32 scaler) { Vec3 res; res.x = vec.x * scaler; @@ -164,7 +189,7 @@ Vec3 scaler_multiply3v(Vec3 vec, float scaler) return res; } -Vec3 scaler_divide3v(Vec3 vec, float scaler) +Vec3 scaler_divide3v(Vec3 vec, r32 scaler) { Vec3 res; res.x = vec.x / scaler; @@ -195,38 +220,38 @@ Vec3 subtract3v(Vec3 a, Vec3 b) return res; } -float dot_multiply3v(Vec3 a, Vec3 b) +r32 dot_multiply3v(Vec3 a, Vec3 b) { - float x = a.x * b.x; - float y = a.y * b.y; - float z = a.z * b.z; + r32 x = a.x * b.x; + r32 y = a.y * b.y; + r32 z = a.z * b.z; - float res = x + y + z; + r32 res = x + y + z; return res; } -float magnitude3v(Vec3 vec) +r32 magnitude3v(Vec3 vec) { - float res = sqrtf(Square(vec.x) + Square(vec.y) + Square(vec.z)); + r32 res = sqrtf(Square(vec.x) + Square(vec.y) + Square(vec.z)); return res; } Vec3 normalize3v(Vec3 vec) { - float magnitude = magnitude3v(vec); + r32 magnitude = magnitude3v(vec); Vec3 res = scaler_divide3v(vec, magnitude); return res; } #ifndef FUN_CALCS -float angle3v(Vec3 a, Vec3 b) +r32 angle3v(Vec3 a, Vec3 b) { Vec3 a_norm = normalize3v(a); Vec3 b_norm = normalize3v(b); - float dot_product = dot_multiply3v(a_norm, b_norm); - float res = acosf(dot_product); + r32 dot_product = dot_multiply3v(a_norm, b_norm); + r32 res = acosf(dot_product); return res; } @@ -244,7 +269,7 @@ Vec3 cross_multiply3v(Vec3 a, Vec3 b) // ============================================== Vec4, Mat4 ============================================== -Vec4 init4v(float x, float y, float z, float w) +Vec4 init4v(r32 x, r32 y, r32 z, r32 w) { Vec4 res; res.x = x; @@ -255,7 +280,7 @@ Vec4 init4v(float x, float y, float z, float w) return res; } -Mat4 init_value4m(float value) +Mat4 init_value4m(r32 value) { Mat4 res = {0}; res.data[0][0] = value; @@ -268,10 +293,10 @@ Mat4 init_value4m(float value) // @note: These operations are just defined and not expressed. They are kept here for completeness sake BUT // since I have not had to do anything related to these, I have not created them. -Vec4 scaler_add4v(Vec4 vec, float scaler); -Vec4 scaler_subtract4v(Vec4 vec, float scaler); -Vec4 scaler_multiply4v(Vec4 vec, float scaler); -Vec4 scaler_divide4v(Vec4 vec, float scaler); +Vec4 scaler_add4v(Vec4 vec, r32 scaler); +Vec4 scaler_subtract4v(Vec4 vec, r32 scaler); +Vec4 scaler_multiply4v(Vec4 vec, r32 scaler); +Vec4 scaler_divide4v(Vec4 vec, r32 scaler); Vec4 add4v(Vec4 a, Vec4 b); Vec4 subtract4v(Vec4 a, Vec4 b); Vec4 dot_multiply4v(Vec4 a, Vec4 b); @@ -370,7 +395,7 @@ Mat4 multiply4m(Mat4 a, Mat4 b) // ==== Matrix Transformation ==== -Mat4 scaling_matrix4m(float x, float y, float z) // generates a 4x4 scaling matrix for scaling each of the x,y,z axis +Mat4 scaling_matrix4m(r32 x, r32 y, r32 z) // generates a 4x4 scaling matrix for scaling each of the x,y,z axis { Mat4 res = init_value4m(1.0f); res.data[0][0] = x; @@ -380,7 +405,7 @@ Mat4 scaling_matrix4m(float x, float y, float z) // generates a 4x4 scaling matr return res; } -Mat4 translation_matrix4m(float x, float y, float z) // generates a 4x4 translation matrix for translation along each of the x,y,z axis +Mat4 translation_matrix4m(r32 x, r32 y, r32 z) // generates a 4x4 translation matrix for translation along each of the x,y,z axis { Mat4 res = init_value4m(1.0f); res.data[0][3] = x; @@ -390,14 +415,14 @@ Mat4 translation_matrix4m(float x, float y, float z) // generates a 4x4 translat return res; } -Mat4 rotation_matrix4m(float angle_radians, Vec3 axis) // generates a 4x4 rotation matrix for rotation along each of the x,y,z axis +Mat4 rotation_matrix4m(r32 angle_radians, Vec3 axis) // generates a 4x4 rotation matrix for rotation along each of the x,y,z axis { Mat4 res = init_value4m(1.0f); axis = normalize3v(axis); - float cos_theta = cosf(angle_radians); - float sin_theta = sinf(angle_radians); - float cos_value = 1.0f - cos_theta; + r32 cos_theta = cosf(angle_radians); + r32 sin_theta = sinf(angle_radians); + r32 cos_value = 1.0f - cos_theta; res.data[0][0] = (axis.x * axis.x * cos_value) + cos_theta; res.data[0][1] = (axis.x * axis.y * cos_value) + (axis.z * sin_theta); @@ -414,7 +439,7 @@ Mat4 rotation_matrix4m(float angle_radians, Vec3 axis) // generates a 4x4 rotati return res; } -Mat4 perspective_projection_matrix4m(float left, float right, float bottom, float top, float near, float far) +Mat4 perspective_projection_matrix4m(r32 left, r32 right, r32 bottom, r32 top, r32 near, r32 far) { Mat4 res = { 0 }; @@ -432,9 +457,9 @@ Mat4 perspective_projection_matrix4m(float left, float right, float bottom, floa return res; } -Mat4 perspective4m(float fov, float aspect_ratio, float near, float far) +Mat4 perspective4m(r32 fov, r32 aspect_ratio, r32 near, r32 far) { - float cotangent = 1.0f / tanf(fov / 2.0f); + r32 cotangent = 1.0f / tanf(fov / 2.0f); Mat4 res = { 0 }; @@ -484,7 +509,7 @@ Mat4 camera_create4m(Vec3 camera_pos, Vec3 camera_look, Vec3 camera_up) return res; } -Vec3 camera_look_around(float angle_pitch, float angle_yaw) +Vec3 camera_look_around(r32 angle_pitch, r32 angle_yaw) { Vec3 camera_look = {0.0}; camera_look.x = cosf(angle_yaw) * cosf(angle_pitch); @@ -529,88 +554,27 @@ int main(int argc, char* argv[]) return -1; } + // load glad if (!gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress)) { printf("Failed to initialize Glad\n"); return 1; } - const char* vertex_source = - "#version 330 core\n" - "layout(location = 0) in vec3 position;\n" - "layout(location = 1) in vec3 normal;\n" - "uniform mat4 Model;\n" - "uniform mat4 View;\n" - "uniform mat4 Projection;\n" - "out vec3 fragNormal;\n" - "out vec3 worldPosition;\n" - "void main() {\n" - " gl_Position = Projection * View * Model * vec4(position, 1.0);\n" - " worldPosition = vec3(Model * vec4(position, 1.0));\n" - " fragNormal = mat3(transpose(inverse(Model))) * normal;\n" - " fragNormal = normalize(normal);\n" - "}"; - - const char* fragment_source = - "#version 330 core\n" - "in vec3 fragNormal;\n" - "in vec3 worldPosition;\n" - "out vec4 FragColor;\n" - "uniform float specularStrength;\n" - "uniform float shineFactor;\n" - "uniform vec3 lightPosition;\n" - "uniform vec3 cameraPosition;\n" - "uniform vec4 lightColor;\n" - "uniform vec4 objectColor;\n" - "uniform sampler2D smilingTexture;\n" - "uniform sampler2D containerTexture;\n" - "void main() {\n" - " float ambientLightStrength = 0.1;\n" - " vec4 ambientLight = ambientLightStrength * lightColor;\n" - "\n" - "// @note: Diffuse calculations\n" - " vec3 lightDir = normalize(lightPosition - worldPosition);\n" - " float diffuseStrength = max(dot(lightDir, fragNormal), 0.0);\n" - " vec4 diffuseLight = diffuseStrength * lightColor;\n" - "\n" - "// @note: Specular calculations\n" - " vec3 viewDir = normalize(cameraPosition - worldPosition);\n" - " vec3 reflectDir = reflect(-lightDir, fragNormal);\n" - " float specularity = max(dot(viewDir, reflectDir), 0.0);\n" - " float shininess = pow(specularity, shineFactor);\n" - " vec4 specularLight = specularStrength * shininess * lightColor;\n" - " " - "\n" - " vec4 color = (ambientLight + diffuseLight + specularLight) * objectColor;\n" - " FragColor = color;\n" - - "}\n"; - - const char* light_vertex_source = - "#version 330 core\n" - "layout(location = 0) in vec3 position;\n" - "uniform mat4 Model;\n" - "uniform mat4 View;\n" - "uniform mat4 Projection;\n" - "void main() {\n" - " gl_Position = Projection * View * Model * vec4(position.x, position.y, position.z, 1.0);\n" - "}\n"; - - const char* light_fragment_source = - "#version 330 core\n" - "out vec4 FragColor;\n" - "void main() {\n" - " FragColor = vec4(1.0);\n" - "}\n"; - - - GLuint vertex_shader = create_vertex_shader(vertex_source); - GLuint fragment_shader = create_fragment_shader(fragment_source); - GLuint shader_program = create_shader_program(vertex_shader, fragment_shader); - printf("Successfully compiled normal shaders.\n"); - GLuint light_vs = create_vertex_shader(light_vertex_source); - GLuint light_fs = create_fragment_shader(light_fragment_source); - GLuint light_sp = create_shader_program(light_vs, light_fs); + // filesystem playground stuff + size_t read_count; + char* vertex_source = (char*)SDL_LoadFile("./source/shaders/light_subject.vs.glsl", &read_count); + char* fragment_source = (char*)SDL_LoadFile("./source/shaders/light_subject.fs.glsl", &read_count); + char* light_vertex_source = (char*)SDL_LoadFile("./source/shaders/light_source.vs.glsl", &read_count); + char* light_fragment_source = (char*)SDL_LoadFile("./source/shaders/light_source.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); + GLuint light_vs = gl_create_vertex_shader(light_vertex_source); + GLuint light_fs = gl_create_fragment_shader(light_fragment_source); + GLuint light_sp = gl_create_shader_program(light_vs, light_fs); + printf("Successfully compiled shaders.\n"); GLfloat rect_vertices[] = { // Position // Color // Texture @@ -625,7 +589,7 @@ int main(int argc, char* argv[]) 2, 1, 3 }; - float cube_normal_vertices[] = { + r32 cube_normal_vertices[] = { -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, @@ -669,7 +633,7 @@ int main(int argc, char* argv[]) -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f }; - float cube_vertices[] = { + r32 cube_vertices[] = { -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, @@ -821,18 +785,44 @@ int main(int argc, char* argv[]) glBindTexture(GL_TEXTURE_2D, 0); } - Vec3 light_location = Vec3{ 0.0, 0.0, 3.0 }; - glUseProgram(shader_program); + glUseProgram(shader_program); + Vec3 ambient_color = Vec3{ 0.0, 0.1, 0.6 }; + Vec3 diffuse_color = Vec3{ 0.0, 0.50980392, 0.50980392}; + Vec3 specular_color = Vec3{ 0.50196078, 0.50196078, 0.50196078}; + Vec3 light_color = Vec3{ 1.0, 1.0, 1.0}; + r32 specular_shine_factor = 128.0 * 0.25; + + // material uniforms + int mat_ambient_loc = glGetUniformLocation(shader_program, "material.ambient"); + glUniform3fv(mat_ambient_loc, 1, ambient_color.data); - float specular_strength = 0.5; - float specular_shine_factor = 32.0; + int mat_diffuse_loc = glGetUniformLocation(shader_program, "material.diffuse"); + glUniform3fv(mat_diffuse_loc, 1, diffuse_color.data); - int spec_strength_loc = glGetUniformLocation(shader_program, "specularStrength"); - glUniform1f(spec_strength_loc, specular_strength); + int mat_specular_loc = glGetUniformLocation(shader_program, "material.specular"); + glUniform3fv(mat_specular_loc, 1, specular_color.data); - int shine_factor_loc = glGetUniformLocation(shader_program, "shineFactor"); + int shine_factor_loc = glGetUniformLocation(shader_program, "material.shininess"); glUniform1f(shine_factor_loc, specular_shine_factor); + // light uniforms + Vec3 light_location = Vec3{ 0.0, 0.0, 3.0 }; + Vec3 light_ambient = Vec3{ 0.2, 0.2, 0.2 }; + Vec3 light_diffuse = Vec3{ 0.5, 0.5, 0.5 }; + Vec3 light_specular = Vec3{ 1.0, 1.0, 1.0}; + + int light_ambient_loc = glGetUniformLocation(shader_program, "light.ambient"); + glUniform3fv(light_ambient_loc, 1, light_ambient.data); + + int light_diffuse_loc = glGetUniformLocation(shader_program, "light.diffuse"); + glUniform3fv(light_diffuse_loc, 1, light_diffuse.data); + + int light_specular_loc = glGetUniformLocation(shader_program, "light.specular"); + glUniform3fv(light_specular_loc, 1, light_diffuse.data); + + int light_pos_loc = glGetUniformLocation(shader_program, "light.position"); + glUniform3fv(light_pos_loc, 1, light_location.data); + // texture uniforms int smiling_loc = glGetUniformLocation(shader_program, "smilingTexture"); glUniform1i(smiling_loc, 0); @@ -840,16 +830,9 @@ int main(int argc, char* argv[]) int container_loc = glGetUniformLocation(shader_program, "containerTexture"); glUniform1i(container_loc, 1); - Vec4 light_color = Vec4{0.5, 1.0, 0.5, 1.0}; int light_uniform_loc = glGetUniformLocation(shader_program, "lightColor"); - glUniform4fv(light_uniform_loc, 1, light_color.data); - - Vec4 object_color = Vec4{ 1.0, 1.0, 0.0, 1.0 }; - int object_uniform_loc = glGetUniformLocation(shader_program, "objectColor"); - glUniform4fv(object_uniform_loc, 1, object_color.data); + glUniform3fv(light_uniform_loc, 1, light_color.data); - int light_pos_loc = glGetUniformLocation(shader_program, "lightPosition"); - glUniform3fv(light_pos_loc, 1, light_location.data); int camera_pos_loc = glGetUniformLocation(shader_program, "cameraPosition"); @@ -862,31 +845,31 @@ int main(int argc, char* argv[]) Vec3{ 3.0, -7.0, -6.0}, }; - float FOV = 90.0; - float time_curr; - float time_prev = SDL_GetTicks64() / 100.0; + r32 FOV = 90.0; + r32 time_curr; + r32 time_prev = SDL_GetTicks64() / 100.0; uint32_t model_loc = glGetUniformLocation(shader_program, "Model"); // camera stuff Vec3 camera_pos = Vec3{ 0.0, 5.0, 10.0f}; Vec3 preset_up_dir = Vec3{ 0.0, 1.0, 0.0 }; - float angle_yaw, angle_pitch, angle_roll; - angle_pitch = (float)To_Radian(0.0f); - angle_yaw = (float)-To_Radian(90.0f); + r32 angle_yaw, angle_pitch, angle_roll; + angle_pitch = (r32)To_Radian(0.0f); + angle_yaw = (r32)-To_Radian(90.0f); Vec3 camera_look = camera_look_around(angle_pitch, angle_yaw); // @todo: remove this, I dont like this and think that this is unnecessary Vec3 camera_look_increment; - float camera_speed = 1.0f; + r32 camera_speed = 1.0f; Mat4 view = camera_create4m(camera_pos, camera_look, preset_up_dir); uint32_t view_loc = glGetUniformLocation(shader_program, "View"); glUniformMatrix4fv(view_loc, 1, GL_TRUE, view.buffer); - Mat4 proj = perspective4m((float)To_Radian(90.0), (float)width / (float)height, 0.1f, 100.0f); + Mat4 proj = perspective4m((r32)To_Radian(90.0), (r32)width / (r32)height, 0.1f, 100.0f); uint32_t proj_loc = glGetUniformLocation(shader_program, "Projection"); glUniformMatrix4fv(proj_loc, 1, GL_TRUE, proj.buffer); @@ -894,6 +877,7 @@ int main(int argc, char* argv[]) Mat4 light_model = translation_matrix4m(light_location.x, light_location.y, light_location.z); //Mat4 light_model = init_value4m(1.0); //light_model = multiply4m(light_translation, light_model); + uint32_t light_model_loc = glGetUniformLocation(light_sp, "Model"); glUniformMatrix4fv(light_model_loc, 1, GL_TRUE, light_model.buffer); @@ -905,22 +889,22 @@ int main(int argc, char* argv[]) glEnable(GL_DEPTH_TEST); - bool game_running = true; + u8 game_running = true; - bool hold_lshift = false; - bool move_w = false; - bool move_a = false; - bool move_s = false; - bool move_d = false; + u8 hold_lshift = false; + u8 move_w = false; + u8 move_a = false; + u8 move_s = false; + u8 move_d = false; while(game_running) { // frame delta time_curr = SDL_GetTicks64() / 100.0; - float time_delta = time_curr - time_prev; + r32 time_delta = time_curr - time_prev; - float camera_speed_adjusted = time_delta * camera_speed; + r32 camera_speed_adjusted = time_delta * camera_speed; camera_look_increment = scaler_multiply3v(camera_look, camera_speed_adjusted); SDL_Event ev; @@ -944,22 +928,12 @@ int main(int argc, char* argv[]) {} if (ev.key.keysym.sym == SDLK_UP) { - if (hold_lshift == true) - { - specular_strength += 0.1; - } - else { specular_shine_factor = specular_shine_factor*2.0; } } if (ev.key.keysym.sym == SDLK_DOWN) { - if (hold_lshift == true) - { - specular_strength -= 0.1; - } - else { specular_shine_factor = specular_shine_factor/2.0; } @@ -1007,8 +981,8 @@ int main(int argc, char* argv[]) case (SDL_MOUSEMOTION): { SDL_MouseMotionEvent mouse_event = ev.motion; - float x_motion = (float)mouse_event.xrel; - float y_motion = (float)mouse_event.yrel; + 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); @@ -1049,16 +1023,13 @@ int main(int argc, char* argv[]) view = camera_create4m(camera_pos, add3v(camera_pos, camera_look), preset_up_dir); // object shader program stuff glUseProgram(shader_program); - glUniform1f(spec_strength_loc, specular_strength); - glUniform1f(shine_factor_loc, specular_shine_factor); + //glUniform1f(shine_factor_loc, specular_shine_factor); + glUniformMatrix4fv(view_loc, 1, GL_TRUE, view.buffer); - glUniform3fv(light_pos_loc, 1, light_location.data); glUniform3fv(camera_pos_loc, 1, camera_pos.data); // light/lamp shader program stuff glUseProgram(light_sp); - light_model = translation_matrix4m(light_location.x, light_location.y, light_location.z); - glUniformMatrix4fv(light_model_loc, 1, GL_TRUE, light_model.buffer); glUniformMatrix4fv(light_view_loc, 1, GL_TRUE, view.buffer); time_prev = time_curr; diff --git a/source/shaders/light_source.fs.glsl b/source/shaders/light_source.fs.glsl new file mode 100644 index 0000000..9e834cb --- /dev/null +++ b/source/shaders/light_source.fs.glsl @@ -0,0 +1,5 @@ +#version 330 core +out vec4 FragColor; +void main() { + FragColor = vec4(1.0); +} diff --git a/source/shaders/light_source.vs.glsl b/source/shaders/light_source.vs.glsl new file mode 100644 index 0000000..bf372d5 --- /dev/null +++ b/source/shaders/light_source.vs.glsl @@ -0,0 +1,10 @@ +#version 330 core +layout(location = 0) in vec3 position; + +uniform mat4 Model; +uniform mat4 View; +uniform mat4 Projection; + +void main() { + gl_Position = Projection * View * Model * vec4(position.x, position.y, position.z, 1.0); +} diff --git a/source/shaders/light_subject.fs.glsl b/source/shaders/light_subject.fs.glsl new file mode 100644 index 0000000..54bf700 --- /dev/null +++ b/source/shaders/light_subject.fs.glsl @@ -0,0 +1,46 @@ +#version 330 core +struct Material { + vec3 ambient; + vec3 diffuse; + vec3 specular; + float shininess; +}; + +struct Light { + vec3 ambient; + vec3 diffuse; + vec3 specular; + + vec3 position; +}; + +in vec3 fragNormal; +in vec3 worldPosition; + +uniform Material material; +uniform Light light; +uniform vec3 cameraPosition; +uniform vec3 lightColor; +uniform sampler2D smilingTexture; +uniform sampler2D containerTexture; + +out vec4 FragColor; + +void main() { + vec3 ambientLight = light.ambient * material.ambient; + +// @note: Diffuse calculations + vec3 lightDir = normalize(light.position - worldPosition); + float diffuseStrength = max(dot(lightDir, fragNormal), 0.0); + vec3 diffuseLight = light.diffuse * (diffuseStrength * material.diffuse); + +// @note: Specular calculations + vec3 viewDir = normalize(cameraPosition - worldPosition); + vec3 reflectDir = reflect(-lightDir, fragNormal); + float specularity = max(dot(viewDir, reflectDir), 0.0); + float shinePower = pow(specularity, material.shininess); + vec3 specularLight = light.specular * (shinePower * material.specular); + + vec3 color = ambientLight + diffuseLight + specularLight; + FragColor = vec4(color, 1.0); +} diff --git a/source/shaders/light_subject.vs.glsl b/source/shaders/light_subject.vs.glsl new file mode 100644 index 0000000..327e896 --- /dev/null +++ b/source/shaders/light_subject.vs.glsl @@ -0,0 +1,18 @@ +#version 330 core + +layout(location = 0) in vec3 position; +layout(location = 1) in vec3 normal; + +uniform mat4 Model; +uniform mat4 View; +uniform mat4 Projection; + +out vec3 fragNormal; +out vec3 worldPosition; + +void main() { + gl_Position = Projection * View * Model * vec4(position, 1.0); + worldPosition = vec3(Model * vec4(position, 1.0)); + fragNormal = mat3(transpose(inverse(Model))) * normal; + fragNormal = normalize(normal); +} |