summaryrefslogtreecommitdiff
path: root/source/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/main.cpp')
-rw-r--r--source/main.cpp255
1 files changed, 200 insertions, 55 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);
}