mirror of
https://github.com/iritmaximus/galaxy-engine.git
synced 2026-10-12 19:13:41 +03:00
changed from bgfx to opengl, glfw and glad
This commit is contained in:
+21
-62
@@ -7,24 +7,21 @@ set(CMAKE_C_STANDARD 99)
|
||||
# creates compile_commands.json
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
|
||||
# Googletest (aka gtest)
|
||||
find_package(OpenGL)
|
||||
|
||||
include(FetchContent)
|
||||
FetchContent_Declare(
|
||||
googletest
|
||||
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
|
||||
)
|
||||
FetchContent_MakeAvailable(googletest)
|
||||
|
||||
FetchContent_Declare(
|
||||
bgfx
|
||||
URL https://github.com/bkaradzic/bgfx.cmake/releases/download/v1.129.8866-492/bgfx.cmake.v1.129.8866-492.zip
|
||||
)
|
||||
FetchContent_Declare(
|
||||
glfw
|
||||
URL https://github.com/glfw/glfw/releases/download/3.4/glfw-3.4.zip
|
||||
GIT_REPOSITORY https://github.com/glfw/glfw.git
|
||||
GIT_TAG 3.4
|
||||
)
|
||||
FetchContent_MakeAvailable(glfw bgfx)
|
||||
|
||||
# uses old version of glad to match with learnopengl.com
|
||||
FetchContent_Declare(
|
||||
glad
|
||||
GIT_REPOSITORY https://github.com/Dav1dde/glad.git
|
||||
GIT_TAG v0.1.36
|
||||
)
|
||||
FetchContent_MakeAvailable(glfw glad)
|
||||
|
||||
set(SOURCES
|
||||
src/galaxy.cpp
|
||||
@@ -34,65 +31,27 @@ set(SOURCES
|
||||
src/gshader.cpp
|
||||
src/vertex.cpp
|
||||
src/todo.cpp
|
||||
${GLAD_OUT_DIR}/src/glad.c
|
||||
)
|
||||
|
||||
# Includes bgfx/src for #include <bgfx_shader.sh> inside shaders
|
||||
set(BGFX_SHADER_INCLUDE_PATH "${BGFX_DIR}/src" CACHE STRING "bgfx include path")
|
||||
set(GALAXY_SHADER_BIN_DIR "${CMAKE_BINARY_DIR}/generated/shaders" CACHE STRING "Shader install location")
|
||||
|
||||
set(SHADER_SOURCES
|
||||
src/shaders/v_simple.sc
|
||||
src/shaders/f_simple.sc
|
||||
)
|
||||
|
||||
bgfx_compile_shaders(
|
||||
TYPE VERTEX
|
||||
SHADERS src/shaders/v_simple.sc
|
||||
VARYING_DEF ${CMAKE_SOURCE_DIR}/src/shaders/varying.def.sc
|
||||
OUTPUT_DIR ${CMAKE_BINARY_DIR}/generated/shaders
|
||||
)
|
||||
|
||||
bgfx_compile_shaders(
|
||||
TYPE FRAGMENT
|
||||
SHADERS src/shaders/f_simple.sc
|
||||
VARYING_DEF ${CMAKE_SOURCE_DIR}/src/shaders/varying.def.sc
|
||||
OUTPUT_DIR ${CMAKE_BINARY_DIR}/generated/shaders
|
||||
)
|
||||
set(GLAD_PROFILE "core" CACHE STRING "glad profile (core or compatibility)")
|
||||
set(GLAD_API "gl=3.3" CACHE STRING "glad api version")
|
||||
|
||||
configure_file(include/env-vars.h.in ${CMAKE_BINARY_DIR}/generated/include/env-vars.h)
|
||||
|
||||
set(INCLUDE_DIRS
|
||||
${BGFX_DIR}/include
|
||||
${BX_DIR}/include
|
||||
${CMAKE_BINARY_DIR}/generated/include # env-vars.h
|
||||
${GLAD_INCLUDE_DIRS} # glad/glad.h
|
||||
)
|
||||
|
||||
add_library(galaxy-engine-lib ${SOURCES} ${SHADER_SOURCES})
|
||||
add_library(galaxy-engine-lib ${SOURCES})
|
||||
add_executable(galaxy-engine src/main.cpp)
|
||||
|
||||
target_include_directories(galaxy-engine PRIVATE include ${INCLUDE_DIRS})
|
||||
target_include_directories(galaxy-engine-lib PRIVATE include ${INCLUDE_DIRS})
|
||||
target_link_libraries(galaxy-engine galaxy-engine-lib bgfx glfw)
|
||||
|
||||
|
||||
|
||||
# Testing
|
||||
enable_testing()
|
||||
|
||||
set(TESTING_SOURCES
|
||||
test/todo.cpp
|
||||
${SOURCES}
|
||||
)
|
||||
|
||||
add_executable(tests ${TESTING_SOURCES})
|
||||
|
||||
target_link_libraries(
|
||||
tests
|
||||
GTest::gtest_main
|
||||
bgfx
|
||||
glfw
|
||||
galaxy-engine
|
||||
galaxy-engine-lib
|
||||
glad
|
||||
glfw
|
||||
OpenGL
|
||||
)
|
||||
|
||||
target_include_directories(tests PRIVATE include ${INCLUDE_DIRS})
|
||||
include(GoogleTest)
|
||||
gtest_discover_tests(tests)
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "gwindow.h"
|
||||
#include "logger.h"
|
||||
#include "vertex.h"
|
||||
|
||||
class GRenderer {
|
||||
public:
|
||||
@@ -16,19 +13,4 @@ public:
|
||||
private:
|
||||
Logger *logger;
|
||||
GWindow *window;
|
||||
|
||||
bgfx::ShaderHandle vsh;
|
||||
bgfx::ShaderHandle fsh;
|
||||
bgfx::ProgramHandle program;
|
||||
|
||||
bgfx::VertexBufferHandle vbh;
|
||||
bgfx::IndexBufferHandle ibh;
|
||||
std::vector<PosColVertex> *vertexes;
|
||||
std::vector<int> *tri_list;
|
||||
|
||||
bgfx::VertexLayout layout;
|
||||
|
||||
void bgfx_init();
|
||||
void shader_init();
|
||||
bgfx::ShaderHandle load_shader(const char *name);
|
||||
};
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#pragma once
|
||||
#include "bgfx/bgfx.h"
|
||||
|
||||
#include "logger.h"
|
||||
|
||||
@@ -9,8 +8,6 @@ public:
|
||||
~GShader() {};
|
||||
void init();
|
||||
|
||||
bgfx::ShaderHandle loadShader(const char *name);
|
||||
|
||||
private:
|
||||
Logger *logger;
|
||||
};
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "bgfx/bgfx.h"
|
||||
#include <cstdint>
|
||||
|
||||
class PosColVertex {
|
||||
@@ -11,7 +10,6 @@ public:
|
||||
};
|
||||
~PosColVertex() {};
|
||||
void init();
|
||||
bgfx::VertexLayout getLayout() { return this->layout; };
|
||||
|
||||
private:
|
||||
float x;
|
||||
@@ -19,6 +17,4 @@ private:
|
||||
float z;
|
||||
|
||||
uint32_t abgr;
|
||||
|
||||
bgfx::VertexLayout layout;
|
||||
};
|
||||
|
||||
+3
-205
@@ -1,12 +1,3 @@
|
||||
#define BX_CONFIG_DEBUG 0
|
||||
#include "bgfx/bgfx.h"
|
||||
#include "bgfx/platform.h"
|
||||
#include "bx/math.h"
|
||||
#include <fstream>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
|
||||
#include "GLFW/glfw3.h"
|
||||
#define GLFW_EXPOSE_NATIVE_X11
|
||||
#include "GLFW/glfw3native.h"
|
||||
@@ -15,213 +6,20 @@
|
||||
#include "grenderer.h"
|
||||
#include "logger.h"
|
||||
|
||||
static PosColVertex st_vertexes[] =
|
||||
{
|
||||
{-1.0f, 1.0f, 1.0f, 0xff000000 },
|
||||
{ 1.0f, 1.0f, 1.0f, 0xff0000ff },
|
||||
{-1.0f, -1.0f, 1.0f, 0xff00ff00 },
|
||||
{ 1.0f, -1.0f, 1.0f, 0xff00ffff },
|
||||
{-1.0f, 1.0f, -1.0f, 0xffff0000 },
|
||||
{ 1.0f, 1.0f, -1.0f, 0xffff00ff },
|
||||
{-1.0f, -1.0f, -1.0f, 0xffffff00 },
|
||||
{ 1.0f, -1.0f, -1.0f, 0xffffffff },
|
||||
};
|
||||
|
||||
static const uint16_t st_tri_list[] =
|
||||
{
|
||||
0, 1, 2,
|
||||
1, 3, 2,
|
||||
4, 6, 5,
|
||||
5, 6, 7,
|
||||
0, 2, 4,
|
||||
4, 2, 6,
|
||||
1, 5, 3,
|
||||
5, 7, 3,
|
||||
0, 4, 1,
|
||||
4, 5, 1,
|
||||
2, 3, 6,
|
||||
6, 3, 7,
|
||||
};
|
||||
|
||||
|
||||
GRenderer::GRenderer(GWindow *w, Logger *l) : window(w), logger(l) {
|
||||
// TODO this is obviously static data
|
||||
static std::vector<PosColVertex> vertexes = {{0.5f, 0.5f, 0.0f, 0xff0000ff},
|
||||
{0.5f, -0.5f, 0.0f, 0xff0000ff},
|
||||
{-0.5f, -0.5f, 0.0f, 0xff00ff00},
|
||||
{-0.5f, 0.5f, 0.0f, 0xff00ff00}};
|
||||
static std::vector<int> tri_list = {0, 1, 3, 1, 2, 3};
|
||||
|
||||
this->vertexes = &vertexes;
|
||||
this->tri_list = &tri_list;
|
||||
}
|
||||
|
||||
GRenderer::~GRenderer() {
|
||||
LOG_DEBUG("Shutting down renderer...");
|
||||
bgfx::shutdown();
|
||||
}
|
||||
|
||||
void GRenderer::init() {
|
||||
LOG_DEBUG("Initializing the GRenderer...");
|
||||
this->bgfx_init();
|
||||
this->shader_init();
|
||||
}
|
||||
|
||||
void GRenderer::bgfx_init() {
|
||||
// Call bgfx::renderFrame before bgfx::init to signal to bgfx not to create a
|
||||
// render thread. Most graphics APIs must be used on the same thread that
|
||||
// created the window.
|
||||
bgfx::renderFrame();
|
||||
|
||||
bgfx::PlatformData pd;
|
||||
pd.ndt = glfwGetX11Display();
|
||||
pd.nwh = (void *)(uintptr_t)glfwGetX11Window(this->window->getWindow());
|
||||
|
||||
bgfx::Init init;
|
||||
init.type = bgfx::RendererType::Count;
|
||||
init.resolution.width = this->window->getWidth();
|
||||
init.resolution.height = this->window->getHeight();
|
||||
init.resolution.reset = BGFX_RESET_VSYNC;
|
||||
init.platformData = pd;
|
||||
|
||||
if (bgfx::init(init) != true) {
|
||||
LOG_ERROR("Failed to initialize bgfx.");
|
||||
glfwTerminate();
|
||||
throw std::runtime_error("Failed to initialize bgfx.");
|
||||
}
|
||||
|
||||
/*
|
||||
bgfx::setDebug(BGFX_DEBUG_TEXT);
|
||||
bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x443355FF, 1.0f,
|
||||
0);
|
||||
bgfx::setViewRect(0, 0, 0, this->window->getWidth(),
|
||||
this->window->getHeight());
|
||||
|
||||
bgfx::touch(0);
|
||||
*/
|
||||
|
||||
// Todo move this to some fancy class
|
||||
static bgfx::VertexLayout layout;
|
||||
this->layout = layout;
|
||||
|
||||
this->layout.begin()
|
||||
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
|
||||
.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
|
||||
.end();
|
||||
}
|
||||
|
||||
void GRenderer::shader_init() {
|
||||
LOG_DEBUG("Loading shaders...");
|
||||
|
||||
LOG_DEBUG("Galaxy shader bin: " GALAXY_SHADER_BIN_DIR);
|
||||
|
||||
const char *path_v = GALAXY_SHADER_BIN_DIR "/glsl/v_simple.sc.bin";
|
||||
const char *path_f = GALAXY_SHADER_BIN_DIR "/glsl/f_simple.sc.bin";
|
||||
|
||||
this->vsh = this->load_shader(path_v);
|
||||
this->fsh = this->load_shader(path_f);
|
||||
|
||||
this->vbh = bgfx::createVertexBuffer(
|
||||
// bgfx::makeRef(this->vertexes, sizeof(*this->vertexes)), this->layout);
|
||||
bgfx::makeRef(&st_vertexes, sizeof(st_vertexes)), this->layout);
|
||||
|
||||
this->ibh = bgfx::createIndexBuffer(
|
||||
// bgfx::makeRef(this->tri_list, sizeof(*this->tri_list)));
|
||||
bgfx::makeRef(&st_tri_list, sizeof(st_tri_list)));
|
||||
|
||||
this->program = bgfx::createProgram(this->vsh, this->fsh, true);
|
||||
|
||||
// Reset window
|
||||
bgfx::reset(this->window->getWidth(), this->window->getHeight(), BGFX_RESET_VSYNC);
|
||||
|
||||
// Enable debug text.
|
||||
bgfx::setDebug(BGFX_DEBUG_TEXT /*| BGFX_DEBUG_STATS*/);
|
||||
|
||||
// Set view rectangle for 0th view
|
||||
bgfx::setViewRect(0, 0, 0, uint16_t(this->window->getWidth()), uint16_t(this->window->getHeight()));
|
||||
|
||||
// Clear the view rect
|
||||
bgfx::setViewClear(0,
|
||||
BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH,
|
||||
0x443355FF, 1.0f, 0);
|
||||
}
|
||||
|
||||
bgfx::ShaderHandle GRenderer::load_shader(const char *name) {
|
||||
char *data = new char[4098];
|
||||
std::ifstream file;
|
||||
size_t file_size;
|
||||
|
||||
file.open(name);
|
||||
if (file.is_open()) {
|
||||
// Get file size
|
||||
file.seekg(0, std::ios::end);
|
||||
file_size = file.tellg();
|
||||
|
||||
file.seekg(0, std::ios::beg);
|
||||
file.read(data, file_size);
|
||||
|
||||
file.close();
|
||||
} else {
|
||||
delete[] data;
|
||||
throw std::runtime_error(std::string("Failed to open shader file: ") +
|
||||
std::string(name));
|
||||
}
|
||||
|
||||
// create bgfx::Memory object and create shader from it
|
||||
const bgfx::Memory *mem = bgfx::copy(data, file_size + 1);
|
||||
mem->data[mem->size - 1] = '\0';
|
||||
bgfx::ShaderHandle handle = bgfx::createShader(mem);
|
||||
bgfx::setName(handle, name);
|
||||
|
||||
delete[] data;
|
||||
return handle;
|
||||
}
|
||||
|
||||
void GRenderer::update() {
|
||||
LOG_DEBUG("Starting rendening update loop...");
|
||||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
const bx::Vec3 at = {0.0f, 0.0f, 0.0f};
|
||||
const bx::Vec3 eye = {0.0f, 0.0f, 10.0f};
|
||||
|
||||
// Set view and projection matrix for view 0.
|
||||
float view[16];
|
||||
bx::mtxLookAt(view, eye, at);
|
||||
|
||||
float proj[16];
|
||||
bx::mtxProj(proj, 60.0f,
|
||||
float(this->window->getWidth()) /
|
||||
float(this->window->getHeight()),
|
||||
0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
|
||||
|
||||
bgfx::setViewTransform(0, view, proj);
|
||||
|
||||
// Set view 0 default viewport.
|
||||
bgfx::setViewRect(0, 0, 0, this->window->getWidth(),
|
||||
this->window->getHeight());
|
||||
|
||||
bgfx::touch(0);
|
||||
|
||||
float mtx[16];
|
||||
bx::mtxRotateY(mtx, 0.0f);
|
||||
|
||||
// position x,y,z
|
||||
mtx[12] = 0.0f;
|
||||
mtx[13] = 0.0f;
|
||||
mtx[14] = 0.0f;
|
||||
|
||||
// Set model matrix for rendering.
|
||||
bgfx::setTransform(mtx);
|
||||
|
||||
// Set vertex and index buffer.
|
||||
bgfx::setVertexBuffer(0, this->vbh);
|
||||
bgfx::setIndexBuffer(this->ibh);
|
||||
|
||||
// Set render states.
|
||||
bgfx::setState(BGFX_STATE_DEFAULT);
|
||||
|
||||
// Submit primitive for rendering to view 0.
|
||||
bgfx::submit(0, this->program);
|
||||
|
||||
LOG_DEBUG("Rendering...");
|
||||
bgfx::frame();
|
||||
glfwSwapBuffers(this->window->getWindow());
|
||||
}
|
||||
|
||||
@@ -1,8 +1,3 @@
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
|
||||
#include "bgfx/bgfx.h"
|
||||
|
||||
#include "logger.h"
|
||||
#include "gshader.h"
|
||||
|
||||
|
||||
+13
-1
@@ -1,3 +1,5 @@
|
||||
#include "glad/glad.h"
|
||||
|
||||
#include "GLFW/glfw3.h"
|
||||
|
||||
#include "gwindow.h"
|
||||
@@ -9,6 +11,7 @@ static void glfw_errorCallback(int error, const char *description) {
|
||||
|
||||
void GWindow::init() {
|
||||
LOG_DEBUG("Initializing the GWindow...");
|
||||
|
||||
glfwSetErrorCallback(glfw_errorCallback);
|
||||
|
||||
glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_X11);
|
||||
@@ -17,7 +20,9 @@ void GWindow::init() {
|
||||
throw std::runtime_error("Failed to initialize GLFW.");
|
||||
}
|
||||
|
||||
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
|
||||
this->window =
|
||||
glfwCreateWindow(this->width, this->height, "Galaxy-engine", NULL, NULL);
|
||||
@@ -27,6 +32,13 @@ void GWindow::init() {
|
||||
glfwTerminate();
|
||||
throw std::runtime_error("Failed to create GLFW window.");
|
||||
}
|
||||
|
||||
glfwMakeContextCurrent(this->window);
|
||||
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||
LOG_ERROR("Failed to initialize GLAD");
|
||||
throw std::runtime_error("Failed to initialize GLAD");
|
||||
}
|
||||
}
|
||||
|
||||
GWindow::~GWindow() {
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
$input v_color0
|
||||
|
||||
#include <bgfx_shader.sh>
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_FragColor = v_color0;
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
$input a_position, a_color0
|
||||
$output v_color0
|
||||
|
||||
#include <bgfx_shader.sh>
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0));
|
||||
v_color0 = a_color0;
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
vec4 v_color0 : COLOR0;
|
||||
|
||||
vec3 a_position : POSITION;
|
||||
vec4 a_color0 : COLOR0;
|
||||
+1
-8
@@ -1,10 +1,3 @@
|
||||
#include "bgfx/bgfx.h"
|
||||
|
||||
#include "vertex.h"
|
||||
|
||||
void PosColVertex::init() {
|
||||
this->layout.begin()
|
||||
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
|
||||
.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
|
||||
.end();
|
||||
}
|
||||
void PosColVertex::init() {}
|
||||
|
||||
Reference in New Issue
Block a user