repo: implement basic animation
This commit is contained in:
parent
4194d8f492
commit
93431eccf0
|
@ -1,4 +1,4 @@
|
|||
cmake_minimum_required(VERSION 3.30.0)
|
||||
cmake_minimum_required(VERSION 3.25.1)
|
||||
project(eo
|
||||
VERSION 0.1
|
||||
DESCRIPTION "Report to Houston"
|
||||
|
@ -10,7 +10,14 @@ add_subdirectory("thirdparty/glfw.cmake")
|
|||
add_subdirectory("thirdparty/imgui.cmake")
|
||||
add_subdirectory("thirdparty/assimp.cmake")
|
||||
add_subdirectory("thirdparty/glm.cmake")
|
||||
add_executable(eo src/eo.cpp)
|
||||
add_executable(eo
|
||||
src/eo.cpp
|
||||
src/common.cpp
|
||||
src/engine.cpp
|
||||
src/engine/quad.cpp
|
||||
src/engine/object.cpp
|
||||
src/engine/animation.cpp
|
||||
)
|
||||
target_link_libraries(eo bx)
|
||||
target_link_libraries(eo bgfx)
|
||||
target_link_libraries(eo bimg)
|
||||
|
@ -18,6 +25,8 @@ target_link_libraries(eo glfw)
|
|||
target_link_libraries(eo imgui)
|
||||
target_link_libraries(eo assimp)
|
||||
target_link_libraries(eo glm)
|
||||
target_include_directories(eo PRIVATE "include/")
|
||||
|
||||
|
||||
# build shaders into /build/shaders/
|
||||
file(GLOB SHADER_SOURCE_FILES "${PROJECT_SOURCE_DIR}/shaders/*.sc")
|
||||
|
@ -50,3 +59,19 @@ foreach(SHADER_SOURCE ${SHADER_SOURCE_FILES})
|
|||
)
|
||||
endforeach()
|
||||
|
||||
# build textures into /build/textures
|
||||
file(GLOB TEXTURE_SOURCE_FILES "${PROJECT_SOURCE_DIR}/textures/*")
|
||||
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/textures")
|
||||
foreach(TEXTURE_SOURCE ${TEXTURE_SOURCE_FILES})
|
||||
cmake_path(ABSOLUTE_PATH TEXTURE_SOURCE NORMALIZE)
|
||||
cmake_path(GET TEXTURE_SOURCE FILENAME TEXTURE_NAME)
|
||||
cmake_path(GET TEXTURE_SOURCE STEM TEXTURE_STEM)
|
||||
|
||||
set(TEXTURE_COMMAND_ARGUMENTS "-f ${TEXTURE_SOURCE} -o ${CMAKE_CURRENT_BINARY_DIR}/textures/${TEXTURE_STEM}.dds")
|
||||
set(TEXTURE_COMMAND "${PROJECT_SOURCE_DIR}/tools/texturec ${TEXTURE_COMMAND_ARGUMENTS}")
|
||||
|
||||
add_custom_target(${TEXTURE_NAME} ALL
|
||||
COMMAND /bin/bash -c "${TEXTURE_COMMAND}"
|
||||
)
|
||||
endforeach()
|
||||
|
||||
|
|
Binary file not shown.
22
include/common.hpp
Normal file
22
include/common.hpp
Normal file
|
@ -0,0 +1,22 @@
|
|||
#ifndef COMMON_HPP
|
||||
#define COMMON_HPP
|
||||
|
||||
#include <iostream>
|
||||
#include <bgfx/bgfx.h>
|
||||
#include <bx/file.h>
|
||||
|
||||
#define DEFAULT_WIDTH 1280
|
||||
#define DEFAULT_HEIGHT 720
|
||||
|
||||
#define LOG(x) std::cout << "--log: " << x << std::endl;
|
||||
#define ERROR(x) std::cout << "--error: " << x << std::endl;
|
||||
|
||||
#define DEFAULT_VERTEX "shaders/vs_basic.bin"
|
||||
#define DEFAULT_FRAGMENT "shaders/fs_basic.bin"
|
||||
|
||||
bgfx::Memory* load_mem(bx::FileReader* reader, bx::FilePath& filepath);
|
||||
bgfx::ShaderHandle load_shader(bx::FileReader* reader, std::string filename);
|
||||
bgfx::ProgramHandle load_program(std::string vs_name, std::string fs_name);
|
||||
bgfx::TextureHandle load_texture(std::string filename);
|
||||
|
||||
#endif
|
44
include/engine.hpp
Normal file
44
include/engine.hpp
Normal file
|
@ -0,0 +1,44 @@
|
|||
#ifndef ENGINE_HPP
|
||||
#define ENGINE_HPP
|
||||
|
||||
#include <iostream>
|
||||
#include <bx/file.h>
|
||||
#include <bx/bx.h>
|
||||
#include <bgfx/bgfx.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <engine/object.hpp>
|
||||
|
||||
#define CAMERA_WIDTH 50.0f
|
||||
#define CAMERA_NEAR 0.01f
|
||||
#define CAMERA_FAR 100.0f
|
||||
|
||||
class Engine {
|
||||
public:
|
||||
Engine(void);
|
||||
int Init(void);
|
||||
void Start(void);
|
||||
int Update(void);
|
||||
void Shutdown(void);
|
||||
static void GlfwErrorCallback(int error, const char *s);
|
||||
|
||||
void Instantiate(EngineObject* obj);
|
||||
|
||||
unsigned int GetInput(void);
|
||||
GLFWwindow* main_window;
|
||||
int main_view;
|
||||
|
||||
private:
|
||||
int width;
|
||||
int height;
|
||||
std::string title;
|
||||
|
||||
bgfx::ProgramHandle program;
|
||||
bgfx::UniformHandle u_position;
|
||||
bgfx::UniformHandle u_rotation;
|
||||
bgfx::UniformHandle u_scale;
|
||||
bgfx::UniformHandle u_texture_color;
|
||||
|
||||
std::vector<EngineObject*> objs;
|
||||
};
|
||||
|
||||
#endif
|
28
include/engine/animation.hpp
Normal file
28
include/engine/animation.hpp
Normal file
|
@ -0,0 +1,28 @@
|
|||
#ifndef ANIMATION_HPP
|
||||
#define ANIMATION_HPP
|
||||
|
||||
#include <iostream>
|
||||
#include <engine.hpp>
|
||||
#include <engine/object.hpp>
|
||||
|
||||
class Animation {
|
||||
public:
|
||||
Animation(
|
||||
Engine *ref,
|
||||
std::string slide_texture,
|
||||
int cell_width,
|
||||
int cell_height,
|
||||
int cell_num,
|
||||
float speed
|
||||
);
|
||||
void Cycle(float time, EngineObject* o);
|
||||
private:
|
||||
Engine *ref;
|
||||
bgfx::TextureHandle slide_texture;
|
||||
int cell_width;
|
||||
int cell_height;
|
||||
int cell_num;
|
||||
float speed;
|
||||
};
|
||||
|
||||
#endif
|
24
include/engine/object.hpp
Normal file
24
include/engine/object.hpp
Normal file
|
@ -0,0 +1,24 @@
|
|||
#ifndef OBJECT_HPP
|
||||
#define OBJECT_HPP
|
||||
|
||||
#include "bgfx/bgfx.h"
|
||||
#include <glm/glm.hpp>
|
||||
#include <engine/quad.hpp>
|
||||
|
||||
class EngineObject {
|
||||
public:
|
||||
EngineObject(void);
|
||||
EngineObject(std::string texture);
|
||||
|
||||
EngineQuad quad_data;
|
||||
|
||||
glm::vec4 position;
|
||||
glm::vec4 rotation;
|
||||
glm::vec4 scale;
|
||||
|
||||
bgfx::TextureHandle texture;
|
||||
bgfx::VertexBufferHandle vbh;
|
||||
bgfx::IndexBufferHandle ibh;
|
||||
};
|
||||
|
||||
#endif
|
26
include/engine/quad.hpp
Normal file
26
include/engine/quad.hpp
Normal file
|
@ -0,0 +1,26 @@
|
|||
#ifndef QUAD_HPP
|
||||
#define QUAD_HPP
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <bgfx/bgfx.h>
|
||||
|
||||
struct VertexData {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
|
||||
float u;
|
||||
float v;
|
||||
};
|
||||
|
||||
class EngineQuad {
|
||||
public:
|
||||
std::vector<VertexData> vertices;
|
||||
std::vector<uint16_t> indices;
|
||||
bgfx::VertexLayout layout;
|
||||
EngineQuad(void);
|
||||
EngineQuad(std::vector<VertexData> _v, std::vector<uint16_t> _i);
|
||||
};
|
||||
|
||||
#endif
|
7988
include/stb/image.h
Normal file
7988
include/stb/image.h
Normal file
File diff suppressed because it is too large
Load Diff
1
shaders/bgfx_shader.sh
Symbolic link
1
shaders/bgfx_shader.sh
Symbolic link
|
@ -0,0 +1 @@
|
|||
../thirdparty/bgfx.cmake/bgfx/src/bgfx_shader.sh
|
|
@ -1,6 +1,14 @@
|
|||
$input v_color0
|
||||
$input v_texcoord0
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_FragColor = v_color0;
|
||||
uniform sampler2D u_texture_color;
|
||||
|
||||
#include <bgfx_shader.sh>
|
||||
#include <shaderlib.sh>
|
||||
|
||||
void main() {
|
||||
vec4 color = texture2D(u_texture_color, v_texcoord0);
|
||||
if (color.r == 0 && color.g == 0 && color.b == 0) {
|
||||
return;
|
||||
}
|
||||
gl_FragColor = color;
|
||||
}
|
||||
|
|
1
shaders/shaderlib.sh
Symbolic link
1
shaders/shaderlib.sh
Symbolic link
|
@ -0,0 +1 @@
|
|||
../thirdparty/bgfx.cmake/bgfx/examples/common/shaderlib.sh
|
|
@ -1,4 +1,5 @@
|
|||
vec2 a_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
|
||||
vec3 a_position : POSITION;
|
||||
vec4 a_color0 : COLOR0;
|
||||
|
||||
vec4 v_color0 : COLOR0 = vec4(1.0, 0.0, 0.0, 1.0);
|
||||
vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
|
||||
|
||||
|
|
|
@ -1,8 +1,17 @@
|
|||
$input a_position, a_color0
|
||||
$output v_color0
|
||||
$input a_position, a_texcoord0
|
||||
$output v_texcoord0
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = vec4(a_position, 1.0);
|
||||
v_color0 = a_color0;
|
||||
uniform vec4 u_position;
|
||||
uniform vec4 u_rotation;
|
||||
uniform vec4 u_scale;
|
||||
|
||||
#include <bgfx_shader.sh>
|
||||
|
||||
void main() {
|
||||
vec4 scale = mul(u_scale, vec4(a_position, 1.0f));
|
||||
vec4 view = mul(u_view, scale);
|
||||
vec4 pos = view + u_position;
|
||||
|
||||
gl_Position = mul(u_proj, pos);
|
||||
v_texcoord0 = a_texcoord0;
|
||||
}
|
||||
|
|
52
src/common.cpp
Normal file
52
src/common.cpp
Normal file
|
@ -0,0 +1,52 @@
|
|||
#include "bgfx/defines.h"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <bx/filepath.h>
|
||||
#include <bx/file.h>
|
||||
#include <bx/string.h>
|
||||
#include <bgfx/bgfx.h>
|
||||
#include <common.hpp>
|
||||
|
||||
bx::DefaultAllocator default_allocator;
|
||||
bx::FileReader* reader = BX_NEW(&default_allocator, bx::FileReader);
|
||||
|
||||
bgfx::Memory* load_mem(bx::FileReader* reader, bx::FilePath& filepath) {
|
||||
if (!bx::open(reader, filepath)) {
|
||||
ERROR("--error: failed to load " + std::string(filepath.getCPtr()));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uint32_t size = (uint32_t)bx::getSize(reader);
|
||||
const bgfx::Memory* mem = bgfx::alloc(size+1);
|
||||
|
||||
bx::read(reader, mem->data, size, bx::ErrorAssert{});
|
||||
bx::close(reader);
|
||||
|
||||
mem->data[mem->size-1] = '\0';
|
||||
|
||||
return (bgfx::Memory *) mem;
|
||||
}
|
||||
|
||||
bgfx::ShaderHandle load_shader(bx::FileReader* reader, std::string filename) {
|
||||
bx::FilePath filepath(filename.c_str());
|
||||
bgfx::ShaderHandle handle = bgfx::createShader(
|
||||
load_mem(reader, filepath));
|
||||
|
||||
LOG("loading shader: " + filename);
|
||||
|
||||
bgfx::setName(handle, filename.c_str(), filename.length());
|
||||
return handle;
|
||||
}
|
||||
|
||||
bgfx::ProgramHandle load_program(std::string vs_name,
|
||||
std::string fs_name) {
|
||||
bgfx::ShaderHandle vsh = load_shader(reader, vs_name);
|
||||
bgfx::ShaderHandle fsh = load_shader(reader, fs_name);
|
||||
|
||||
return bgfx::createProgram(vsh, fsh, true);
|
||||
}
|
||||
|
||||
bgfx::TextureHandle load_texture(std::string filename) {
|
||||
bx::FilePath filepath(filename.c_str());
|
||||
return bgfx::createTexture(load_mem(reader, filepath), BGFX_TEXTURE_BLIT_DST|BGFX_SAMPLER_POINT);
|
||||
}
|
180
src/engine.cpp
Normal file
180
src/engine.cpp
Normal file
|
@ -0,0 +1,180 @@
|
|||
#include "bgfx/defines.h"
|
||||
#include "engine/object.hpp"
|
||||
#include <bx/file.h>
|
||||
#include <bx/math.h>
|
||||
#include <glm/fwd.hpp>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <engine.hpp>
|
||||
#include <bgfx/bgfx.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <common.hpp>
|
||||
#include <GLFW/glfw3.h>
|
||||
#define GLFW_EXPOSE_NATIVE_X11
|
||||
#include <GLFW/glfw3native.h>
|
||||
#include <stb/image.h>
|
||||
#include <engine/quad.hpp>
|
||||
|
||||
Engine::Engine(void) {
|
||||
this->width = DEFAULT_WIDTH;
|
||||
this->height = DEFAULT_HEIGHT;
|
||||
this->title = "EO";
|
||||
|
||||
this->main_view = 0;
|
||||
}
|
||||
|
||||
int Engine::Init(void) {
|
||||
int ret = 0;
|
||||
glfwSetErrorCallback(Engine::GlfwErrorCallback);
|
||||
ret = glfwInit();
|
||||
if (ret < 0) {
|
||||
ERROR("failed initializing glfw");
|
||||
return -1;
|
||||
}
|
||||
|
||||
this->main_window = glfwCreateWindow(this->width, this->height, this->title.c_str(), NULL, NULL);
|
||||
if (!this->main_window) {
|
||||
ERROR("failed creating window");
|
||||
return -1;
|
||||
}
|
||||
|
||||
bgfx::Init init;
|
||||
init.platformData.ndt = glfwGetX11Display();
|
||||
init.platformData.nwh = (void*) (uintptr_t) glfwGetX11Window(this->main_window);
|
||||
|
||||
glfwGetWindowSize(this->main_window, &this->width, &this->height);
|
||||
init.resolution.width = this->width;
|
||||
init.resolution.height = this->height;
|
||||
init.resolution.reset = BGFX_RESET_VSYNC;
|
||||
if (!bgfx::init(init)) {
|
||||
ERROR("failed initializing bgfx");
|
||||
return -1;
|
||||
}
|
||||
|
||||
bgfx::setViewClear(this->main_view,
|
||||
BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH,
|
||||
0x478effff,
|
||||
1.0f,
|
||||
0);
|
||||
|
||||
{
|
||||
bx::Vec3 at = {0.0f, 0.0f, 0.0f};
|
||||
bx::Vec3 eye = {0.0f, 0.0f, -35.0f};
|
||||
|
||||
float view[16];
|
||||
bx::mtxLookAt(view, eye, at);
|
||||
|
||||
float ortho[16];
|
||||
bx::mtxOrtho(ortho,
|
||||
(float) -this->width/(CAMERA_WIDTH*2),
|
||||
(float) this->width/(CAMERA_WIDTH*2),
|
||||
(float) -this->height/(CAMERA_WIDTH*2),
|
||||
(float) this->height/(CAMERA_WIDTH*2),
|
||||
CAMERA_NEAR,
|
||||
CAMERA_FAR,
|
||||
0.0f,
|
||||
bgfx::getCaps()->homogeneousDepth
|
||||
);
|
||||
|
||||
bgfx::setViewTransform(this->main_view, view, ortho);
|
||||
}
|
||||
|
||||
bgfx::setViewRect(this->main_view, 0, 0, this->width, this->height);
|
||||
|
||||
this->u_texture_color = bgfx::createUniform("u_texture_color", bgfx::UniformType::Sampler);
|
||||
|
||||
this->u_position = bgfx::createUniform("u_position", bgfx::UniformType::Vec4);
|
||||
this->u_rotation = bgfx::createUniform("u_rotation", bgfx::UniformType::Vec4);
|
||||
this->u_scale = bgfx::createUniform("u_scale", bgfx::UniformType::Vec4);
|
||||
this->program = load_program(DEFAULT_VERTEX, DEFAULT_FRAGMENT);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Engine::Start(void) {
|
||||
}
|
||||
|
||||
int Engine::Update(void) {
|
||||
std::int32_t new_width;
|
||||
std::int32_t new_height;
|
||||
glfwGetFramebufferSize(this->main_window, &new_width, &new_height);
|
||||
if (new_width != this->width || new_height != this->height) {
|
||||
bgfx::reset(new_width, new_height, 0);
|
||||
bgfx::setViewRect(this->main_view, 0, 0, bgfx::BackbufferRatio::Equal);
|
||||
this->width = new_width;
|
||||
this->height = new_height;
|
||||
}
|
||||
|
||||
bgfx::touch(this->main_view);
|
||||
|
||||
for (int i = 0; i < this->objs.size(); i++) {
|
||||
bgfx::setState(
|
||||
BGFX_STATE_WRITE_R |
|
||||
BGFX_STATE_WRITE_G |
|
||||
BGFX_STATE_WRITE_B |
|
||||
BGFX_STATE_WRITE_A |
|
||||
BGFX_STATE_BLEND_ALPHA
|
||||
);
|
||||
|
||||
EngineObject* obj = this->objs[i];
|
||||
|
||||
bgfx::setVertexBuffer(0, obj->vbh);
|
||||
bgfx::setIndexBuffer(obj->ibh);
|
||||
|
||||
bgfx::setUniform(this->u_position, &obj->position);
|
||||
bgfx::setUniform(this->u_rotation, &obj->rotation);
|
||||
bgfx::setUniform(this->u_scale, &obj->scale);
|
||||
|
||||
bgfx::setTexture(0, this->u_texture_color, obj->texture);
|
||||
|
||||
bgfx::submit(this->main_view, this->program);
|
||||
}
|
||||
|
||||
bgfx::frame();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Engine::Shutdown(void) {
|
||||
for (int i = 0; i < this->objs.size(); i++) {
|
||||
EngineObject* obj = this->objs[i];
|
||||
bgfx::destroy(obj->vbh);
|
||||
bgfx::destroy(obj->ibh);
|
||||
}
|
||||
|
||||
bgfx::destroy(this->u_position);
|
||||
bgfx::destroy(this->u_rotation);
|
||||
bgfx::destroy(this->u_scale);
|
||||
bgfx::destroy(this->u_texture_color);
|
||||
|
||||
bgfx::destroy(this->program);
|
||||
|
||||
bgfx::shutdown();
|
||||
glfwTerminate();
|
||||
}
|
||||
|
||||
void Engine::GlfwErrorCallback(int error, const char *s) {
|
||||
ERROR("glfw failed -> " << s);
|
||||
}
|
||||
|
||||
void Engine::Instantiate(EngineObject* obj) {
|
||||
this->objs.push_back(obj);
|
||||
}
|
||||
|
||||
unsigned int Engine::GetInput(void) {
|
||||
unsigned int ret = 0;
|
||||
|
||||
int keys[] = {
|
||||
GLFW_KEY_A,
|
||||
GLFW_KEY_D,
|
||||
GLFW_KEY_W,
|
||||
GLFW_KEY_S,
|
||||
};
|
||||
|
||||
for (int i = sizeof(keys)/sizeof(int); i >= 0; i--) {
|
||||
int state = glfwGetKey(this->main_window, keys[i]);
|
||||
ret = (ret << 1) + (state == GLFW_PRESS ? 1 : 0);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
34
src/engine/animation.cpp
Normal file
34
src/engine/animation.cpp
Normal file
|
@ -0,0 +1,34 @@
|
|||
#include <engine/animation.hpp>
|
||||
#include <engine/object.hpp>
|
||||
#include <common.hpp>
|
||||
|
||||
Animation::Animation(
|
||||
Engine* ref,
|
||||
std::string slide_texture,
|
||||
int cell_width,
|
||||
int cell_height,
|
||||
int cell_num,
|
||||
float speed
|
||||
) {
|
||||
this->ref = ref;
|
||||
this->slide_texture = load_texture(slide_texture);
|
||||
this->cell_width = cell_width;
|
||||
this->cell_height = cell_height;
|
||||
this->cell_num = cell_num;
|
||||
this->speed = speed;
|
||||
}
|
||||
|
||||
void Animation::Cycle(float time, EngineObject *o) {
|
||||
int stage = (int) (time / (this->speed*1000.0f)) % this->cell_num;
|
||||
bgfx::blit(
|
||||
this->ref->main_view,
|
||||
o->texture,
|
||||
0,
|
||||
0,
|
||||
this->slide_texture,
|
||||
stage*cell_width,
|
||||
0,
|
||||
this->cell_width,
|
||||
this->cell_height
|
||||
);
|
||||
}
|
35
src/engine/object.cpp
Normal file
35
src/engine/object.cpp
Normal file
|
@ -0,0 +1,35 @@
|
|||
#include "common.hpp"
|
||||
#include <engine/object.hpp>
|
||||
#include <bgfx/bgfx.h>
|
||||
|
||||
EngineObject::EngineObject(void) : quad_data() {
|
||||
this->position = glm::vec4(0.0f);
|
||||
this->rotation = glm::vec4(0.0f);
|
||||
this->scale = glm::vec4(1.0f);
|
||||
|
||||
this->vbh = bgfx::createVertexBuffer(bgfx::makeRef(
|
||||
this->quad_data.vertices.data(),
|
||||
this->quad_data.vertices.size()*sizeof(VertexData)),
|
||||
this->quad_data.layout);
|
||||
this->ibh = bgfx::createIndexBuffer(bgfx::makeRef(
|
||||
this->quad_data.indices.data(),
|
||||
this->quad_data.indices.size()*sizeof(uint16_t))
|
||||
);
|
||||
}
|
||||
|
||||
EngineObject::EngineObject(std::string texture) : quad_data() {
|
||||
this->position = glm::vec4(0.0f);
|
||||
this->rotation = glm::vec4(0.0f);
|
||||
this->scale = glm::vec4(1.0f);
|
||||
|
||||
this->vbh = bgfx::createVertexBuffer(bgfx::makeRef(
|
||||
this->quad_data.vertices.data(),
|
||||
this->quad_data.vertices.size()*sizeof(VertexData)),
|
||||
this->quad_data.layout);
|
||||
this->ibh = bgfx::createIndexBuffer(bgfx::makeRef(
|
||||
this->quad_data.indices.data(),
|
||||
this->quad_data.indices.size()*sizeof(uint16_t))
|
||||
);
|
||||
|
||||
this->texture = load_texture(texture);
|
||||
}
|
26
src/engine/quad.cpp
Normal file
26
src/engine/quad.cpp
Normal file
|
@ -0,0 +1,26 @@
|
|||
#include <engine/quad.hpp>
|
||||
#include <bgfx/bgfx.h>
|
||||
|
||||
EngineQuad::EngineQuad(void) {
|
||||
this->vertices = {
|
||||
{-0.5f, -0.5f, 0.0f, 0, 1},
|
||||
{-0.5f, 0.5f, 0.0f, 0, 0},
|
||||
{0.5f, -0.5f, 0.0f, 1, 1},
|
||||
{0.5f, 0.5f, 0.0f, 1, 0}
|
||||
};
|
||||
this->indices = {0, 1, 2, 1, 2, 3};
|
||||
this->layout.begin()
|
||||
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
|
||||
.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
|
||||
.end();
|
||||
}
|
||||
|
||||
EngineQuad::EngineQuad(std::vector<VertexData> _v, std::vector<uint16_t> _i) {
|
||||
this->vertices = _v;
|
||||
this->indices = _i;
|
||||
this->layout.begin()
|
||||
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
|
||||
.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
|
||||
.end();
|
||||
}
|
||||
|
194
src/eo.cpp
194
src/eo.cpp
|
@ -1,173 +1,55 @@
|
|||
#include "bx/allocator.h"
|
||||
#include "bx/file.h"
|
||||
#include "bx/filepath.h"
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <GLFW/glfw3.h>
|
||||
#define GLFW_EXPOSE_NATIVE_X11
|
||||
#include "GLFW/glfw3native.h"
|
||||
#include <bx/bx.h>
|
||||
#include <bgfx/bgfx.h>
|
||||
#include <bgfx/platform.h>
|
||||
#include <bgfx/defines.h>
|
||||
#include <assimp/Importer.hpp>
|
||||
#include <assimp/scene.h>
|
||||
#include <assimp/postprocess.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <common.hpp>
|
||||
#include <engine.hpp>
|
||||
#include <engine/animation.hpp>
|
||||
#include <chrono>
|
||||
#include <ratio>
|
||||
|
||||
#define DEFAULT_WIDTH 1280
|
||||
#define DEFAULT_HEIGHT 720
|
||||
|
||||
int width = DEFAULT_WIDTH;
|
||||
int height = DEFAULT_HEIGHT;
|
||||
bx::DefaultAllocator g_allocator;
|
||||
bgfx::ProgramHandle program;
|
||||
|
||||
struct ColorVertex {
|
||||
glm::vec3 pos;
|
||||
uint32_t col;
|
||||
};
|
||||
|
||||
|
||||
void glfw_error_callback(int error, const char *desc) {
|
||||
std::cout << "--error: " << desc << std::endl;
|
||||
}
|
||||
|
||||
bool load_model(std::string path) {
|
||||
Assimp::Importer importer;
|
||||
|
||||
const aiScene *scene = importer.ReadFile(path,
|
||||
aiProcess_CalcTangentSpace | aiProcess_Triangulate |
|
||||
aiProcess_JoinIdenticalVertices | aiProcess_SortByPType);
|
||||
if (scene == nullptr) {
|
||||
std::cout << "--error: failed loading model" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
const bgfx::Memory* load_mem(bx::FileReader* reader, bx::FilePath& filepath) {
|
||||
if (bx::open(reader, filepath) )
|
||||
{
|
||||
uint32_t size = (uint32_t)bx::getSize(reader);
|
||||
const bgfx::Memory* mem = bgfx::alloc(size+1);
|
||||
bx::read(reader, mem->data, size, bx::ErrorAssert{});
|
||||
bx::close(reader);
|
||||
mem->data[mem->size-1] = '\0';
|
||||
return mem;
|
||||
}
|
||||
|
||||
std::cout << "--error: failed to load " << filepath.getCPtr() << std::endl;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bgfx::ShaderHandle load_shader(bx::FileReader* reader, const char *filename) {
|
||||
bx::FilePath filepath(filename);
|
||||
|
||||
bgfx::ShaderHandle handle = bgfx::createShader(load_mem(reader, filepath));
|
||||
|
||||
fprintf(stdout, "loading shader: %s %d\n", filename, (int) strlen(filename));
|
||||
bgfx::setName(handle, filename, strlen(filename));
|
||||
|
||||
return handle;
|
||||
}
|
||||
|
||||
bgfx::ProgramHandle load_program(bx::FileReader *reader, const char *vs_name, const char *fs_name) {
|
||||
bgfx::ShaderHandle vsh = load_shader(reader, vs_name);
|
||||
bgfx::ShaderHandle fsh = load_shader(reader, fs_name);
|
||||
|
||||
return bgfx::createProgram(vsh, fsh, true);
|
||||
}
|
||||
#define GET_MS \
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>( \
|
||||
std::chrono::system_clock::now().time_since_epoch() \
|
||||
);
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
glfwSetErrorCallback(glfw_error_callback);
|
||||
int ret = glfwInit();
|
||||
Engine engine;
|
||||
|
||||
int ret = engine.Init();
|
||||
if (ret < 0) {
|
||||
std::cout << "--error: failed initializing glfw" << std::endl;
|
||||
return -1;
|
||||
ERROR("failed initializing engine");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
// glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
||||
Animation idle_animation(&engine, "textures/idle.dds", 32, 32, 4, 0.2f);
|
||||
Animation run_animation(&engine, "textures/run.dds", 32, 32, 8, 0.2f);
|
||||
Animation* active_animation;
|
||||
|
||||
GLFWwindow *w = glfwCreateWindow(800, 600, "eo", NULL, NULL);
|
||||
if (!w) {
|
||||
std::cout << "--error: failed creating window" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
EngineObject* character = new EngineObject("textures/knight.dds");
|
||||
character->scale.x = 2.5f;
|
||||
character->scale.y = 2.5f;
|
||||
engine.Instantiate(character);
|
||||
|
||||
bx::AllocatorI* allocator = &g_allocator;
|
||||
bx::FileReader* _reader = BX_NEW(allocator, bx::FileReader);
|
||||
engine.Start();
|
||||
|
||||
bgfx::Init init;
|
||||
init.platformData.ndt = glfwGetX11Display();
|
||||
init.platformData.nwh = (void *)(uintptr_t) glfwGetX11Window(w);
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
glfwGetWindowSize(w, &width, &height);
|
||||
init.resolution.width = width;
|
||||
init.resolution.height = height;
|
||||
init.resolution.reset = BGFX_RESET_VSYNC;
|
||||
if (!bgfx::init(init)) {
|
||||
std::cout << "--error: failed initializing bgfx" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
bgfx::ViewId clear_view = 0;
|
||||
bgfx::setViewClear(clear_view, BGFX_CLEAR_COLOR, 0x000000FF);
|
||||
bgfx::setViewRect(clear_view, 0, 0, width, height);
|
||||
|
||||
ColorVertex kTriangleVertices[] = {
|
||||
{{-0.5f, -0.5f, 0.0f}, 0x339933FF},
|
||||
{{0.5f, -0.5f, 0.0f}, 0x993333FF},
|
||||
{{0.0f, 0.5f, 0.0f}, 0x333399FF},
|
||||
};
|
||||
|
||||
const uint16_t kTriangleIndices[] = {
|
||||
0, 1, 2,
|
||||
};
|
||||
|
||||
bgfx::VertexLayout layout;
|
||||
layout.begin()
|
||||
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
|
||||
.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
|
||||
.end();
|
||||
|
||||
bgfx::VertexBufferHandle vertex_buffer = bgfx::createVertexBuffer(bgfx::makeRef(kTriangleVertices, sizeof(kTriangleVertices)), layout);
|
||||
bgfx::IndexBufferHandle index_buffer = bgfx::createIndexBuffer(bgfx::makeRef(kTriangleIndices, sizeof(kTriangleIndices)));
|
||||
|
||||
program = load_program(_reader, "shaders/vs_basic.bin", "shaders/fs_basic.bin");
|
||||
|
||||
while (!glfwWindowShouldClose(w)) {
|
||||
std::chrono::milliseconds ms = GET_MS;
|
||||
int delta_time = 0;
|
||||
int time = 0;
|
||||
while (!glfwWindowShouldClose(engine.main_window)) {
|
||||
glfwPollEvents();
|
||||
|
||||
std::int32_t display_w, display_h;
|
||||
glfwGetFramebufferSize(w, &display_w, &display_h);
|
||||
if (display_w != width || display_h != height) {
|
||||
bgfx::reset(display_w,display_h, BGFX_RESET_VSYNC);
|
||||
bgfx::setViewRect(clear_view, 0, 0, bgfx::BackbufferRatio::Equal);
|
||||
width = display_w;
|
||||
height = display_h;
|
||||
}
|
||||
unsigned int input = engine.GetInput();
|
||||
active_animation = input > 0 ? &run_animation : &idle_animation;
|
||||
active_animation->Cycle(time, character);
|
||||
|
||||
bgfx::touch(clear_view);
|
||||
bgfx::setState(
|
||||
BGFX_STATE_WRITE_R
|
||||
| BGFX_STATE_WRITE_G
|
||||
| BGFX_STATE_WRITE_B
|
||||
| BGFX_STATE_WRITE_A
|
||||
);
|
||||
bgfx::setVertexBuffer(0, vertex_buffer);
|
||||
bgfx::setIndexBuffer(index_buffer);
|
||||
bgfx::submit(clear_view, program);
|
||||
bgfx::frame();
|
||||
std::chrono::milliseconds tmp = GET_MS;
|
||||
delta_time = tmp.count() - ms.count();
|
||||
time += delta_time;
|
||||
ms = tmp;
|
||||
engine.Update();
|
||||
}
|
||||
engine.Shutdown();
|
||||
|
||||
bgfx::shutdown();
|
||||
glfwTerminate();
|
||||
delete(character);
|
||||
|
||||
return 0;
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
BIN
textures/idle.png
Normal file
BIN
textures/idle.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1021 B |
BIN
textures/knight.png
Normal file
BIN
textures/knight.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 788 B |
BIN
textures/run.png
Normal file
BIN
textures/run.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.1 KiB |
|
@ -1 +0,0 @@
|
|||
../build/thirdparty/bgfx.cmake/cmake/bgfx/shaderc
|
BIN
tools/shaderc
Executable file
BIN
tools/shaderc
Executable file
Binary file not shown.
BIN
tools/texturec
Executable file
BIN
tools/texturec
Executable file
Binary file not shown.
Loading…
Reference in New Issue
Block a user