2016-09-23 09:06:46 +02:00
|
|
|
#include "RuntimeModule.h"
|
|
|
|
#include "Globals.h"
|
|
|
|
#include "RenderModule.h"
|
2017-07-02 20:18:34 +02:00
|
|
|
|
2016-09-23 09:06:46 +02:00
|
|
|
struct Renderer;
|
|
|
|
|
2018-02-13 12:05:07 +01:00
|
|
|
static const GLfloat g_vertex_buffer_data[] = {
|
|
|
|
-1.0f, -1.0f, 0.0f,
|
|
|
|
1.0f, -1.0f, 0.0f,
|
|
|
|
0.0f, 1.0f, 0.0f
|
|
|
|
};
|
|
|
|
|
2018-02-12 21:35:44 +01:00
|
|
|
//
|
|
|
|
// Module
|
|
|
|
//
|
2016-09-23 09:06:46 +02:00
|
|
|
struct module_state {
|
|
|
|
Renderer *renderer;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct module_state *module_init() {
|
2018-02-12 13:05:19 +01:00
|
|
|
gLog ("%s %s called", __FILE__, __FUNCTION__);
|
2016-09-23 09:06:46 +02:00
|
|
|
assert (gWindow != nullptr && "Cannot initialize renderer module without gWindow!");
|
|
|
|
|
|
|
|
module_state *state = (module_state*) malloc(sizeof(*state));
|
|
|
|
state->renderer = new Renderer();
|
|
|
|
assert (state->renderer != nullptr);
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2017-01-09 21:24:20 +01:00
|
|
|
template <typename Serializer>
|
|
|
|
static void module_serialize (
|
|
|
|
struct module_state *state,
|
|
|
|
Serializer* serializer) {
|
2018-02-12 21:35:44 +01:00
|
|
|
// // get the state from the serializer
|
|
|
|
// Camera* camera = &gRenderer->cameras[gRenderer->activeCameraIndex];
|
|
|
|
// assert (camera != nullptr);
|
2017-01-09 21:24:20 +01:00
|
|
|
|
2018-02-12 21:35:44 +01:00
|
|
|
// SerializeBool (*serializer, "protot.RenderModule.draw_floor", gRenderer->drawFloor);
|
|
|
|
// SerializeBool (*serializer, "protot.RenderModule.draw_skybox", gRenderer->drawSkybox);
|
|
|
|
// SerializeBool (*serializer, "protot.RenderModule.debug_enabled", gRenderer->drawDebug);
|
|
|
|
// SerializeVec3 (*serializer, "protot.RenderModule.camera.eye", camera->eye);
|
|
|
|
// SerializeVec3 (*serializer, "protot.RenderModule.camera.poi", camera->poi);
|
2017-01-09 21:24:20 +01:00
|
|
|
}
|
|
|
|
|
2016-09-23 09:06:46 +02:00
|
|
|
static void module_finalize(struct module_state *state) {
|
2018-02-12 13:05:19 +01:00
|
|
|
gLog ("%s %s called (state %p)", __FILE__, __FUNCTION__, state);
|
2016-09-23 09:06:46 +02:00
|
|
|
|
|
|
|
assert (state->renderer != nullptr);
|
|
|
|
delete state->renderer;
|
|
|
|
|
|
|
|
free(state);
|
|
|
|
}
|
|
|
|
|
2017-01-09 21:24:20 +01:00
|
|
|
static void module_reload(struct module_state *state, void *read_serializer) {
|
2018-02-12 13:05:19 +01:00
|
|
|
gLog ("%s %s called (state %p)", __FILE__, __FUNCTION__, state);
|
2016-09-23 09:06:46 +02:00
|
|
|
assert (gWindow != nullptr);
|
|
|
|
|
2017-02-05 10:36:37 +01:00
|
|
|
gLog ("Renderer initialize");
|
2016-09-23 09:06:46 +02:00
|
|
|
assert (state != nullptr);
|
2018-02-12 21:35:44 +01:00
|
|
|
state->renderer->Initialize(100, 100);
|
2018-02-13 12:05:07 +01:00
|
|
|
|
2016-09-23 09:06:46 +02:00
|
|
|
gRenderer = state->renderer;
|
2016-11-20 21:50:53 +01:00
|
|
|
|
2017-01-09 21:24:20 +01:00
|
|
|
// load the state of the module
|
|
|
|
if (read_serializer != nullptr) {
|
|
|
|
module_serialize(state, static_cast<ReadSerializer*>(read_serializer));
|
|
|
|
}
|
2016-09-23 09:06:46 +02:00
|
|
|
}
|
|
|
|
|
2017-01-09 21:24:20 +01:00
|
|
|
static void module_unload(struct module_state *state, void* write_serializer) {
|
|
|
|
// serialize the state of the module
|
|
|
|
if (write_serializer != nullptr) {
|
|
|
|
module_serialize(state, static_cast<WriteSerializer*>(write_serializer));
|
|
|
|
}
|
2016-11-20 21:50:53 +01:00
|
|
|
|
2016-09-23 09:06:46 +02:00
|
|
|
gRenderer = nullptr;
|
2018-02-12 21:35:44 +01:00
|
|
|
state->renderer->Shutdown();
|
2016-11-20 21:50:53 +01:00
|
|
|
|
2017-02-05 10:36:37 +01:00
|
|
|
gLog ("RenderModule unload called");
|
2016-09-23 09:06:46 +02:00
|
|
|
}
|
|
|
|
|
2016-11-07 21:34:18 +01:00
|
|
|
static bool module_step(struct module_state *state, float dt) {
|
2016-09-23 09:06:46 +02:00
|
|
|
int width, height;
|
|
|
|
assert (gWindow != nullptr);
|
2018-02-12 21:35:44 +01:00
|
|
|
state->renderer->RenderGui();
|
|
|
|
state->renderer->RenderGl();
|
2017-02-18 17:22:21 +01:00
|
|
|
|
2016-09-23 09:06:46 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
const struct module_api MODULE_API = {
|
|
|
|
.init = module_init,
|
|
|
|
.reload = module_reload,
|
|
|
|
.step = module_step,
|
|
|
|
.unload = module_unload,
|
|
|
|
.finalize = module_finalize
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-12-17 23:04:50 +01:00
|
|
|
|
|
|
|
|
2016-09-23 09:06:46 +02:00
|
|
|
//
|
2018-02-12 21:35:44 +01:00
|
|
|
// Camera
|
2016-09-23 09:06:46 +02:00
|
|
|
//
|
|
|
|
void Camera::updateMatrices() {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-12 13:05:19 +01:00
|
|
|
//
|
2018-02-12 21:35:44 +01:00
|
|
|
// Camera
|
|
|
|
//
|
|
|
|
void Renderer::Initialize(int width, int height) {
|
2018-02-13 12:05:07 +01:00
|
|
|
glGenVertexArrays(1, &mMesh.mVertexArrayId);
|
|
|
|
glBindVertexArray(mMesh.mVertexArrayId);
|
|
|
|
glGenBuffers(1, &mMesh.mVertexBuffer);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, mMesh.mVertexBuffer);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
|
|
|
|
|
|
|
|
mProgram = RenderProgram("data/shaders/vs_simple.glsl", "data/shaders/fs_simple.glsl");
|
|
|
|
bool load_result = mProgram.Load();
|
|
|
|
assert(load_result);
|
2016-11-06 22:30:09 +01:00
|
|
|
}
|
|
|
|
|
2018-02-12 21:35:44 +01:00
|
|
|
void Renderer::Shutdown() {
|
2018-02-13 12:05:07 +01:00
|
|
|
glDeleteVertexArrays(1, &mMesh.mVertexArrayId);
|
2016-11-27 22:02:17 +01:00
|
|
|
}
|
2018-02-12 21:35:44 +01:00
|
|
|
void Renderer::RenderGl() {
|
2018-02-13 12:05:07 +01:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
|
|
|
glUseProgram(mProgram.mProgramId);
|
|
|
|
|
|
|
|
glEnableVertexAttribArray(0);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, mMesh.mVertexBuffer);
|
|
|
|
glVertexAttribPointer(
|
|
|
|
0, // attribute 0
|
|
|
|
3, // size
|
|
|
|
GL_FLOAT, // type
|
|
|
|
GL_FALSE, // normalized?
|
|
|
|
0, // stride
|
|
|
|
(void*)0 // offset
|
|
|
|
);
|
|
|
|
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 3); // starting from vertex 0; 3 vertices total
|
|
|
|
glDisableVertexAttribArray(0);
|
2016-11-28 21:29:19 +01:00
|
|
|
}
|
|
|
|
|
2018-02-12 21:35:44 +01:00
|
|
|
void Renderer::RenderGui() {
|
2017-01-21 23:22:32 +01:00
|
|
|
}
|
|
|
|
|
2018-02-12 21:35:44 +01:00
|
|
|
void Renderer::Resize (int width, int height) {
|
|
|
|
assert(false);
|
2017-01-22 20:51:12 +01:00
|
|
|
}
|