Skip to content

Commit

Permalink
fix: lighting
Browse files Browse the repository at this point in the history
  • Loading branch information
JulioJPinto committed May 19, 2024
1 parent 920290d commit e65f69e
Show file tree
Hide file tree
Showing 5 changed files with 366 additions and 19 deletions.
2 changes: 2 additions & 0 deletions engine/include/light.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Light createSpotLight(glm::vec4 position, glm::vec4 direction, float cutoff);

void setupLights(std::vector<Light> lights);

void drawLights(std::vector<Light> lights);

typedef struct {
glm::vec4 ambient;
glm::vec4 diffuse;
Expand Down
44 changes: 36 additions & 8 deletions engine/src/light.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,50 @@ void setupMaterial(Material m) {
}

void setupLights(std::vector<Light> lights) {
if (lights.size() != 0) {
std::cout << "Setting up lights\n";
float amb[4] = {1.0f, 1.0f, 1.0f, 1.0f};

glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
glEnable(GL_LIGHTING);
for (int i = 0; i < lights.size(); i++) {
float white[4] = {1.0, 1.0, 1.0, 1.0};

glEnable(GL_LIGHT0 + i);
glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, white);
glLightfv(GL_LIGHT0 + i, GL_SPECULAR, white);
std::cout << "Light " << i << std::endl;
}
}
}

void drawLights(std::vector<Light> lights) {
for (int i = 0; i < lights.size() && lights.size() < 8; i++) {
Light light = lights[i];

switch (light.type) {
case DIRECTIONAL:
glLightfv(GL_LIGHT0 + i, GL_POSITION, glm::value_ptr(light.direction));
case DIRECTIONAL: {
float direction[4] = {light.direction.x, light.direction.y,
light.direction.z, 0.0f};
glLightfv(GL_LIGHT0 + i, GL_POSITION, direction);
break;
case POINT:
glLightfv(GL_LIGHT0 + i, GL_POSITION, glm::value_ptr(light.position));
}
case POINT: {
float position[4] = {light.position.x, light.position.y,
light.position.z, 1.0f};
glLightfv(GL_LIGHT0 + i, GL_POSITION, position);
break;
case SPOT:
glLightfv(GL_LIGHT0 + i, GL_POSITION, glm::value_ptr(light.position));
glLightfv(GL_LIGHT0 + i, GL_SPOT_DIRECTION,
glm::value_ptr(light.direction));
}
case SPOT: {
float postion[4] = {light.position.x, light.position.y,
light.position.z, 1.0f};
glLightfv(GL_LIGHT0 + i, GL_POSITION, postion);
float direction[4] = {light.direction.x, light.direction.y,
light.direction.z, 0.0f};
glLightfv(GL_LIGHT0 + i, GL_SPOT_DIRECTION, direction);
glLightf(GL_LIGHT0 + i, GL_SPOT_CUTOFF, light.cutoff);
break;
}
}
}
}
12 changes: 2 additions & 10 deletions engine/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void renderScene(void) {

drawAxis();

setupLights(c.lights);
drawLights(c.lights);

c.group.drawGroup();

Expand Down Expand Up @@ -245,15 +245,7 @@ int main(int argc, char** argv) {
// some OpenGL settings
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);

if (c.lights.size() != 0) {
std::cout << "Setting up lights\n";
glEnable(GL_LIGHTING);
for (int i = 0; i < c.lights.size(); i++) {
glEnable(GL_LIGHT0 + i);
std::cout << "Light " << i << std::endl;
}
}
setupLights(c.lights);

// enter GLUT�s main cycle
glutMainLoop();
Expand Down
Loading

0 comments on commit e65f69e

Please sign in to comment.