History

Include the "sky from space" shaders, too, and use them when the camera is outside the atmosphere's outer radius.

Andrzej Kapolka committed 13 years ago

Commit 82f16e71 parent 310c0284 files · +246 −47Browse files

Showing the changes to interface/src/Environment.cpp (1 of 4 files). Show all

Unified · Split

interface/src/Environment.cpp+63−34
@@ -5,6 +5,8 @@ · 4 unmodified lines
55 // Created by Andrzej Kapolka on 5/6/13.
66 // Copyright (c) 2013 High Fidelity, Inc. All rights reserved.
77
8+#include <QByteArray>
9+
810 #include "Camera.h"
911 #include "Environment.h"
1012 #include "renderer/ProgramObject.h"
@@ -12,24 +14,8 @@ · 1 unmodified line
1214 #include "world.h"
1315
1416 void Environment::init() {
15− _skyFromAtmosphereProgram = new ProgramObject();
16− _skyFromAtmosphereProgram->attachFromSourceFile(GL_VERTEX_SHADER_ARB, "resources/shaders/SkyFromAtmosphere.vert");
17− _skyFromAtmosphereProgram->attachFromSourceFile(GL_FRAGMENT_SHADER_ARB, "resources/shaders/SkyFromAtmosphere.frag");
18− _skyFromAtmosphereProgram->link();
19−
20− _cameraPosLocation = _skyFromAtmosphereProgram->getUniformLocation("v3CameraPos");
21− _lightPosLocation = _skyFromAtmosphereProgram->getUniformLocation("v3LightPos");
22− _invWavelengthLocation = _skyFromAtmosphereProgram->getUniformLocation("v3InvWavelength");
23− _innerRadiusLocation = _skyFromAtmosphereProgram->getUniformLocation("fInnerRadius");
24− _krESunLocation = _skyFromAtmosphereProgram->getUniformLocation("fKrESun");
25− _kmESunLocation = _skyFromAtmosphereProgram->getUniformLocation("fKmESun");
26− _kr4PiLocation = _skyFromAtmosphereProgram->getUniformLocation("fKr4PI");
27− _km4PiLocation = _skyFromAtmosphereProgram->getUniformLocation("fKm4PI");
28− _scaleLocation = _skyFromAtmosphereProgram->getUniformLocation("fScale");
29− _scaleDepthLocation = _skyFromAtmosphereProgram->getUniformLocation("fScaleDepth");
30− _scaleOverScaleDepthLocation = _skyFromAtmosphereProgram->getUniformLocation("fScaleOverScaleDepth");
31− _gLocation = _skyFromAtmosphereProgram->getUniformLocation("g");
32− _g2Location = _skyFromAtmosphereProgram->getUniformLocation("g2");
17+ _skyFromAtmosphereProgram = createSkyProgram("Atmosphere", _skyFromAtmosphereUniformLocations);
18+ _skyFromSpaceProgram = createSkyProgram("Space", _skyFromSpaceUniformLocations);
3319 }
3420
3521 void Environment::renderAtmosphere(Camera& camera) {
@@ -44,32 +30,48 @@ void Environment::renderAtmosphere(Camera& camera) { · 8 unmodified lines
4430 float projection[16];
4531 glGetFloatv(GL_PROJECTION_MATRIX, projection);
4632 glm::vec3 relativeCameraPos = camera.getPosition() - getAtmosphereCenter();
47− float near = camera.getNearClip(), far = glm::length(relativeCameraPos) + getAtmosphereOuterRadius();
33+ float height = glm::length(relativeCameraPos);
34+ float near = camera.getNearClip(), far = height + getAtmosphereOuterRadius();
4835 projection[10] = (far + near) / (near - far);
4936 projection[14] = (2.0f * far * near) / (near - far);
5037 glLoadMatrixf(projection);
5138
39+ // use the appropriate shader depending on whether we're inside or outside
40+ ProgramObject* program;
41+ int* locations;
42+ if (height < getAtmosphereOuterRadius()) {
43+ program = _skyFromAtmosphereProgram;
44+ locations = _skyFromAtmosphereUniformLocations;
45+
46+ } else {
47+ program = _skyFromSpaceProgram;
48+ locations = _skyFromSpaceUniformLocations;
49+ }
50+
5251 // the constants here are from Sean O'Neil's GPU Gems entry
5352 // (http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter16.html), GameEngine.cpp
54− _skyFromAtmosphereProgram->bind();
55− _skyFromAtmosphereProgram->setUniform(_cameraPosLocation, relativeCameraPos);
53+ program->bind();
54+ program->setUniform(locations[CAMERA_POS_LOCATION], relativeCameraPos);
5655 glm::vec3 lightDirection = glm::normalize(getSunLocation());
57− _skyFromAtmosphereProgram->setUniform(_lightPosLocation, lightDirection);
58− _skyFromAtmosphereProgram->setUniform(_invWavelengthLocation,
56+ program->setUniform(locations[LIGHT_POS_LOCATION], lightDirection);
57+ program->setUniform(locations[INV_WAVELENGTH_LOCATION],
5958 1 / powf(getScatteringWavelengths().r, 4.0f),
6059 1 / powf(getScatteringWavelengths().g, 4.0f),
6160 1 / powf(getScatteringWavelengths().b, 4.0f));
62− _skyFromAtmosphereProgram->setUniform(_innerRadiusLocation, getAtmosphereInnerRadius());
63− _skyFromAtmosphereProgram->setUniform(_krESunLocation, getRayleighScattering() * getSunBrightness());
64− _skyFromAtmosphereProgram->setUniform(_kmESunLocation, getMieScattering() * getSunBrightness());
65− _skyFromAtmosphereProgram->setUniform(_kr4PiLocation, getRayleighScattering() * 4.0f * PIf);
66− _skyFromAtmosphereProgram->setUniform(_km4PiLocation, getMieScattering() * 4.0f * PIf);
67− _skyFromAtmosphereProgram->setUniform(_scaleLocation, 1.0f / (getAtmosphereOuterRadius() - getAtmosphereInnerRadius()));
68− _skyFromAtmosphereProgram->setUniform(_scaleDepthLocation, 0.25f);
69− _skyFromAtmosphereProgram->setUniform(_scaleOverScaleDepthLocation,
61+ program->setUniform(locations[CAMERA_HEIGHT2_LOCATION], height * height);
62+ program->setUniform(locations[OUTER_RADIUS_LOCATION], getAtmosphereOuterRadius());
63+ program->setUniform(locations[OUTER_RADIUS2_LOCATION], getAtmosphereOuterRadius() * getAtmosphereOuterRadius());
64+ program->setUniform(locations[INNER_RADIUS_LOCATION], getAtmosphereInnerRadius());
65+ program->setUniform(locations[KR_ESUN_LOCATION], getRayleighScattering() * getSunBrightness());
66+ program->setUniform(locations[KM_ESUN_LOCATION], getMieScattering() * getSunBrightness());
67+ program->setUniform(locations[KR_4PI_LOCATION], getRayleighScattering() * 4.0f * PIf);
68+ program->setUniform(locations[KM_4PI_LOCATION], getMieScattering() * 4.0f * PIf);
69+ program->setUniform(locations[SCALE_LOCATION], 1.0f / (getAtmosphereOuterRadius() - getAtmosphereInnerRadius()));
70+ program->setUniform(locations[SCALE_DEPTH_LOCATION], 0.25f);
71+ program->setUniform(locations[SCALE_OVER_SCALE_DEPTH_LOCATION],
7072 (1.0f / (getAtmosphereOuterRadius() - getAtmosphereInnerRadius())) / 0.25f);
71− _skyFromAtmosphereProgram->setUniform(_gLocation, -0.990f);
72− _skyFromAtmosphereProgram->setUniform(_g2Location, -0.990f * -0.990f);
73+ program->setUniform(locations[G_LOCATION], -0.990f);
74+ program->setUniform(locations[G2_LOCATION], -0.990f * -0.990f);
7375
7476 glFrontFace(GL_CW);
7577 glDepthMask(GL_FALSE);
@@ -78,10 +80,37 @@ void Environment::renderAtmosphere(Camera& camera) { · 2 unmodified lines
7880 glDepthMask(GL_TRUE);
7981 glFrontFace(GL_CCW);
8082
81− _skyFromAtmosphereProgram->release();
83+ program->release();
8284
8385 glPopMatrix();
8486
8587 glMatrixMode(GL_MODELVIEW);
8688 glPopMatrix();
8789 }
90+
91+ProgramObject* Environment::createSkyProgram(const char* from, int* locations) {
92+ ProgramObject* program = new ProgramObject();
93+ QByteArray prefix = QByteArray("resources/shaders/SkyFrom") + from;
94+ program->attachFromSourceFile(GL_VERTEX_SHADER_ARB, prefix + ".vert");
95+ program->attachFromSourceFile(GL_FRAGMENT_SHADER_ARB, prefix + ".frag");
96+ program->link();
97+
98+ locations[CAMERA_POS_LOCATION] = program->getUniformLocation("v3CameraPos");
99+ locations[LIGHT_POS_LOCATION] = program->getUniformLocation("v3LightPos");
100+ locations[INV_WAVELENGTH_LOCATION] = program->getUniformLocation("v3InvWavelength");
101+ locations[CAMERA_HEIGHT2_LOCATION] = program->getUniformLocation("fCameraHeight2");
102+ locations[OUTER_RADIUS_LOCATION] = program->getUniformLocation("fOuterRadius");
103+ locations[OUTER_RADIUS2_LOCATION] = program->getUniformLocation("fOuterRadius2");
104+ locations[INNER_RADIUS_LOCATION] = program->getUniformLocation("fInnerRadius");
105+ locations[KR_ESUN_LOCATION] = program->getUniformLocation("fKrESun");
106+ locations[KM_ESUN_LOCATION] = program->getUniformLocation("fKmESun");
107+ locations[KR_4PI_LOCATION] = program->getUniformLocation("fKr4PI");
108+ locations[KM_4PI_LOCATION] = program->getUniformLocation("fKm4PI");
109+ locations[SCALE_LOCATION] = program->getUniformLocation("fScale");
110+ locations[SCALE_DEPTH_LOCATION] = program->getUniformLocation("fScaleDepth");
111+ locations[SCALE_OVER_SCALE_DEPTH_LOCATION] = program->getUniformLocation("fScaleOverScaleDepth");
112+ locations[G_LOCATION] = program->getUniformLocation("g");
113+ locations[G2_LOCATION] = program->getUniformLocation("g2");
114+
115+ return program;
116+}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t