Skip to content

Commit

Permalink
Add support for tex coord transform in genersated shader
Browse files Browse the repository at this point in the history
  • Loading branch information
levinli303 committed Aug 8, 2023
1 parent 4bcab1c commit 48cbb60
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 36 deletions.
19 changes: 19 additions & 0 deletions src/celengine/glshader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,25 @@ FloatShaderParameter::operator=(float f)
}


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

Vec2ShaderParameter::Vec2ShaderParameter(GLuint obj, const char* name)
{
slot = glGetUniformLocation(obj, name);
}

Vec2ShaderParameter&
Vec2ShaderParameter::operator=(const Eigen::Vector2f& v)
{
if (slot != -1)
glUniform2fv(slot, 1, v.data());
return *this;
}


Vec3ShaderParameter::Vec3ShaderParameter() :
slot(-1)
{
Expand Down
13 changes: 13 additions & 0 deletions src/celengine/glshader.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@ class FloatShaderParameter
};


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

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

private:
int slot;
};


class Vec3ShaderParameter
{
public:
Expand Down
32 changes: 20 additions & 12 deletions src/celengine/lodspheremesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@ createVertices(std::vector<float>& vertices,

for (int tex = 0; tex < tc.nTexturesUsed; tex++)
{
vertices.push_back(tc.u0[tex] - static_cast<float>(theta) * tc.du[tex]);
vertices.push_back(tc.v0[tex] - static_cast<float>(phi) * tc.dv[tex]);
vertices.push_back(static_cast<float>(theta));
vertices.push_back(static_cast<float>(phi));
}
}
}
Expand All @@ -292,16 +292,18 @@ void
LODSphereMesh::render(const celmath::Frustum& frustum,
float pixWidth,
Texture** tex,
int nTextures)
int nTextures,
CelestiaGLProgram *program)
{
render(Normals, frustum, pixWidth, tex, nTextures);
render(Normals, frustum, pixWidth, tex, nTextures, program);
}


void
LODSphereMesh::render(unsigned int attributes,
const celmath::Frustum& frustum,
float pixWidth,
CelestiaGLProgram *program,
Texture* tex0,
Texture* tex1,
Texture* tex2,
Expand All @@ -317,15 +319,16 @@ LODSphereMesh::render(unsigned int attributes,
if (tex3 != nullptr)
tex.try_push_back(tex3);
render(attributes, frustum, pixWidth,
tex.data(), static_cast<int>(tex.size()));
tex.data(), static_cast<int>(tex.size()), program);
}


void LODSphereMesh::render(unsigned int attributes,
const celmath::Frustum& frustum,
float pixWidth,
Texture** tex,
int nTextures)
int nTextures,
CelestiaGLProgram *program)
{
int lod = 64;
int lodBias = getSphereLOD(pixWidth);
Expand Down Expand Up @@ -478,7 +481,7 @@ void LODSphereMesh::render(unsigned int attributes,

if (split == 1)
{
renderSection(0, 0, thetaExtent, ri);
renderSection(0, 0, thetaExtent, ri, program);
}
else
{
Expand Down Expand Up @@ -517,7 +520,7 @@ void LODSphereMesh::render(unsigned int attributes,
for (int j = 0; j < 2; j++)
{
renderPatches(i * extent / 2, j * extent,
extent, split / 2, ri);
extent, split / 2, ri, program);
}
}
}
Expand Down Expand Up @@ -549,7 +552,8 @@ void
LODSphereMesh::renderPatches(int phi0, int theta0,
int extent,
int level,
const RenderInfo& ri)
const RenderInfo& ri,
CelestiaGLProgram *program)
{
int thetaExtent = extent;
int phiExtent = extent / 2;
Expand Down Expand Up @@ -606,7 +610,7 @@ LODSphereMesh::renderPatches(int phi0, int theta0,

if (level == 1)
{
renderSection(phi0, theta0, thetaExtent, ri);
renderSection(phi0, theta0, thetaExtent, ri, program);
return;
}

Expand All @@ -618,15 +622,16 @@ LODSphereMesh::renderPatches(int phi0, int theta0,
theta0 + thetaExtent / 2 * j,
extent / 2,
level / 2,
ri);
ri,
program);
}
}
}


void
LODSphereMesh::renderSection(int phi0, int theta0, int extent,
const RenderInfo& ri)
const RenderInfo& ri, CelestiaGLProgram *program)

{
auto stride = static_cast<GLsizei>(vertexSize * sizeof(float));
Expand Down Expand Up @@ -710,6 +715,9 @@ LODSphereMesh::renderSection(int phi0, int theta0, int extent,
tc.u0[tex] = tc.u0[tex] * tile.du + tile.u;
tc.v0[tex] = tc.v0[tex] * tile.dv + tile.v;

program->texCoordTransforms[tex].base = Eigen::Vector2f(tc.u0[tex], tc.v0[tex]);
program->texCoordTransforms[tex].delta = Eigen::Vector2f(-tc.du[tex], -tc.dv[tex]);

// We track the current texture to avoid unnecessary and costly
// texture state changes.
if (tile.texID != subtextures[tex])
Expand Down
13 changes: 8 additions & 5 deletions src/celengine/lodspheremesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ namespace celmath
class Frustum;
}

class CelestiaGLProgram;

class LODSphereMesh
{
public:
Expand All @@ -40,12 +42,12 @@ class LODSphereMesh
LODSphereMesh& operator=(LODSphereMesh&&) = delete;

void render(unsigned int attributes, const celmath::Frustum&, float pixWidth,
Texture** tex, int nTextures);
void render(unsigned int attributes, const celmath::Frustum&, float pixWidth,
Texture** tex, int nTextures, CelestiaGLProgram *);
void render(unsigned int attributes, const celmath::Frustum&, float pixWidth, CelestiaGLProgram *,
Texture* tex0 = nullptr, Texture* tex1 = nullptr,
Texture* tex2 = nullptr, Texture* tex3 = nullptr);
void render(const celmath::Frustum&, float pixWidth,
Texture** tex, int nTextures);
Texture** tex, int nTextures, CelestiaGLProgram *);

enum
{
Expand Down Expand Up @@ -74,9 +76,10 @@ class LODSphereMesh
void renderPatches(int phi0, int theta0,
int extent,
int level,
const RenderInfo&);
const RenderInfo&,
CelestiaGLProgram *);

void renderSection(int phi0, int theta0, int extent, const RenderInfo&);
void renderSection(int phi0, int theta0, int extent, const RenderInfo&, CelestiaGLProgram *);

int vertexSize{ 0 };

Expand Down
9 changes: 5 additions & 4 deletions src/celengine/render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1834,11 +1834,12 @@ static void renderSphereUnlit(const RenderInfo& ri,
celestia::util::ArrayVector<Texture*, LODSphereMesh::MAX_SPHERE_MESH_TEXTURES> textures;

ShaderProperties shadprop;
shadprop.texUsage = ShaderProperties::TextureCoordTransform;

// Set up the textures used by this object
if (ri.baseTex != nullptr)
{
shadprop.texUsage = ShaderProperties::DiffuseTexture;
shadprop.texUsage |= ShaderProperties::DiffuseTexture;
textures.try_push_back(ri.baseTex);
}
if (ri.nightTex != nullptr)
Expand Down Expand Up @@ -1869,7 +1870,7 @@ static void renderSphereUnlit(const RenderInfo& ri,
r->setPipelineState(ps);

g_lodSphere->render(frustum, ri.pixWidth,
textures.data(), static_cast<int>(textures.size()));
textures.data(), static_cast<int>(textures.size()), prog);
}


Expand All @@ -1881,7 +1882,7 @@ static void renderCloudsUnlit(const RenderInfo& ri,
Renderer *r)
{
ShaderProperties shadprop;
shadprop.texUsage = ShaderProperties::DiffuseTexture;
shadprop.texUsage = ShaderProperties::DiffuseTexture | ShaderProperties::TextureCoordTransform;
shadprop.lightModel = ShaderProperties::UnlitModel;

// Get a shader for the current rendering configuration
Expand All @@ -1898,7 +1899,7 @@ static void renderCloudsUnlit(const RenderInfo& ri,
ps.depthTest = true;
r->setPipelineState(ps);

g_lodSphere->render(frustum, ri.pixWidth, &cloudTex, 1);
g_lodSphere->render(frustum, ri.pixWidth, &cloudTex, 1, prog);
}

void
Expand Down
10 changes: 6 additions & 4 deletions src/celengine/renderglsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,13 @@ void renderEllipsoid_GLSL(const RenderInfo& ri,
celestia::util::ArrayVector<Texture*, LODSphereMesh::MAX_SPHERE_MESH_TEXTURES> textures;

ShaderProperties shadprop;
shadprop.texUsage = ShaderProperties::TextureCoordTransform;
shadprop.nLights = std::min(ls.nLights, MaxShaderLights);

// Set up the textures used by this object
if (ri.baseTex != nullptr)
{
shadprop.texUsage = ShaderProperties::DiffuseTexture;
shadprop.texUsage |= ShaderProperties::DiffuseTexture;
textures.try_push_back(ri.baseTex);
}

Expand Down Expand Up @@ -435,7 +436,7 @@ void renderEllipsoid_GLSL(const RenderInfo& ri,
textures.erase(endTextures, textures.end());
g_lodSphere->render(attributes,
frustum, ri.pixWidth,
textures.data(), static_cast<int>(textures.size()));
textures.data(), static_cast<int>(textures.size()), prog);
}


Expand Down Expand Up @@ -630,12 +631,13 @@ void renderClouds_GLSL(const RenderInfo& ri,
celestia::util::ArrayVector<Texture*, LODSphereMesh::MAX_SPHERE_MESH_TEXTURES> textures;

ShaderProperties shadprop;
shadprop.texUsage = ShaderProperties::TextureCoordTransform;
shadprop.nLights = ls.nLights;

// Set up the textures used by this object
if (cloudTex != nullptr)
{
shadprop.texUsage = ShaderProperties::DiffuseTexture;
shadprop.texUsage |= ShaderProperties::DiffuseTexture;
textures.try_push_back(cloudTex);
}

Expand Down Expand Up @@ -718,7 +720,7 @@ void renderClouds_GLSL(const RenderInfo& ri,
textures.erase(endTextures, textures.end());
g_lodSphere->render(attributes,
frustum, ri.pixWidth,
textures.data(), static_cast<int>(textures.size()));
textures.data(), static_cast<int>(textures.size()), prog);

prog->textureOffset = 0.0f;
}
Expand Down
Loading

0 comments on commit 48cbb60

Please sign in to comment.