From b7e867fbf2a305dcd9321f2c831096f984a5c3be Mon Sep 17 00:00:00 2001 From: kts Date: Sat, 21 Feb 2015 05:53:40 -0800 Subject: [PATCH] Updates VS project. Replaced snprintf with std::ostringstream in Program::attachTexture. Added RenderCamera functionality for orientation and position. Much more to be added, but likely need to implement the State machine so majority of the program logic is complete. --- build/vs/vs.vcxproj | 7 +++- build/vs/vs.vcxproj.filters | 18 ++++++++++ src/Program.cpp | 22 +++++-------- src/RenderCamera.cpp | 65 +++++++++++++++++++++++++++++++++++++ src/RenderCamera.hpp | 16 ++++++++- src/RenderScene.cpp | 2 +- src/main.cpp | 7 ---- 7 files changed, 113 insertions(+), 24 deletions(-) diff --git a/build/vs/vs.vcxproj b/build/vs/vs.vcxproj index 3f34956..89d9865 100644 --- a/build/vs/vs.vcxproj +++ b/build/vs/vs.vcxproj @@ -57,7 +57,8 @@ xcopy /d /y "..\..\..\..\Dev\glew-1.11.0\bin\Release\Win32\glew32.dll" "$(OutDir)" -xcopy /d /y "..\..\..\..\Dev\SDL2-2.0.3\lib\x86\SDL2.dll" "$(OutDir)" +xcopy /d /y "..\..\..\..\Dev\SDL2-2.0.3\lib\x86\SDL2.dll" "$(OutDir)" +xcopy /s /e /d /y "..\..\data" "$(OutDir)data\" @@ -79,6 +80,7 @@ xcopy /d /y "..\..\..\..\Dev\SDL2-2.0.3\lib\x86\SDL2.dll" "$(OutDir)" + @@ -95,12 +97,15 @@ xcopy /d /y "..\..\..\..\Dev\SDL2-2.0.3\lib\x86\SDL2.dll" "$(OutDir)" true + + + diff --git a/build/vs/vs.vcxproj.filters b/build/vs/vs.vcxproj.filters index 056fc30..034d35d 100644 --- a/build/vs/vs.vcxproj.filters +++ b/build/vs/vs.vcxproj.filters @@ -19,6 +19,12 @@ {e0e1a725-2ac7-4b9b-bc04-51850b74c96c} + + {09c9659c-b804-479c-a003-fd38125fb2b1} + + + {67fb4fb5-992e-4ddb-9696-d1c4d51d1204} + @@ -51,6 +57,9 @@ Classes + + Classes + @@ -59,6 +68,12 @@ Build Copy + + Build Copy\data\shaders + + + Build Copy\data\shaders + @@ -91,5 +106,8 @@ Classes + + Classes + \ No newline at end of file diff --git a/src/Program.cpp b/src/Program.cpp index 558179b..a7d145f 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -7,6 +7,7 @@ loading, creating, and compiling OpenGL programs. #include "Program.hpp" #include "Log.hpp" #include +#include // ostringstream #include // oor /* ======== Constructors and Destructors ======== */ Program::Program() { @@ -46,7 +47,7 @@ int Program::doCompile() { // Let's first create our program program = glCreateProgram(); // Now we compile our shaders - int i; + size_t i; for (i = 0; i < shader_prog.size(); i++) { GLuint shader = shader_prog.at(i); glCompileShader(shader); @@ -78,14 +79,7 @@ GLuint Program::getProgram() { // int Program::activate() { glUseProgram(program); - // TODO: foreach texture, activate and bind - /*glActiveTexture(r_texture++); - glBindTexture(GL_TEXTURE_2D, r_view->getFBO()); - // FIXME: not sure how else this should be done! - char texture_str[7]; - snprintf(texture_str, 7, "tex%d", r_texture); - glUniform1i(glGetUniformLocation(program->getProgram(), texture_str), r_texture-1);*/ - int i; + size_t i; for (i = 0; i < shader_textures.size(); i++) { glActiveTexture(GL_TEXTURE0 + shader_textures[i].index); glBindTexture(GL_TEXTURE_2D, shader_textures[i].texture); @@ -95,7 +89,7 @@ int Program::activate() { } int Program::deactivate() { // TODO: deactivate textures - int i; + size_t i; for (i = 0; i < shader_textures.size(); i++) { // ?? FIXME } @@ -111,13 +105,13 @@ int Program::doClean() { // Textures can be "inserted" into a Program via the following function. The texture index position corresponds to an OpenGL texture position as well as the GLSL uniform that matches "tex"+"1-99" (e.g., "tex1"). int Program::attachTexture(GLuint index, GLuint texture) { if (texture == 0) return 1; - char texture_str[7]; - snprintf(texture_str, 7, "tex%d", index); - + std::ostringstream texture_str; + texture_str << "tex" << index; + Texture tex; tex.index = index; tex.texture = texture; - tex.location = glGetUniformLocation(program, texture_str); + tex.location = glGetUniformLocation(program, texture_str.str().c_str()); // Insert it onto the textures record for later usage shader_textures.insert(shader_textures.begin()+index, tex); diff --git a/src/RenderCamera.cpp b/src/RenderCamera.cpp index 657e746..f72bbf5 100644 --- a/src/RenderCamera.cpp +++ b/src/RenderCamera.cpp @@ -4,19 +4,84 @@ RenderCamera This header file defines the RenderCamera class. ================================================================ */ #include "RenderCamera.hpp" +#include // fminf, M_PI +#ifndef fminf // Just for the winderps +#define fminf(a,b) (a) < (b) ? (a) : (b) +#endif /* ======== Constructors and Destructors ======== */ RenderCamera::RenderCamera() { + fov_ratio = 0.75f; + near = 0.1f; + far = 512.0f; + pitch = yaw = 0; render_mode = 0; + flags = 0; + width = height = 1; render_view = NULL; } RenderCamera::~RenderCamera() { } +/* ======== Update functions ======== */ +void RenderCamera::updateProjection(int width_, int height_) { + width = width_; + height = height_; + GLfloat wf, hf; + GLfloat ratio_xy, ratio_x, ratio_y; + GLfloat ratio_zw, ratio_z, ratio_w; + + wf = (GLfloat)width; + hf = (GLfloat)height; + ratio_xy = fminf(wf, hf) * 1.0f/fov_ratio; + ratio_x = ratio_xy / wf; + ratio_y = ratio_xy / hf; + ratio_zw = 1.0f / (far - near); + ratio_z = (near + far) * ratio_zw; + ratio_w = -2.0f * near * far * ratio_zw; + + p_matrix[0] = Vec4(ratio_x, 0.0f, 0.0f, 0.0f); + p_matrix[1] = Vec4(0.0f, ratio_y, 0.0f, 0.0f); + p_matrix[2] = Vec4(0.0f, 0.0f, ratio_z, 1.0f); + p_matrix[3] = Vec4(0.0f, 0.0f, ratio_w, 0.0f); +} +void RenderCamera::doRefresh() { + if (!(flags & DIRTY)) return; + float c_p = cos(pitch * M_PI / 180.0f); + float s_p = sin(pitch * M_PI / 180.0f); + float c_y = cos(yaw * M_PI / 180.0f); + float s_y = sin(yaw * M_PI / 180.0f); + + Vec3 x(c_y, 0, -s_y); + Vec3 y(s_y*s_p, c_p, c_y * s_p); + Vec3 z(s_y * c_p, -s_p, c_p * c_y); + + mv_matrix = Mat4( + Vec4(x.x, y.x, z.x, 0.0f), + Vec4(x.y, y.y, z.y, 0.0f), + Vec4(x.z, y.z, z.z, 0.0f), + Vec4(-x.dotProduct(position), -y.dotProduct(position), -z.dotProduct(position), 1.0f) + ); + flags &= ~DIRTY; +} /* ======== Setter functions ======== */ int RenderCamera::setRenderView(RenderView *rv) { if (rv == NULL) return 1; render_view = rv; return 0; } +void RenderCamera::setPosition(float x, float y, float z) { + position.x = x; + position.y = y; + position.z = z; + flags |= DIRTY; +} +void RenderCamera::setPitch(float pitch_) { + pitch = pitch_; + flags |= DIRTY; +} +void RenderCamera::setYaw(float yaw_) { + yaw = yaw_; + flags |= DIRTY; +} /* ======== Getter functions ======== */ RenderView* RenderCamera::getRenderView() { return render_view; diff --git a/src/RenderCamera.hpp b/src/RenderCamera.hpp index 3887782..9ced4fa 100644 --- a/src/RenderCamera.hpp +++ b/src/RenderCamera.hpp @@ -12,15 +12,29 @@ RenderScene and outputs all data to the friend class, RenderView. #include "common.hpp" #include "RenderView.hpp" #include "Vec.hpp" +#include "Mat4.hpp" +#define DIRTY (1 << 1) class RenderCamera { public: RenderCamera(); ~RenderCamera(); int setRenderView(RenderView *view); RenderView* getRenderView(); + void updateProjection(int width, int height); + void doRefresh(); + void setPosition(float x, float y, float z); + void setPitch(float pitch); + void setYaw(float yaw); private: - Vec3 x, y, z; // TODO: replace with quaternion + Vec3 position; // The camera's coordinates in 3D space + float pitch, yaw; // + float fov_ratio; + float near, far; // wherever you are int render_mode; // 0 = perspective, 1 = orthogonal RenderView *render_view; // RenderView to render the scene to + Mat4 p_matrix; // Our camera's projection matrix + Mat4 mv_matrix; // modelview matrix + int width, height; + int flags; }; #endif diff --git a/src/RenderScene.cpp b/src/RenderScene.cpp index 829313b..485998c 100644 --- a/src/RenderScene.cpp +++ b/src/RenderScene.cpp @@ -45,7 +45,7 @@ int RenderScene::renderTo(RenderView *target_view, Program *render_program) { // Use this provided program for rendering. Should refer to a framebuffer shader glUseProgram(render_program->getProgram()); // Bind to the provided target view's fbo - glBindFramebuffer(GL_FRAMEBUFFER, view->fbo); + glBindFramebuffer(GL_FRAMEBUFFER, target_view->fbo); // Render each of our camera's framebuffers with the appropriate changes for (camera_it = cameras.begin(), camera_end = cameras.end(); camera_it != camera_end; ++camera_it) { camera = *camera_it; diff --git a/src/main.cpp b/src/main.cpp index 48b1777..792a2d1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -125,13 +125,6 @@ int main(int argc, char *argv[]) { glDrawArrays(GL_TRIANGLES, 0, mesh->vertices.size()); // close 'em glDisableVertexAttribArray(program_vp); - - /*glActiveTexture(r_texture++); - glBindTexture(GL_TEXTURE_2D, r_view->getFBO()); - // FIXME: not sure how else this should be done! - char texture_str[7]; - snprintf(texture_str, 7, "tex%d", r_texture); - glUniform1i(glGetUniformLocation(program->getProgram(), texture_str), r_texture-1);*/ } program->deactivate();