Fixed some inconsistencies that broke rotation and rendering
This commit is contained in:
parent
199eeb4ee1
commit
966027815c
4 changed files with 25 additions and 24 deletions
|
|
@ -5,6 +5,6 @@ uniform mat4 MVP;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0) * MVP;
|
gl_Position = MVP * vec4(aPos.x, aPos.y, aPos.z, 1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
30
src/camera.c
30
src/camera.c
|
|
@ -20,21 +20,21 @@ void camera_tick(Camera *camera) {
|
||||||
// Mouse input
|
// Mouse input
|
||||||
SDL_GetRelativeMouseState(&mouse_x, &mouse_y);
|
SDL_GetRelativeMouseState(&mouse_x, &mouse_y);
|
||||||
camera->yaw += mouse_x * glm_rad(MOUSE_SENSITIVITY);
|
camera->yaw += mouse_x * glm_rad(MOUSE_SENSITIVITY);
|
||||||
camera->pitch += mouse_y * glm_rad(MOUSE_SENSITIVITY);
|
camera->pitch -= mouse_y * glm_rad(MOUSE_SENSITIVITY);
|
||||||
// Restrict pitch
|
// Restrict pitch
|
||||||
if (camera->pitch > 89.0f)
|
if (camera->pitch > glm_rad(89.0f))
|
||||||
camera->pitch = 89.0f;
|
camera->pitch = glm_rad(89.0f);
|
||||||
if (camera->pitch < -89.0f)
|
if (camera->pitch < glm_rad(-89.0f))
|
||||||
camera->pitch = -89.0f;
|
camera->pitch = glm_rad(-89.0f);
|
||||||
|
|
||||||
// Camera Direction
|
// Camera Direction
|
||||||
camera->forward[0] = cos(camera->yaw) * cos(camera->pitch);
|
camera->forward[0] = cosf(camera->yaw) * cosf(camera->pitch);
|
||||||
camera->forward[1] = sin(camera->pitch);
|
camera->forward[1] = sinf(camera->pitch);
|
||||||
camera->forward[2] = sin(camera->yaw) * cos(camera->pitch);
|
camera->forward[2] = sinf(camera->yaw) * cosf(camera->pitch);
|
||||||
glm_normalize(camera->forward);
|
glm_normalize(camera->forward);
|
||||||
vec3 camera_right = GLM_VEC3_ZERO_INIT;
|
|
||||||
vec3 up = GLM_VEC3_ZERO_INIT;
|
vec3 camera_right;
|
||||||
up[2] = 1.0f;
|
vec3 up = {0.0f, 1.0f ,0.0f};
|
||||||
glm_cross(up, camera->forward, camera_right);
|
glm_cross(up, camera->forward, camera_right);
|
||||||
glm_normalize(camera_right);
|
glm_normalize(camera_right);
|
||||||
|
|
||||||
|
|
@ -62,12 +62,11 @@ void camera_tick(Camera *camera) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void calc_camera_mvp(mat4 *mvp_result, Camera *camera) {
|
void camera_calc_mvp(mat4 *mvp_result, Camera *camera) {
|
||||||
// Get position to look and up vector
|
// Get position to look and up vector
|
||||||
vec3 camera_target;
|
vec3 camera_target;
|
||||||
glm_vec3_add(camera->position , camera->forward, camera_target);
|
glm_vec3_add(camera->position , camera->forward, camera_target);
|
||||||
vec3 up = GLM_VEC3_ZERO_INIT;
|
vec3 up = {0.0f, 1.0f ,0.0f};
|
||||||
up[2] = 1.0f;
|
|
||||||
|
|
||||||
mat4 model;
|
mat4 model;
|
||||||
glm_mat4_identity(model);
|
glm_mat4_identity(model);
|
||||||
|
|
@ -82,6 +81,5 @@ void calc_camera_mvp(mat4 *mvp_result, Camera *camera) {
|
||||||
float far = 100.0f;
|
float far = 100.0f;
|
||||||
glm_perspective(fov, aspect, near, far, projection);
|
glm_perspective(fov, aspect, near, far, projection);
|
||||||
|
|
||||||
glm_mat4_mul(model, view, *mvp_result);
|
glm_mat4_mulN((mat4*[]){&projection, &view, &model}, 3, *mvp_result);
|
||||||
glm_mat4_mul(*mvp_result, projection, *mvp_result);
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ struct Camera_s {
|
||||||
typedef struct Camera_s Camera;
|
typedef struct Camera_s Camera;
|
||||||
extern Camera Camera_default;
|
extern Camera Camera_default;
|
||||||
|
|
||||||
void calc_camera_mvp (mat4 *mvp_result, Camera *camera);
|
void camera_calc_mvp (mat4 *mvp_result, Camera *camera);
|
||||||
|
|
||||||
void get_camera_position (vec3 *position_result, Camera *camera);
|
void get_camera_position (vec3 *position_result, Camera *camera);
|
||||||
|
|
||||||
|
|
|
||||||
15
src/main.c
15
src/main.c
|
|
@ -1,10 +1,8 @@
|
||||||
#include <SDL3/SDL_init.h>
|
#include <SDL3/SDL.h>
|
||||||
#include <SDL3/SDL_log.h>
|
|
||||||
#include <SDL3/SDL_render.h>
|
|
||||||
#include <SDL3/SDL_video.h>
|
|
||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
#include "camera.h"
|
#include "camera.h"
|
||||||
#include "cglm/io.h"
|
#include "cglm/io.h"
|
||||||
|
|
@ -147,6 +145,11 @@ int main(int argc, char *argv[]) {
|
||||||
// Initialise camera
|
// Initialise camera
|
||||||
Camera camera = Camera_default;
|
Camera camera = Camera_default;
|
||||||
|
|
||||||
|
// Lock and hide mouse to window
|
||||||
|
SDL_SetWindowRelativeMouseMode(window, true);
|
||||||
|
SDL_GetRelativeMouseState(NULL, NULL);
|
||||||
|
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
// handle input
|
// handle input
|
||||||
SDL_PollEvent(&event);
|
SDL_PollEvent(&event);
|
||||||
|
|
@ -159,8 +162,8 @@ int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
// TODO: make shader file
|
// TODO: make shader file
|
||||||
mat4 mvp;
|
mat4 mvp;
|
||||||
calc_camera_mvp(&mvp, &camera);
|
camera_calc_mvp(&mvp, &camera);
|
||||||
glm_mat4_print(mvp,stdout);
|
//glm_mat4_print(mvp,stdout);
|
||||||
int mvp_uniform_location = glGetUniformLocation(shaderProgram, "MVP");
|
int mvp_uniform_location = glGetUniformLocation(shaderProgram, "MVP");
|
||||||
glUniformMatrix4fv(mvp_uniform_location, 1, GL_FALSE, &mvp[0][0]);
|
glUniformMatrix4fv(mvp_uniform_location, 1, GL_FALSE, &mvp[0][0]);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue