Serializing camera parameters, added Textures and default texture

simple_math_single_header
Martin Felis 2018-02-19 14:33:29 +01:00
parent 9c56d3f061
commit 39fe10ffc7
6 changed files with 7635 additions and 31 deletions

7462
3rdparty/stb/stb_image.h vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@ -122,6 +122,12 @@ bool SerializeInt (Serializer &serializer, const std::string &key, int& value) {
return serializer.SerializeData(key, reinterpret_cast<char*>(&value), sizeof(int));
}
template <typename Serializer>
bool SerializeFloat (Serializer &serializer, const std::string &key, float& value) {
return serializer.SerializeData(key, reinterpret_cast<char*>(&value), sizeof(float));
}
template <typename Serializer>
bool SerializedUint16 (Serializer &serializer, const std::string &key, uint16_t& value) {
return serializer.SerializeData(key, reinterpret_cast<char*>(&value), sizeof(uint16_t));

View File

@ -32,6 +32,15 @@ static const GLfloat g_quad_vertex_buffer_data[] = {
1.0f, 1.0f, 0.0f
};
static const GLfloat g_textured_quad_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f, 0.0f, 1.0f,
1.0f, -1.0f, 0.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 0.0f, 0.0f, 0.0f,
-1.0f, 1.0f, 0.0f, 0.0f, 0.0f,
1.0f, -1.0f, 0.0f, 1.0f, 1.0f,
1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
};
//
// Module
//
@ -55,6 +64,9 @@ static void module_serialize (
struct module_state *state,
Serializer* serializer) {
SerializeBool(*serializer, "protot.RenderModule.DrawDepth", sRendererSettings.DrawDepth);
SerializeBool(*serializer, "protot.RenderModule.Camera.mIsOrthographic", gRenderer->mCamera.mIsOrthographic);
SerializeFloat(*serializer, "protot.RenderModule.Camera.mNear", gRenderer->mCamera.mNear);
SerializeFloat(*serializer, "protot.RenderModule.Camera.mFar", gRenderer->mCamera.mFar);
// // get the state from the serializer
// Camera* camera = &gRenderer->cameras[gRenderer->activeCameraIndex];
// assert (camera != nullptr);
@ -130,23 +142,40 @@ const struct module_api MODULE_API = {
// Camera
//
void Camera::UpdateMatrices() {
mViewMatrix = LookAt(eye, poi, up);
mViewMatrix = LookAt(mEye, mPoi, mUp);
if (orthographic) {
mProjectionMatrix = Ortho(-1.0f, 1.0f, -1.0f, 1.0f, near, far);
if (mIsOrthographic) {
mProjectionMatrix = Ortho(-1.0f, 1.0f, -1.0f, 1.0f, mNear, mFar);
} else {
}
}
void Camera::DrawGui() {
ImGui::Checkbox("Orthographic", &mIsOrthographic);
ImGui::SliderFloat("Near", &mNear, -10, 10);
ImGui::SliderFloat("Far", &mFar, -10, 10);
}
//
// Camera
// Renderer
//
void Renderer::Initialize(int width, int height) {
mDefaultTexture.MakeGrid(128, Vector3f (0.8, 0.8f, 0.8f), Vector3f (0.2f, 0.2f, 0.2f));
// Mesh
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);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
// Plane
glGenVertexArrays(1, &mPlane.mVertexArrayId);
glBindVertexArray(mPlane.mVertexArrayId);
glGenBuffers(1, &mPlane.mVertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, mPlane.mVertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
// Simple Shader
mDefaultProgram = RenderProgram("data/shaders/vs_simple.glsl", "data/shaders/fs_simple.glsl");
@ -189,6 +218,9 @@ glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data
void Renderer::Shutdown() {
glDeleteVertexArrays(1, &mMesh.mVertexArrayId);
glDeleteBuffers(1, &mMesh.mVertexBuffer);
glDeleteVertexArrays(1, &mPlane.mVertexArrayId);
glDeleteBuffers(1, &mPlane.mVertexBuffer);
}
@ -198,12 +230,9 @@ void Renderer::RenderGl() {
if (width != mWidth || height != mHeight)
Resize(width, height);
mCamera.eye = Vector3f (0.0f, 0.0f, 4.0f);
mCamera.poi = Vector3f (0.0f, 0.0f, 0.0f);
mCamera.up = Vector3f (0.0f, 1.0f, 0.0f);
mCamera.near = 0.0f;
mCamera.far = 4.0f;
mCamera.orthographic = true;
mCamera.mEye = Vector3f (0.0f, 0.0f, 4.0f);
mCamera.mPoi = Vector3f (0.0f, 0.0f, 0.0f);
mCamera.mUp = Vector3f (0.0f, 1.0f, 0.0f);
mCamera.UpdateMatrices();
@ -259,9 +288,9 @@ void Renderer::RenderGl() {
glUniform1i(muRenderQuadTexture, 0);
// TODO: adjust for perspective
glUniform1f(muRenderQuadDepthNear, mCamera.near);
glUniform1f(muRenderQuadDepthNear, mCamera.mNear);
// TODO: why do I have to divide by depth range?
glUniform1f(muRenderQuadDepthFar, mCamera.far / (mCamera.far - mCamera.near));
glUniform1f(muRenderQuadDepthFar, mCamera.mFar / (mCamera.mFar - mCamera.mNear));
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, mRenderQuadVertexBufferId);
@ -306,6 +335,19 @@ void Renderer::RenderGui() {
);
}
ImGui::EndDock();
if (ImGui::BeginDock("Render Settings")) {
mCamera.DrawGui();
ImGui::Text("Default Texture");
const ImVec2 content_avail = ImGui::GetContentRegionAvail();
ImGui::Image((void*) mDefaultTexture.mTextureId,
ImVec2(content_avail.x, content_avail.x),
ImVec2(0.0f, 1.0f),
ImVec2(1.0f, 0.0f)
);
}
ImGui::EndDock();
}
void Renderer::Resize (int width, int height) {

View File

@ -13,30 +13,30 @@
#include "RenderUtils.h"
struct Camera {
Vector3f eye;
Vector3f poi;
Vector3f up;
Vector3f mEye;
Vector3f mPoi;
Vector3f mUp;
float near;
float far;
float fov;
bool orthographic;
float width;
float height;
float mNear;
float mFar;
float mFov;
bool mIsOrthographic;
float mWidth;
float mHeight;
Matrix44f mProjectionMatrix;
Matrix44f mViewMatrix;
Camera() :
eye {5.f, 4.f, 5.f},
poi {0.f, 2.f, 0.f},
up {0.f, 1.f, 0.f},
near (0.1f),
far (150.f),
fov (60.f),
orthographic (false),
width (-1.f),
height (-1.f),
mEye {5.f, 4.f, 5.f},
mPoi {0.f, 2.f, 0.f},
mUp {0.f, 1.f, 0.f},
mNear (0.1f),
mFar (150.f),
mFov (60.f),
mIsOrthographic (false),
mWidth (-1.f),
mHeight (-1.f),
mProjectionMatrix (
1.f, 0.f, 0.f, 0.f,
@ -51,6 +51,7 @@ struct Camera {
{}
void UpdateMatrices();
void DrawGui();
};
struct Light {
@ -117,6 +118,9 @@ struct Renderer {
Camera mCamera;
Mesh mMesh;
Mesh mPlane;
Texture mDefaultTexture;
RenderProgram mDefaultProgram;
GLuint muDefaultModelViewProjection;

View File

@ -8,6 +8,10 @@
#include "RenderUtils.h"
#include "Globals.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
using namespace SimpleMath;
//
@ -243,3 +247,79 @@ void RenderTarget::RenderToLinearizedDepth(bool render_to_depth) {
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, mColorTexture, 0);
}
}
//
// Texture
//
Texture::~Texture() {
if (mTextureId != -1) {
glDeleteTextures(1, &mTextureId);
mTextureId = -1;
}
}
void Texture::MakeGrid(const int& size, const Vector3f &c1, const Vector3f &c2) {
mWidth = size;
mHeight = size;
unsigned char buffer[size * size * 3];
int size_half = size / 2;
for (int i = 0; i < size_half; ++i) {
for (int j = 0; j < size_half; ++j) {
buffer[(i * size * 3) + (j * 3) + 0] = static_cast<unsigned char>(c1[0] * 255.0f);
buffer[(i * size * 3) + (j * 3) + 1] = static_cast<unsigned char>(c1[1] * 255.0f);
buffer[(i * size * 3) + (j * 3) + 2] = static_cast<unsigned char>(c1[2] * 255.0f);
}
}
for (int i = size_half; i < size; ++i) {
for (int j = 0; j < size_half; ++j) {
buffer[(i * size * 3) + (j * 3) + 0] = static_cast<unsigned char>(c2[0] * 255.0f);
buffer[(i * size * 3) + (j * 3) + 1] = static_cast<unsigned char>(c2[1] * 255.0f);
buffer[(i * size * 3) + (j * 3) + 2] = static_cast<unsigned char>(c2[2] * 255.0f);
}
}
for (int i = size_half; i < size; ++i) {
for (int j = size_half; j < size; ++j) {
buffer[(i * size * 3) + (j * 3) + 0] = static_cast<unsigned char>(c1[0] * 255.0f);
buffer[(i * size * 3) + (j * 3) + 1] = static_cast<unsigned char>(c1[1] * 255.0f);
buffer[(i * size * 3) + (j * 3) + 2] = static_cast<unsigned char>(c1[2] * 255.0f);
}
}
for (int i = 0; i < size_half; ++i) {
for (int j = size_half; j < size; ++j) {
buffer[(i * size * 3) + (j * 3) + 0] = static_cast<unsigned char>(c2[0] * 255.0f);
buffer[(i * size * 3) + (j * 3) + 1] = static_cast<unsigned char>(c2[1] * 255.0f);
buffer[(i * size * 3) + (j * 3) + 2] = static_cast<unsigned char>(c2[2] * 255.0f);
}
}
glGenTextures(1, &mTextureId);
glBindTexture(GL_TEXTURE_2D, mTextureId);
glTexImage2D(GL_TEXTURE_2D,
0, // level
GL_RGB, // internal format
size, // width
size, // height
0, // border (must be 0)
GL_RGB, // format of pixel data
GL_UNSIGNED_BYTE, // type of pixel data
buffer // pixel data
);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
bool Texture::Load(const char* filename, int num_components) {
// unsigned char* rgb = stbi_load(filename, &mWidth, &mHeight, num_components);
assert(false);
}

View File

@ -176,4 +176,14 @@ struct RenderTarget {
void RenderToLinearizedDepth(bool render_to_depth);
};
struct Texture {
GLuint mTextureId;
GLuint mWidth;
GLuint mHeight;
~Texture();
void MakeGrid(const int& size, const Vector3f &c1, const Vector3f &c2);
bool Load(const char* path, int num_components = 3);
};
#endif