Added C based scene module and render commands

simple_math_single_header
Martin Felis 2018-12-28 17:28:49 +01:00
parent 3e6b89f22c
commit 69c56269de
13 changed files with 233 additions and 36 deletions

View File

@ -21,7 +21,6 @@ extern WriteSerializer* gWriteSerializer;
struct ReadSerializer;
extern ReadSerializer* gReadSerializer;
struct GuiInputState;
extern GuiInputState* gGuiInputState;
struct FileModificationObserver;

View File

@ -32,6 +32,10 @@ struct module_api {
bool (*step)(struct module_state *state, float dt);
};
#ifdef __cplusplus
extern "C" {
#endif
extern const struct module_api MODULE_API;
#ifdef __cplusplus
}
#endif

View File

@ -6,6 +6,14 @@
FILE *gLogFile = NULL;
void GuiInputState_Init (GuiInputState* input_state) {
input_state->mouseX = 0;
input_state->mouseY = 0;
input_state->mouseButton = 0;
input_state->mouseScroll = 0;
input_state->key = 0;
}
void LoggingInit() {
// check if protot.log exists and rename it to protot.log.1 if found
std::ifstream source ("protot.log", std::ios::binary);

View File

@ -1,16 +1,21 @@
#pragma once
#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <cassert>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <cstdarg>
#include <stdarg.h>
#include <stdbool.h>
struct GuiInputState {
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
int32_t mousedX;
int32_t mousedY;
int32_t mouseX;
@ -18,15 +23,9 @@ struct GuiInputState {
uint8_t mouseButton;
int32_t mouseScroll;
char key;
} GuiInputState;
GuiInputState() :
mouseX(0),
mouseY(0),
mouseButton(0),
mouseScroll(0),
key(0) {
}
};
void GuiInputState_Init (GuiInputState* input_state);
inline void gGetFileModTime (const char* filename, int *sec, int *nsec) {
struct stat attr;
@ -45,7 +44,7 @@ inline double gGetCurrentTime () {
struct timespec spec;
clock_gettime(CLOCK_REALTIME, &spec);
return static_cast<double>(spec.tv_sec) + spec.tv_nsec * 1.0e-9;
return (double)(spec.tv_sec) + spec.tv_nsec * 1.0e-9;
}
extern double gTimeAtStart;
@ -81,5 +80,9 @@ inline void gLog (const char* format, ...) {
}
inline float gRandomFloat() {
return (static_cast<float>(rand()) / static_cast<float>(RAND_MAX));
return ((float)(rand()) / (float)(RAND_MAX));
}
#ifdef __cplusplus
}
#endif

View File

@ -194,6 +194,7 @@ int main(void)
RuntimeModuleManager module_manager;
module_manager.RegisterModule("src/modules/libRenderModule.so");
module_manager.RegisterModule("src/modules/libTestModule.so");
module_manager.RegisterModule("src/modules/libSceneModule.so");
// module_manager.RegisterModule("src/modules/libCharacterModule.so");
// Setup global variables

View File

@ -5,13 +5,12 @@ INCLUDE_DIRECTORIES (
ADD_LIBRARY (RenderModule SHARED
RenderModule.cc
RenderUtils.cc
RenderCommands.cc
gltf_implementation.cc
)
ADD_LIBRARY (TestModule SHARED
TestModule.cc
)
ADD_LIBRARY (TestModule SHARED TestModule.cc)
TARGET_LINK_LIBRARIES ( TestModule RenderModule)
TARGET_LINK_LIBRARIES ( TestModule
RenderModule
)
ADD_LIBRARY (SceneModule SHARED SceneModule.c)
TARGET_LINK_LIBRARIES ( SceneModule RenderModule)

View File

@ -0,0 +1,39 @@
#include "Globals.h"
#include "RenderCommands.h"
#include "RenderModule.h"
#include <vector>
#include <cstring>
using namespace std;
void RenderCommandsClear() {
if (gRenderer == NULL) {
gLog ("Warning: Cannot clear render commands: no renderer found!");
return;
}
gRenderer->mRenderCommands.clear();
}
void RenderSubmit (
RenderObject object,
float* transform,
float color[4]
) {
RenderCommand command;
command.mObject = object;
memcpy (command.mTransform, transform, sizeof (float) * 16);
memcpy (command.mColor, color, sizeof(float) * 4);
gRenderer->mRenderCommands.push_back(command);
}
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif

View File

@ -0,0 +1,24 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
DebugCube = 1,
DebugFrame = 2,
DebugSphere = 3,
DebugBone = 4
} RenderObject;
void RenderCommandsClear ();
void RenderSubmit (
RenderObject object,
float* transform,
float color[4]
);
#ifdef __cplusplus
}
#endif

View File

@ -0,0 +1,10 @@
#include "RenderCommands.h"
#include <vector>
#include <cstring>
struct RenderCommand {
RenderObject mObject;
float mTransform[16];
float mColor[4];
};

View File

@ -935,7 +935,66 @@ void Renderer::DebugDrawShadowCascades() {
glEnable(GL_DEPTH_TEST);
}
void Renderer::SubmitRenderCommands (RenderProgram &program, const Camera &camera) {
glUseProgram(program.mProgramId);
program.SetMat44("uViewMatrix", camera.mViewMatrix);
program.SetMat44("uProjectionMatrix", camera.mProjectionMatrix);
program.SetVec4("uColor", Vector4f (1.0f, 1.0f, 1.0f, 1.0f));
program.SetVec3("uLightDirection", mLight.mDirection);
program.SetVec3("uViewPosition", camera.mEye);
program.SetMat44("uLightSpaceMatrix", mLight.mLightSpaceMatrix);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mLight.mShadowMapTarget.mDepthTexture);
program.SetInt("uShadowMap", 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, mDefaultTexture.mTextureId);
program.SetInt("uAlbedoTexture", 1);
for (int i = 0, n = mRenderCommands.size(); i < n; ++i) {
const RenderCommand& command = mRenderCommands[i];
Matrix44f transform;
memcpy (transform.data(), command.mTransform, sizeof(float) * 16);
Vector4f color;
memcpy (color.data(), command.mColor, sizeof(float) * 4);
program.SetVec4("uColor", color);
program.SetMat44("uModelMatrix", transform);
Matrix33f normal_matrix = transform.block<3,3>(0,0).transpose();
normal_matrix = normal_matrix.inverse();
program.SetMat33("uNormalMatrix", normal_matrix);
if (command.mObject == DebugCube) {
DebugDrawCube(program,
transform
);
} else if (command.mObject == DebugSphere) {
DebugDrawSphere(program,
transform,
color
);
} else if (command.mObject == DebugFrame) {
DebugDrawFrame(program,
transform
);
} else if (command.mObject == DebugBone) {
DebugDrawBone(program,
transform,
color
);
}
}
}
void Renderer::RenderScene(RenderProgram &program, const Camera& camera) {
SubmitRenderCommands(program, camera);
glUseProgram(program.mProgramId);
Matrix44f model_matrix = TranslateMat44(3.0f, 0.0f, 1.0f);

View File

@ -13,6 +13,7 @@
#include "Globals.h"
#include "RenderUtils.h"
#include "RenderCommands_p.h"
struct Light {
Vector3f mPosition;
@ -115,6 +116,8 @@ struct Renderer {
int mSSAOBlurSize = 1;
bool mSSAODisableColor = false;
std::vector<RenderCommand> mRenderCommands;
Renderer() :
mInitialized(false),
mWidth (0),
@ -126,6 +129,7 @@ struct Renderer {
void CheckRenderBuffers();
void ResizeTargets(int width, int height);
void RenderGl();
void SubmitRenderCommands (RenderProgram &program, const Camera &camera);
void RenderScene(RenderProgram &program, const Camera& camera);
void DebugDrawShadowCascades();
void DrawGui();

View File

@ -242,6 +242,11 @@ struct RenderProgram : AFileModificationListener {
glUniform1f(location, val);
return location;
}
GLint SetFloatArray(const char* name, const int count, const float* array) {
GLint location = glGetUniformLocation(mProgramId, name);
glUniform1fv(location, count, array);
return location;
}
GLint SetVec3(const char* name, const Vector3f& vec) {
GLint location = glGetUniformLocation(mProgramId, name);
glUniform3fv(location, 1, vec.data());
@ -604,19 +609,6 @@ struct AssetFile {
void DrawGui();
};
struct RenderCommand {
typedef enum {
EnableShadowPass = 1
} Flags;
int mFlags;
GLenum mDrawMode;
Vector4f mColor = Vector4f (1.0f, 1.0f, 1.0f, 1.0f);
Matrix44f mTransform;
VertexArrayMesh *mMesh;
};
// Debug Draw Stuff
void DebugDrawInitialize();
void DebugDrawShutdown();

55
src/modules/SceneModule.c Normal file
View File

@ -0,0 +1,55 @@
#include "RuntimeModule.h"
#include "RenderCommands.h"
#include "Utils.h"
typedef struct {
} module_state;
static struct module_state *module_init() {
gLog ("%s %s called", __FILE__, __FUNCTION__);
module_state *state = (module_state*) malloc(sizeof(*state));
return state;
}
static void module_finalize(struct module_state *state) {
gLog ("%s %s called (state %p)", __FILE__, __FUNCTION__, state);
free(state);
}
static void module_reload(struct module_state *state, void* read_serializer) {
gLog ("%s %s called (state %p)", __FILE__, __FUNCTION__, state);
}
static void module_unload(struct module_state *state, void* write_serializer) {
gLog ("%s %s called (state %p)", __FILE__, __FUNCTION__, state);
}
static bool module_step(struct module_state *state, float dt) {
gLog ("Scene step4");
float mat[16] = {
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
float color[4] = { 1.0f, 1.0f, 1.0f, 1.0f};
RenderCommandsClear();
RenderSubmit(
DebugSphere,
mat,
color
);
return true;
}
const struct module_api MODULE_API = {
.init = module_init,
.finalize = module_finalize,
.reload = module_reload,
.unload = module_unload,
.step = module_step
};