Skip to content

Commit 3ad0175

Browse files
committed
fixed "sampler arrays indexed with non-constant expressions are forbidden in GLSL 1.30 and later"
fixed inadvertently multi texturing some objects fixed initial camera jump stepped down the sim speeds
1 parent 0fe79d1 commit 3ad0175

3 files changed

Lines changed: 34 additions & 19 deletions

File tree

SolarSystemSimulation++/jge/RenderPipeline.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ void RenderPipeline::DrawShadowCastingModels(ShaderProgram* shader)
280280

281281
void RenderPipeline::DrawModel(ShaderProgram& shader, Model& m, int lvl)
282282
{
283-
// bind texture 1 (multitextuing)
283+
// bind texture 1 (multitexturing)
284284
bool useMultitexturing = m.textures[1] > 0;
285285
shader.UpdateUniform(UF_USETEX[1], useMultitexturing);
286286
if (useMultitexturing)
@@ -383,7 +383,7 @@ void RenderPipeline::DrawNonShadowCastingModels(ShaderProgram* defaultShader)
383383
}
384384

385385
int lvl = scene->DetermineLODLevel(*m);
386-
DrawModelMinimal(*currentShader, *m, lvl);
386+
DrawModel(*currentShader, *m, lvl);
387387

388388
lastShader = currentShader;
389389
}

SolarSystemSimulation++/main.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const float SIMULATION_DELTA = 1.0f / SIMULATION_FREQ;
5454
float simSpeeds[] =
5555
{
5656
0.0f,
57+
0.03f / SIMULATION_FREQ,
5758
0.1f / SIMULATION_FREQ,
5859
1.0f / SIMULATION_FREQ,
5960
5.0f / SIMULATION_FREQ,
@@ -62,7 +63,6 @@ float simSpeeds[] =
6263
200.0f / SIMULATION_FREQ,
6364
400.0f / SIMULATION_FREQ,
6465
800.0f / SIMULATION_FREQ,
65-
1200.0f / SIMULATION_FREQ,
6666
};
6767
double simulationTime;
6868
float simulationTick = simSpeeds[2];
@@ -153,13 +153,15 @@ void OnKeyPressedCallback(GLFWwindow* window, int key, int scancode, int action,
153153

154154
/**
155155
* Called when the cursor position changed. Triggers also on hidden cursors!
156-
* The function updates the camera based on the cursor movement
156+
* The function updates the camera based on the cursor movement.
157157
*/
158158
void OnCursorPosChangedCallback(GLFWwindow* window, double x, double y)
159159
{
160-
// This variable is hack to stop glfwSetCursorPos from triggering an event callback to Mouse(...)
161-
// This avoids it being called recursively and hanging up the event loop
162-
static bool just_warped = false;
160+
// This variable is a hack to prevent the initial camera jump when the cursor isn't caught yet
161+
static bool caught_cursor = false;
162+
// This variable is a hack to stop glfwSetCursorPos from triggering OnCursorPosChangedCallback() recursively.
163+
// Maybe using GLFW_CURSOR_DISABLED is the correct approach?
164+
static bool just_warped = false;
163165

164166
if (just_warped) {
165167
just_warped = false;
@@ -170,17 +172,18 @@ void OnCursorPosChangedCallback(GLFWwindow* window, double x, double y)
170172
int dx = (int)x - windowWidth / 2;
171173
int dy = (int)y - windowHeight / 2;
172174

173-
if (dx) {
175+
if (caught_cursor) {
174176
camera->RotateYaw((float)dx);
175-
}
176-
177-
if (dy) {
178-
camera->RotatePitch((float)-dy);
177+
camera->RotatePitch((float)-dy);
179178
}
180179

181180
just_warped = true;
181+
caught_cursor = true;
182182
glfwSetCursorPos(window, windowWidth / 2, windowHeight / 2);
183183
}
184+
else {
185+
caught_cursor = false;
186+
}
184187
}
185188

186189

@@ -428,8 +431,8 @@ void InitGraphics()
428431
GLuint defaultVShader = LoadShader(GL_VERTEX_SHADER, "shader\\master.vert");
429432
GLuint defaultFShader = LoadShader(GL_FRAGMENT_SHADER, "shader\\masterOptimised.frag");
430433
lightingShader.Create(defaultVShader, defaultFShader);
431-
lightingShader.RegisterUniform("useTexture[1]"); // uniform doesn't count as active, so register it manually
432-
lightingShader.RegisterUniform("texTransform[1]"); // uniform doesn't count as active, so register it manually
434+
lightingShader.RegisterUniform("useTexture[1]"); // uniform doesn't count as active, so register it manually?
435+
lightingShader.RegisterUniform("texTransform[1]"); // uniform doesn't count as active, so register it manually?
433436

434437
GLuint shadowVShader = LoadShader(GL_VERTEX_SHADER, "shader\\vsm.vert");
435438
GLuint shadowFShader = LoadShader(GL_FRAGMENT_SHADER, "shader\\vsm.frag");

SolarSystemSimulation++/shader/masterOptimised.frag

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ float chebyshevUpperBound(samplerCube cube,float distance, vec3 dir)
115115
}
116116

117117
// Calculate the shadow factor for a light
118-
float calculateShadowFactor(int index)
118+
float calculateShadowFactor(samplerCube cube, lightSource light)
119119
{
120-
vec3 fragmentToLightWorld = (lights[index].position - inout_positionWorld).xyz;
121-
return chebyshevUpperBound(shadowCubes[index], length(fragmentToLightWorld), -fragmentToLightWorld);
120+
vec3 fragmentToLightWorld = (light.position - inout_positionWorld).xyz;
121+
return chebyshevUpperBound(cube, length(fragmentToLightWorld), -fragmentToLightWorld);
122122
}
123123

124124

@@ -150,8 +150,6 @@ void pointOrSpotLight(in int i, in vec3 N, in vec3 V, in vec3 D, in float shinin
150150
// vec3 H = normalize(L + E);
151151
// float pf = pow(max(dot(N,H), 0.0), shininess);
152152

153-
shadowFactors[i] = calculateShadowFactor(i);
154-
155153
diffuse += lights[i].diffuse * attenuation * cosAngleIncidence * att2 * shadowFactors[i];
156154
specular += lights[i].specular * attenuation * pf * shadowFactors[i] * spec;
157155
}
@@ -176,6 +174,20 @@ void directionalLight(in int i, in vec3 N, in vec3 V, in vec3 D, in float shinin
176174

177175
void calculateLighting(in int numLights, in vec3 N, in vec3 V, in vec3 D, in float shininess, in float spec, inout vec4 ambient, inout vec4 diffuse, inout vec4 specular)
178176
{
177+
// 'error: sampler arrays indexed with non-constant expressions are forbidden in GLSL 1.30 and later'
178+
if(numLights > 0) {
179+
shadowFactors[0] = calculateShadowFactor(shadowCubes[0], lights[0]);
180+
}
181+
if(numLights > 1) {
182+
shadowFactors[1] = calculateShadowFactor(shadowCubes[1], lights[1]);
183+
}
184+
if(numLights > 2) {
185+
shadowFactors[2] = calculateShadowFactor(shadowCubes[2], lights[2]);
186+
}
187+
if(numLights > 3) {
188+
shadowFactors[3] = calculateShadowFactor(shadowCubes[3], lights[3]);
189+
}
190+
179191
for (int i = 0; i < numLights; i++)
180192
{
181193
if (lights[i].position.w == 0.0)

0 commit comments

Comments
 (0)