Compare commits

...

4 Commits

8 changed files with 64 additions and 293 deletions

View File

@ -1,14 +1,29 @@
cmake_minimum_required(VERSION 3.16) cmake_minimum_required(VERSION 3.16)
project(CodingGame LANGUAGES C CXX) project(CodingGame LANGUAGES C CXX)
# --- deps via vcpkg --- if (UNIX)
# (vcpkg installs decide static vs shared; no "SDL3-shared" component needed) include(FetchContent)
FetchContent_Declare(
glm
GIT_REPOSITORY https://github.com/g-truc/glm.git
GIT_TAG bf71a834948186f4097caa076cd2663c69a10e1e #refs/tags/1.0.1
)
FetchContent_MakeAvailable(glm)
find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3-shared)
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
elseif (MSVC) # vcpkg
find_package(SDL3 CONFIG REQUIRED) find_package(SDL3 CONFIG REQUIRED)
find_package(OpenGL REQUIRED) find_package(OpenGL REQUIRED)
find_package(GLEW CONFIG REQUIRED) find_package(GLEW CONFIG REQUIRED)
find_package(glm CONFIG REQUIRED) find_package(glm CONFIG REQUIRED)
endif()
# Add -ggdb to debug builds
# set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -ggdb")
# set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -ggdb")
# --- exe ---
add_executable(CodingGame add_executable(CodingGame
src/prelude.cpp src/prelude.cpp
src/file_manager.cpp src/file_manager.cpp
@ -23,21 +38,20 @@ add_executable(CodingGame
set_property(TARGET CodingGame PROPERTY CXX_STANDARD 17) set_property(TARGET CodingGame PROPERTY CXX_STANDARD 17)
set_property(TARGET CodingGame PROPERTY CXX_STANDARD_REQUIRED ON) set_property(TARGET CodingGame PROPERTY CXX_STANDARD_REQUIRED ON)
file(COPY ${CMAKE_SOURCE_DIR}/src/shaders DESTINATION ${CMAKE_BINARY_DIR}/) file(COPY src/shaders DESTINATION ${CMAKE_BINARY_DIR}/)
target_include_directories(CodingGame PRIVATE target_include_directories(CodingGame PRIVATE
${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/contrib ${CMAKE_SOURCE_DIR}/contrib
) )
target_link_libraries(CodingGame PRIVATE target_link_libraries(CodingGame PRIVATE
glm::glm SDL3::SDL3
OpenGL::GL OpenGL::GL
SDL3::SDL3 # vcpkgs SDL3 target
GLEW::GLEW GLEW::GLEW
glm::glm
) )
# Debug flags per toolchain # Debug flags
if (MSVC) if (MSVC)
target_compile_options(CodingGame PRIVATE $<$<CONFIG:Debug>:/Zi>) target_compile_options(CodingGame PRIVATE $<$<CONFIG:Debug>:/Zi>)
target_link_options(CodingGame PRIVATE $<$<CONFIG:Debug>:/DEBUG:FULL>) target_link_options(CodingGame PRIVATE $<$<CONFIG:Debug>:/DEBUG:FULL>)
@ -45,8 +59,6 @@ else()
target_compile_options(CodingGame PRIVATE $<$<CONFIG:Debug>:-ggdb>) target_compile_options(CodingGame PRIVATE $<$<CONFIG:Debug>:-ggdb>)
endif() endif()
# --- copy runtime DLLs next to the exe on Windows ---
# (CMake 3.21+)
if (WIN32) if (WIN32)
add_custom_command(TARGET CodingGame POST_BUILD add_custom_command(TARGET CodingGame POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different COMMAND ${CMAKE_COMMAND} -E copy_if_different

15
README.md Normal file
View File

@ -0,0 +1,15 @@
# Project Description
This is a basic future game engine for OpenGL 3D rendered games
## Multi-GPU Devices
If you want to use non-primary GPU on your device when launching the game specifically on Linux you should specify additional environment variables before running. For example in my case I have a hybrid gaming laptop with 2 GPUs AMD from CPU and NVIDIA discrete.
The run command in that case would look following:
```console
__NV_PRIME_RENDER_OFFLOAD=1 __GLX_VENDOR_LIBRARY_NAME=nvidia ./build/CodingGame
```

View File

@ -1,25 +0,0 @@
# Setup VCPKG
```console
git clone https://github.com/microsoft/vcpkg C:\vcpkg
C:\vcpkg\bootstrap-vcpkg.bat
# install deps for 64-bit Windows
C:\vcpkg\vcpkg install sdl3 glew glm --triplet x64-windows
```
# Configure
```console
cmake -S . -B build `
-G "Visual Studio 17 2022" -A x64 `
-DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake `
-DVCPKG_TARGET_TRIPLET=x64-windows `
-DCMAKE_BUILD_TYPE=Debug
```
# Build
```console
cmake --build build --config Debug
```

View File

@ -1,6 +1,6 @@
#ifndef PRELUDE_H_ #ifndef PRELUDE_H_
#define PRELUDE_H_ #define PRELUDE_H_
// #define GLEW_STATIC #define GLEW_STATIC
#include <GL/glew.h> #include <GL/glew.h>
#include "SDL3/SDL.h" #include "SDL3/SDL.h"

View File

@ -1,229 +0,0 @@
#include <iostream>
#include <vector>
#include <glm/glm.hpp>
#include <glm/ext/matrix_transform.hpp>
#include <glm/ext/matrix_clip_space.hpp>
#include <GL/glew.h>
#include <SDL3/SDL.h>
#include "shader.h"
#include "file_manager.h"
#include "prelude.h"
#include "block.h"
#include "vertex.h"
#include "model.h"
#define WIDTH 1024
#define HEIGHT 768
int main() {
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
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_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);
}
glEnable(GL_DEBUG_OUTPUT);
glEnable(GL_DEPTH_TEST);
glDebugMessageCallback(MessageCallback, 0);
// brightness multipliers for faces
const float FACE_BRIGHTNESS[6] = {
1.0f, // front
0.7f, // right
0.5f, // back
0.7f, // left
1.2f, // top
0.4f // bottom
};
// position, normal, color
glm::vec4 cubeColor = {1.0f, 0.5f, 0.31f, 1.0f};
std::vector<Point> cubeVerts = {
// front face (z = 0, normal = +Z)
{ {-0.5f, -0.5f, 0.5f}, {0.f, 0.f, 1.f}, cubeColor },
{ {0.5f, -0.5f, 0.5f}, {0.f, 0.f, 1.f}, cubeColor },
{ {0.5f, 0.5f, 0.5f}, {0.f, 0.f, 1.f}, cubeColor },
{ {-0.5f, 0.5f, 0.5f}, {0.f, 0.f, 1.f}, cubeColor },
// back face (z = 1, normal = -Z)
{ {-0.5f, -0.5f, -0.5f}, {0.f, 0.f, -1.f}, cubeColor },
{ {0.5f, -0.5f, -0.5f}, {0.f, 0.f, -1.f}, cubeColor },
{ {0.5f, 0.5f, -0.5f}, {0.f, 0.f, -1.f}, cubeColor },
{ {-0.5f, 0.5f, -0.5f}, {0.f, 0.f, -1.f}, cubeColor },
// left face (x = 0, normal = -X)
{ {-0.5f, -0.5f, -0.5f}, {-1.f, 0.f, 0.f}, cubeColor },
{ {-0.5f, -0.5f, 0.5f}, {-1.f, 0.f, 0.f}, cubeColor },
{ {-0.5f, 0.5f, 0.5f}, {-1.f, 0.f, 0.f}, cubeColor },
{ {-0.5f, 0.5f, -0.5f}, {-1.f, 0.f, 0.f}, cubeColor },
// right face (x = 1, normal = +X)
{ {0.5f, -0.5f, -0.5f}, {1.f, 0.f, 0.f}, cubeColor },
{ {0.5f, -0.5f, 0.5f}, {1.f, 0.f, 0.f}, cubeColor },
{ {0.5f, 0.5f, 0.5f}, {1.f, 0.f, 0.f}, cubeColor },
{ {0.5f, 0.5f, -0.5f}, {1.f, 0.f, 0.f}, cubeColor },
// top face (y = 1, normal = +Y)
{ {-0.5f, 0.5f, 0.5f}, {0.f, 1.f, 0.f}, cubeColor },
{ {0.5f, 0.5f, 0.5f}, {0.f, 1.f, 0.f}, cubeColor },
{ {0.5f, 0.5f, -0.5f}, {0.f, 1.f, 0.f}, cubeColor },
{ {-0.5f, 0.5f, -0.5f}, {0.f, 1.f, 0.f}, cubeColor },
// bottom face (y = 0, normal = -Y)
{ {-0.5f, -0.5f, 0.5f}, {0.f, -1.f, 0.f}, cubeColor },
{ {0.5f, -0.5f, 0.5f}, {0.f, -1.f, 0.f}, cubeColor },
{ {0.5f, -0.5f, -0.5f}, {0.f, -1.f, 0.f}, cubeColor },
{ {-0.5f, -0.5f, -0.5f}, {0.f, -1.f, 0.f}, cubeColor },
};
std::vector<unsigned int> cubeIndices = {
0,1,2, 2,3,0, // front
4,5,6, 6,7,4, // back
8,9,10, 10,11,8, // left
12,13,14, 14,15,12, // right
16,17,18, 18,19,16, // top
20,21,22, 22,23,20 // bottom
};
Vertices vertices;
for (auto &v : cubeVerts) vertices.PushVertex(v);
for (auto i : cubeIndices) vertices.PushIndex(i);
vertices.Upload();
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 = cameraPosition;
glm::mat4 model(1.f);
glm::mat4 view = glm::lookAt(
cameraPosition,
cameraPosition + cameraViewDirection,
glm::vec3(0.f, 1.f, 0.f)
);
glm::mat4 projection = glm::perspective(
(float)M_PI_2,
(float)screenWidth / (float)screenHeight,
0.01f,
100.0f
);
float angle = 3.45f;
Uint64 lastTicks = SDL_GetTicks();
Object cube = Object::LoadFile("./assets/cube.obj");
Object monkey = Object::LoadFile("./assets/monkey.obj");
bool paused = false;
bool quit = false;
while (!quit) {
Uint64 currentTicks = SDL_GetTicks();
float deltaTime = (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)) {
glViewport(
0,
0,
width,
height);
}
break;
case SDL_EVENT_KEY_DOWN:
switch (event.key.key) {
case SDLK_SPACE:
paused = !paused;
break;
// case SDLK_F5:
// reload_shaders(&context);
// break;
default: break;
};
break;
default: break;
};
}
// 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
}
}
// std::cout << "angle = " << angle << std::endl;
glClearColor(0x18/255.0f, 0x18/255.0f, 0x18/255.0f, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Triangle render
{
simpleShader.use();
model = glm::rotate(
glm::mat4(1.f),
angle,
glm::vec3(0.8f, -0.4f, 0.5f)
);
// lightPosition -= glm::vec3(0.05f, 0.f, 0.f) * deltaTime;
simpleShader.setMat4("u_model", model);
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);
simpleShader.setFloat("ambientStrength", 0.2f);
simpleShader.setFloat("specularStrength", 0.5f);
vertices.Draw();
}
SDL_GL_SwapWindow(window);
}
SDL_GL_DestroyContext(glcontext);
SDL_DestroyWindow(window);
return 0;
}

View File

@ -1,6 +1,4 @@
#include <iostream> #include <iostream>
#define _USE_MATH_DEFINES
#include <cmath>
#include <vector> #include <vector>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <glm/ext/quaternion_geometric.hpp> #include <glm/ext/quaternion_geometric.hpp>

View File

@ -215,7 +215,7 @@ Object Object::LoadFile(const std::string& filename) {
iss >> mtlFile; iss >> mtlFile;
std::filesystem::path fullPath = filename; std::filesystem::path fullPath = filename;
std::filesystem::path mtlPath = fullPath.replace_filename(mtlFile); std::filesystem::path mtlPath = fullPath.replace_filename(mtlFile);
obj.LoadMaterials(mtlPath.u8string()); obj.LoadMaterials(mtlPath);
std::cout << "loaded mtl at '" << mtlPath << "' with " std::cout << "loaded mtl at '" << mtlPath << "' with "
<< obj.m_materials.size() << " materials" << std::endl; << obj.m_materials.size() << " materials" << std::endl;
break; break;

View File

@ -6,7 +6,7 @@
#include <errno.h> #include <errno.h>
#include "prelude.h" #include "prelude.h"
// #define GLEW_STATIC #define GLEW_STATIC
#include <GL/glew.h> #include <GL/glew.h>
#define SCREEN_WIDTH 1024 #define SCREEN_WIDTH 1024