Skip to content

Commit 3a112ab

Browse files
committed
wip
1 parent aaf22f1 commit 3a112ab

File tree

5 files changed

+120
-35
lines changed

5 files changed

+120
-35
lines changed

shaders/star_frag.glsl

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,59 @@
1-
uniform sampler2D starTex;
2-
varying vec4 color;
1+
const float degree_per_px = 0.05;
2+
const float br_limit = 1.0 / (255.0 * 12.92);
3+
4+
varying vec3 v_color;
5+
varying vec3 v_max_tetha_hk;
6+
varying float pointSize;
7+
8+
float psf_square(float theta, float min_theta, float max_theta, float h, float k, float b)
9+
{
10+
// Human eye's point source function, optimized to fit a square.
11+
// Lower limit on brightness and angular size: 1 Vega and 0.05 degrees per pixel.
12+
// No upper limits.
13+
14+
if (theta < min_theta)
15+
return 1.0; // overexposed
16+
17+
if (theta < max_theta)
18+
{
19+
float brackets = b / (theta - h) - 1.0;
20+
return brackets * brackets / k;
21+
}
22+
23+
return 0.0; // after max_theta function starts to grow again
24+
}
25+
26+
/*
27+
float psf_fullscreen(float theta, float min_theta):
28+
{
29+
// Human eye's point source function, optimized to be a full-screen shader.
30+
// The price to pay for simplification is a brightness reduction compared to the original PSF.
31+
32+
if (theta2 < min_theta)
33+
return 1; // overexposed
34+
35+
return 4.43366571e-6 / theta;
36+
}
37+
*/
338

439
void main(void)
540
{
6-
gl_FragColor = texture2D(starTex, gl_PointCoord) * color;
41+
float max_theta = v_max_tetha_hk.x;
42+
if (max_theta == -1.0)
43+
{
44+
gl_FragColor = vec4(v_color, 1.0);
45+
}
46+
else
47+
{
48+
float h = v_max_tetha_hk.y;
49+
float k = v_max_tetha_hk.z;
50+
51+
float b = max_theta - h;
52+
float min_theta = h + b / (sqrt(k) + 1.0);
53+
54+
vec2 offset = (gl_PointCoord.xy - vec2(0.5)) * pointSize;
55+
float theta = length(offset) * degree_per_px;
56+
57+
gl_FragColor = vec4(v_color * psf_square(theta, min_theta, max_theta, h, k, b), 1.0);
58+
}
759
}

shaders/star_vert.glsl

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,45 @@
1-
attribute vec3 in_Position;
2-
attribute vec4 in_Color;
3-
attribute float in_PointSize;
4-
varying vec4 color;
1+
2+
const float degree_per_px = 0.05;
3+
const float br_limit = 1.0 / (255.0 * 12.92);
4+
5+
varying vec3 v_color; // 12
6+
varying vec3 v_max_tetha_hk; // 24
7+
varying float pointSize; // 28
8+
9+
uniform vec2 viewportSize;
10+
11+
attribute vec4 in_Position;
12+
attribute vec3 in_Color;
13+
attribute float in_PointSize; // scaled brightness measured in Vegas
14+
15+
float psf_max_theta(float br)
16+
{
17+
return 0.012 + 1.488 / (sqrt(br_limit * 1000000.0 / (11.0 * br)) + 1.0);
18+
}
519

620
void main(void)
721
{
8-
gl_PointSize = in_PointSize;
9-
color = in_Color;
10-
set_vp(vec4(in_Position, 1.0));
22+
float linearBr = pow(10.0, 0.4f * in_PointSize) * br_limit;
23+
vec3 color0 = in_Color * (linearBr / in_Color.g); // scaling on brightness and normalizing by green channel
24+
25+
vec3 check_vec = step(vec3(1.0), color0); // step(edge, x) - For element i of the return value, 0.0 is returned if x[i] < edge[i], and 1.0 is returned otherwise.
26+
float check = check_vec.x + check_vec.y + check_vec.z;
27+
if (check == 0.0)
28+
{
29+
pointSize = 1.0;
30+
v_max_tetha_hk = vec3(-1.0);
31+
}
32+
else
33+
{
34+
float max_theta = 0.33435822702992773 * sqrt(max(color0.r, max(color0.g, color0.b))); // glare radius
35+
pointSize = 2.0 * ceil(max_theta / degree_per_px);
36+
37+
float h = 0.0082234880783653 * pow(max_theta, 0.7369983254906639); // h, k, b - common constants, depending originally on star brightness
38+
float k = 38581.577272697796 * pow(max_theta, 2.368787717957141);
39+
v_max_tetha_hk = vec3(max_theta, h, k);
40+
}
41+
42+
gl_PointSize = pointSize;
43+
v_color = color0;
44+
set_vp(in_Position);
1145
}

src/celengine/pointstarrenderer.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111
#include <celengine/starcolors.h>
1212
#include <celengine/star.h>
1313
#include <celengine/univcoord.h>
14+
#include <cmath>
1415
#include "pointstarvertexbuffer.h"
1516
#include "render.h"
1617
#include "pointstarrenderer.h"
1718

19+
#include <fmt/format.h>
20+
1821
using namespace std;
1922
using namespace Eigen;
2023

@@ -95,19 +98,10 @@ void PointStarRenderer::process(const Star& star, float distance, float appMag)
9598
// planets.
9699
if (distance > SolarSystemMaxDistance)
97100
{
98-
float pointSize, alpha, glareSize, glareAlpha;
99-
float size = BaseStarDiscSize * static_cast<float>(renderer->getScreenDpi()) / 96.0f;
100-
renderer->calculatePointSize(appMag,
101-
size,
102-
pointSize,
103-
alpha,
104-
glareSize,
105-
glareAlpha);
106-
107-
if (glareSize != 0.0f)
108-
glareVertexBuffer->addStar(relPos, Color(starColor, glareAlpha), glareSize);
109-
if (pointSize != 0.0f)
110-
starVertexBuffer->addStar(relPos, Color(starColor, alpha), pointSize);
101+
if (appMag < faintestMag)
102+
{
103+
starVertexBuffer->addStar(relPos, starColor, faintestMag - appMag);
104+
}
111105

112106
// Place labels for stars brighter than the specified label threshold brightness
113107
if (((labelMode & Renderer::StarLabels) != 0) && appMag < labelThresholdMag)

src/celengine/pointstarvertexbuffer.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void PointStarVertexBuffer::startBasicPoints()
4848

4949
void PointStarVertexBuffer::render()
5050
{
51-
if (m_nStars != 0)
51+
if (m_nStars != 0 && m_prog != nullptr)
5252
{
5353
makeCurrent();
5454

@@ -69,7 +69,7 @@ void PointStarVertexBuffer::render()
6969

7070
void PointStarVertexBuffer::makeCurrent()
7171
{
72-
if (current == this || m_prog == nullptr)
72+
if (current == this)
7373
return;
7474

7575
if (current != nullptr)
@@ -79,6 +79,9 @@ void PointStarVertexBuffer::makeCurrent()
7979

8080
m_prog->use();
8181
m_prog->setMVPMatrices(m_renderer.getCurrentProjectionMatrix(), m_renderer.getCurrentModelViewMatrix());
82+
int x, y, w, h;
83+
m_renderer.getViewport(&x, &y, &w, &h);
84+
m_prog->vec2Param("viewportSize") = Eigen::Vector2f(w-x, h-y);
8285
if (m_pointSizeFromVertex)
8386
{
8487
m_prog->samplerParam("starTex") = 0;

src/celengine/render.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ Renderer::Renderer() :
269269

270270
{
271271
pointStarVertexBuffer = new PointStarVertexBuffer(*this, 2048);
272-
glareVertexBuffer = new PointStarVertexBuffer(*this, 2048);
272+
//glareVertexBuffer = new PointStarVertexBuffer(*this, 2048);
273273

274274
for (int i = 0; i < (int) FontCount; i++)
275275
{
@@ -282,7 +282,7 @@ Renderer::Renderer() :
282282
Renderer::~Renderer()
283283
{
284284
delete pointStarVertexBuffer;
285-
delete glareVertexBuffer;
285+
//delete glareVertexBuffer;
286286
delete shaderManager;
287287

288288
m_atmosphereRenderer->deinitGL();
@@ -1806,8 +1806,10 @@ void Renderer::renderObjectAsPoint(const Vector3f& position,
18061806
gaussianGlareTex->bind();
18071807
if (glareSize > gl::maxPointSize)
18081808
m_largeStarRenderer->render(center, {color, glareAlpha}, glareSize, mvp);
1809+
/*
18091810
else
18101811
glareVertexBuffer->addStar(center, {color, glareAlpha}, glareSize);
1812+
*/
18111813
}
18121814
}
18131815
}
@@ -3829,7 +3831,7 @@ void Renderer::renderPointStars(const StarDatabase& starDB,
38293831
starRenderer.viewNormal = getCameraOrientationf().conjugate() * -Vector3f::UnitZ();
38303832
starRenderer.renderList = &renderList;
38313833
starRenderer.starVertexBuffer = pointStarVertexBuffer;
3832-
starRenderer.glareVertexBuffer = glareVertexBuffer;
3834+
//starRenderer.glareVertexBuffer = glareVertexBuffer;
38333835
starRenderer.fov = fov;
38343836
starRenderer.cosFOV = (float) cos(degToRad(calcMaxFOV(fov, getAspectRatio())) / 2.0f);
38353837

@@ -3848,11 +3850,11 @@ void Renderer::renderPointStars(const StarDatabase& starDB,
38483850
gaussianDiscTex->bind();
38493851
starRenderer.starVertexBuffer->setTexture(gaussianDiscTex);
38503852
starRenderer.starVertexBuffer->setPointScale(screenDpi / 96.0f);
3851-
starRenderer.glareVertexBuffer->setTexture(gaussianGlareTex);
3852-
starRenderer.glareVertexBuffer->setPointScale(screenDpi / 96.0f);
3853+
//starRenderer.glareVertexBuffer->setTexture(gaussianGlareTex);
3854+
//starRenderer.glareVertexBuffer->setPointScale(screenDpi / 96.0f);
38533855

38543856
PointStarVertexBuffer::enable();
3855-
starRenderer.glareVertexBuffer->startSprites();
3857+
//starRenderer.glareVertexBuffer->startSprites();
38563858
if (starStyle == PointStars)
38573859
starRenderer.starVertexBuffer->startBasicPoints();
38583860
else
@@ -3881,7 +3883,7 @@ void Renderer::renderPointStars(const StarDatabase& starDB,
38813883
#endif
38823884

38833885
starRenderer.starVertexBuffer->finish();
3884-
starRenderer.glareVertexBuffer->finish();
3886+
//starRenderer.glareVertexBuffer->finish();
38853887
PointStarVertexBuffer::disable();
38863888

38873889
#ifndef GL_ES
@@ -5366,9 +5368,9 @@ Renderer::renderSolarSystemObjects(const Observer &observer,
53665368
setPipelineState(ps);
53675369

53685370
PointStarVertexBuffer::enable();
5369-
glareVertexBuffer->startSprites();
5370-
glareVertexBuffer->render();
5371-
glareVertexBuffer->finish();
5371+
//glareVertexBuffer->startSprites();
5372+
//glareVertexBuffer->render();
5373+
//glareVertexBuffer->finish();
53725374
if (starStyle == PointStars)
53735375
pointStarVertexBuffer->startBasicPoints();
53745376
else

0 commit comments

Comments
 (0)