Compare commits
10 Commits
windows
...
69dbe5ae2f
Author | SHA1 | Date | |
---|---|---|---|
69dbe5ae2f | |||
de6496ff81 | |||
807e0ce9d9 | |||
58e25b530b | |||
39f528d7ad | |||
6dc269ce13 | |||
9d5bb51463 | |||
c5d5536836 | |||
0d147adfe5 | |||
67ba331ba5 |
@ -1,55 +1,67 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(CodingGame LANGUAGES C CXX)
|
||||
|
||||
# --- deps via vcpkg ---
|
||||
# (vcpkg installs decide static vs shared; no "SDL3-shared" component needed)
|
||||
find_package(SDL3 CONFIG REQUIRED)
|
||||
find_package(OpenGL REQUIRED)
|
||||
find_package(GLEW CONFIG REQUIRED)
|
||||
find_package(glm CONFIG REQUIRED)
|
||||
if (UNIX)
|
||||
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(OpenGL REQUIRED)
|
||||
find_package(GLEW 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
|
||||
src/prelude.cpp
|
||||
src/file_manager.cpp
|
||||
src/shader.cpp
|
||||
src/block.cpp
|
||||
src/vertex.cpp
|
||||
src/texture.cpp
|
||||
src/model.cpp
|
||||
src/main.cpp
|
||||
src/prelude.cpp
|
||||
src/file_manager.cpp
|
||||
src/shader.cpp
|
||||
src/block.cpp
|
||||
src/vertex.cpp
|
||||
src/texture.cpp
|
||||
src/model.cpp
|
||||
src/main.cpp
|
||||
)
|
||||
|
||||
set_property(TARGET CodingGame PROPERTY CXX_STANDARD 17)
|
||||
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
|
||||
${CMAKE_SOURCE_DIR}/include
|
||||
${CMAKE_SOURCE_DIR}/contrib
|
||||
${CMAKE_SOURCE_DIR}/include
|
||||
${CMAKE_SOURCE_DIR}/contrib
|
||||
)
|
||||
|
||||
target_link_libraries(CodingGame PRIVATE
|
||||
glm::glm
|
||||
OpenGL::GL
|
||||
SDL3::SDL3 # vcpkg’s SDL3 target
|
||||
GLEW::GLEW
|
||||
SDL3::SDL3
|
||||
OpenGL::GL
|
||||
GLEW::GLEW
|
||||
glm::glm
|
||||
)
|
||||
|
||||
# Debug flags per toolchain
|
||||
# Debug flags
|
||||
if (MSVC)
|
||||
target_compile_options(CodingGame PRIVATE $<$<CONFIG:Debug>:/Zi>)
|
||||
target_link_options(CodingGame PRIVATE $<$<CONFIG:Debug>:/DEBUG:FULL>)
|
||||
target_compile_options(CodingGame PRIVATE $<$<CONFIG:Debug>:/Zi>)
|
||||
target_link_options(CodingGame PRIVATE $<$<CONFIG:Debug>:/DEBUG:FULL>)
|
||||
else()
|
||||
target_compile_options(CodingGame PRIVATE $<$<CONFIG:Debug>:-ggdb>)
|
||||
target_compile_options(CodingGame PRIVATE $<$<CONFIG:Debug>:-ggdb>)
|
||||
endif()
|
||||
|
||||
# --- copy runtime DLLs next to the exe on Windows ---
|
||||
# (CMake 3.21+)
|
||||
if (WIN32)
|
||||
add_custom_command(TARGET CodingGame POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||
$<TARGET_RUNTIME_DLLS:CodingGame> $<TARGET_FILE_DIR:CodingGame>
|
||||
COMMAND_EXPAND_LISTS)
|
||||
endif()
|
||||
add_custom_command(TARGET CodingGame POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||
$<TARGET_RUNTIME_DLLS:CodingGame> $<TARGET_FILE_DIR:CodingGame>
|
||||
COMMAND_EXPAND_LISTS)
|
||||
endif()
|
29
README.md
Normal file
29
README.md
Normal file
@ -0,0 +1,29 @@
|
||||
|
||||
|
||||
# Project Description
|
||||
|
||||
This is a basic future game engine for OpenGL 3D rendered games
|
||||
|
||||
## Building on Windows
|
||||
|
||||
In order to configure and run project on windows platform use following commands.
|
||||
|
||||
Configuring:
|
||||
|
||||
```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
|
||||
```
|
||||
|
||||
## 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
|
||||
```
|
25
WINDOWS.md
25
WINDOWS.md
@ -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
|
||||
```
|
@ -2,6 +2,7 @@
|
||||
#define MODEL_H_
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
#include <unordered_map>
|
||||
#include <glm/glm.hpp>
|
||||
#include <memory>
|
||||
@ -120,7 +121,7 @@ public:
|
||||
static Object LoadFile(const std::string& filename);
|
||||
|
||||
private:
|
||||
void LoadMaterials(const std::string& filename);
|
||||
void LoadMaterials(const std::filesystem::path& filename);
|
||||
private:
|
||||
void AddMaterial(std::string name, std::shared_ptr<Material> material);
|
||||
std::shared_ptr<Material> GetMaterial(std::string name);
|
||||
|
@ -1,7 +1,11 @@
|
||||
#ifndef PRELUDE_H_
|
||||
#define PRELUDE_H_
|
||||
// #define GLEW_STATIC
|
||||
|
||||
#ifndef WIN32
|
||||
#define GLEW_STATIC
|
||||
#endif
|
||||
#include <GL/glew.h>
|
||||
|
||||
#include "SDL3/SDL.h"
|
||||
|
||||
struct RenderContext {
|
||||
|
229
src/main.bkp.cpp
229
src/main.bkp.cpp
@ -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;
|
||||
}
|
@ -1,6 +1,10 @@
|
||||
#include <iostream>
|
||||
|
||||
// #ifdef WIN32
|
||||
#define _USE_MATH_DEFINES
|
||||
#include <cmath>
|
||||
// #endif
|
||||
|
||||
#include <vector>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/ext/quaternion_geometric.hpp>
|
||||
|
@ -86,7 +86,7 @@ Object::Object() {
|
||||
m_texCoords = std::vector<glm::vec2>();
|
||||
}
|
||||
|
||||
void Object::LoadMaterials(const std::string& filename) {
|
||||
void Object::LoadMaterials(const std::filesystem::path& filename) {
|
||||
std::ifstream file(filename);
|
||||
|
||||
std::string currentMaterialName;
|
||||
@ -215,7 +215,7 @@ Object Object::LoadFile(const std::string& filename) {
|
||||
iss >> mtlFile;
|
||||
std::filesystem::path fullPath = filename;
|
||||
std::filesystem::path mtlPath = fullPath.replace_filename(mtlFile);
|
||||
obj.LoadMaterials(mtlPath.u8string());
|
||||
obj.LoadMaterials(mtlPath);
|
||||
std::cout << "loaded mtl at '" << mtlPath << "' with "
|
||||
<< obj.m_materials.size() << " materials" << std::endl;
|
||||
break;
|
||||
|
@ -6,7 +6,9 @@
|
||||
#include <errno.h>
|
||||
#include "prelude.h"
|
||||
|
||||
// #define GLEW_STATIC
|
||||
#ifndef WIN32
|
||||
#define GLEW_STATIC
|
||||
#endif
|
||||
#include <GL/glew.h>
|
||||
|
||||
#define SCREEN_WIDTH 1024
|
||||
|
Reference in New Issue
Block a user