Compare commits
6 Commits
8a044cbe86
...
e8057c97ff
Author | SHA1 | Date | |
---|---|---|---|
e8057c97ff | |||
5b6092f9d4 | |||
09d715b9f7 | |||
5f69ed6434 | |||
4a40fe6e1a | |||
6ba8a0e3f6 |
@ -36,6 +36,10 @@ add_executable(CodingGame
|
|||||||
src/renderer/shader.cpp
|
src/renderer/shader.cpp
|
||||||
src/renderer/texture.cpp
|
src/renderer/texture.cpp
|
||||||
src/renderer/wavefront.cpp
|
src/renderer/wavefront.cpp
|
||||||
|
src/renderer/engine.cpp
|
||||||
|
|
||||||
|
include/window/event.hpp
|
||||||
|
src/window/window.cpp
|
||||||
|
|
||||||
src/main.cpp
|
src/main.cpp
|
||||||
)
|
)
|
||||||
|
30
include/renderer/engine.h
Normal file
30
include/renderer/engine.h
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
#ifndef ENGINE_H_
|
||||||
|
#define ENGINE_H_
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
|
#include "window/window.h"
|
||||||
|
#include "window/events/window.h"
|
||||||
|
|
||||||
|
class Engine {
|
||||||
|
private:
|
||||||
|
std::unique_ptr<Window> m_window;
|
||||||
|
bool m_isRunning;
|
||||||
|
private:
|
||||||
|
glm::mat4 m_projection;
|
||||||
|
public:
|
||||||
|
Engine();
|
||||||
|
~Engine();
|
||||||
|
private:
|
||||||
|
void Stop();
|
||||||
|
void Destroy() const;
|
||||||
|
[[nodiscard]] bool Running() const;
|
||||||
|
private:
|
||||||
|
void HandleWindowResized(const WindowResized& event);
|
||||||
|
public:
|
||||||
|
void Run();
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif // ENGINE_H_
|
71
include/window/event.hpp
Normal file
71
include/window/event.hpp
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
#ifndef EVENT_H_
|
||||||
|
#define EVENT_H_
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <vector>
|
||||||
|
#include <typeindex>
|
||||||
|
|
||||||
|
class EventBus {
|
||||||
|
using Type = std::type_index;
|
||||||
|
using RawFn = std::function<void(const void*)>;
|
||||||
|
|
||||||
|
struct Slot { std::size_t id; RawFn fn; };
|
||||||
|
|
||||||
|
std::unordered_map<Type, std::vector<Slot>> subs_;
|
||||||
|
std::size_t next_id_ = 1;
|
||||||
|
|
||||||
|
public:
|
||||||
|
struct Handle {
|
||||||
|
std::type_index type{typeid(void)};
|
||||||
|
std::size_t id{0};
|
||||||
|
explicit operator bool() const { return id != 0; }
|
||||||
|
};
|
||||||
|
|
||||||
|
template<class E, class F>
|
||||||
|
Handle subscribe(F&& f) {
|
||||||
|
auto& vec = subs_[Type(typeid(E))];
|
||||||
|
Handle h{ Type(typeid(E)), next_id_++ };
|
||||||
|
// Wrap strongly typed callback into type-erased RawFn
|
||||||
|
RawFn wrapper = [fn = std::function<void(const E&)>(std::forward<F>(f))](const void* p){
|
||||||
|
fn(*static_cast<const E*>(p));
|
||||||
|
};
|
||||||
|
vec.push_back(Slot{h.id, std::move(wrapper)});
|
||||||
|
return h;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unsubscribe with handle
|
||||||
|
void unsubscribe(const Handle& h) {
|
||||||
|
auto it = subs_.find(h.type);
|
||||||
|
if (it == subs_.end()) return;
|
||||||
|
auto& vec = it->second;
|
||||||
|
vec.erase(std::remove_if(vec.begin(), vec.end(),
|
||||||
|
[&](const Slot& s){ return s.id == h.id; }),
|
||||||
|
vec.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Publish immediately
|
||||||
|
template<class E>
|
||||||
|
void publish(const E& e) const {
|
||||||
|
auto it = subs_.find(Type(typeid(E)));
|
||||||
|
if (it == subs_.end()) return;
|
||||||
|
for (auto& slot : it->second) slot.fn(&e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Optional RAII helper
|
||||||
|
struct ScopedSub {
|
||||||
|
EventBus* bus{};
|
||||||
|
EventBus::Handle h{};
|
||||||
|
ScopedSub() = default;
|
||||||
|
ScopedSub(EventBus& b, EventBus::Handle hh) : bus(&b), h(hh) {}
|
||||||
|
ScopedSub(ScopedSub&& o) noexcept { *this = std::move(o); }
|
||||||
|
ScopedSub& operator=(ScopedSub&& o) noexcept {
|
||||||
|
if (this != &o) { reset(); bus = o.bus; h = o.h; o.bus = nullptr; }
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
~ScopedSub(){ reset(); }
|
||||||
|
void reset(){ if (bus && h) bus->unsubscribe(h); bus=nullptr; h={}; }
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // EVENT_H_
|
7
include/window/events/window.h
Normal file
7
include/window/events/window.h
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#ifndef WINDOW_EVENTS_H_
|
||||||
|
#define WINDOW_EVENTS_H_
|
||||||
|
|
||||||
|
struct WindowResized { int w, h; };
|
||||||
|
struct WindowCloseRequested {};
|
||||||
|
|
||||||
|
#endif // WINDOW_EVENTS_H_
|
36
include/window/window.h
Normal file
36
include/window/window.h
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
#ifndef WINDOW_H_
|
||||||
|
#define WINDOW_H_
|
||||||
|
#include <SDL3/SDL.h>
|
||||||
|
|
||||||
|
#include "event.hpp"
|
||||||
|
|
||||||
|
#define ENGINE_GL_MAJOR_VERSION 4
|
||||||
|
#define ENGINE_GL_MINOR_VERSION 6
|
||||||
|
#define ENGINE_GL_MULTISAMPLE_BUFFERS 1
|
||||||
|
#define ENGINE_GL_MULTISAMPLE_SAMPLES 8
|
||||||
|
|
||||||
|
#define DEFAULT_WIDTH 1024
|
||||||
|
#define DEFAULT_HEIGHT 768
|
||||||
|
|
||||||
|
class Window : public EventBus {
|
||||||
|
private:
|
||||||
|
SDL_Window *m_window;
|
||||||
|
SDL_GLContext m_context;
|
||||||
|
|
||||||
|
int m_width;
|
||||||
|
int m_height;
|
||||||
|
public:
|
||||||
|
Window();
|
||||||
|
~Window();
|
||||||
|
public:
|
||||||
|
[[nodiscard]] inline int GetWidth() const { return m_width; }
|
||||||
|
[[nodiscard]] inline int GetHeight() const { return m_height; }
|
||||||
|
public:
|
||||||
|
void ProcessEvents();
|
||||||
|
public:
|
||||||
|
void SwapBuffers() const;
|
||||||
|
public:
|
||||||
|
void Destroy() const;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif //WINDOW_H_
|
230
src/main.cpp
230
src/main.cpp
@ -1,237 +1,13 @@
|
|||||||
#include <iostream>
|
|
||||||
|
|
||||||
// #ifdef WIN32
|
|
||||||
#define _USE_MATH_DEFINES
|
|
||||||
#include <cmath>
|
|
||||||
// #endif
|
|
||||||
|
|
||||||
#ifndef WIN32
|
#ifndef WIN32
|
||||||
#define GLEW_STATIC
|
#define GLEW_STATIC
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <vector>
|
#include "renderer/engine.h"
|
||||||
#include <glm/glm.hpp>
|
|
||||||
#include <glm/ext/quaternion_geometric.hpp>
|
|
||||||
#include <glm/ext/matrix_transform.hpp>
|
|
||||||
#include <glm/ext/matrix_clip_space.hpp>
|
|
||||||
#include <GL/glew.h>
|
|
||||||
#include <SDL3/SDL.h>
|
|
||||||
|
|
||||||
#include "renderer/shader.h"
|
|
||||||
#include "IO/file_manager.h"
|
|
||||||
#include "renderer/debug.h"
|
|
||||||
#include "renderer/wavefront.h"
|
|
||||||
|
|
||||||
#define WIDTH 1024
|
|
||||||
#define HEIGHT 768
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
|
Engine engine;
|
||||||
|
|
||||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
|
engine.Run();
|
||||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 6);
|
|
||||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
|
||||||
|
|
||||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
|
|
||||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8);
|
|
||||||
|
|
||||||
SDL_Window *window = SDL_CreateWindow("OpenGL Test", WIDTH, HEIGHT, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE|SDL_WINDOW_ALWAYS_ON_TOP);
|
|
||||||
|
|
||||||
SDL_SetWindowRelativeMouseMode(window, true);
|
|
||||||
|
|
||||||
SDL_GLContext glcontext = SDL_GL_CreateContext(window);
|
|
||||||
|
|
||||||
glewExperimental = GL_TRUE;
|
|
||||||
if (GLEW_OK != glewInit()) {
|
|
||||||
fprintf(stderr, "Could not initialize GLEW!\n");
|
|
||||||
SDL_GL_DestroyContext(glcontext);
|
|
||||||
SDL_DestroyWindow(window);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::cout << "GL_VENDOR: " << glGetString(GL_VENDOR) << std::endl;
|
|
||||||
std::cout << "GL_RENDERER: " << glGetString(GL_RENDERER) << std::endl;
|
|
||||||
std::cout << "GL_VERSION: " << glGetString(GL_VERSION) << std::endl;
|
|
||||||
|
|
||||||
glEnable(GL_DEBUG_OUTPUT);
|
|
||||||
glEnable(GL_DEPTH_TEST);
|
|
||||||
glDebugMessageCallback(MessageCallback, nullptr);
|
|
||||||
|
|
||||||
Shader simpleShader;
|
|
||||||
simpleShader.init(
|
|
||||||
FileManager::read("./src/shaders/simple.vs"),
|
|
||||||
FileManager::read("./src/shaders/simple.fs")
|
|
||||||
);
|
|
||||||
|
|
||||||
int screenWidth = WIDTH, screenHeight = HEIGHT;
|
|
||||||
|
|
||||||
glm::vec3 cameraPosition(0.f, 0.f, 2.f);
|
|
||||||
// glm::vec3 cameraViewDirection(0.f, 0.f, -1.f);
|
|
||||||
// glm::vec3 lightPosition(1.f, 3.5f, -2.f);
|
|
||||||
glm::vec3 lightPosition(-5.f, 5.f, 5.f);
|
|
||||||
|
|
||||||
glm::mat4 model(1.f);
|
|
||||||
|
|
||||||
glm::mat4 projection = glm::perspective(
|
|
||||||
static_cast<float>(M_PI_2),
|
|
||||||
static_cast<float>(screenWidth) / static_cast<float>(screenHeight),
|
|
||||||
0.01f,
|
|
||||||
100.0f
|
|
||||||
);
|
|
||||||
|
|
||||||
float angle = 3.45f;
|
|
||||||
Uint64 lastTicks = SDL_GetTicks();
|
|
||||||
|
|
||||||
// Object teapot = Object::LoadFile("./assets/kastrula/kastrula.obj");
|
|
||||||
// Object bricks = Object::LoadFile("./assets/bricks/bricks.obj");
|
|
||||||
Object lightSource = Object::LoadFile("./assets/cube.obj");
|
|
||||||
Object target = Object::LoadFile("./assets/monkey.obj");
|
|
||||||
|
|
||||||
bool paused = false;
|
|
||||||
|
|
||||||
float yaw = -90.0f; // looking along -Z initially
|
|
||||||
float pitch = 0.0f; // no vertical tilt
|
|
||||||
|
|
||||||
// FPS tracking
|
|
||||||
Uint64 startTicks = SDL_GetTicks();
|
|
||||||
int frameCount = 0;
|
|
||||||
|
|
||||||
bool quit = false;
|
|
||||||
while (!quit) {
|
|
||||||
Uint64 currentTicks = SDL_GetTicks();
|
|
||||||
float deltaTime = static_cast<float>(currentTicks - lastTicks) / 1000.0f; // seconds
|
|
||||||
|
|
||||||
lastTicks = currentTicks;
|
|
||||||
|
|
||||||
SDL_Event event;
|
|
||||||
while(SDL_PollEvent(&event)) {
|
|
||||||
switch (event.type) {
|
|
||||||
case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
|
|
||||||
case SDL_EVENT_QUIT:
|
|
||||||
quit = true;
|
|
||||||
break;
|
|
||||||
case SDL_EVENT_WINDOW_RESIZED:
|
|
||||||
int width, height;
|
|
||||||
if (SDL_GetWindowSize(window, &width, &height)) {
|
|
||||||
screenWidth = width;
|
|
||||||
screenHeight = height;
|
|
||||||
glViewport(
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
width,
|
|
||||||
height);
|
|
||||||
projection = glm::perspective(
|
|
||||||
(float)M_PI_2,
|
|
||||||
(float)screenWidth / (float)screenHeight,
|
|
||||||
0.01f,
|
|
||||||
100.0f
|
|
||||||
);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default: break;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
float mouseXRel, mouseYRel;
|
|
||||||
SDL_GetRelativeMouseState(&mouseXRel, &mouseYRel);
|
|
||||||
|
|
||||||
float sensitivity = 0.1f; // tweak as needed
|
|
||||||
yaw += mouseXRel * sensitivity;
|
|
||||||
pitch -= mouseYRel * sensitivity; // invert Y for typical FPS control
|
|
||||||
|
|
||||||
// clamp pitch to avoid flipping
|
|
||||||
if (pitch > 89.0f) pitch = 89.0f;
|
|
||||||
if (pitch < -89.0f) pitch = -89.0f;
|
|
||||||
|
|
||||||
// convert to direction vector
|
|
||||||
glm::vec3 cameraViewDirection(0.f, 0.f, -1.f);
|
|
||||||
cameraViewDirection.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
|
|
||||||
cameraViewDirection.y = sin(glm::radians(pitch));
|
|
||||||
cameraViewDirection.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
|
|
||||||
cameraViewDirection = glm::normalize(cameraViewDirection);
|
|
||||||
|
|
||||||
glm::vec3 velocity(0.f);
|
|
||||||
|
|
||||||
const bool* state = SDL_GetKeyboardState(nullptr);
|
|
||||||
|
|
||||||
if (state[SDL_SCANCODE_P]) paused = !paused;
|
|
||||||
|
|
||||||
glm::vec3 front = glm::normalize(glm::vec3(cameraViewDirection.x, 0.f, cameraViewDirection.z));
|
|
||||||
glm::vec3 right = glm::normalize(glm::cross(front, glm::vec3(0.f, 1.f, 0.f)));
|
|
||||||
|
|
||||||
if (state[SDL_SCANCODE_W]) velocity += front;
|
|
||||||
if (state[SDL_SCANCODE_S]) velocity -= front;
|
|
||||||
if (state[SDL_SCANCODE_A]) velocity -= right;
|
|
||||||
if (state[SDL_SCANCODE_D]) velocity += right;
|
|
||||||
if (state[SDL_SCANCODE_SPACE]) velocity.y += 1.f;
|
|
||||||
if (state[SDL_SCANCODE_LSHIFT]) velocity.y -= 1.f;
|
|
||||||
|
|
||||||
cameraPosition += velocity * deltaTime * 2.5f; // speed is e.g. 2.5f
|
|
||||||
|
|
||||||
glm::mat4 view = glm::lookAt(
|
|
||||||
cameraPosition,
|
|
||||||
cameraPosition + cameraViewDirection,
|
|
||||||
glm::vec3(0.f, 1.f, 0.f)
|
|
||||||
);
|
|
||||||
|
|
||||||
// update rotation
|
|
||||||
if (!paused) {
|
|
||||||
angle += glm::radians(45.0f) * deltaTime; // 72° per second
|
|
||||||
if (angle > glm::two_pi<float>()) {
|
|
||||||
angle -= glm::two_pi<float>(); // keep value small
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
glClearColor(0x18/255.0f, 0x18/255.0f, 0x18/255.0f, 1);
|
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
||||||
|
|
||||||
// Triangle render
|
|
||||||
{
|
|
||||||
simpleShader.use();
|
|
||||||
|
|
||||||
simpleShader.setMat4("u_view", view);
|
|
||||||
simpleShader.setMat4("u_projection", projection);
|
|
||||||
|
|
||||||
simpleShader.setVec3("lightColor", glm::vec3(1.0f, 1.0f, 1.0f));
|
|
||||||
simpleShader.setVec3("lightPos", lightPosition);
|
|
||||||
simpleShader.setVec3("viewPos", cameraPosition);
|
|
||||||
|
|
||||||
model = glm::mat4(1.f);
|
|
||||||
model = glm::translate(model, lightPosition);
|
|
||||||
|
|
||||||
simpleShader.setMat4("u_model", model);
|
|
||||||
|
|
||||||
lightSource.Render(simpleShader);
|
|
||||||
|
|
||||||
// lightPosition -= glm::vec3(0.05f, 0.f, 0.f) * deltaTime;
|
|
||||||
|
|
||||||
model = glm::rotate(
|
|
||||||
glm::mat4(1.f),
|
|
||||||
angle,
|
|
||||||
glm::vec3(0.f, -0.5f, 0.0f)
|
|
||||||
) * 0.5f;
|
|
||||||
|
|
||||||
simpleShader.setMat4("u_model", model);
|
|
||||||
|
|
||||||
target.Render(simpleShader);
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_GL_SwapWindow(window);
|
|
||||||
|
|
||||||
frameCount++;
|
|
||||||
currentTicks = SDL_GetTicks();
|
|
||||||
Uint64 elapsed = currentTicks - startTicks;
|
|
||||||
|
|
||||||
if (elapsed >= 1000) { // one second passed
|
|
||||||
double fps = static_cast<double>(frameCount) / (static_cast<double>(elapsed) / 1000.0);
|
|
||||||
std::cout << "FPS: " << fps << std::endl;
|
|
||||||
frameCount = 0;
|
|
||||||
startTicks = currentTicks;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_GL_DestroyContext(glcontext);
|
|
||||||
SDL_DestroyWindow(window);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
195
src/renderer/engine.cpp
Normal file
195
src/renderer/engine.cpp
Normal file
@ -0,0 +1,195 @@
|
|||||||
|
#include "renderer/engine.h"
|
||||||
|
|
||||||
|
#include <corecrt_math_defines.h>
|
||||||
|
#include <GL/glew.h>
|
||||||
|
#include <glm/ext/matrix_clip_space.hpp>
|
||||||
|
#include <glm/ext/matrix_transform.hpp>
|
||||||
|
|
||||||
|
#include "IO/file_manager.h"
|
||||||
|
#include "renderer/shader.h"
|
||||||
|
#include "renderer/wavefront.h"
|
||||||
|
|
||||||
|
Engine::Engine() {
|
||||||
|
m_window = std::make_unique<Window>();
|
||||||
|
m_isRunning = true;
|
||||||
|
m_projection = glm::perspective(
|
||||||
|
static_cast<float>(M_PI_2),
|
||||||
|
static_cast<float>(m_window->GetWidth()) / static_cast<float>(m_window->GetHeight()),
|
||||||
|
0.01f,
|
||||||
|
100.0f
|
||||||
|
);
|
||||||
|
|
||||||
|
m_window->subscribe<WindowResized>([this](const WindowResized& e) {
|
||||||
|
std::cout << "ENGINE: Window just resized" << std::endl;
|
||||||
|
HandleWindowResized(e);
|
||||||
|
});
|
||||||
|
|
||||||
|
m_window->subscribe<WindowCloseRequested>([this](const WindowCloseRequested& e) {
|
||||||
|
std::cout << "ENGINE: Window closed" << std::endl;
|
||||||
|
Stop();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Engine::Running() const {
|
||||||
|
return m_isRunning;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Engine::Stop() {
|
||||||
|
m_isRunning = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Engine::HandleWindowResized(const WindowResized& event) {
|
||||||
|
m_projection = glm::perspective(
|
||||||
|
static_cast<float>(M_PI_2),
|
||||||
|
static_cast<float>(event.w) / static_cast<float>(event.h),
|
||||||
|
0.01f,
|
||||||
|
100.0f
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Engine::Run() {
|
||||||
|
Shader simpleShader;
|
||||||
|
simpleShader.init(
|
||||||
|
FileManager::read("./src/shaders/simple.vs"),
|
||||||
|
FileManager::read("./src/shaders/simple.fs")
|
||||||
|
);
|
||||||
|
|
||||||
|
glm::vec3 cameraPosition(0.f, 0.f, 2.f);
|
||||||
|
// glm::vec3 cameraViewDirection(0.f, 0.f, -1.f);
|
||||||
|
// glm::vec3 lightPosition(1.f, 3.5f, -2.f);
|
||||||
|
glm::vec3 lightPosition(-5.f, 5.f, 5.f);
|
||||||
|
|
||||||
|
glm::mat4 model(1.f);
|
||||||
|
|
||||||
|
float angle = 3.45f;
|
||||||
|
Uint64 lastTicks = SDL_GetTicks();
|
||||||
|
|
||||||
|
Object lightSource = Object::LoadFile("./assets/cube.obj");
|
||||||
|
Object target = Object::LoadFile("./assets/monkey.obj");
|
||||||
|
|
||||||
|
bool paused = false;
|
||||||
|
|
||||||
|
float yaw = -90.0f; // looking along -Z initially
|
||||||
|
float pitch = 0.0f; // no vertical tilt
|
||||||
|
|
||||||
|
// FPS tracking
|
||||||
|
Uint64 startTicks = SDL_GetTicks();
|
||||||
|
int frameCount = 0;
|
||||||
|
|
||||||
|
while (m_isRunning) {
|
||||||
|
m_window->ProcessEvents();
|
||||||
|
|
||||||
|
Uint64 currentTicks = SDL_GetTicks();
|
||||||
|
float deltaTime = static_cast<float>(currentTicks - lastTicks) / 1000.0f; // seconds
|
||||||
|
|
||||||
|
lastTicks = currentTicks;
|
||||||
|
|
||||||
|
float mouseXRel, mouseYRel;
|
||||||
|
SDL_GetRelativeMouseState(&mouseXRel, &mouseYRel);
|
||||||
|
|
||||||
|
float sensitivity = 0.1f; // tweak as needed
|
||||||
|
yaw += mouseXRel * sensitivity;
|
||||||
|
pitch -= mouseYRel * sensitivity; // invert Y for typical FPS control
|
||||||
|
|
||||||
|
// clamp pitch to avoid flipping
|
||||||
|
// if (pitch > 89.0f) pitch = 89.0f;
|
||||||
|
// if (pitch < -89.0f) pitch = -89.0f;
|
||||||
|
pitch = glm::clamp(pitch, -89.0f, 89.0f);
|
||||||
|
|
||||||
|
// convert to direction vector
|
||||||
|
glm::vec3 cameraViewDirection(0.f, 0.f, -1.f);
|
||||||
|
cameraViewDirection.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
|
||||||
|
cameraViewDirection.y = sin(glm::radians(pitch));
|
||||||
|
cameraViewDirection.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
|
||||||
|
cameraViewDirection = glm::normalize(cameraViewDirection);
|
||||||
|
|
||||||
|
glm::vec3 velocity(0.f);
|
||||||
|
|
||||||
|
const bool* state = SDL_GetKeyboardState(nullptr);
|
||||||
|
|
||||||
|
if (state[SDL_SCANCODE_P]) paused = !paused;
|
||||||
|
|
||||||
|
glm::vec3 front = glm::normalize(glm::vec3(cameraViewDirection.x, 0.f, cameraViewDirection.z));
|
||||||
|
glm::vec3 right = glm::normalize(glm::cross(front, glm::vec3(0.f, 1.f, 0.f)));
|
||||||
|
|
||||||
|
if (state[SDL_SCANCODE_W]) velocity += front;
|
||||||
|
if (state[SDL_SCANCODE_S]) velocity -= front;
|
||||||
|
if (state[SDL_SCANCODE_A]) velocity -= right;
|
||||||
|
if (state[SDL_SCANCODE_D]) velocity += right;
|
||||||
|
if (state[SDL_SCANCODE_SPACE]) velocity.y += 1.f;
|
||||||
|
if (state[SDL_SCANCODE_LSHIFT]) velocity.y -= 1.f;
|
||||||
|
|
||||||
|
cameraPosition += velocity * deltaTime * 2.5f; // speed is e.g. 2.5f
|
||||||
|
|
||||||
|
glm::mat4 view = glm::lookAt(
|
||||||
|
cameraPosition,
|
||||||
|
cameraPosition + cameraViewDirection,
|
||||||
|
glm::vec3(0.f, 1.f, 0.f)
|
||||||
|
);
|
||||||
|
|
||||||
|
// update rotation
|
||||||
|
if (!paused) {
|
||||||
|
angle += glm::radians(45.0f) * deltaTime; // 72° per second
|
||||||
|
if (angle > glm::two_pi<float>()) {
|
||||||
|
angle -= glm::two_pi<float>(); // keep value small
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
glClearColor(0x18/255.0f, 0x18/255.0f, 0x18/255.0f, 1);
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
|
// Triangle render
|
||||||
|
{
|
||||||
|
simpleShader.use();
|
||||||
|
|
||||||
|
simpleShader.setMat4("u_view", view);
|
||||||
|
simpleShader.setMat4("u_projection", m_projection);
|
||||||
|
|
||||||
|
simpleShader.setVec3("lightColor", glm::vec3(1.0f, 1.0f, 1.0f));
|
||||||
|
simpleShader.setVec3("lightPos", lightPosition);
|
||||||
|
simpleShader.setVec3("viewPos", cameraPosition);
|
||||||
|
|
||||||
|
model = glm::mat4(1.f);
|
||||||
|
model = glm::translate(model, lightPosition);
|
||||||
|
|
||||||
|
simpleShader.setMat4("u_model", model);
|
||||||
|
|
||||||
|
lightSource.Render(simpleShader);
|
||||||
|
|
||||||
|
// lightPosition -= glm::vec3(0.05f, 0.f, 0.f) * deltaTime;
|
||||||
|
|
||||||
|
model = glm::rotate(
|
||||||
|
glm::mat4(1.f),
|
||||||
|
angle,
|
||||||
|
glm::vec3(0.f, -0.5f, 0.0f)
|
||||||
|
) * 0.5f;
|
||||||
|
|
||||||
|
simpleShader.setMat4("u_model", model);
|
||||||
|
|
||||||
|
target.Render(simpleShader);
|
||||||
|
}
|
||||||
|
|
||||||
|
m_window->SwapBuffers();
|
||||||
|
|
||||||
|
frameCount++;
|
||||||
|
currentTicks = SDL_GetTicks();
|
||||||
|
Uint64 elapsed = currentTicks - startTicks;
|
||||||
|
|
||||||
|
if (elapsed >= 1000) { // one second passed
|
||||||
|
double fps = static_cast<double>(frameCount) / (static_cast<double>(elapsed) / 1000.0);
|
||||||
|
std::cout << "FPS: " << fps << std::endl;
|
||||||
|
frameCount = 0;
|
||||||
|
startTicks = currentTicks;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Engine::~Engine() {
|
||||||
|
Destroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Engine::Destroy() const {
|
||||||
|
m_window->Destroy();
|
||||||
|
}
|
||||||
|
|
114
src/window/window.cpp
Normal file
114
src/window/window.cpp
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
#include <SDL3/SDL.h>
|
||||||
|
|
||||||
|
#include "window/window.h"
|
||||||
|
#include "window/events/window.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <GL/glew.h>
|
||||||
|
|
||||||
|
#include "renderer/debug.h"
|
||||||
|
|
||||||
|
Window::Window() {
|
||||||
|
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
|
||||||
|
|
||||||
|
std::cout << "Setting gl context version " << ENGINE_GL_MAJOR_VERSION << "." << ENGINE_GL_MINOR_VERSION << std::endl;
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, ENGINE_GL_MAJOR_VERSION);
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, ENGINE_GL_MINOR_VERSION);
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
||||||
|
|
||||||
|
std::cout << "Setting gl context multisample " << ENGINE_GL_MULTISAMPLE_BUFFERS
|
||||||
|
<< "buffers " << ENGINE_GL_MULTISAMPLE_SAMPLES << " samples" << std::endl;
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, ENGINE_GL_MULTISAMPLE_BUFFERS);
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, ENGINE_GL_MULTISAMPLE_SAMPLES);
|
||||||
|
|
||||||
|
m_width = DEFAULT_WIDTH;
|
||||||
|
m_height = DEFAULT_HEIGHT;
|
||||||
|
|
||||||
|
std::cout << "Width: " << m_width << std::endl;
|
||||||
|
std::cout << "Height: " << m_height << std::endl;
|
||||||
|
|
||||||
|
m_window = SDL_CreateWindow("OpenGL Test", m_width, m_height, SDL_WINDOW_OPENGL|SDL_WINDOW_ALWAYS_ON_TOP|SDL_WINDOW_RESIZABLE);
|
||||||
|
|
||||||
|
if (!m_window) {
|
||||||
|
std::cerr << "Failed to create window" << std::endl;
|
||||||
|
std::exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_SetWindowRelativeMouseMode(m_window, true);
|
||||||
|
|
||||||
|
m_context = SDL_GL_CreateContext(m_window);
|
||||||
|
|
||||||
|
if (!SDL_GL_MakeCurrent(m_window, m_context)) {
|
||||||
|
std::cerr << "SDL_GL_MakeCurrent failed: " << SDL_GetError() << "\n";
|
||||||
|
SDL_DestroyWindow(m_window);
|
||||||
|
std::exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
glewExperimental = GL_TRUE;
|
||||||
|
if (GLEW_OK != glewInit()) {
|
||||||
|
std::cerr << "Could not initialize GLEW!" << std::endl;
|
||||||
|
SDL_GL_DestroyContext(m_context);
|
||||||
|
SDL_DestroyWindow(m_window);
|
||||||
|
std::exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "GL_VENDOR: " << glGetString(GL_VENDOR) << std::endl;
|
||||||
|
std::cout << "GL_RENDERER: " << glGetString(GL_RENDERER) << std::endl;
|
||||||
|
std::cout << "GL_VERSION: " << glGetString(GL_VERSION) << std::endl;
|
||||||
|
|
||||||
|
glEnable(GL_DEBUG_OUTPUT);
|
||||||
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
glDebugMessageCallback(MessageCallback, nullptr);
|
||||||
|
|
||||||
|
glViewport(0, 0, m_width, m_height);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Window::ProcessEvents() {
|
||||||
|
SDL_Event event;
|
||||||
|
while(SDL_PollEvent(&event)) {
|
||||||
|
switch (event.type) {
|
||||||
|
case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
|
||||||
|
case SDL_EVENT_QUIT:
|
||||||
|
std::cout << "Close requested" << std::endl;
|
||||||
|
publish(WindowCloseRequested());
|
||||||
|
break;
|
||||||
|
case SDL_EVENT_KEY_DOWN:
|
||||||
|
if (event.key.scancode == SDL_SCANCODE_ESCAPE) {
|
||||||
|
std::cout << "Close requested" << std::endl;
|
||||||
|
publish(WindowCloseRequested());
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case SDL_EVENT_WINDOW_RESIZED:
|
||||||
|
int width, height;
|
||||||
|
if (SDL_GetWindowSizeInPixels(m_window, &width, &height)) {
|
||||||
|
std::cout << "Window resized: " << width << ", " << height << std::endl;
|
||||||
|
m_width = width;
|
||||||
|
m_height = height;
|
||||||
|
glViewport(
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
width,
|
||||||
|
height);
|
||||||
|
publish(WindowResized{ m_width, m_height });
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default: break;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Window::SwapBuffers() const {
|
||||||
|
SDL_GL_SwapWindow(m_window);
|
||||||
|
}
|
||||||
|
|
||||||
|
Window::~Window() {
|
||||||
|
Destroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Window::Destroy() const {
|
||||||
|
if (m_context)
|
||||||
|
SDL_GL_DestroyContext(m_context);
|
||||||
|
if (m_window)
|
||||||
|
SDL_DestroyWindow(m_window);
|
||||||
|
}
|
||||||
|
|
Reference in New Issue
Block a user