Fix crash if scene not set

This commit is contained in:
MrLetsplay 2023-09-12 22:01:50 +02:00
parent 8ce8f8a0dd
commit cc9a582717
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
2 changed files with 69 additions and 65 deletions

View File

@ -186,87 +186,89 @@ int start() {
cb.second(kekData.window); cb.second(kekData.window);
} }
kekData.shader->use(); if(kekData.activeScene) {
kekData.shader->use();
glm::mat4 view = kekData.activeCamera->transformationMatrix(); glm::mat4 view = kekData.activeCamera->transformationMatrix();
glm::mat4 projection; glm::mat4 projection;
projection = glm::perspective(glm::radians(90.0f), kekData.screenWidth / (float) kekData.screenHeight, KEK_CAMERA_NEAR, KEK_CAMERA_FAR); projection = glm::perspective(glm::radians(90.0f), kekData.screenWidth / (float) kekData.screenHeight, KEK_CAMERA_NEAR, KEK_CAMERA_FAR);
glm::vec3 position = kekData.activeCamera->getPosition(); glm::vec3 position = kekData.activeCamera->getPosition();
glUniformMatrix4fv(glGetUniformLocation(kekData.shader->id, "view"), 1, GL_FALSE, glm::value_ptr(view)); glUniformMatrix4fv(glGetUniformLocation(kekData.shader->id, "view"), 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(glGetUniformLocation(kekData.shader->id, "projection"), 1, GL_FALSE, glm::value_ptr(projection)); glUniformMatrix4fv(glGetUniformLocation(kekData.shader->id, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glUniform3fv(glGetUniformLocation(kekData.shader->id, "cameraPos"), 1, glm::value_ptr(position)); glUniform3fv(glGetUniformLocation(kekData.shader->id, "cameraPos"), 1, glm::value_ptr(position));
if(kekData.shader->lighting) { if(kekData.shader->lighting) {
LightList *lights = kekData.activeScene->lights; LightList *lights = kekData.activeScene->lights;
std::vector<Light *> shaderLights; // TODO: Maybe don't compute on every frame std::vector<Light *> shaderLights; // TODO: Maybe don't compute on every frame
for(PointLight *light : lights->point) { for(PointLight *light : lights->point) {
if(glm::length2(glm::abs(light->getPosition() - kekData.activeCamera->getPosition())) < KEK_LIGHT_MAX_DISTANCE_SQUARED) { if(glm::length2(glm::abs(light->getPosition() - kekData.activeCamera->getPosition())) < KEK_LIGHT_MAX_DISTANCE_SQUARED) {
shaderLights.push_back(light);
}
}
for(DirectionalLight *light : lights->directional) {
shaderLights.push_back(light); shaderLights.push_back(light);
} }
}
for(DirectionalLight *light : lights->directional) { for(SpotLight *light : lights->spot) {
shaderLights.push_back(light); if(glm::length2(glm::abs(light->getPosition() - kekData.activeCamera->getPosition())) < KEK_LIGHT_MAX_DISTANCE_SQUARED) {
} shaderLights.push_back(light);
for(SpotLight *light : lights->spot) {
if(glm::length2(glm::abs(light->getPosition() - kekData.activeCamera->getPosition())) < KEK_LIGHT_MAX_DISTANCE_SQUARED) {
shaderLights.push_back(light);
}
}
unsigned int i = 0;
int numPointLights = 0, numDirectionalLights = 0, numSpotLights = 0;
for(Light *light : shaderLights) {
ShaderLight shLight;
memcpy(shLight.color, glm::value_ptr(light->color), sizeof(shLight.color));
switch(light->getType()) {
case LightType::POINT:
{
PointLight *l = (PointLight *) light;
memcpy(&shLight.position, glm::value_ptr(l->getPosition()), sizeof(shLight.position));
memcpy(&shLight.attenuation, glm::value_ptr(glm::vec3(l->constant, l->linear, l->quadratic)), sizeof(shLight.attenuation));
numPointLights++;
break;
}
case LightType::DIRECTIONAL:
{
DirectionalLight *l = (DirectionalLight *) light;
memcpy(&shLight.direction, glm::value_ptr(l->direction), sizeof(shLight.direction));
numDirectionalLights++;
break;
}
case LightType::SPOT:
{
SpotLight *l = (SpotLight *) light;
memcpy(&shLight.position, glm::value_ptr(l->getPosition()), sizeof(shLight.position));
memcpy(&shLight.direction, glm::value_ptr(l->direction), sizeof(shLight.direction));
memcpy(&shLight.attenuation, glm::value_ptr(glm::vec3(l->constant, l->linear, l->quadratic)), sizeof(shLight.attenuation));
memcpy(&shLight.cutoff, glm::value_ptr(glm::vec2(glm::cos(l->innerCutoff), glm::cos(l->outerCutoff))), sizeof(shLight.cutoff));
numSpotLights++;
break;
} }
} }
kekData.shader->lighting->buffer[i] = shLight;
i++; unsigned int i = 0;
int numPointLights = 0, numDirectionalLights = 0, numSpotLights = 0;
for(Light *light : shaderLights) {
ShaderLight shLight;
memcpy(shLight.color, glm::value_ptr(light->color), sizeof(shLight.color));
switch(light->getType()) {
case LightType::POINT:
{
PointLight *l = (PointLight *) light;
memcpy(&shLight.position, glm::value_ptr(l->getPosition()), sizeof(shLight.position));
memcpy(&shLight.attenuation, glm::value_ptr(glm::vec3(l->constant, l->linear, l->quadratic)), sizeof(shLight.attenuation));
numPointLights++;
break;
}
case LightType::DIRECTIONAL:
{
DirectionalLight *l = (DirectionalLight *) light;
memcpy(&shLight.direction, glm::value_ptr(l->direction), sizeof(shLight.direction));
numDirectionalLights++;
break;
}
case LightType::SPOT:
{
SpotLight *l = (SpotLight *) light;
memcpy(&shLight.position, glm::value_ptr(l->getPosition()), sizeof(shLight.position));
memcpy(&shLight.direction, glm::value_ptr(l->direction), sizeof(shLight.direction));
memcpy(&shLight.attenuation, glm::value_ptr(glm::vec3(l->constant, l->linear, l->quadratic)), sizeof(shLight.attenuation));
memcpy(&shLight.cutoff, glm::value_ptr(glm::vec2(glm::cos(l->innerCutoff), glm::cos(l->outerCutoff))), sizeof(shLight.cutoff));
numSpotLights++;
break;
}
}
kekData.shader->lighting->buffer[i] = shLight;
i++;
}
glNamedBufferData(kekData.shader->lighting->ubo, kekData.shader->lighting->blockSize, &kekData.shader->lighting->buffer, GL_DYNAMIC_DRAW);
glBindBufferBase(GL_UNIFORM_BUFFER, KEK_UNIFORM_LIGHTS_BINDING, kekData.shader->lighting->ubo);
glUniform1i(glGetUniformLocation(kekData.shader->id, "numPointLights"), numPointLights);
glUniform1i(glGetUniformLocation(kekData.shader->id, "numDirectionalLights"), numDirectionalLights);
glUniform1i(glGetUniformLocation(kekData.shader->id, "numSpotLights"), numSpotLights);
} }
glNamedBufferData(kekData.shader->lighting->ubo, kekData.shader->lighting->blockSize, &kekData.shader->lighting->buffer, GL_DYNAMIC_DRAW); kekData.activeScene->draw(kekData.shader);
glBindBufferBase(GL_UNIFORM_BUFFER, KEK_UNIFORM_LIGHTS_BINDING, kekData.shader->lighting->ubo);
glUniform1i(glGetUniformLocation(kekData.shader->id, "numPointLights"), numPointLights);
glUniform1i(glGetUniformLocation(kekData.shader->id, "numDirectionalLights"), numDirectionalLights);
glUniform1i(glGetUniformLocation(kekData.shader->id, "numSpotLights"), numSpotLights);
} }
if(kekData.activeScene) kekData.activeScene->draw(kekData.shader);
glDisable(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

View File

@ -32,6 +32,8 @@ void destroy() {
} }
void step(float deltaT) { void step(float deltaT) {
if(!kekData.activeScene) return;
for(GameObject *obj : kekData.activeScene->objects) { for(GameObject *obj : kekData.activeScene->objects) {
if(obj->physics) { if(obj->physics) {
obj->physics->body->getWorldTransform().setOrigin(fromGLM(obj->getPosition())); obj->physics->body->getWorldTransform().setOrigin(fromGLM(obj->getPosition()));