added camera movement
This commit is contained in:
@@ -13,7 +13,8 @@ add_executable(${PROJECT_NAME} src/main.cpp
|
||||
src/Shader.cpp
|
||||
include/Shader.hpp
|
||||
include/IndexBuffer.hpp
|
||||
libs/stb_image.hpp)
|
||||
libs/stb_image.hpp
|
||||
include/Camera.hpp)
|
||||
|
||||
target_compile_definitions(${PROJECT_NAME} PRIVATE $<$<CONFIG:Debug>:_DEBUG>)
|
||||
|
||||
|
||||
31
include/Camera.hpp
Normal file
31
include/Camera.hpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include "glm/glm.hpp"
|
||||
#include "glm/ext/matrix_transform.hpp"
|
||||
#include "glm/gtc/matrix_transform.hpp"
|
||||
|
||||
class Camera {
|
||||
private:
|
||||
glm::mat4 projection;
|
||||
glm::mat4 view;
|
||||
glm::mat4 viewProj;
|
||||
|
||||
public:
|
||||
Camera(float fov, float width, float height) {
|
||||
projection = glm::perspective(fov/2.0f, width/height, 0.1f, 1000.f);
|
||||
view = glm::mat4(1.0f);
|
||||
update();
|
||||
}
|
||||
|
||||
glm::mat4 getViewProj() {
|
||||
return viewProj;
|
||||
}
|
||||
|
||||
void update() {
|
||||
viewProj = projection * view;
|
||||
}
|
||||
|
||||
void translate(glm::vec3 v) {
|
||||
view = glm::translate(view, v*-1.0f);
|
||||
}
|
||||
};
|
||||
63
src/main.cpp
63
src/main.cpp
@@ -9,6 +9,7 @@
|
||||
#include "VertexBuffer.hpp"
|
||||
#include "Shader.hpp"
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "Camera.hpp"
|
||||
#include "stb_image.hpp"
|
||||
#include "glm/glm.hpp"
|
||||
#include "glm/ext/matrix_transform.hpp"
|
||||
@@ -80,7 +81,7 @@ int main() {
|
||||
#ifdef _DEBUG
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG); //remove for stable release
|
||||
#endif
|
||||
|
||||
SDL_GL_SetSwapInterval(1);
|
||||
|
||||
uint32 flags = SDL_WINDOW_OPENGL; //| SDL_WINDOW_FULLSCREEN;
|
||||
|
||||
@@ -169,12 +170,11 @@ int main() {
|
||||
auto model = glm::mat4(1.0f);
|
||||
model = glm::scale(model, glm::vec3(1.2f));
|
||||
|
||||
auto projection = glm::ortho(-2.0f, 2.0f, -2.0f, 2.0f, -10.0f, 100.0f);
|
||||
projection = glm::perspective(glm::radians(45.0f), 4.0f/3.0f, 0.1f, 100.0f);
|
||||
Camera camera(90.0f, 800.0f, 600.0f);
|
||||
camera.translate(glm::vec3(0.0f, 0.0f, 5.0f));
|
||||
camera.update();
|
||||
|
||||
auto view = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -3.0f));
|
||||
|
||||
auto modelViewProj = projection * view * model;
|
||||
auto modelViewProj = camera.getViewProj() * model;
|
||||
|
||||
int modelViewProjMatrixLocation = glGetUniformLocation(shader.getShaderId(), "u_modelViewProj");
|
||||
|
||||
@@ -185,13 +185,56 @@ int main() {
|
||||
|
||||
bool running = true;
|
||||
SDL_Event e;
|
||||
|
||||
bool btnW;
|
||||
bool btnS;
|
||||
bool btnA;
|
||||
bool btnD;
|
||||
|
||||
while (running) {
|
||||
while (SDL_PollEvent(&e)) {
|
||||
if (e.type == SDL_EVENT_QUIT) {
|
||||
running = false;
|
||||
} else if (e.type == SDL_EVENT_KEY_DOWN) {
|
||||
switch (e.key.key) {
|
||||
case SDLK_W: btnW = true; break;
|
||||
case SDLK_S: btnS = true; break;
|
||||
case SDLK_A: btnA = true; break;
|
||||
case SDLK_D: btnD = true; break;
|
||||
default: break;
|
||||
}
|
||||
} else if (e.type == SDL_EVENT_KEY_UP) {
|
||||
switch (e.key.key) {
|
||||
case SDLK_W: btnW = false; break;
|
||||
case SDLK_S: btnS = false; break;
|
||||
case SDLK_A: btnA = false; break;
|
||||
case SDLK_D: btnD = false; break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
time += delta;
|
||||
|
||||
if (btnW) {
|
||||
camera.translate(glm::vec3(0.0f, 0.0f, -2.0f*delta));
|
||||
}
|
||||
if (btnS) {
|
||||
camera.translate(glm::vec3(0.0f, 0.0f, 2.0f*delta));
|
||||
}
|
||||
if (btnA) {
|
||||
camera.translate(glm::vec3(-1.0f*delta, 0.0f, 0.0f));
|
||||
}
|
||||
if (btnD) {
|
||||
camera.translate(glm::vec3(1.0f*delta, 0.0f, 0.0f));
|
||||
}
|
||||
|
||||
camera.update();
|
||||
|
||||
model = glm::rotate(model, 1.0f*delta, glm::vec3(0.0f, 1.0f, 0.0f));
|
||||
modelViewProj = projection * view * model;
|
||||
modelViewProj = camera.getViewProj() * model;
|
||||
|
||||
if (colorUniformLocation != 1) {
|
||||
glUniform4f(colorUniformLocation, sinf(time)*sinf(time), 0.0f, 1.0f, 1.0f);
|
||||
@@ -209,12 +252,6 @@ int main() {
|
||||
|
||||
SDL_GL_SwapWindow(window);
|
||||
|
||||
while (SDL_PollEvent(&e)) {
|
||||
if (e.type == SDL_EVENT_QUIT) {
|
||||
running = false;
|
||||
}
|
||||
}
|
||||
|
||||
const uint64 endCounter = SDL_GetPerformanceCounter();
|
||||
const uint64 counterElapsed = endCounter - lastCounter;
|
||||
delta = (static_cast<float32>(counterElapsed) / static_cast<float32>(perfCounterFrequency));
|
||||
|
||||
Reference in New Issue
Block a user