Compare commits
3 Commits
67ba331ba5
...
9d5bb51463
Author | SHA1 | Date | |
---|---|---|---|
9d5bb51463 | |||
c5d5536836 | |||
0d147adfe5 |
@ -1,29 +1,67 @@
|
||||
cmake_minimum_required(VERSION 3.11)
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(CodingGame LANGUAGES C CXX)
|
||||
|
||||
include(FetchContent)
|
||||
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)
|
||||
|
||||
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)
|
||||
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")
|
||||
# set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -ggdb")
|
||||
# set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -ggdb")
|
||||
|
||||
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)
|
||||
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
|
||||
)
|
||||
|
||||
set_property(TARGET CodingGame PROPERTY CXX_STANDARD_REQUIRED 17)
|
||||
set_property(TARGET CodingGame PROPERTY CXX_STANDARD 17)
|
||||
set_property(TARGET CodingGame PROPERTY CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
file(COPY src/shaders DESTINATION ${CMAKE_BINARY_DIR}/)
|
||||
|
||||
target_include_directories(CodingGame PRIVATE ${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/contrib)
|
||||
target_link_libraries(CodingGame PRIVATE SDL3::SDL3 OpenGL::GL GLEW::GLEW glm::glm)
|
||||
target_include_directories(CodingGame PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/include
|
||||
${CMAKE_SOURCE_DIR}/contrib
|
||||
)
|
||||
target_link_libraries(CodingGame PRIVATE
|
||||
SDL3::SDL3
|
||||
OpenGL::GL
|
||||
GLEW::GLEW
|
||||
glm::glm
|
||||
)
|
||||
|
||||
# Debug flags
|
||||
if (MSVC)
|
||||
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>)
|
||||
endif()
|
||||
|
||||
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()
|
15
README.md
Normal file
15
README.md
Normal 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
|
||||
```
|
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;
|
||||
}
|
Reference in New Issue
Block a user