diff --git a/src/celengine/galaxy.cpp b/src/celengine/galaxy.cpp index c67464cb8..532e5896c 100644 --- a/src/celengine/galaxy.cpp +++ b/src/celengine/galaxy.cpp @@ -414,7 +414,7 @@ void Galaxy::renderGalaxyPointSprites(const Vector3f& offset, GLushort j = 0; prog->use(); - prog->mat4Param("MVPMatrix") = (*ms.projection) * mv; + prog->MVPMatrix = (*ms.projection) * mv; prog->samplerParam("galaxyTex") = 0; prog->samplerParam("colorTex") = 1; diff --git a/src/celengine/glmarker.cpp b/src/celengine/glmarker.cpp index 0b08e9004..4ca5b040a 100644 --- a/src/celengine/glmarker.cpp +++ b/src/celengine/glmarker.cpp @@ -363,7 +363,7 @@ void Renderer::renderSelectionPointer(const Observer& observer, prog->use(); const Vector3f ¢er = cameraMatrix.col(2); - prog->mat4Param("MVPMatrix") = getProjectionMatrix() * getModelViewMatrix() * vecgl::translate(Vector3f(-center)); + prog->MVPMatrix = getProjectionMatrix() * getModelViewMatrix() * vecgl::translate(Vector3f(-center)); prog->vec4Param("color") = Color(SelectionCursorColor, 0.6f).toVector4(); prog->floatParam("pixelSize") = pixelSize; prog->floatParam("s") = s; @@ -401,7 +401,7 @@ void Renderer::renderEclipticLine() initVO(markerVO); prog->use(); - prog->mat4Param("MVPMatrix") = getProjectionMatrix() * getModelViewMatrix(); + prog->MVPMatrix = getProjectionMatrix() * getModelViewMatrix(); prog->vec4Param("color") = EclipticColor.toVector4(); markerVO.draw(GL_LINE_LOOP, EclipticCount, EclipticOffset); @@ -437,7 +437,7 @@ void Renderer::renderCrosshair(float selectionSizeInPixels, float cursorGrow = max(1.0f, min(2.5f, (selectionSizeInPixels - 10.0f) / 100.0f)); prog->use(); - prog->mat4Param("MVPMatrix") = (*m.projection) * (*m.modelview); + prog->MVPMatrix = (*m.projection) * (*m.modelview); prog->vec4Param("color") = color.toVector4(); prog->floatParam("radius") = cursorRadius; prog->floatParam("width") = minCursorWidth * cursorGrow; diff --git a/src/celengine/globular.cpp b/src/celengine/globular.cpp index 8ffc0720a..0c969456e 100644 --- a/src/celengine/globular.cpp +++ b/src/celengine/globular.cpp @@ -468,7 +468,7 @@ void Globular::renderGlobularPointSprites( centerTex[ic]->bind(); Matrix4f mvp = (*m.projection) * (*m.modelview); - tidalProg->mat4Param("MVPMatrix") = mvp; + tidalProg->MVPMatrix = mvp; Matrix3f viewMat = viewerOrientation.conjugate().toRotationMatrix(); tidalProg->vec4Param("color") = Vector4f(Rr, Gg, Bb, min(2 * brightness * pixelWeight, 1.0f)); @@ -492,8 +492,8 @@ void Globular::renderGlobularPointSprites( globProg->use(); globularTex->bind(); - globProg->mat4Param("MVPMatrix") = mvp; - globProg->mat4Param("ModelViewMatrix") = vecgl::translate(*m.modelview, offset); + globProg->MVPMatrix = mvp; + globProg->ModelViewMatrix = vecgl::translate(*m.modelview, offset); Matrix3f mx = Scaling(form->scale) * getOrientation().toRotationMatrix() * Scaling(tidalSize); globProg->mat3Param("m") = mx; globProg->vec3Param("offset") = offset; diff --git a/src/celengine/glshader.cpp b/src/celengine/glshader.cpp index c8f7e6ac9..a552f58cf 100644 --- a/src/celengine/glshader.cpp +++ b/src/celengine/glshader.cpp @@ -68,16 +68,6 @@ GLShader::~GLShader() //************* GLxxxProperty ********** -FloatShaderParameter::FloatShaderParameter() : - slot(-1) -{ -} - -FloatShaderParameter::FloatShaderParameter(GLuint obj, const char* name) -{ - slot = glGetUniformLocation(obj, name); -} - FloatShaderParameter& FloatShaderParameter::operator=(float f) { @@ -86,17 +76,6 @@ FloatShaderParameter::operator=(float f) return *this; } - -Vec3ShaderParameter::Vec3ShaderParameter() : - slot(-1) -{ -} - -Vec3ShaderParameter::Vec3ShaderParameter(GLuint obj, const char* name) -{ - slot = glGetUniformLocation(obj, name); -} - Vec3ShaderParameter& Vec3ShaderParameter::operator=(const Eigen::Vector3f& v) { @@ -105,16 +84,6 @@ Vec3ShaderParameter::operator=(const Eigen::Vector3f& v) return *this; } -Vec4ShaderParameter::Vec4ShaderParameter() : - slot(-1) -{ -} - -Vec4ShaderParameter::Vec4ShaderParameter(GLuint obj, const char* name) -{ - slot = glGetUniformLocation(obj, name); -} - Vec4ShaderParameter& Vec4ShaderParameter::operator=(const Eigen::Vector4f& v) { @@ -123,17 +92,6 @@ Vec4ShaderParameter::operator=(const Eigen::Vector4f& v) return *this; } - -IntegerShaderParameter::IntegerShaderParameter() : - slot(-1) -{ -} - -IntegerShaderParameter::IntegerShaderParameter(GLuint obj, const char* name) -{ - slot = glGetUniformLocation(obj, name); -} - IntegerShaderParameter& IntegerShaderParameter::operator=(int i) { @@ -142,17 +100,6 @@ IntegerShaderParameter::operator=(int i) return *this; } - -Mat3ShaderParameter::Mat3ShaderParameter() : - slot(-1) -{ -} - -Mat3ShaderParameter::Mat3ShaderParameter(GLuint obj, const char* name) -{ - slot = glGetUniformLocation(obj, name); -} - Mat3ShaderParameter& Mat3ShaderParameter::operator=(const Eigen::Matrix3f& v) { @@ -161,17 +108,6 @@ Mat3ShaderParameter::operator=(const Eigen::Matrix3f& v) return *this; } - -Mat4ShaderParameter::Mat4ShaderParameter() : - slot(-1) -{ -} - -Mat4ShaderParameter::Mat4ShaderParameter(GLuint obj, const char* name) -{ - slot = glGetUniformLocation(obj, name); -} - Mat4ShaderParameter& Mat4ShaderParameter::operator=(const Eigen::Matrix4f& v) { diff --git a/src/celengine/glshader.h b/src/celengine/glshader.h index f0361de5c..541d944f4 100644 --- a/src/celengine/glshader.h +++ b/src/celengine/glshader.h @@ -84,82 +84,79 @@ class GLProgram friend class GLShaderLoader; }; - -class FloatShaderParameter +class ShaderParameter { public: - FloatShaderParameter(); - FloatShaderParameter(GLuint obj, const char* name); + ShaderParameter() = default; + ~ShaderParameter() = default; + ShaderParameter(const ShaderParameter&) = default; + ShaderParameter(ShaderParameter&&) = default; + ShaderParameter& operator=(const ShaderParameter&) = default; + ShaderParameter& operator=(ShaderParameter&&) = default; + ShaderParameter(GLuint obj, const char* name) + { + slot = glGetUniformLocation(obj, name); + } + + bool valid() const noexcept + { + return slot != -1; + }; + + operator bool() const noexcept + { + return valid(); + } + protected: + int slot { -1 }; +}; + +class FloatShaderParameter : public ShaderParameter +{ + public: + using ShaderParameter::ShaderParameter; FloatShaderParameter& operator=(float); - - private: - int slot; }; -class Vec3ShaderParameter +class Vec3ShaderParameter : public ShaderParameter { public: - Vec3ShaderParameter(); - Vec3ShaderParameter(GLuint obj, const char* name); - + using ShaderParameter::ShaderParameter; Vec3ShaderParameter& operator=(const Eigen::Vector3f&); - - private: - int slot; }; -class Vec4ShaderParameter +class Vec4ShaderParameter : public ShaderParameter { public: - Vec4ShaderParameter(); - Vec4ShaderParameter(GLuint obj, const char* name); - + using ShaderParameter::ShaderParameter; Vec4ShaderParameter& operator=(const Eigen::Vector4f&); - - private: - int slot; }; -class IntegerShaderParameter +class IntegerShaderParameter : public ShaderParameter { public: - IntegerShaderParameter(); - IntegerShaderParameter(GLuint obj, const char* name); - + using ShaderParameter::ShaderParameter; IntegerShaderParameter& operator=(int); - - private: - int slot; }; -class Mat3ShaderParameter +class Mat3ShaderParameter : public ShaderParameter { public: - Mat3ShaderParameter(); - Mat3ShaderParameter(GLuint obj, const char* name); - + using ShaderParameter::ShaderParameter; Mat3ShaderParameter& operator=(const Eigen::Matrix3f&); - - private: - int slot; }; -class Mat4ShaderParameter +class Mat4ShaderParameter : public ShaderParameter { public: - Mat4ShaderParameter(); - Mat4ShaderParameter(GLuint obj, const char* name); - + using ShaderParameter::ShaderParameter; Mat4ShaderParameter& operator=(const Eigen::Matrix4f&); - - private: - int slot; }; diff --git a/src/celengine/pointstarvertexbuffer.cpp b/src/celengine/pointstarvertexbuffer.cpp index 83a93a295..042e6065e 100644 --- a/src/celengine/pointstarvertexbuffer.cpp +++ b/src/celengine/pointstarvertexbuffer.cpp @@ -36,8 +36,10 @@ void PointStarVertexBuffer::startSprites() if (prog == nullptr) return; prog->use(); - prog->mat4Param("MVPMatrix") = renderer.getProjectionMatrix() * renderer.getModelViewMatrix(); - prog->samplerParam("starTex") = 0; + prog->MVPMatrix = renderer.getProjectionMatrix() * renderer.getModelViewMatrix(); + if (!starTexParam) + starTexParam = prog->samplerParam("starTex"); + starTexParam = 0; unsigned int stride = sizeof(StarVertex); glEnableVertexAttribArray(CelestiaGLProgram::VertexCoordAttributeIndex); @@ -106,7 +108,7 @@ void PointStarVertexBuffer::render() glPointSize(1.0f); } #endif - glVertexAttribPointer(CelestiaGLProgram::VertexCoordAttributeIndex, + glVertexAttribPointer(CelestiaGLProgram::VertexCoordAttributeIndex, 3, GL_FLOAT, GL_FALSE, stride, &vertices[0].position); glVertexAttribPointer(CelestiaGLProgram::ColorAttributeIndex, diff --git a/src/celengine/pointstarvertexbuffer.h b/src/celengine/pointstarvertexbuffer.h index 0aca72068..48444f456 100644 --- a/src/celengine/pointstarvertexbuffer.h +++ b/src/celengine/pointstarvertexbuffer.h @@ -11,6 +11,7 @@ #pragma once #include +#include "glshader.h" class Color; class Renderer; @@ -53,6 +54,7 @@ private: StarVertex* vertices { nullptr }; Texture* texture { nullptr }; bool useSprites { false }; + IntegerShaderParameter starTexParam; }; inline void PointStarVertexBuffer::addStar(const Eigen::Vector3f& pos, diff --git a/src/celengine/rendcontext.cpp b/src/celengine/rendcontext.cpp index 21dc86256..f4d7fc4ac 100644 --- a/src/celengine/rendcontext.cpp +++ b/src/celengine/rendcontext.cpp @@ -577,7 +577,9 @@ GLSL_RenderContext::makeCurrent(const Material& m) shadowBias.diagonal() = Vector4f(0.5f, 0.5f, 0.5f, 1.0f); shadowBias.col(3) = Vector4f(0.5f, 0.5f, 0.5f, 1.0f); prog->ShadowMatrix0 = shadowBias * (*lightMatrix); - prog->floatParam("shadowMapSize") = static_cast(shadowMapWidth); + if (!shadowMapSizeParam) + shadowMapSizeParam = prog->floatParam("shadowMapSize"); + shadowMapSizeParam = static_cast(shadowMapWidth); } // setLightParameters() expects opacity in the alpha channel of the diffuse color diff --git a/src/celengine/rendcontext.h b/src/celengine/rendcontext.h index 917b14ac1..c260090b7 100644 --- a/src/celengine/rendcontext.h +++ b/src/celengine/rendcontext.h @@ -120,6 +120,7 @@ class GLSL_RenderContext : public RenderContext const Eigen::Matrix4f *lightMatrix { nullptr }; GLuint shadowMap { 0 }; GLuint shadowMapWidth { 0 }; + FloatShaderParameter shadowMapSizeParam; }; diff --git a/src/celengine/render.cpp b/src/celengine/render.cpp index c9f225881..2975b9b32 100644 --- a/src/celengine/render.cpp +++ b/src/celengine/render.cpp @@ -1799,7 +1799,7 @@ void renderPoint(const Renderer &renderer, prog->use(); prog->samplerParam("starTex") = 0; - prog->mat4Param("MVPMatrix") = (*m.projection) * (*m.modelview); + prog->MVPMatrix = (*m.projection) * (*m.modelview); #ifndef GL_ES glEnable(GL_POINT_SPRITE); @@ -3687,7 +3687,7 @@ void Renderer::renderCometTail(const Body& body, setBlendingFactors(GL_SRC_ALPHA, GL_ONE); prog->use(); - prog->mat4Param("MVPMatrix") = (*m.projection) * (*m.modelview) * vecgl::translate(pos); + prog->MVPMatrix = (*m.projection) * (*m.modelview) * vecgl::translate(pos); glEnableVertexAttribArray(CelestiaGLProgram::VertexCoordAttributeIndex); glEnableVertexAttribArray(CelestiaGLProgram::NormalAttributeIndex); diff --git a/src/celengine/renderglsl.cpp b/src/celengine/renderglsl.cpp index e50743f90..0d8f63702 100644 --- a/src/celengine/renderglsl.cpp +++ b/src/celengine/renderglsl.cpp @@ -943,7 +943,7 @@ void renderGeometryShadow_GLSL(Geometry* geometry, Matrix4f projMat = Ortho(-1.f, 1.f, -1.f, 1.f, -1.f, 1.f); Matrix4f modelViewMat = directionalLightMatrix(ls.lights[lightIndex].direction_obj); *lightMatrix = projMat * modelViewMat; - prog->mat4Param("MVPMatrix") = *lightMatrix; + prog->MVPMatrix = *lightMatrix; geometry->render(rc, tsec); diff --git a/src/celengine/shadermanager.cpp b/src/celengine/shadermanager.cpp index efd6a95fd..2811b69ba 100644 --- a/src/celengine/shadermanager.cpp +++ b/src/celengine/shadermanager.cpp @@ -3352,6 +3352,7 @@ CelestiaGLProgram::CelestiaGLProgram(GLProgram& _program, CelestiaGLProgram::CelestiaGLProgram(GLProgram& _program) : program(&_program) { + initCommonParameters(); } CelestiaGLProgram::~CelestiaGLProgram() @@ -3417,10 +3418,17 @@ CelestiaGLProgram::attribIndex(const std::string& paramName) const void -CelestiaGLProgram::initParameters() +CelestiaGLProgram::initCommonParameters() { ModelViewMatrix = mat4Param("ModelViewMatrix"); MVPMatrix = mat4Param("MVPMatrix"); +} + + +void +CelestiaGLProgram::initParameters() +{ + initCommonParameters(); for (unsigned int i = 0; i < props.nLights; i++) { diff --git a/src/celengine/shadermanager.h b/src/celengine/shadermanager.h index 26e937a6f..ce6896026 100644 --- a/src/celengine/shadermanager.h +++ b/src/celengine/shadermanager.h @@ -270,6 +270,7 @@ class CelestiaGLProgram int attribIndex(const std::string&) const; private: + void initCommonParameters(); void initParameters(); void initSamplers();