Skip to content

Commit

Permalink
fix: parser
Browse files Browse the repository at this point in the history
  • Loading branch information
JulioJPinto committed May 18, 2024
1 parent 98056d4 commit 9cdc858
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 64 deletions.
8 changes: 0 additions & 8 deletions engine/imgui.ini

This file was deleted.

7 changes: 7 additions & 0 deletions engine/include/Configuration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ class Configuration {
Window window;
Camera camera;
Group group;
std::vector<Light> lights;

Configuration() = default;
Configuration(const Window &window, const Camera &camera, const Group &group);
Configuration(const Window &window, const Camera &camera, const Group &group,
const std::vector<Light> &lights);

bool addLight(const Light &light);
bool removeLight(int index);
// std::string toString();
};

Expand Down
46 changes: 46 additions & 0 deletions engine/include/light.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#ifndef LIGHT_HPP
#define LIGHT_HPP

extern "C" {
#include <GL/gl.h>
#ifdef __APPLE_CC__
#include <GLUT/glut.h>
#else
#include <GL/freeglut.h>
#endif
}
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>

enum LightType { DIRECTIONAL, POINT, SPOT };

typedef struct {
LightType type;
glm::vec4 position;
glm::vec4 direction;
float cutoff;
} Light;

Light createDirectionLight(glm::vec4 direction);

Light createPointLight(glm::vec4 position);

Light createSpotLight(glm::vec4 position, glm::vec4 direction, float cutoff);

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

typedef struct {
glm::vec4 ambient;
glm::vec4 diffuse;
glm::vec4 specular;
glm::vec4 emission;
float shininess;
} Material;

Material createMaterial(glm::vec4 ambient, glm::vec4 diffuse,
glm::vec4 specular, glm::vec4 emission,
float shininess);

void setupMaterial(Material m);

#endif // LIGHT_HPP
2 changes: 2 additions & 0 deletions engine/include/model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ extern "C" {
#include <set>
#include <vector>

#include "light.hpp"
#include "utils.hpp"
#include "vertex.hpp"

Expand All @@ -30,6 +31,7 @@ class Model {
std::vector<unsigned int> ibo;
int id;
bool initialized = false;
Material material;

Model();
Model(std::string filename, std::vector<Vertex> points);
Expand Down
28 changes: 28 additions & 0 deletions engine/src/Configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,32 @@ Configuration::Configuration(const Window &window, const Camera &camera,
this->window = window;
this->camera = camera;
this->group = group;
this->lights = {};
}

Configuration::Configuration(const Window &window, const Camera &camera,
const Group &group,
const std::vector<Light> &lights) {
this->window = window;
this->camera = camera;
this->group = group;
this->lights = lights;
}

bool Configuration::addLight(const Light &light) {
if (this->lights.size() > 8) {
return false;
}

this->lights.push_back(light);
return true;
}

bool Configuration::removeLight(int index) {
if (index < 0 || index >= this->lights.size()) {
return false;
}

this->lights.erase(this->lights.begin() + index);
return true;
}
63 changes: 63 additions & 0 deletions engine/src/light.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "light.hpp"

Light createDirectionLight(glm::vec4 direction) {
Light light;
light.type = DIRECTIONAL;
light.direction = direction;
return light;
}

Light createPointLight(glm::vec4 position) {
Light light;
light.type = POINT;
light.position = position;
return light;
}

Light createSpotLight(glm::vec4 position, glm::vec4 direction, float cutoff) {
Light light;
light.type = SPOT;
light.position = position;
light.direction = direction;
light.cutoff = cutoff;
return light;
}

Material createMaterial(glm::vec4 ambient, glm::vec4 diffuse,
glm::vec4 specular, glm::vec4 emission,
float shininess) {
Material material;
material.ambient = ambient;
material.diffuse = diffuse;
material.specular = specular;
material.emission = emission;
material.shininess = shininess;
return material;
}

void setupMaterial(Material m) {
glMaterialfv(GL_FRONT, GL_AMBIENT, glm::value_ptr(m.ambient));
glMaterialfv(GL_FRONT, GL_DIFFUSE, glm::value_ptr(m.diffuse));
glMaterialfv(GL_FRONT, GL_SPECULAR, glm::value_ptr(m.specular));
glMaterialfv(GL_FRONT, GL_EMISSION, glm::value_ptr(m.emission));
glMaterialf(GL_FRONT, GL_SHININESS, m.shininess);
}

void setupLights(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));
break;
case POINT:
glLightfv(GL_LIGHT0 + i, GL_POSITION, glm::value_ptr(light.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));
glLightf(GL_LIGHT0 + i, GL_SPOT_CUTOFF, light.cutoff);
break;
}
}
}
43 changes: 18 additions & 25 deletions engine/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ float cameraAngleY = 0.0f;

float zoom = 1.0f;
int axis = 1;
int wireframe = 1;

int timebase;
float frames;
Expand Down Expand Up @@ -101,20 +102,10 @@ void renderScene(void) {
glRotatef(cameraAngle, 1.0f, 0.0f, 1.0f);
glScalef(zoom, zoom, zoom);

drawAxis();

// put drawing instructions here

float pos[4] = {3.0, 3.0, 3.0, 0.0};
glLightfv(GL_LIGHT0, GL_POSITION, pos);

float dark[] = {0.2, 0.2, 0.2, 1.0};
float white[] = {0.8, 0.8, 0.8, 1.0};
float red[] = {0.8, 0.2, 0.2, 1.0};
//setup all lights
setupLights(c.lights);

glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, white);
glMaterialfv(GL_FRONT, GL_SPECULAR, white);
glMaterialf(GL_FRONT, GL_SHININESS, 128);
drawAxis();

// glutSolidTeapot(1.0);
c.group.drawGroup();
Expand Down Expand Up @@ -168,6 +159,14 @@ void processNormalKeys(unsigned char key, int x, int y) {
case 'i':
zoom += value;
break;
case 'c':
if (wireframe) {
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
wireframe = 0;
} else {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
wireframe = 1;
}
default:
break;
}
Expand All @@ -177,9 +176,11 @@ void setupConfig(char* arg) {
filename.assign(arg);

if (filename.substr(filename.size() - 4) == ".xml") {
printf("XML\n");
c = parseConfig(filename);
} else {
c = parseConfig("../scenes/default.xml");
std::cout << "Invalid file format\n";
exit(1);
}
}

Expand Down Expand Up @@ -216,17 +217,9 @@ int main(int argc, char** argv) {
// some OpenGL settings
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

float dark[4] = {0.2, 0.2, 0.2, 1.0};
float white[4] = {1.0, 1.0, 1.0, 1.0};
float black[4] = {0.0f, 0.0f, 0.0f, 0.0f};
// light colors
glLightfv(GL_LIGHT0, GL_AMBIENT, dark);
glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
glLightfv(GL_LIGHT0, GL_SPECULAR, white);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, black);
if(c.lights.size() != 0) {
glEnable(GL_LIGHTING);
}

// enter GLUT�s main cycle
glutMainLoop();
Expand Down
2 changes: 2 additions & 0 deletions engine/src/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ void Model::drawModel() {
setupModel();
}

setupMaterial(this->material);

glColor3f(1.0f, 1.0f, 1.0f);
glBindBuffer(GL_ARRAY_BUFFER, this->_vbo);
glVertexPointer(3, GL_FLOAT, 0, 0);
Expand Down
Loading

0 comments on commit 9cdc858

Please sign in to comment.