Merge pull request #5380 from jherico/maggie

Restoring starfield size and color variation
This commit is contained in:
Brad Hefta-Gaub 2015-07-22 07:58:15 -07:00
commit 2bd2004bda
5 changed files with 179 additions and 84 deletions

View file

@ -16,3 +16,4 @@ ExternalProject_Get_Property(${EXTERNAL_NAME} SOURCE_DIR)
set(${EXTERNAL_NAME_UPPER}_INCLUDE_DIRS ${SOURCE_DIR}/include CACHE TYPE INTERNAL)
set_target_properties(${EXTERNAL_NAME} PROPERTIES FOLDER "hidden/externals")

View file

@ -14,7 +14,7 @@
#include <mutex>
#include <QElapsedTimer>
#include <gpu/GPUConfig.h>
#include <gpu/Batch.h>
#include <gpu/Context.h>
#include <NumericalConstants.h>
@ -24,14 +24,17 @@
#include <RenderArgs.h>
#include <ViewFrustum.h>
#include "../../libraries/render-utils/standardTransformPNTC_vert.h"
#include "../../libraries/render-utils/stars_vert.h"
#include "../../libraries/render-utils/stars_frag.h"
#include "../../libraries/render-utils/standardTransformPNTC_vert.h"
#include "../../libraries/render-utils/starsGrid_frag.h"
//static const float TILT = 0.23f;
static const float TILT = 0.0f;
static const unsigned int STARFIELD_NUM_STARS = 50000;
static const unsigned int STARFIELD_SEED = 1;
//static const float STAR_COLORIZATION = 0.1f;
static const float STAR_COLORIZATION = 0.1f;
static const float TAU = 6.28318530717958f;
//static const float HALF_TAU = TAU / 2.0f;
@ -109,52 +112,81 @@ unsigned computeStarColor(float colorization) {
return red | (green << 8) | (blue << 16);
}
struct StarVertex {
vec4 position;
vec4 colorAndSize;
};
// FIXME star colors
void Stars::render(RenderArgs* renderArgs, float alpha) {
static gpu::BufferPointer vertexBuffer;
static gpu::Stream::FormatPointer streamFormat;
static gpu::Element positionElement, colorElement;
static gpu::PipelinePointer _pipeline;
static gpu::PipelinePointer _gridPipeline;
static gpu::PipelinePointer _starsPipeline;
static int32_t _timeSlot{ -1 };
static std::once_flag once;
const int VERTICES_SLOT = 0;
//const int COLOR_SLOT = 2;
const int COLOR_SLOT = 1;
std::call_once(once, [&] {
{
auto vs = gpu::ShaderPointer(gpu::Shader::createVertex(std::string(standardTransformPNTC_vert)));
auto ps = gpu::ShaderPointer(gpu::Shader::createPixel(std::string(starsGrid_frag)));
auto program = gpu::ShaderPointer(gpu::Shader::createProgram(vs, ps));
gpu::Shader::makeProgram((*program));
_timeSlot = program->getBuffers().findLocation(UNIFORM_TIME_NAME);
if (_timeSlot == gpu::Shader::INVALID_LOCATION) {
_timeSlot = program->getUniforms().findLocation(UNIFORM_TIME_NAME);
}
auto state = gpu::StatePointer(new gpu::State());
// enable decal blend
state->setDepthTest(gpu::State::DepthTest(false));
state->setBlendFunction(true, gpu::State::SRC_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::INV_SRC_ALPHA);
_gridPipeline.reset(gpu::Pipeline::create(program, state));
}
{
auto vs = gpu::ShaderPointer(gpu::Shader::createVertex(std::string(stars_vert)));
auto ps = gpu::ShaderPointer(gpu::Shader::createPixel(std::string(stars_frag)));
auto program = gpu::ShaderPointer(gpu::Shader::createProgram(vs, ps));
gpu::Shader::makeProgram((*program));
auto state = gpu::StatePointer(new gpu::State());
// enable decal blend
state->setDepthTest(gpu::State::DepthTest(false));
state->setBlendFunction(true, gpu::State::SRC_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::INV_SRC_ALPHA);
_starsPipeline.reset(gpu::Pipeline::create(program, state));
}
QElapsedTimer startTime;
startTime.start();
vertexBuffer.reset(new gpu::Buffer);
srand(STARFIELD_SEED);
unsigned limit = STARFIELD_NUM_STARS;
std::vector<vec3> points;
std::vector<StarVertex> points;
points.resize(limit);
for (size_t star = 0; star < limit; ++star) {
points[star] = fromPolar(randPolar());
//auto color = computeStarColor(STAR_COLORIZATION);
//vertexBuffer->append(sizeof(color), (const gpu::Byte*)&color);
points[star].position = vec4(fromPolar(randPolar()), 1);
float size = frand() * 5.0f + 0.5f;
if (frand() < STAR_COLORIZATION) {
vec3 color(frand() / 2.0f + 0.5f, frand() / 2.0f + 0.5f, frand() / 2.0f + 0.5f);
points[star].colorAndSize = vec4(color, size);
} else {
vec3 color(frand() / 2.0f + 0.5f);
points[star].colorAndSize = vec4(color, size);
}
}
vertexBuffer->append(sizeof(vec3) * limit, (const gpu::Byte*)&points[0]);
streamFormat.reset(new gpu::Stream::Format()); // 1 for everyone
streamFormat->setAttribute(gpu::Stream::POSITION, VERTICES_SLOT, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ), 0);
positionElement = streamFormat->getAttributes().at(gpu::Stream::POSITION)._element;
double timeDiff = (double)startTime.nsecsElapsed() / 1000000.0; // ns to ms
qDebug() << "Total time to generate stars: " << timeDiff << " msec";
auto vs = gpu::ShaderPointer(gpu::Shader::createVertex(std::string(standardTransformPNTC_vert)));
auto ps = gpu::ShaderPointer(gpu::Shader::createPixel(std::string(stars_frag)));
auto program = gpu::ShaderPointer(gpu::Shader::createProgram(vs, ps));
gpu::Shader::makeProgram((*program));
_timeSlot = program->getBuffers().findLocation(UNIFORM_TIME_NAME);
if (_timeSlot == gpu::Shader::INVALID_LOCATION) {
_timeSlot = program->getUniforms().findLocation(UNIFORM_TIME_NAME);
}
auto state = gpu::StatePointer(new gpu::State());
// enable decal blend
state->setDepthTest(gpu::State::DepthTest(false));
state->setBlendFunction(true, gpu::State::SRC_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::INV_SRC_ALPHA);
_pipeline.reset(gpu::Pipeline::create(program, state));
vertexBuffer->append(sizeof(StarVertex) * limit, (const gpu::Byte*)&points[0]);
streamFormat.reset(new gpu::Stream::Format()); // 1 for everyone
streamFormat->setAttribute(gpu::Stream::POSITION, VERTICES_SLOT, gpu::Element(gpu::VEC4, gpu::FLOAT, gpu::XYZW), 0);
streamFormat->setAttribute(gpu::Stream::COLOR, COLOR_SLOT, gpu::Element(gpu::VEC4, gpu::FLOAT, gpu::RGBA));
positionElement = streamFormat->getAttributes().at(gpu::Stream::POSITION)._element;
colorElement = streamFormat->getAttributes().at(gpu::Stream::COLOR)._element;
});
auto geometryCache = DependencyManager::get<GeometryCache>();
@ -168,18 +200,31 @@ void Stars::render(RenderArgs* renderArgs, float alpha) {
batch.setResourceTexture(0, textureCache->getWhiteTexture());
// Render the world lines
batch.setPipeline(_pipeline);
batch.setPipeline(_gridPipeline);
static auto start = usecTimestampNow();
float msecs = (float)(usecTimestampNow() - start) / (float)USECS_PER_MSEC;
float secs = msecs / (float)MSECS_PER_SECOND;
batch._glUniform1f(_timeSlot, secs);
geometryCache->renderUnitCube(batch);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
static const size_t VERTEX_STRIDE = sizeof(StarVertex);
size_t offset = offsetof(StarVertex, position);
gpu::BufferView posView(vertexBuffer, offset, vertexBuffer->getSize(), VERTEX_STRIDE, positionElement);
offset = offsetof(StarVertex, colorAndSize);
gpu::BufferView colView(vertexBuffer, offset, vertexBuffer->getSize(), VERTEX_STRIDE, colorElement);
// Render the stars
geometryCache->useSimpleDrawPipeline(batch);
batch.setPipeline(_starsPipeline);
batch._glEnable(GL_PROGRAM_POINT_SIZE_EXT);
batch._glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
batch._glEnable(GL_POINT_SMOOTH);
batch.setInputFormat(streamFormat);
batch.setInputBuffer(VERTICES_SLOT, gpu::BufferView(vertexBuffer, positionElement));
batch.setInputBuffer(VERTICES_SLOT, posView);
batch.setInputBuffer(COLOR_SLOT, colView);
batch.draw(gpu::Primitive::POINTS, STARFIELD_NUM_STARS);
renderArgs->_context->render(batch);
}

View file

@ -1,63 +1,17 @@
<@include gpu/Config.slh@>
<$VERSION_HEADER$>
#line __LINE__
// Generated on <$_SCRIBE_DATE$>
// stars.frag
// fragment shader
//
// Created by Bradley Austin Davis on 2015/06/19
// Created by Bradley Austin Davis on 6/10/15.
// Copyright 2015 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
varying vec2 varTexcoord;
varying vec3 varNomral;
varying vec3 varPosition;
uniform float iGlobalTime;
const float PI = 3.14159;
const float TAU = 3.14159 * 2.0;
const int latitudeCount = 5;
const float latitudeDist = PI / 2.0 / float(latitudeCount);
const int meridianCount = 4;
const float merdianDist = PI / float(meridianCount);
float clampLine(float val, float target) {
return clamp((1.0 - abs((val - target)) - 0.998) * 500.0, 0.0, 1.0);
}
float latitude(vec2 pos, float angle) {
float result = clampLine(pos.y, angle);
if (angle != 0.0) {
result += clampLine(pos.y, -angle);
}
return result;
}
float meridian(vec2 pos, float angle) {
return clampLine(pos.x, angle) + clampLine(pos.x + PI, angle);
}
vec2 toPolar(in vec3 dir) {
vec2 polar = vec2(atan(dir.z, dir.x), asin(dir.y));
return polar;
}
void mainVR( out vec4 fragColor, in vec2 fragCoord, in vec3 fragRayOri, in vec3 fragRayDir )
{
vec2 polar = toPolar(fragRayDir);
//polar.x += mod(iGlobalTime / 12.0, PI / 4.0) - PI / 4.0;
float c = 0.0;
for (int i = 0; i < latitudeCount - 1; ++i) {
c += latitude(polar, float(i) * latitudeDist);
}
for (int i = 0; i < meridianCount; ++i) {
c += meridian(polar, float(i) * merdianDist);
}
const vec3 col_lines = vec3(102.0 / 255.0, 136.0 / 255.0, 221.0 / 255.0);
fragColor = vec4(c * col_lines, 0.2);
}
varying vec4 varColor;
void main(void) {
mainVR(gl_FragColor, gl_FragCoord.xy, vec3(0.0), normalize(varPosition));
gl_FragColor = varColor; //vec4(varColor, 1.0);
}

View file

@ -0,0 +1,32 @@
<@include gpu/Config.slh@>
<$VERSION_HEADER$>
// Generated on <$_SCRIBE_DATE$>
//
// standardTransformPNTC.slv
// vertex shader
//
// Created by Sam Gateau on 6/10/2015.
// Copyright 2015 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
<@include gpu/Transform.slh@>
<$declareStandardTransform()$>
varying vec3 varPosition;
varying vec4 varColor;
void main(void) {
varColor = gl_Color.rgba;
// standard transform
TransformCamera cam = getTransformCamera();
TransformObject obj = getTransformObject();
<$transformModelToClipPos(cam, obj, gl_Vertex, gl_Position)$>
varPosition = gl_Vertex.xyz;
gl_PointSize = gl_Color.a;
}

View file

@ -0,0 +1,63 @@
<@include gpu/Config.slh@>
<$VERSION_HEADER$>
#line __LINE__
// Generated on <$_SCRIBE_DATE$>
// stars.frag
// fragment shader
//
// Created by Bradley Austin Davis on 2015/06/19
varying vec2 varTexcoord;
varying vec3 varNomral;
varying vec3 varPosition;
uniform float iGlobalTime;
const float PI = 3.14159;
const float TAU = 3.14159 * 2.0;
const int latitudeCount = 5;
const float latitudeDist = PI / 2.0 / float(latitudeCount);
const int meridianCount = 4;
const float merdianDist = PI / float(meridianCount);
float clampLine(float val, float target) {
return clamp((1.0 - abs((val - target)) - 0.998) * 500.0, 0.0, 1.0);
}
float latitude(vec2 pos, float angle) {
float result = clampLine(pos.y, angle);
if (angle != 0.0) {
result += clampLine(pos.y, -angle);
}
return result;
}
float meridian(vec2 pos, float angle) {
return clampLine(pos.x, angle) + clampLine(pos.x + PI, angle);
}
vec2 toPolar(in vec3 dir) {
vec2 polar = vec2(atan(dir.z, dir.x), asin(dir.y));
return polar;
}
void mainVR( out vec4 fragColor, in vec2 fragCoord, in vec3 fragRayOri, in vec3 fragRayDir )
{
vec2 polar = toPolar(fragRayDir);
//polar.x += mod(iGlobalTime / 12.0, PI / 4.0) - PI / 4.0;
float c = 0.0;
for (int i = 0; i < latitudeCount - 1; ++i) {
c += latitude(polar, float(i) * latitudeDist);
}
for (int i = 0; i < meridianCount; ++i) {
c += meridian(polar, float(i) * merdianDist);
}
const vec3 col_lines = vec3(102.0 / 255.0, 136.0 / 255.0, 221.0 / 255.0);
fragColor = vec4(c * col_lines, 0.2);
}
void main(void) {
mainVR(gl_FragColor, gl_FragCoord.xy, vec3(0.0), normalize(varPosition));
}