tidies changeset

This commit is contained in:
tosh 2013-04-16 14:06:03 +02:00
parent 1e388347dc
commit ac55c610be
6 changed files with 67 additions and 47 deletions

View file

@ -60,8 +60,10 @@ public:
*/ */
void activate() const { void activate() const {
if (_hndProg != 0u) if (_hndProg != 0u) {
oGlLog( glUseProgram(_hndProg) ); oGlLog( glUseProgram(_hndProg) );
}
} }
/** /**
@ -77,16 +79,20 @@ public:
*/ */
bool addShader(GLenum type, GLsizei nStrings, GLchar const** strings) { bool addShader(GLenum type, GLsizei nStrings, GLchar const** strings) {
if (! _hndProg) { _hndProg = glCreateProgram(); } if (! _hndProg && !! glCreateProgram) {
_hndProg = glCreateProgram();
}
if (! _hndProg) { return false; }
GLuint s = glCreateShader(type); GLuint s = glCreateShader(type);
glShaderSource(s, nStrings, strings, 0l); glShaderSource(s, nStrings, strings, 0l);
glCompileShader(s); glCompileShader(s);
GLint status; GLint status;
glGetShaderiv(s, GL_COMPILE_STATUS, & status); glGetShaderiv(s, GL_COMPILE_STATUS, & status);
if (!! status) if (status != 0)
glAttachShader(_hndProg, s); glAttachShader(_hndProg, s);
#ifdef NDEBUG #ifdef NDEBUG // always fetch log in debug mode
else else
#endif #endif
fetchLog(s, glGetShaderiv, glGetShaderInfoLog); fetchLog(s, glGetShaderiv, glGetShaderInfoLog);
@ -104,12 +110,21 @@ public:
glLinkProgram(_hndProg); glLinkProgram(_hndProg);
GLint status; GLint status;
glGetProgramiv(_hndProg, GL_LINK_STATUS, & status); glGetProgramiv(_hndProg, GL_LINK_STATUS, & status);
#ifdef NDEBUG #ifndef NDEBUG // always fetch log in debug mode
if (status == 0) fetchLog(_hndProg, glGetProgramiv, glGetProgramInfoLog);
#endif #endif
if (status == 0) {
#ifdef NDEBUG // only on error in release mode
fetchLog(_hndProg, glGetProgramiv, glGetProgramInfoLog); fetchLog(_hndProg, glGetProgramiv, glGetProgramInfoLog);
#endif
glDeleteProgram(_hndProg);
_hndProg = 0u;
return false;
return status != 0; } else {
return true;
}
} }
private: private:

View file

@ -72,16 +72,17 @@ namespace starfield {
size_t _valLodNalloc; size_t _valLodNalloc;
size_t _valLodNrender; size_t _valLodNrender;
BrightnessLevels _seqLodBrightness; BrightnessLevels _seqLodBrightness;
BrightnessLevel _valLodAllocBrightness;
#if STARFIELD_MULTITHREADING #if STARFIELD_MULTITHREADING
atomic<BrightnessLevel> _valLodBrightness; atomic<BrightnessLevel> _valLodBrightness;
BrightnessLevel _valLodAllocBrightness;
atomic<Renderer*> _ptrRenderer; atomic<Renderer*> _ptrRenderer;
typedef lock_guard<mutex> lock; typedef lock_guard<mutex> lock;
#else #else
BrightnessLevel _valLodBrightness; BrightnessLevel _valLodBrightness;
BrightnessLevel _valLodAllocBrightness;
Renderer* _ptrRenderer; Renderer* _ptrRenderer;
@ -89,6 +90,10 @@ namespace starfield {
#define _(x) #define _(x)
#endif #endif
static inline size_t toBufSize(double f) {
return size_t(floor(f + 0.5f));
}
public: public:
Controller() : Controller() :
@ -99,8 +104,8 @@ namespace starfield {
_valLodOveralloc(1.2), _valLodOveralloc(1.2),
_valLodNalloc(0), _valLodNalloc(0),
_valLodNrender(0), _valLodNrender(0),
_valLodAllocBrightness(0),
_valLodBrightness(0), _valLodBrightness(0),
_valLodAllocBrightness(0),
_ptrRenderer(0l) { _ptrRenderer(0l) {
} }
@ -144,8 +149,8 @@ namespace starfield {
rcpChange = 1.0; rcpChange = 1.0;
nRender = lrint(_valLodFraction * newLast); nRender = toBufSize(_valLodFraction * newLast);
n = min(newLast, size_t(lrint(_valLodOveralloc * nRender))); n = min(newLast, toBufSize(_valLodOveralloc * nRender));
} else { } else {
@ -282,7 +287,7 @@ namespace starfield {
// calculate allocation size and corresponding brightness // calculate allocation size and corresponding brightness
// threshold // threshold
double oaFract = std::min(fraction * (1.0 + overalloc), 1.0); double oaFract = std::min(fraction * (1.0 + overalloc), 1.0);
n = lrint(oaFract * last); n = toBufSize(oaFract * last);
bMin = _seqLodBrightness[n]; bMin = _seqLodBrightness[n];
n = std::upper_bound( n = std::upper_bound(
_seqLodBrightness.begin() + n - 1, _seqLodBrightness.begin() + n - 1,
@ -290,7 +295,7 @@ namespace starfield {
bMin, GreaterBrightness() ) - _seqLodBrightness.begin(); bMin, GreaterBrightness() ) - _seqLodBrightness.begin();
// also determine number of vertices to render and brightness // also determine number of vertices to render and brightness
nRender = lrint(fraction * last); nRender = toBufSize(fraction * last);
// Note: nRender does not have to be accurate // Note: nRender does not have to be accurate
b = _seqLodBrightness[nRender]; b = _seqLodBrightness[nRender];
// this setting controls the renderer, also keep b as the // this setting controls the renderer, also keep b as the
@ -304,7 +309,7 @@ namespace starfield {
// fprintf(stderr, "Stars.cpp: " // fprintf(stderr, "Stars.cpp: "
// "fraction = %lf, oaFract = %lf, n = %d, n' = %d, bMin = %d, b = %d\n", // "fraction = %lf, oaFract = %lf, n = %d, n' = %d, bMin = %d, b = %d\n",
// fraction, oaFract, lrint(oaFract * last)), n, bMin, b); // fraction, oaFract, toBufSize(oaFract * last)), n, bMin, b);
// will not have to reallocate? set new fraction right away // will not have to reallocate? set new fraction right away
// (it is consistent with the rest of the state in this case) // (it is consistent with the rest of the state in this case)

View file

@ -50,8 +50,8 @@ namespace starfield {
return false; return false;
} }
fprintf(stderr, "Stars.cpp: read %d stars, rendering %ld\n", fprintf(stderr, "Stars.cpp: read %u vertices, using %lu\n",
_valRecordsRead, _ptrVertices->size()); _valRecordsRead, _ptrVertices->size());
return true; return true;
} }

View file

@ -26,8 +26,8 @@ namespace starfield {
InputVertex(float azimuth, float altitude, unsigned color) { InputVertex(float azimuth, float altitude, unsigned color) {
_valColor = (color >> 16 & 0xffu) | (color & 0xff00u) | _valColor = ((color >> 16) & 0xffu) | (color & 0xff00u) |
(color << 16 & 0xff0000u) | 0xff000000u; ((color << 16) & 0xff0000u) | 0xff000000u;
azimuth = angleConvert<Degrees,Radians>(azimuth); azimuth = angleConvert<Degrees,Radians>(azimuth);
altitude = angleConvert<Degrees,Radians>(altitude); altitude = angleConvert<Degrees,Radians>(altitude);

View file

@ -13,15 +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
#ifdef _WIN32
#include "../Config.h"
#define lrint(x) (floor(x + (x > 0) ? 0.5 : -0.5))
inline float remainder(float x, float y) { return std::fmod(x, y); }
inline int round(float x) { return (floor(x + 0.5)); }
double log2( double n ) { return log( n ) / log( 2 ); }
#else
#include "starfield/Config.h" #include "starfield/Config.h"
#endif
namespace starfield { namespace starfield {
class Tiling { class Tiling {
@ -35,7 +28,7 @@ namespace starfield {
Tiling(unsigned k) : Tiling(unsigned k) :
_valK(k), _valK(k),
_valRcpSlice(k / Radians::twicePi()) { _valRcpSlice(k / Radians::twicePi()) {
_valBits = ceil(log2(getTileCount())); _valBits = ceil(log(getTileCount()) * 1.4426950408889634); // log2
} }
unsigned getAzimuthalTiles() const { return _valK; } unsigned getAzimuthalTiles() const { return _valK; }
@ -58,7 +51,7 @@ namespace starfield {
private: private:
unsigned discreteAngle(float unsigned_angle) const { unsigned discreteAngle(float unsigned_angle) const {
return unsigned(round(unsigned_angle * _valRcpSlice)); return unsigned(floor(unsigned_angle * _valRcpSlice + 0.5f));
} }
unsigned discreteAzimuth(float a) const { unsigned discreteAzimuth(float a) const {

View file

@ -32,9 +32,9 @@ struct Rotations {
static float halfPi() { return 0.25f; } static float halfPi() { return 0.25f; }
}; };
/** //
* Converts an angle from one unit to another. // Converts an angle from one unit to another.
*/ //
template< class UnitFrom, class UnitTo > template< class UnitFrom, class UnitTo >
float angleConvert(float a) { float angleConvert(float a) {
@ -42,21 +42,28 @@ float angleConvert(float a) {
} }
/** //
* Clamps an angle to the range of [-180; 180) degrees. // Clamps an angle to the range of [-180; 180) degrees.
*/ //
template< class Unit > template< class Unit >
float angleSignedNormal(float a) { float angleSignedNormal(float a) {
float result = remainder(a, Unit::twicePi()); // result is remainder(a, Unit::twicePi());
if (result == Unit::pi()) float result = fmod(a, Unit::twicePi());
result = -Unit::pi(); if (result >= Unit::pi()) {
result -= Unit::twicePi();
} else if (result < -Unit::pi()) {
result += Unit::twicePi();
}
return result; return result;
} }
/** //
* Clamps an angle to the range of [0; 360) degrees. // Clamps an angle to the range of [0; 360) degrees.
*/ //
template< class Unit > template< class Unit >
float angleUnsignedNormal(float a) { float angleUnsignedNormal(float a) {
@ -64,13 +71,13 @@ float angleUnsignedNormal(float a) {
} }
/** //
* Clamps a polar direction so that azimuth is in the range of [0; 360) // Clamps a polar direction so that azimuth is in the range of [0; 360)
* degrees and altitude is in the range of [-90; 90] degrees. // degrees and altitude is in the range of [-90; 90] degrees.
* //
* The so normalized angle still contains ambiguity due to gimbal lock: // The so normalized angle still contains ambiguity due to gimbal lock:
* Both poles can be reached from any azimuthal direction. // Both poles can be reached from any azimuthal direction.
*/ //
template< class Unit > template< class Unit >
void angleHorizontalPolar(float& azimuth, float& altitude) { void angleHorizontalPolar(float& azimuth, float& altitude) {