When the stars and the atmosphere are both on, adjust the stars' alpha value

based on the distance from the atmosphere.
This commit is contained in:
Andrzej Kapolka 2013-05-08 13:13:56 -07:00
parent 8a21823469
commit caeccf59ec
5 changed files with 35 additions and 15 deletions

View file

@ -34,7 +34,7 @@ float Stars::changeLOD(float fraction, float overalloc, float realloc) {
return float(_ptrController->changeLOD(fraction, overalloc, realloc)); return float(_ptrController->changeLOD(fraction, overalloc, realloc));
} }
void Stars::render(float fovY, float aspect, float nearZ) { void Stars::render(float fovY, float aspect, float nearZ, float alpha) {
// determine length of screen diagonal from quadrant height and aspect ratio // determine length of screen diagonal from quadrant height and aspect ratio
float quadrantHeight = nearZ * tan(angleConvert<Degrees,Radians>(fovY) * 0.5f); float quadrantHeight = nearZ * tan(angleConvert<Degrees,Radians>(fovY) * 0.5f);
@ -46,7 +46,7 @@ void Stars::render(float fovY, float aspect, float nearZ) {
// pull the modelview matrix off the GL stack // pull the modelview matrix off the GL stack
glm::mat4 view; glGetFloatv(GL_MODELVIEW_MATRIX, glm::value_ptr(view)); glm::mat4 view; glGetFloatv(GL_MODELVIEW_MATRIX, glm::value_ptr(view));
_ptrController->render(fovDiagonal, aspect, glm::affineInverse(view)); _ptrController->render(fovDiagonal, aspect, glm::affineInverse(view), alpha);
} }

View file

@ -37,7 +37,7 @@ class Stars {
// Renders the starfield from a local viewer's perspective. // Renders the starfield from a local viewer's perspective.
// The parameters specifiy the field of view. // The parameters specifiy the field of view.
// //
void render(float fovY, float aspect, float nearZ); void render(float fovY, float aspect, float nearZ, float alpha);
// //
// Sets the resolution for FOV culling. // Sets the resolution for FOV culling.

View file

@ -686,8 +686,21 @@ void displaySide(Camera& whichCamera) {
if (::starsOn) { if (::starsOn) {
// should be the first rendering pass - w/o depth buffer / lighting // should be the first rendering pass - w/o depth buffer / lighting
// compute starfield alpha based on distance from atmosphere
float alpha = 1.0f;
if (::atmosphereOn) {
float height = glm::distance(whichCamera.getPosition(), environment.getAtmosphereCenter());
if (height < environment.getAtmosphereInnerRadius()) {
alpha = 0.0f;
} else if (height < environment.getAtmosphereOuterRadius()) {
alpha = (height - environment.getAtmosphereInnerRadius()) /
(environment.getAtmosphereOuterRadius() - environment.getAtmosphereInnerRadius());
}
}
// finally render the starfield // finally render the starfield
stars.render(whichCamera.getFieldOfView(), aspectRatio, whichCamera.getNearClip()); stars.render(whichCamera.getFieldOfView(), aspectRatio, whichCamera.getNearClip(), alpha);
} }
// draw the sky dome // draw the sky dome

View file

@ -361,7 +361,7 @@ namespace starfield {
public: public:
void render(float perspective, float angle, mat4 const& orientation) { void render(float perspective, float angle, mat4 const& orientation, float alpha) {
#if STARFIELD_MULTITHREADING #if STARFIELD_MULTITHREADING
// check out renderer // check out renderer
@ -377,7 +377,7 @@ namespace starfield {
#else #else
BrightnessLevel b = _valLodBrightness; BrightnessLevel b = _valLodBrightness;
#endif #endif
renderer->render(perspective, angle, orientation, b); renderer->render(perspective, angle, orientation, b, alpha);
} }
#if STARFIELD_MULTITHREADING #if STARFIELD_MULTITHREADING

View file

@ -13,6 +13,8 @@
#error "This is an implementation file - not intended for direct inclusion." #error "This is an implementation file - not intended for direct inclusion."
#endif #endif
#include "renderer/ProgramObject.h"
#include "starfield/Config.h" #include "starfield/Config.h"
#include "starfield/data/InputVertex.h" #include "starfield/data/InputVertex.h"
#include "starfield/data/BrightnessLevel.h" #include "starfield/data/BrightnessLevel.h"
@ -70,7 +72,8 @@ namespace starfield {
GLint* _arrBatchOffs; GLint* _arrBatchOffs;
GLsizei* _arrBatchCount; GLsizei* _arrBatchCount;
GLuint _hndVertexArray; GLuint _hndVertexArray;
OGlProgram _objProgram; ProgramObject _objProgram;
int _alphaLocation;
Tiling _objTiling; Tiling _objTiling;
@ -123,7 +126,8 @@ namespace starfield {
void render(float perspective, void render(float perspective,
float aspect, float aspect,
mat4 const& orientation, mat4 const& orientation,
BrightnessLevel minBright) { BrightnessLevel minBright,
float alpha) {
// printLog(" // printLog("
// Stars.cpp: rendering at minimal brightness %d\n", minBright); // Stars.cpp: rendering at minimal brightness %d\n", minBright);
@ -186,7 +190,7 @@ namespace starfield {
# define matrix matrix_debug # define matrix matrix_debug
#endif #endif
this->glBatch(glm::value_ptr(matrix), prepareBatch( this->glBatch(glm::value_ptr(matrix), prepareBatch(
(unsigned*) _arrBatchOffs, _itrOutIndex) ); (unsigned*) _arrBatchOffs, _itrOutIndex), alpha);
#if STARFIELD_DEBUG_CULLING #if STARFIELD_DEBUG_CULLING
# undef matrix # undef matrix
@ -463,24 +467,26 @@ namespace starfield {
GLchar const* const VERTEX_SHADER = GLchar const* const VERTEX_SHADER =
"#version 120\n" "#version 120\n"
"uniform float alpha;\n"
"void main(void) {\n" "void main(void) {\n"
" vec3 c = gl_Color.rgb * 1.0125;\n" " vec3 c = gl_Color.rgb * 1.0125;\n"
" float s = max(1.0, dot(c, c) * 0.7);\n" " float s = max(1.0, dot(c, c) * 0.7);\n"
" gl_Position = ftransform();\n" " gl_Position = ftransform();\n"
" gl_FrontColor= gl_Color;\n" " gl_FrontColor= gl_Color * alpha;\n"
" gl_PointSize = s;\n" " gl_PointSize = s;\n"
"}\n"; "}\n";
_objProgram.addShader(GL_VERTEX_SHADER, VERTEX_SHADER); _objProgram.attachFromSourceCode(GL_VERTEX_SHADER, VERTEX_SHADER);
GLchar const* const FRAGMENT_SHADER = GLchar const* const FRAGMENT_SHADER =
"#version 120\n" "#version 120\n"
"void main(void) {\n" "void main(void) {\n"
" gl_FragColor = gl_Color;\n" " gl_FragColor = gl_Color;\n"
"}\n"; "}\n";
_objProgram.addShader(GL_FRAGMENT_SHADER, FRAGMENT_SHADER); _objProgram.attachFromSourceCode(GL_FRAGMENT_SHADER, FRAGMENT_SHADER);
_objProgram.link(); _objProgram.link();
_alphaLocation = _objProgram.getUniformLocation("alpha");
glGenBuffersARB(1, & _hndVertexArray); glGenBuffersARB(1, & _hndVertexArray);
} }
@ -499,7 +505,7 @@ namespace starfield {
glBindBufferARB(GL_ARRAY_BUFFER, 0); glBindBufferARB(GL_ARRAY_BUFFER, 0);
} }
void glBatch(GLfloat const* matrix, GLsizei n_ranges) { void glBatch(GLfloat const* matrix, GLsizei n_ranges, float alpha) {
// printLog("Stars.cpp: rendering %d-multibatch\n", n_ranges); // printLog("Stars.cpp: rendering %d-multibatch\n", n_ranges);
@ -527,7 +533,8 @@ namespace starfield {
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE); glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
// select shader and vertex array // select shader and vertex array
_objProgram.activate(); _objProgram.bind();
_objProgram.setUniform(_alphaLocation, alpha);
glBindBufferARB(GL_ARRAY_BUFFER, _hndVertexArray); glBindBufferARB(GL_ARRAY_BUFFER, _hndVertexArray);
glInterleavedArrays(GL_C4UB_V3F, sizeof(GpuVertex), 0l); glInterleavedArrays(GL_C4UB_V3F, sizeof(GpuVertex), 0l);
@ -537,7 +544,7 @@ namespace starfield {
// restore state // restore state
glBindBufferARB(GL_ARRAY_BUFFER, 0); glBindBufferARB(GL_ARRAY_BUFFER, 0);
glUseProgram(0); _objProgram.release();
glDisable(GL_VERTEX_PROGRAM_POINT_SIZE); glDisable(GL_VERTEX_PROGRAM_POINT_SIZE);
glDisable(GL_POINT_SMOOTH); glDisable(GL_POINT_SMOOTH);
glPopMatrix(); glPopMatrix();