From d15ae9d578cc7d150eb555f59be1efda38490f9c Mon Sep 17 00:00:00 2001 From: michal Date: Sat, 30 Aug 2014 20:29:14 -0400 Subject: [PATCH] some code cleanup. loading of test-target speckfile. --- shaders/star_fs.glsl | 3 + shaders/star_fs_points.glsl | 2 +- shaders/star_ge.glsl | 12 ++- shaders/star_ge_points.glsl | 5 +- shaders/star_vs.glsl | 7 +- src/rendering/stars/renderablestars.cpp | 122 ++++++++++++------------ 6 files changed, 83 insertions(+), 68 deletions(-) diff --git a/shaders/star_fs.glsl b/shaders/star_fs.glsl index 5e1baca179..2600ca057e 100644 --- a/shaders/star_fs.glsl +++ b/shaders/star_fs.glsl @@ -5,6 +5,9 @@ uniform vec3 Color; in vec4 vs_position; in vec2 texCoord; +layout(location = 2) in vec3 vs_brightness; + + out vec4 diffuse; const float k = 10.0; diff --git a/shaders/star_fs_points.glsl b/shaders/star_fs_points.glsl index f5fc5850fd..9e1535e0a9 100644 --- a/shaders/star_fs_points.glsl +++ b/shaders/star_fs_points.glsl @@ -79,7 +79,7 @@ void main(void) // set the depth gl_FragDepth = depth; - diffuse = vec4(vs_brightness[0],0.5,0.5,1);//vs_brightness[1],vs_brightness[2],1); + diffuse = vec4(1,1,1,1);//vs_brightness[1],vs_brightness[2],1); /* if(dot(gl_PointCoord-0.5,gl_PointCoord-0.5)>0.25) // HAX METHOD. diff --git a/shaders/star_ge.glsl b/shaders/star_ge.glsl index cbce4e2d98..f1d09986a1 100644 --- a/shaders/star_ge.glsl +++ b/shaders/star_ge.glsl @@ -9,20 +9,26 @@ const vec2 corners[4] = { layout(points) in; //layout(points, max_vertices = 1) out; layout(triangle_strip, max_vertices = 4) out; +layout(location = 2) in vec3 vs_brightness[]; +layout(location = 2) out vec3 ge_brightness[]; + uniform vec4 campos; -float spriteSize = 0.000007; // set here for now. +float spriteSize = 0.000001; // set here for now. out vec2 texCoord; void main(){ + ge_brightness[0] = vs_brightness[0]; + //TODO: implement distance metric. float distToPoint = 1;//(50.0*(length(gl_in[0].gl_Position - campos)) ); - + float luminocity = 1;//vs_brightness[0].x; + // EMIT QUAD for(int i=0; i<4; ++i){ vec4 pos = gl_in[0].gl_Position; - pos.xy += spriteSize*distToPoint *(corners[i] - vec2(0.5)); + pos.xy += spriteSize * distToPoint * luminocity *(corners[i] - vec2(0.5)); gl_Position = pos; texCoord = corners[i]; EmitVertex(); diff --git a/shaders/star_ge_points.glsl b/shaders/star_ge_points.glsl index 1d1599dfd7..d7c8abdad3 100644 --- a/shaders/star_ge_points.glsl +++ b/shaders/star_ge_points.glsl @@ -21,13 +21,12 @@ out vec2 texCoord; void main(){ ge_brightness[0] = vs_brightness[0]; - - + float distToPoint = 1;//(50.0*(length(gl_in[0].gl_Position - campos)) ); float radius = 1.f; // EMIT POINT - // pointscaling not enabled yet + // pointscaling not enabled yet, but possible. gl_Position = gl_in[0].gl_Position; /*float dist = length(gl_Position.xyz - campos.xyz); float psize = (radius*1000000.f) / dist;*/ diff --git a/shaders/star_vs.glsl b/shaders/star_vs.glsl index 1bf23ee18f..75f7166852 100644 --- a/shaders/star_vs.glsl +++ b/shaders/star_vs.glsl @@ -8,8 +8,11 @@ uniform vec4 objpos; uniform sampler2D texture1; -in vec4 in_position; +layout(location = 0) in vec4 in_position; +layout(location = 2) in vec3 in_brightness; + out vec4 vs_position; +out vec3 vs_brightness; //out vec4 distanceToStar; @@ -54,6 +57,8 @@ vec4 psc_scaling(vec4 v1, vec2 v2) { void main() { + vs_brightness = in_brightness; + // rotate and scale vertex with model transform and add the translation vec3 local_vertex_pos = mat3(ModelTransform) * in_position.xyz; //vec4 lvp = ModelTransform * in_position; diff --git a/src/rendering/stars/renderablestars.cpp b/src/rendering/stars/renderablestars.cpp index de2a4a47aa..cfe72f3105 100644 --- a/src/rendering/stars/renderablestars.cpp +++ b/src/rendering/stars/renderablestars.cpp @@ -57,7 +57,9 @@ int printOglError(char *file, int line) return retCode; } -#define SPRITES + +#define GLSPRITES +#define GLPOINTS namespace { const std::string _loggerCat = "RenderableStars"; @@ -101,14 +103,17 @@ std::ifstream& RenderableStars::skipToLine(std::ifstream& file, unsigned int num } bool RenderableStars::readSpeckFile(const std::string& path){ + + bool readCache = false; + std::ifstream file; std::string str, starDescription, datastr; std::vector strvec; - std::vector positions; + std::vector starcluster; std::vector floatingPointData; int count = 0; - + std::cout << path << std::endl; const std::string absPath = FileSys.absolutePath(path); std::string::size_type last = absPath.find_last_of(ghoul::filesystem::FileSystem::PathSeparator); @@ -118,11 +123,7 @@ bool RenderableStars::readSpeckFile(const std::string& path){ cacheName = basePath + "\\" + cacheName + ".bin"; /** - TODO: - Right now we are not reading cache files as luminocity, appmag etc are not cached - in the first place. This is the reason for longer loading times, will get fixed. - - The READ LOGIC is simple: + The READ LOGIC: 1. skip metadata 2. read everything in line until # symbol (nongeneric reader) 3. split line on whitespaces @@ -132,10 +133,14 @@ bool RenderableStars::readSpeckFile(const std::string& path){ 7. convert to psc 8. pass to vector 9. pass vectors internal arr for vbo creation + + TODO: + Right now we are not reading cache files as luminocity, appmag etc are not cached + in the first place. This is the reason for longer loading times, will get fixed. */ //if (!FileSys.fileExists(cacheName)){ - if (FileSys.fileExists(cacheName)){ // TODO: fix so that reads/writes cache. + if (!readCache){ // dumb boolean for now. std::ofstream cache; cache.open(cacheName, std::ios::binary); @@ -161,32 +166,33 @@ bool RenderableStars::readSpeckFile(const std::string& path){ if (mid != std::string::npos){ datastr = str.substr(0, mid); std::size_t end = str.find('\n'); - if (end == std::string::npos) + if (end == std::string::npos){ starDescription = str.substr(mid, end); + } } + + std::cout << datastr << std::endl; // split data string on whitespace -> push to to vector std::istringstream ss(datastr); std::copy(std::istream_iterator(ss), std::istream_iterator(), std::back_inserter >(strvec)); - ss.clear(); + ss.clear(); // conv. string vector to doubles floatingPointData.reserve(strvec.size()); transform(strvec.begin(), strvec.end(), back_inserter(floatingPointData), [](std::string const& val) {return std::stod(val); }); - // store data concerning apparent luminocity, brigthness etc. - // convert to powerscaled coordinate psc powerscaled = - PowerScaledCoordinate::CreatePowerScaledCoordinate(floatingPointData[0], - floatingPointData[1], - floatingPointData[2]); + PowerScaledCoordinate::CreatePowerScaledCoordinate(floatingPointData[0], + floatingPointData[1], + floatingPointData[2]); // Convert parsecs -> meter - // Could convert floatingPointData instead + // Could convert floatingPointData instead ?? // (possible as 3.4 × 10^38 is max rep nr of float) PowerScaledScalar parsecsToMetersFactor = glm::vec2(0.308567758, 17); - //powerscaled *= parsecsToMetersFactor; //<--- buggy. + //powerscaled *= parsecsToMetersFactor; // <--- buggy dont use. powerscaled[0] *= parsecsToMetersFactor[0]; powerscaled[1] *= parsecsToMetersFactor[0]; powerscaled[2] *= parsecsToMetersFactor[0]; @@ -195,21 +201,19 @@ bool RenderableStars::readSpeckFile(const std::string& path){ // We use vector to store data // needs no preallocation and has tightly packed arr. for (int i = 0; i < 4; i++){ - positions.push_back(powerscaled[i]); + starcluster.push_back(powerscaled[i]); cache << ' ' << powerscaled[i]; } // will need more elegant solution here. // TODO - positions.push_back(floatingPointData[3]); - positions.push_back(floatingPointData[4]); - positions.push_back(floatingPointData[5]); - + starcluster.push_back(floatingPointData[3]); + starcluster.push_back(floatingPointData[4]); + starcluster.push_back(floatingPointData[5]); + strvec.clear(); floatingPointData.clear(); count++; } while (file.good()); - } - else // read cached positions, NOTE: this is not fully functional. - { + }else{ LINFO("Found cached data, loading"); file.open(cacheName, std::ios::binary); while (file.good()){ @@ -217,19 +221,18 @@ bool RenderableStars::readSpeckFile(const std::string& path){ count++; float cachedValue; file >> cachedValue; - positions.push_back(cachedValue); + starcluster.push_back(cachedValue); } } // pass in the vectors internal array to create vbo method - v_size = positions.size(); // account for size of extra data. + v_size = starcluster.size(); - // create vbo (now positions ONLY) + // create vao and interleaved vbo glGenVertexArrays(1, &_vaoID); glGenBuffers(1, &_vboID); glBindVertexArray(_vaoID); glBindBuffer(GL_ARRAY_BUFFER, _vboID); - glBufferData(GL_ARRAY_BUFFER, v_size*sizeof(GLfloat), &positions[0], GL_DYNAMIC_DRAW); // x,y,z,lum,appmag,absol.. - + glBufferData(GL_ARRAY_BUFFER, v_size*sizeof(GLfloat), &starcluster[0], GL_DYNAMIC_DRAW); // x,y,z,lum,appmag,absol.. glBindVertexArray(0); return true; } @@ -249,9 +252,7 @@ bool RenderableStars::initialize(){ if (!readSpeckFile(_speckPath)) LERROR("Failed to read speck file for path : '" << _speckPath << "'"); -#ifdef SPRITES loadTexture(); -#endif completeSuccess &= (_texture != nullptr); return completeSuccess; @@ -263,8 +264,6 @@ bool RenderableStars::deinitialize(){ return true; } -#define GLSPRITES -#define GLPOINTS void RenderableStars::render(const Camera* camera, const psc& thisPosition){ assert(_programObject); printOpenGLError(); @@ -272,33 +271,30 @@ void RenderableStars::render(const Camera* camera, const psc& thisPosition){ _programObject->activate(); // fetch data - psc currentPosition = glm::vec4(0);// thisPosition; // NOTE : currentPosition now same as Earth. + psc currentPosition = glm::vec4(0); // thisPosition; psc campos = camera->position(); glm::mat4 camrot = camera->viewRotationMatrix(); PowerScaledScalar scaling = camera->scaling(); - // scale the planet to appropriate size since the planet is a unit sphere glm::mat4 transform = glm::mat4(1); - // why? - scaling = glm::vec2(1, -22); - //scaling = glm::vec2(1, -4); - - GLint vertsToDraw = v_size / 7; // account for data size. - GLsizei stride = sizeof(GLfloat) * 7; // 7 component stride - + scaling = glm::vec2(1, -22); // we have no boundingsphere? + GLint vertsToDraw = v_size / 7; // account for data size. + GLsizei stride = sizeof(GLfloat) * 7; // 7 component stride + /* transform = glm::rotate(transform, 1.1f * static_cast(sgct::Engine::instance()->getTime()), glm::vec3(0.0f, 1.0f, 0.0f)); - + */ positionAttrib = _programObject->attributeLocation("in_position"); + brightnessDataAttrib = _programObjectPoints->attributeLocation("in_brightness"); // disable depth test, enable additative blending glDisable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ONE); -// ---------------------- RENDER HALOS ----------------------------- #ifdef GLSPRITES +// ---------------------- RENDER HALOS ----------------------------- _programObject->setUniform("ViewProjection", camera->viewProjectionMatrix()); _programObject->setUniform("ModelTransform", transform); _programObject->setUniform("campos", campos.vec4()); @@ -315,20 +311,22 @@ void RenderableStars::render(const Camera* camera, const psc& thisPosition){ // activate the VBO. glBindVertexArray(_vaoID); glBindBuffer(GL_ARRAY_BUFFER, _vboID); - glEnableVertexAttribArray(positionAttrib); - glVertexAttribPointer(positionAttrib, 4, GL_FLOAT, GL_FALSE, stride, (void*)0); - glDrawArrays(GL_POINTS, 0, vertsToDraw); + glEnableVertexAttribArray(positionAttrib); // enable acess attribute in_position + glEnableVertexAttribArray(brightnessDataAttrib); // enable acess attribute in_brigthness + glVertexAttribPointer(positionAttrib, 4, GL_FLOAT, GL_FALSE, stride, (void*)0); // psc coordinates + glVertexAttribPointer(brightnessDataAttrib, 3, GL_FLOAT, GL_FALSE, stride, (void*)(4 * sizeof(GLfloat)));// brigthness properties + glDrawArrays(GL_POINTS, 0, vertsToDraw); // render! glDisableVertexAttribArray(positionAttrib); + glDisableVertexAttribArray(brightnessDataAttrib); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); - glDisable(GL_BLEND); - glEnable(GL_DEPTH_TEST); + #endif _programObject->deactivate(); -// ---------------------- RENDER POINTS ----------------------------- #ifdef GLPOINTS +// ---------------------- RENDER POINTS ----------------------------- _programObjectPoints->activate(); _programObjectPoints->setUniform("ViewProjection", camera->viewProjectionMatrix()); @@ -338,22 +336,26 @@ void RenderableStars::render(const Camera* camera, const psc& thisPosition){ _programObjectPoints->setUniform("camrot", camrot); _programObjectPoints->setUniform("scaling", scaling.vec2()); - glEnable(GL_PROGRAM_POINT_SIZE_EXT); //Allows shader to determine pointsize. - brightnessDataAttrib = _programObjectPoints->attributeLocation("in_brightness"); + glEnable(GL_PROGRAM_POINT_SIZE_EXT); // Allows shader to determine pointsize. + //glEnable(GL_POINT_SMOOTH); //decrepated in core profile, workaround in frag. glBindVertexArray(_vaoID); - glBindBuffer(GL_ARRAY_BUFFER, _vboID); - glEnableVertexAttribArray(positionAttrib); - glEnableVertexAttribArray(brightnessDataAttrib); - glVertexAttribPointer(positionAttrib, 4, GL_FLOAT, GL_FALSE, stride, (void*)0); // xyz - glVertexAttribPointer(brightnessDataAttrib, 3, GL_FLOAT, GL_FALSE, stride, (void*)(4 * sizeof(GLfloat))); - glDrawArrays(GL_POINTS, 0, vertsToDraw); + glBindBuffer(GL_ARRAY_BUFFER, _vboID); + glEnableVertexAttribArray(positionAttrib); // enable acess attribute in_position + glEnableVertexAttribArray(brightnessDataAttrib); // enable acess attribute in_brigthness + glVertexAttribPointer(positionAttrib, 4, GL_FLOAT, GL_FALSE, stride, (void*)0); // psc coordinates + glVertexAttribPointer(brightnessDataAttrib, 3, GL_FLOAT, GL_FALSE, stride, (void*)(4 * sizeof(GLfloat))); // brigthness properties + glDrawArrays(GL_POINTS, 0, vertsToDraw); // render! glDisableVertexAttribArray(positionAttrib); glDisableVertexAttribArray(brightnessDataAttrib); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); + glDisable(GL_BLEND); + glEnable(GL_DEPTH_TEST); + _programObjectPoints->deactivate(); + #endif }