Skip to content
Browse files

Renderer now using lambda-function for each C_GfxEntity

  • Loading branch information...
1 parent 0f95e23 commit 88786b20fa65e57495f48681d6261720d0ed9b6e @Detegr committed Apr 1, 2013
Showing with 61 additions and 24 deletions.
  1. +21 −2 client.cpp
  2. +1 −1 entity.cpp
  3. +1 −1 entity.h
  4. +4 −18 renderer.cpp
  5. +5 −1 renderer.h
  6. +21 −0 shadermanager.cpp
  7. +8 −1 shadermanager.h
View
23 client.cpp
@@ -5,6 +5,8 @@
#include "packetparser.h"
#include "input.h"
#include "timer.h"
+#include <functional>
+#include "glm/gtc/type_ptr.hpp"
using namespace dtglib;
@@ -57,6 +59,23 @@ unsigned char getkeys()
return keyvec;
}
+std::function<void (C_GfxEntity*)> drawentity = [] (C_GfxEntity* e)
+{
+ C_Renderer* r = C_Singleton::M_Renderer();
+ C_ShaderManager* s = C_Singleton::M_ShaderManager();
+
+ if(e->M_ModelName() == "triangle" || e->M_ModelName() == "bullet") s->Use("green");
+ else s->Use("minimal");
+
+ if(e->IsPlayer())
+ {
+ r->SetView(e->GetPosition());
+ }
+ glm::mat4 MVP=r->ProjectionMatrix()*r->ViewMatrix()*e->ModelMatrix();
+ glUniformMatrix4fv(s->GetUniformFromCurrent("MVP"), 1, GL_FALSE, glm::value_ptr(MVP));
+ e->M_Draw();
+};
+
int main()
{
#ifdef _WIN32
@@ -78,7 +97,6 @@ int main()
s->Load("minimal");
s->Load("green");
- //r->UseShader(s->Get("minimal"));
bool running=true;
p.M_Clear();
@@ -113,7 +131,8 @@ int main()
}
g_Sleep(1);
p.M_Clear();
- r->Draw();
+
+ r->Draw(drawentity);
running=!(C_Singleton::M_InputHandler()->Get(ESC));
}
View
2 entity.cpp
@@ -85,7 +85,7 @@ void C_GfxEntity::M_ExtrapolateRotation(double dt)
float ca=m_Er.M_Current() + ((a - m_Er.M_Current()) * (dt/0.04));
m_RotationMatrix = glm::rotate(glm::mat4(1.0), (const float)ca, glm::vec3(0.0f, 0.0f, 1.0f));
}
-const glm::mat4& C_GfxEntity::M_ModelMatrix()
+const glm::mat4& C_GfxEntity::ModelMatrix()
{
m_ModelMatrix = m_ScaleMatrix * m_TranslationMatrix * m_RotationMatrix;
return m_ModelMatrix;
View
2 entity.h
@@ -44,7 +44,7 @@ class C_GfxEntity
void M_SetRotation(float amount);
void M_ExtrapolateRotation(double dt);
const std::string& M_ModelName() const;
- const glm::mat4& M_ModelMatrix();
+ const glm::mat4& ModelMatrix();
void M_Draw() const;
unsigned int M_Id() const { return m_Id; }
bool IsPlayer() const { return m_IsPlayer; }
View
22 renderer.cpp
@@ -3,6 +3,7 @@
#include <stdexcept>
#include "glm/gtc/type_ptr.hpp"
#include "singleton.h"
+#include <functional>
C_Renderer::C_Renderer(unsigned width, unsigned height) :
m_AspectRatio((float)width/(float)height),
@@ -31,7 +32,7 @@ C_Renderer::C_Renderer(unsigned width, unsigned height) :
glfwTerminate();
throw std::runtime_error("OpenGL 3.3 is not supported.");
}
- glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
+ glClearColor(0.0f, 0.0f, 0.2f, 1.0f);
glGenVertexArrays(1,&m_VertexArray);
glBindVertexArray(m_VertexArray);
@@ -48,27 +49,12 @@ void C_Renderer::SetView(const C_Vec2& pos)
m_View=glm::lookAt(glm::vec3(pos.x,pos.y,1), glm::vec3(pos.x, pos.y, 0), glm::vec3(0,1,0));
}
-void C_Renderer::UseShader(const C_Shader& s)
-{
- m_CurrentShader=s.M_Id();
- glUseProgram(m_CurrentShader);
- m_MVP = glGetUniformLocation(m_CurrentShader, "MVP");
-}
-
-void C_Renderer::Draw()
+void C_Renderer::Draw(std::function<void (C_GfxEntity*) > renderfunc)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for(std::vector<C_GfxEntity*>::iterator it=m_Entities.begin(); it!=m_Entities.end(); ++it)
{
- if((*it)->M_ModelName() == "triangle" || (*it)->M_ModelName() == "bullet") UseShader(C_Singleton::M_ShaderManager()->Get("green"));
- else UseShader(C_Singleton::M_ShaderManager()->Get("minimal"));
- if((*it)->IsPlayer())
- {
- SetView((*it)->GetPosition());
- }
- glm::mat4 MVP=m_Projection*m_View*(*it)->M_ModelMatrix();
- glUniformMatrix4fv(m_MVP, 1, GL_FALSE, glm::value_ptr(MVP));
- (*it)->M_Draw();
+ renderfunc(*it);
}
glfwSwapBuffers();
}
View
6 renderer.h
@@ -26,12 +26,16 @@ class C_Renderer
~C_Renderer();
public:
bool IsOpened() {return glfwGetWindowParam(GLFW_OPENED);}
- void Draw();
+ void Draw(std::function<void (C_GfxEntity*) > renderfunc);
GLuint CurrentShaderId() const;
void UseShader(const C_Shader& s);
void AddEntity(C_GfxEntity* e);
void DeleteEntity(C_GfxEntity* e);
C_GfxEntity* GetEntity(unsigned short id);
const std::vector<C_GfxEntity*>& Entities() const { return m_Entities; }
void SetView(const C_Vec2& pos);
+ void Clear();
+
+ const glm::mat4& ProjectionMatrix() const { return m_Projection; }
+ const glm::mat4& ViewMatrix() const { return m_View; }
};
View
21 shadermanager.cpp
@@ -67,3 +67,24 @@ const C_Shader& C_ShaderManager::Get(const std::string& name) const
}
throw std::runtime_error("Shader " + name + " does not exist!");
}
+
+GLuint C_ShaderManager::GetUniformLocation(const C_Shader& s, const std::string& locname)
+{
+ return glGetUniformLocation(s.M_Id(), locname.c_str());
+}
+
+GLuint C_ShaderManager::GetUniformFromCurrent(const std::string& locname)
+{
+ return GetUniformLocation(*m_CurrentShader, locname.c_str());
+}
+
+void C_ShaderManager::Use(const std::string& name)
+{
+ Use(Get(name));
+}
+
+void C_ShaderManager::Use(const C_Shader& shader)
+{
+ m_CurrentShader = &shader;
+ glUseProgram(m_CurrentShader->M_Id());
+}
View
9 shadermanager.h
@@ -23,7 +23,7 @@ class C_ShaderManager
{
friend class C_Singleton;
private:
- C_ShaderManager()
+ C_ShaderManager() : m_CurrentShader(NULL)
{
std::cout << "Initializing shadermanager..." << std::flush;
std::cout << "OK!" << std::endl;
@@ -39,7 +39,14 @@ class C_ShaderManager
}
std::vector<C_Shader> m_Shaders;
bool M_CheckResult(GLuint id, GLuint status);
+
+ const C_Shader* m_CurrentShader;
public:
bool Load(const std::string& name);
const C_Shader& Get(const std::string& name) const;
+ void Use(const std::string& name);
+ void Use(const C_Shader& shader);
+ const C_Shader* Current() const { return m_CurrentShader; }
+ GLuint GetUniformLocation(const C_Shader& s, const std::string& locname);
+ GLuint GetUniformFromCurrent(const std::string& locname);
};

0 comments on commit 88786b2

Please sign in to comment.
Something went wrong with that request. Please try again.