From 09045028fd371c6ebfc5ade2f530c92f04d08a89 Mon Sep 17 00:00:00 2001 From: Peter Schaefer Date: Wed, 13 Apr 2016 18:01:42 +0200 Subject: [PATCH] static setUniformLocation removed plane&skybox VS --- CGUE2015_Weave.sln | 2 -- Weave/Graphix/Model/IMesh.cpp | 16 ---------------- Weave/Graphix/Model/Model.cpp | 10 ++++------ Weave/Graphix/Model/Plane.cpp | 5 +++++ Weave/Graphix/Model/SkyBox.cpp | 5 +---- Weave/Graphix/Shader.cpp | 27 +++++++++++++++++++++++++++ Weave/Graphix/Shader.h | 6 ++++++ Weave/Graphix/Texture.cpp | 13 +++---------- Weave/Graphix/ViewPort.cpp | 29 ++++++----------------------- shader/basic_VS.hlsl | 6 +++--- shader/plane_VS.hlsl | 12 ------------ shader/skybox_VS.hlsl | 15 --------------- 12 files changed, 55 insertions(+), 91 deletions(-) delete mode 100644 shader/plane_VS.hlsl delete mode 100644 shader/skybox_VS.hlsl diff --git a/CGUE2015_Weave.sln b/CGUE2015_Weave.sln index 04a4572..014bd36 100644 --- a/CGUE2015_Weave.sln +++ b/CGUE2015_Weave.sln @@ -14,8 +14,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "shader", "shader", "{75179E shader\blur_FS.hlsl = shader\blur_FS.hlsl shader\lightingTexture_FS.hlsl = shader\lightingTexture_FS.hlsl shader\perspective_VS.hlsl = shader\perspective_VS.hlsl - shader\plane_VS.hlsl = shader\plane_VS.hlsl - shader\skybox_VS.hlsl = shader\skybox_VS.hlsl shader\skyplane_color_FS.hlsl = shader\skyplane_color_FS.hlsl shader\skyplane_FS.hlsl = shader\skyplane_FS.hlsl shader\skyplane_VS.hlsl = shader\skyplane_VS.hlsl diff --git a/Weave/Graphix/Model/IMesh.cpp b/Weave/Graphix/Model/IMesh.cpp index 81c0cec..a0f1138 100644 --- a/Weave/Graphix/Model/IMesh.cpp +++ b/Weave/Graphix/Model/IMesh.cpp @@ -50,22 +50,6 @@ IMesh::~IMesh() { } -//void IMesh::useModelMat(const mat4& _model, Shader* _shader) const -//{ -// // Model::useModelMat(_model, _shader); -// // Model::useModelMat(_model * modelMat, _shader); -// int tmp = _shader->getUniformLocation("uModel"); -// if (tmp >= 0) -// glUniformMatrix4fv(tmp, 1, GL_FALSE, value_ptr(_model* modelMat)); -//} - - -//void IMesh::drawBBox(const mat4& _modelMat, const vec4& _color) const -//{ -// Model::drawBBox(_modelMat, _color); -//} - - string IMesh::type2str() const { return "IMesh"; diff --git a/Weave/Graphix/Model/Model.cpp b/Weave/Graphix/Model/Model.cpp index 7afe8de..a7bce97 100644 --- a/Weave/Graphix/Model/Model.cpp +++ b/Weave/Graphix/Model/Model.cpp @@ -156,9 +156,9 @@ void Model::drawBBox(const mat4& _modelMat, const vec4& _color) const { Graphix::getGlError(); Shader::gBasic()->useShader(); - int tmp = Shader::gBasic()->getUniformLocation("uFragmentColor"); - if (tmp >= 0) - glUniform4fv(tmp, 1, value_ptr(_color)); + + Shader::setUniformLocation("uFragmentColor", _color); + Graphix::getGlError(); btVector3 minB, maxB; @@ -243,9 +243,7 @@ void Model::useModel(const mat4& _modelMat) const /* use ModelMAT*/ - int tmp = Shader::gActive()->getUniformLocation("uModel"); - if (tmp >= 0) - glUniformMatrix4fv(tmp, 1, GL_FALSE, value_ptr(_modelMat)); + Shader::setUniformLocation("uModel", _modelMat); } diff --git a/Weave/Graphix/Model/Plane.cpp b/Weave/Graphix/Model/Plane.cpp index 454a48d..597cde0 100644 --- a/Weave/Graphix/Model/Plane.cpp +++ b/Weave/Graphix/Model/Plane.cpp @@ -2,6 +2,7 @@ #include "GL\glew.h" #include "../../GLM.h" +#include "../Shader.h" Plane::Plane() @@ -28,6 +29,10 @@ Plane::~Plane() void Plane::drawModel() const { + useModel(mat4(1.f)); + + Shader::setUniformLocation("uPlain", 1); + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glBindVertexArray(0); } diff --git a/Weave/Graphix/Model/SkyBox.cpp b/Weave/Graphix/Model/SkyBox.cpp index 4ee0f1c..fb963af 100644 --- a/Weave/Graphix/Model/SkyBox.cpp +++ b/Weave/Graphix/Model/SkyBox.cpp @@ -38,10 +38,7 @@ void SkyBox::drawModel(const mat4& _modelMat) const useModel(_modelMat); - GLboolean far = true; - int tmp = Shader::gBasicTexture()->getUniformLocation("uFar"); - if (tmp >= 0) - glUniform1i(tmp, far); + Shader::setUniformLocation("uFar", 1); Model::drawModel(); //glEnable(GL_DEPTH_TEST); diff --git a/Weave/Graphix/Shader.cpp b/Weave/Graphix/Shader.cpp index 0ba0ffa..285eac6 100644 --- a/Weave/Graphix/Shader.cpp +++ b/Weave/Graphix/Shader.cpp @@ -49,6 +49,33 @@ int Shader::getUniformLocation(const string& name) const return ind; } +int Shader::setUniformLocation(const string& _name, const mat4& _mat) +{ + int tmp = glGetUniformLocation(*shActive, _name.c_str()); + if (tmp >= 0) + glUniformMatrix4fv(tmp, 1, GL_FALSE, value_ptr(_mat)); + return tmp; +} + +int Shader::setUniformLocation(const string& _name, const vec4& _vec) +{ + int tmp = glGetUniformLocation(*shActive, _name.c_str()); + if (tmp >= 0) + glUniform4fv(tmp, 1, value_ptr(_vec)); + return tmp; +} + +int Shader::setUniformLocation(const string& _name, const int& _i) +{ + int tmp = glGetUniformLocation(*shActive, _name.c_str()); + if (tmp >= 0) + glUniform1i(tmp, _i); + return tmp; +} + + + + GLuint Shader::getHandle() const { return handle; diff --git a/Weave/Graphix/Shader.h b/Weave/Graphix/Shader.h index 2128e12..1473e55 100644 --- a/Weave/Graphix/Shader.h +++ b/Weave/Graphix/Shader.h @@ -2,6 +2,7 @@ #include #include +#include "../GLM.h" using std::string; @@ -20,6 +21,11 @@ public: uint getHandle() const; operator uint() const; + //static int setAttribLocation(const string& name) const; + static int setUniformLocation(const string& name, const mat4& mat); + static int setUniformLocation(const string& name, const vec4& vec); + static int setUniformLocation(const string& name, const int& i); + static const Shader * gActive(); static const Shader * gBasic(); static const Shader * gBasicTexture(); diff --git a/Weave/Graphix/Texture.cpp b/Weave/Graphix/Texture.cpp index 0acf729..98529c5 100644 --- a/Weave/Graphix/Texture.cpp +++ b/Weave/Graphix/Texture.cpp @@ -141,21 +141,14 @@ void Texture::useTexture() const { /* bind Texture*/ - auto tmp = Shader::gActive()->getUniformLocation("uColorTexture"); - if (tmp < 0) { - return; - } - int unit = 0; glActiveTexture(GL_TEXTURE0 + unit); glBindTexture(TEXTURE_TYPE, handle); - glUniform1i(tmp, unit); + + Shader::setUniformLocation("uColorTexture", unit); /* bind Material*/ - int tmp2 = Shader::gActive()->getUniformLocation("material"); - if (tmp2 >= 0) - glUniform4fv(tmp2, 1, value_ptr(material)); - + Shader::setUniformLocation("material", material); } Texture::operator string() const diff --git a/Weave/Graphix/ViewPort.cpp b/Weave/Graphix/ViewPort.cpp index 8ef047e..058ca31 100644 --- a/Weave/Graphix/ViewPort.cpp +++ b/Weave/Graphix/ViewPort.cpp @@ -38,36 +38,19 @@ void ViewPort::useViewPort() const void ViewPort::bindView(const Shader* shader) const{ shader->useShader(); - int tmp = -1; - tmp = shader->getUniformLocation("uProjection"); - if (tmp >= 0) - glUniformMatrix4fv(tmp, 1, false, value_ptr(projection)); + Shader::setUniformLocation("uProjection", projection); + Shader::setUniformLocation("uInvProjection", glm::inverse(projection)); + Shader::setUniformLocation("uView", view); - tmp = shader->getUniformLocation("uInvProjection"); - if (tmp >= 0) - glUniformMatrix4fv(tmp, 1, false, value_ptr(glm::inverse(projection))); - - tmp = shader->getUniformLocation("uView"); - if (tmp >= 0) - glUniformMatrix4fv(tmp, 1, false, value_ptr(view)); } void ViewPort::bindView(const Shader* shader, vec3 pos) const{ shader->useShader(); - int tmp = -1; - - tmp = shader->getUniformLocation("uProjection"); - if (tmp>=0) - glUniformMatrix4fv(tmp, 1, false, value_ptr(projection)); - - tmp = shader->getUniformLocation("uInvProjection"); - if (tmp >= 0) - glUniformMatrix4fv(tmp, 1, false, value_ptr(glm::inverse(projection))); - tmp = shader->getUniformLocation("uView"); - if (tmp >= 0) - glUniformMatrix4fv(tmp, 1, false, value_ptr(translate(view,-pos))); + Shader::setUniformLocation("uProjection", projection); + Shader::setUniformLocation("uInvProjection", glm::inverse(projection)); + Shader::setUniformLocation("uView", translate(view, -pos)); } void ViewPort::rotateView(float angle_x, float angle_y){ diff --git a/shader/basic_VS.hlsl b/shader/basic_VS.hlsl index 56a8a28..4b0bf86 100644 --- a/shader/basic_VS.hlsl +++ b/shader/basic_VS.hlsl @@ -2,7 +2,7 @@ #version 330 core in vec3 aPosition; -in vec2 aUVs; +in vec2 aUV; uniform mat4 uProjection; uniform mat4 uModel = mat4(1.f); @@ -16,14 +16,14 @@ out vec2 fUVs; void main() { vec4 position; - fUVs = aUVs; + fUVs = aUV; if(uPlain) position = vec4(aPosition, 1.f); else position = uProjection * uView * uModel * vec4(aPosition, 1.f); - if (uFar) + if(uFar) gl_Position = position.xyww; else gl_Position = position; diff --git a/shader/plane_VS.hlsl b/shader/plane_VS.hlsl deleted file mode 100644 index 845903c..0000000 --- a/shader/plane_VS.hlsl +++ /dev/null @@ -1,12 +0,0 @@ -//Vertex Shader -#version 330 core -in vec3 aPosition; -in vec2 aUVs; - -out vec2 fUVs; - -void main() -{ - fUVs = aUVs; - gl_Position = vec4(aPosition, 1.0f); -} \ No newline at end of file diff --git a/shader/skybox_VS.hlsl b/shader/skybox_VS.hlsl deleted file mode 100644 index 5afc37f..0000000 --- a/shader/skybox_VS.hlsl +++ /dev/null @@ -1,15 +0,0 @@ -//Vertex Shader -#version 330 - -in vec3 aNormal, aPosition; -in vec2 aUV; - -out vec2 fUVs; - -uniform mat4 uProjection, uView, uModel=mat4(1.f); - -void main() -{ - fUVs = aUV; - gl_Position = (uProjection * uView * uModel * vec4(aPosition, 1.f)).xyww; -} \ No newline at end of file -- 2.47.3