Created a camera system that has changing mvp and setting the uniform
Now to correct whatever is wrong with it.
This commit is contained in:
parent
5944fcda92
commit
199eeb4ee1
5 changed files with 128 additions and 2 deletions
|
|
@ -88,6 +88,7 @@ target_include_directories(${PROJECT_NAME} PRIVATE
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(${PROJECT_NAME} PRIVATE
|
target_link_libraries(${PROJECT_NAME} PRIVATE
|
||||||
|
m
|
||||||
${SDL3_LIBRARIES}
|
${SDL3_LIBRARIES}
|
||||||
${SDL3_image_LIBRARIES}
|
${SDL3_image_LIBRARIES}
|
||||||
${CGLM_LIBRARIES}
|
${CGLM_LIBRARIES}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,10 @@
|
||||||
#version 330 core
|
#version 330 core
|
||||||
layout (location = 0) in vec3 aPos;
|
layout (location = 0) in vec3 aPos;
|
||||||
|
|
||||||
|
uniform mat4 MVP;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
|
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0) * MVP;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
87
src/camera.c
Normal file
87
src/camera.c
Normal file
|
|
@ -0,0 +1,87 @@
|
||||||
|
#include "camera.h"
|
||||||
|
#include "SDL3/SDL_keyboard.h"
|
||||||
|
#include "cglm/mat4.h"
|
||||||
|
#include "cglm/cam.h"
|
||||||
|
#include "cglm/vec3.h"
|
||||||
|
#include <SDL3/SDL_mouse.h>
|
||||||
|
#include <cglm/util.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
static float mouse_x, mouse_y;
|
||||||
|
|
||||||
|
Camera Camera_default = {
|
||||||
|
.position = {0.0f, 0.0f, 3.0f},
|
||||||
|
.forward = {0.0f, 0.0f, -1.0f},
|
||||||
|
.yaw = -1.5707963268f, // -90 deg in radians
|
||||||
|
.pitch = 0
|
||||||
|
};
|
||||||
|
|
||||||
|
void camera_tick(Camera *camera) {
|
||||||
|
// Mouse input
|
||||||
|
SDL_GetRelativeMouseState(&mouse_x, &mouse_y);
|
||||||
|
camera->yaw += mouse_x * glm_rad(MOUSE_SENSITIVITY);
|
||||||
|
camera->pitch += mouse_y * glm_rad(MOUSE_SENSITIVITY);
|
||||||
|
// Restrict pitch
|
||||||
|
if (camera->pitch > 89.0f)
|
||||||
|
camera->pitch = 89.0f;
|
||||||
|
if (camera->pitch < -89.0f)
|
||||||
|
camera->pitch = -89.0f;
|
||||||
|
|
||||||
|
// Camera Direction
|
||||||
|
camera->forward[0] = cos(camera->yaw) * cos(camera->pitch);
|
||||||
|
camera->forward[1] = sin(camera->pitch);
|
||||||
|
camera->forward[2] = sin(camera->yaw) * cos(camera->pitch);
|
||||||
|
glm_normalize(camera->forward);
|
||||||
|
vec3 camera_right = GLM_VEC3_ZERO_INIT;
|
||||||
|
vec3 up = GLM_VEC3_ZERO_INIT;
|
||||||
|
up[2] = 1.0f;
|
||||||
|
glm_cross(up, camera->forward, camera_right);
|
||||||
|
glm_normalize(camera_right);
|
||||||
|
|
||||||
|
// Move camera based on wasd;
|
||||||
|
const bool *keyboardState = SDL_GetKeyboardState(NULL);
|
||||||
|
if (keyboardState[SDL_SCANCODE_W]) {
|
||||||
|
vec3 calc_buff;
|
||||||
|
glm_vec3_scale(camera->forward, MOVEMENT_SPEED, calc_buff);
|
||||||
|
glm_vec3_add(camera->position, calc_buff, camera->position);
|
||||||
|
}
|
||||||
|
if (keyboardState[SDL_SCANCODE_S]) {
|
||||||
|
vec3 calc_buff;
|
||||||
|
glm_vec3_scale(camera->forward, MOVEMENT_SPEED, calc_buff);
|
||||||
|
glm_vec3_sub(camera->position, calc_buff, camera->position);
|
||||||
|
}
|
||||||
|
if (keyboardState[SDL_SCANCODE_A]) {
|
||||||
|
vec3 calc_buff;
|
||||||
|
glm_vec3_scale(camera_right, MOVEMENT_SPEED, calc_buff);
|
||||||
|
glm_vec3_add(camera->position, calc_buff, camera->position);
|
||||||
|
}
|
||||||
|
if (keyboardState[SDL_SCANCODE_D]) {
|
||||||
|
vec3 calc_buff;
|
||||||
|
glm_vec3_scale(camera_right, MOVEMENT_SPEED, calc_buff);
|
||||||
|
glm_vec3_sub(camera->position, calc_buff, camera->position);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void calc_camera_mvp(mat4 *mvp_result, Camera *camera) {
|
||||||
|
// Get position to look and up vector
|
||||||
|
vec3 camera_target;
|
||||||
|
glm_vec3_add(camera->position , camera->forward, camera_target);
|
||||||
|
vec3 up = GLM_VEC3_ZERO_INIT;
|
||||||
|
up[2] = 1.0f;
|
||||||
|
|
||||||
|
mat4 model;
|
||||||
|
glm_mat4_identity(model);
|
||||||
|
|
||||||
|
mat4 view;
|
||||||
|
glm_lookat(camera->position, camera_target, up, view);
|
||||||
|
|
||||||
|
mat4 projection;
|
||||||
|
float fov = glm_rad(45.0f); // fov y
|
||||||
|
float aspect = 800.0f / 600.0f;
|
||||||
|
float near = 0.1f;
|
||||||
|
float far = 100.0f;
|
||||||
|
glm_perspective(fov, aspect, near, far, projection);
|
||||||
|
|
||||||
|
glm_mat4_mul(model, view, *mvp_result);
|
||||||
|
glm_mat4_mul(*mvp_result, projection, *mvp_result);
|
||||||
|
}
|
||||||
21
src/camera.h
Normal file
21
src/camera.h
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cglm/types.h>
|
||||||
|
|
||||||
|
#define MOVEMENT_SPEED 0.02f
|
||||||
|
#define MOUSE_SENSITIVITY 0.05f
|
||||||
|
|
||||||
|
struct Camera_s {
|
||||||
|
vec3 position;
|
||||||
|
vec3 forward;
|
||||||
|
float yaw;
|
||||||
|
float pitch;
|
||||||
|
};
|
||||||
|
typedef struct Camera_s Camera;
|
||||||
|
extern Camera Camera_default;
|
||||||
|
|
||||||
|
void calc_camera_mvp (mat4 *mvp_result, Camera *camera);
|
||||||
|
|
||||||
|
void get_camera_position (vec3 *position_result, Camera *camera);
|
||||||
|
|
||||||
|
void camera_tick(Camera *camera);
|
||||||
17
src/main.c
17
src/main.c
|
|
@ -3,9 +3,11 @@
|
||||||
#include <SDL3/SDL_render.h>
|
#include <SDL3/SDL_render.h>
|
||||||
#include <SDL3/SDL_video.h>
|
#include <SDL3/SDL_video.h>
|
||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "arena_allocator.h"
|
#include "camera.h"
|
||||||
|
#include "cglm/io.h"
|
||||||
|
|
||||||
// settings
|
// settings
|
||||||
const unsigned int SCR_WIDTH = 800;
|
const unsigned int SCR_WIDTH = 800;
|
||||||
|
|
@ -142,6 +144,9 @@ int main(int argc, char *argv[]) {
|
||||||
// VBOs) when it's not directly necessary.
|
// VBOs) when it's not directly necessary.
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
|
|
||||||
|
// Initialise camera
|
||||||
|
Camera camera = Camera_default;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
// handle input
|
// handle input
|
||||||
SDL_PollEvent(&event);
|
SDL_PollEvent(&event);
|
||||||
|
|
@ -149,6 +154,16 @@ int main(int argc, char *argv[]) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle camera
|
||||||
|
camera_tick(&camera);
|
||||||
|
|
||||||
|
// TODO: make shader file
|
||||||
|
mat4 mvp;
|
||||||
|
calc_camera_mvp(&mvp, &camera);
|
||||||
|
glm_mat4_print(mvp,stdout);
|
||||||
|
int mvp_uniform_location = glGetUniformLocation(shaderProgram, "MVP");
|
||||||
|
glUniformMatrix4fv(mvp_uniform_location, 1, GL_FALSE, &mvp[0][0]);
|
||||||
|
|
||||||
// render
|
// render
|
||||||
glClearColor(0.1f, 0.3f, 0.4f, 1.f);
|
glClearColor(0.1f, 0.3f, 0.4f, 1.f);
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue