Skip to content

Commit

Permalink
Only save original texture coordinate once
Browse files Browse the repository at this point in the history
  • Loading branch information
levinli303 committed Aug 8, 2023
1 parent 48cbb60 commit 434938b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 59 deletions.
34 changes: 0 additions & 34 deletions src/celengine/glshader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,6 @@ GLShader::~GLShader()

//************* GLxxxProperty **********

FloatShaderParameter::FloatShaderParameter() :
slot(-1)
{
}

FloatShaderParameter::FloatShaderParameter(GLuint obj, const char* name)
{
slot = glGetUniformLocation(obj, name);
Expand All @@ -130,11 +125,6 @@ FloatShaderParameter::operator=(float f)
}


Vec2ShaderParameter::Vec2ShaderParameter() :
slot(-1)
{
}

Vec2ShaderParameter::Vec2ShaderParameter(GLuint obj, const char* name)
{
slot = glGetUniformLocation(obj, name);
Expand All @@ -149,11 +139,6 @@ Vec2ShaderParameter::operator=(const Eigen::Vector2f& v)
}


Vec3ShaderParameter::Vec3ShaderParameter() :
slot(-1)
{
}

Vec3ShaderParameter::Vec3ShaderParameter(GLuint obj, const char* name)
{
slot = glGetUniformLocation(obj, name);
Expand All @@ -167,10 +152,6 @@ Vec3ShaderParameter::operator=(const Eigen::Vector3f& v)
return *this;
}

Vec4ShaderParameter::Vec4ShaderParameter() :
slot(-1)
{
}

Vec4ShaderParameter::Vec4ShaderParameter(GLuint obj, const char* name)
{
Expand All @@ -186,11 +167,6 @@ Vec4ShaderParameter::operator=(const Eigen::Vector4f& v)
}


IntegerShaderParameter::IntegerShaderParameter() :
slot(-1)
{
}

IntegerShaderParameter::IntegerShaderParameter(GLuint obj, const char* name)
{
slot = glGetUniformLocation(obj, name);
Expand All @@ -205,11 +181,6 @@ IntegerShaderParameter::operator=(int i)
}


Mat3ShaderParameter::Mat3ShaderParameter() :
slot(-1)
{
}

Mat3ShaderParameter::Mat3ShaderParameter(GLuint obj, const char* name)
{
slot = glGetUniformLocation(obj, name);
Expand All @@ -224,11 +195,6 @@ Mat3ShaderParameter::operator=(const Eigen::Matrix3f& v)
}


Mat4ShaderParameter::Mat4ShaderParameter() :
slot(-1)
{
}

Mat4ShaderParameter::Mat4ShaderParameter(GLuint obj, const char* name)
{
slot = glGetUniformLocation(obj, name);
Expand Down
28 changes: 14 additions & 14 deletions src/celengine/glshader.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,91 +98,91 @@ class GLProgram
class FloatShaderParameter
{
public:
FloatShaderParameter();
FloatShaderParameter() = default;
FloatShaderParameter(GLuint obj, const char* name);

FloatShaderParameter& operator=(float);

private:
int slot;
int slot{ -1 };
};


class Vec2ShaderParameter
{
public:
Vec2ShaderParameter();
Vec2ShaderParameter() = default;
Vec2ShaderParameter(GLuint obj, const char* name);

Vec2ShaderParameter& operator=(const Eigen::Vector2f&);

private:
int slot;
int slot{ -1 };
};


class Vec3ShaderParameter
{
public:
Vec3ShaderParameter();
Vec3ShaderParameter() = default;
Vec3ShaderParameter(GLuint obj, const char* name);

Vec3ShaderParameter& operator=(const Eigen::Vector3f&);

private:
int slot;
int slot{ -1 };
};


class Vec4ShaderParameter
{
public:
Vec4ShaderParameter();
Vec4ShaderParameter() = default;
Vec4ShaderParameter(GLuint obj, const char* name);

Vec4ShaderParameter& operator=(const Eigen::Vector4f&);

private:
int slot;
int slot{ -1 };
};


class IntegerShaderParameter
{
public:
IntegerShaderParameter();
IntegerShaderParameter() = default;
IntegerShaderParameter(GLuint obj, const char* name);

IntegerShaderParameter& operator=(int);

private:
int slot;
int slot{ -1 };
};


class Mat3ShaderParameter
{
public:
Mat3ShaderParameter();
Mat3ShaderParameter() = default;
Mat3ShaderParameter(GLuint obj, const char* name);

Mat3ShaderParameter& operator=(const Eigen::Matrix3f&);

private:
int slot;
int slot{ -1 };
};


class Mat4ShaderParameter
{
public:
Mat4ShaderParameter();
Mat4ShaderParameter() = default;
Mat4ShaderParameter(GLuint obj, const char* name);

Mat4ShaderParameter& operator=(const Eigen::Matrix4f&);

private:
int slot;
int slot{ -1 };
};


Expand Down
8 changes: 4 additions & 4 deletions src/celengine/lodspheremesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ createVertices(std::vector<float>& vertices,
vertices.push_back(-ctheta);
}

for (int tex = 0; tex < tc.nTexturesUsed; tex++)
if (tc.nTexturesUsed > 0)
{
vertices.push_back(static_cast<float>(theta));
vertices.push_back(static_cast<float>(phi));
Expand Down Expand Up @@ -464,7 +464,7 @@ void LODSphereMesh::render(unsigned int attributes,
vertexSize = 3;
if ((attributes & Tangents) != 0)
vertexSize += 3;
for (int i = 0; i < nTextures; i++)
if (nTextures > 0)
vertexSize += 2;

glEnableVertexAttribArray(CelestiaGLProgram::VertexCoordAttributeIndex);
Expand Down Expand Up @@ -651,7 +651,7 @@ LODSphereMesh::renderSection(int phi0, int theta0, int extent,
{
glVertexAttribPointer(CelestiaGLProgram::TextureCoord0AttributeIndex + tc,
2, GL_FLOAT, GL_FALSE,
stride, PTR(((tc * 2) + texCoordOffset) * sizeof(float)));
stride, PTR(texCoordOffset * sizeof(float)));
}

if ((ri.attributes & Tangents) != 0)
Expand Down Expand Up @@ -731,7 +731,7 @@ LODSphereMesh::renderSection(int phi0, int theta0, int extent,
vertices.clear();
int perVertexFloats = (ri.attributes & Tangents) == 0 ? 3 : 6;
int expectedVertices = ((phi1 - phi0) / ri.step + 1) *
((theta1 - theta0) / ri.step + 1) * (perVertexFloats + nTexturesUsed * 2);
((theta1 - theta0) / ri.step + 1) * (perVertexFloats + (nTexturesUsed > 0 ? 2 : 0));
assert(expectedVertices <= maxVertices * MaxVertexSize);
vertices.reserve(expectedVertices);
if ((ri.attributes & Tangents) == 0)
Expand Down
7 changes: 2 additions & 5 deletions src/celengine/shadermanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -870,10 +870,7 @@ ShadowDepth(unsigned int i)
std::string
TexCoord2D(unsigned int i, bool transform)
{
auto str = fmt::format("in_TexCoord{}.st", i);
if (!transform)
return str;
return fmt::format("(texCoordBase{} + texCoordDelta{} * {})", i, i, str);
return transform ? fmt::format("(texCoordBase{} + texCoordDelta{} * in_TexCoord{}.st)", i, i, i) : fmt::format("in_TexCoord{}.st", i);
}


Expand Down Expand Up @@ -3496,7 +3493,7 @@ CelestiaGLProgram::initParameters()

if (props.hasTextureCoordTransform())
{
for (unsigned int i = 0; i < maxTextureCount; ++i)
for (unsigned int i = 0; i < texCoordTransforms.size(); ++i)
{
texCoordTransforms[i].base = vec2Param(fmt::format("texCoordBase{}", i).c_str());
texCoordTransforms[i].delta = vec2Param(fmt::format("texCoordDelta{}", i).c_str());
Expand Down
4 changes: 2 additions & 2 deletions src/celengine/shadermanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#pragma once

#include <array>
#include <cstdint>
#include <map>
#include <string>
Expand Down Expand Up @@ -235,8 +236,7 @@ class CelestiaGLProgram
FloatShaderParameter cloudHeight;
FloatShaderParameter shadowTextureOffset;

static constexpr std::size_t maxTextureCount = 4;
CelestiaGLProgramTextureTransform texCoordTransforms[maxTextureCount];
std::array<CelestiaGLProgramTextureTransform, 4> texCoordTransforms;

// Parameters for atmospheric scattering; all distances are normalized for
// a unit sphere.
Expand Down

0 comments on commit 434938b

Please sign in to comment.