Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Nebula 3D Rendering to enable Distant Nebulas #2132

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified resources/Nebula1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified resources/Nebula2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified resources/Nebula3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions resources/shaders/nebula.shader
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[vertex]
uniform vec4 color;
uniform mat4 view;
uniform mat4 projection;
uniform mat4 model;

attribute vec3 position;
attribute vec2 texcoords;

varying vec2 fragtexcoords;
varying vec4 fragcolor;

void main()
{
fragtexcoords = texcoords;
gl_Position = projection * ((view * model * vec4(position, 1.0)) + vec4((texcoords.x - 0.5) * color.a, (texcoords.y - 0.5) * color.a, 0.0, 0.0));
fragcolor = vec4(color.rgb, 1.0);
}

[fragment]
uniform sampler2D textureMap;

varying vec4 fragcolor;
varying vec2 fragtexcoords;

void main()
{
vec4 texColor = texture2D(textureMap, fragtexcoords.st);
texColor.rgb *= texColor.a; // Pre-multiply alpha channel

gl_FragColor = texColor * fragcolor;
}
1 change: 1 addition & 0 deletions src/shaderRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace ShaderRegistry
"shaders/basic",
"shaders/basicColor",
"shaders/billboard",
"shaders/nebula",
"shaders/objectShader",
"shaders/objectShader:ILLUMINATION",
"shaders/objectShader:SPECULAR",
Expand Down
1 change: 1 addition & 0 deletions src/shaderRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace ShaderRegistry
Basic = 0,
BasicColor,
Billboard,
Nebula,
Object,
ObjectIllumination,
ObjectSpecular,
Expand Down
28 changes: 20 additions & 8 deletions src/spaceObjects/nebula.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Nebula::Nebula()

void Nebula::draw3DTransparent()
{
ShaderRegistry::ScopedShader shader(ShaderRegistry::Shaders::Billboard);
ShaderRegistry::ScopedShader shader(ShaderRegistry::Shaders::Nebula);

std::array<VertexAndTexCoords, 4> quad{
glm::vec3{}, {0.f, 1.f},
Expand All @@ -71,34 +71,46 @@ void Nebula::draw3DTransparent()
gl::ScopedVertexAttribArray positions(shader.get().attribute(ShaderRegistry::Attributes::Position));
gl::ScopedVertexAttribArray texcoords(shader.get().attribute(ShaderRegistry::Attributes::Texcoords));

for(int n=0; n<cloud_count; n++)
glVertexAttribPointer(positions.get(), 3, GL_FLOAT, GL_FALSE, sizeof(VertexAndTexCoords), (GLvoid*)quad.data());
glVertexAttribPointer(texcoords.get(), 2, GL_FLOAT, GL_FALSE, sizeof(VertexAndTexCoords), (GLvoid*)((char*)quad.data() + sizeof(glm::vec3)));

// Enable blending for pre-multiplied alpha textures
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBlendEquation(GL_FUNC_ADD);
for(int n = 0; n < cloud_count; n++)
{
NebulaCloud& cloud = clouds[n];

glm::vec3 position = glm::vec3(getPosition().x, getPosition().y, 0) + glm::vec3(cloud.offset.x, cloud.offset.y, 0);
float size = cloud.size;

float distance = glm::length(camera_position - position);
float alpha = 1.0f - (distance / 10000.0f);
if (alpha < 0.0f)
float alpha = distance / 10000.0f; // Linearly scale alpha based on distance

if (alpha < 0.0f) // Skip clouds if their alpha is below 0
continue;
if (alpha > 1.0f) // Clamp alpha to 1.0 for clouds further away than the limit
alpha = 1.0f;

// setup our quad.
// Setup quad vertices with the current position
for (auto& point : quad)
{
point.vertex = position;
}

textureManager.getTexture("Nebula" + string(cloud.texture) + ".png")->bind();
glUniform4f(shader.get().uniform(ShaderRegistry::Uniforms::Color), alpha * 0.8f, alpha * 0.8f, alpha * 0.8f, size);
// Update uniforms for each cloud
glUniform4f(shader.get().uniform(ShaderRegistry::Uniforms::Color), alpha, alpha, alpha, size);
auto model_matrix = glm::translate(getModelMatrix(), {cloud.offset.x, cloud.offset.y, 0});
glUniformMatrix4fv(shader.get().uniform(ShaderRegistry::Uniforms::Model), 1, GL_FALSE, glm::value_ptr(model_matrix));

glVertexAttribPointer(positions.get(), 3, GL_FLOAT, GL_FALSE, sizeof(VertexAndTexCoords), (GLvoid*)quad.data());
glVertexAttribPointer(texcoords.get(), 2, GL_FLOAT, GL_FALSE, sizeof(VertexAndTexCoords), (GLvoid*)((char*)quad.data() + sizeof(glm::vec3)));
std::initializer_list<uint16_t> indices = { 0, 3, 2, 0, 2, 1 };
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, std::begin(indices));
}

// Restore blending after the loop
glBlendFunc(GL_ONE, GL_ONE);
glBlendEquation(GL_FUNC_ADD);
}

void Nebula::drawOnRadar(sp::RenderTarget& renderer, glm::vec2 position, float scale, float rotation, bool long_range)
Expand Down