Skip to content

Commit

Permalink
fix lighting
Browse files Browse the repository at this point in the history
  • Loading branch information
JulioJPinto committed May 8, 2024
1 parent 4cab087 commit 5ef613f
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 33 deletions.
2 changes: 1 addition & 1 deletion engine/include/model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Model {
std::vector<Vertex> getPoints();

private:
GLuint _vbo, _ibo;
GLuint _vbo, _ibo, _normals, _textures;
std::vector<Vertex> _points;
Model(std::string filename, std::vector<Vertex> vbo,
std::vector<unsigned int> ibo, int id, std::vector<Vertex> points);
Expand Down
6 changes: 0 additions & 6 deletions engine/src/group.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
#include "group.hpp"

#include <array>
#include <cmath> // for M_PI
#include <cstring> // for memcpy
#include <iostream>
#include <string>

#include "utils.hpp"

Group::Group() {
Expand Down
4 changes: 2 additions & 2 deletions engine/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ void renderScene(void) {
glMaterialfv(GL_FRONT, GL_SPECULAR, white);
glMaterialf(GL_FRONT, GL_SHININESS, 128);

glutSolidTeapot(1.0);
// c.group.drawGroup();
// glutSolidTeapot(1.0);
c.group.drawGroup();

// renderMenu();

Expand Down
48 changes: 31 additions & 17 deletions engine/src/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,29 @@ extern "C" {

unsigned int counter = 0;

std::vector<float> vPointstoFloats(std::vector<Vertex> points) {
std::vector<float> positionsFloats(std::vector<Vertex> points) {
std::vector<float> floats;
for (const Vertex& point : points) {
floats.push_back(point.position.x);
floats.push_back(point.position.y);
floats.push_back(point.position.z);
}
return floats;
}

std::vector<float> normalFloats(std::vector<Vertex> points) {
std::vector<float> floats;
for (const Vertex& point : points) {
floats.push_back(point.normal.x);
floats.push_back(point.normal.y);
floats.push_back(point.normal.z);
}
return floats;
}

std::vector<float> textureFloats(std::vector<Vertex> points) {
std::vector<float> floats;
for (const Vertex& point : points) {
floats.push_back(point.texture.x);
floats.push_back(point.texture.y);
}
Expand Down Expand Up @@ -86,28 +100,25 @@ Model::Model(std::string filename, std::vector<Vertex> points) {
}

void Model::setupModel() {
std::vector<float> floats = vPointstoFloats(this->vbo);
std::vector<float> points = positionsFloats(this->vbo);
std::vector<float> normals = normalFloats(this->vbo);
std::vector<float> textures = textureFloats(this->vbo);

// Generate and bind vertex buffer
glGenBuffers(1, &this->_vbo);
glBindBuffer(GL_ARRAY_BUFFER, this->_vbo);
glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(float) * floats.size(),
floats.data(), GL_STATIC_DRAW);

// Specify the layout of the position data
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
floats.data());
glEnableVertexAttribArray(0);
glBufferData(GL_ARRAY_BUFFER, 3 * sizeof(float) * points.size(),
points.data(), GL_STATIC_DRAW);

// Specify the layout of the vertex normal data
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
floats.data() + 3);
glEnableVertexAttribArray(1);
glGenBuffers(1, &this->_normals);
glBindBuffer(GL_ARRAY_BUFFER, this->_normals);
glBufferData(GL_ARRAY_BUFFER, 3 * sizeof(float) * normals.size(),
normals.data(), GL_STATIC_DRAW);

// Specify the layout of the texture coordinate data
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
floats.data() + 6);
glEnableVertexAttribArray(2);
glGenBuffers(1, &this->_textures);
glBindBuffer(GL_ARRAY_BUFFER, this->_textures);
glBufferData(GL_ARRAY_BUFFER, 2 * sizeof(float) * textures.size(),
textures.data(), GL_STATIC_DRAW);

// Generate and bind index buffer
glGenBuffers(1, &this->_ibo);
Expand All @@ -126,6 +137,9 @@ void Model::drawModel() {
glBindBuffer(GL_ARRAY_BUFFER, this->_vbo);
glVertexPointer(3, GL_FLOAT, 0, 0);

glBindBuffer(GL_ARRAY_BUFFER, this->_normals);
glNormalPointer(GL_FLOAT,0,0);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->_ibo);
glDrawElements(GL_TRIANGLES, this->ibo.size(), GL_UNSIGNED_INT, 0);
}
Expand Down
6 changes: 0 additions & 6 deletions engine/src/parse.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
#include "parse.hpp"

#include <fstream>
#include <iostream>
#include <string>
#include <vector>

#include "read.hpp"

Configuration parseConfig(std::string filename) {
Expand Down
2 changes: 1 addition & 1 deletion scenes/advanced/default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</camera>
<group>
<models>
<model file="models/advanced/box.3d" />
<model file="models/advanced/sphere.3d" />
</models>
</group>
</world>

0 comments on commit 5ef613f

Please sign in to comment.