Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Textures (Engine) #31

Merged
merged 14 commits into from
May 23, 2024
4 changes: 1 addition & 3 deletions common/include/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ struct Point2D {
return x == other.x && y == other.y;
}

auto cross(const Point2D& other) {
return x * other.y - y * other.x;
}
auto cross(const Point2D& other) { return x * other.y - y * other.x; }
};

struct PointHash {
Expand Down
62 changes: 53 additions & 9 deletions engine/include/curves.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,34 @@
#include "model.hpp"
#include "utils.hpp"

enum TimeTransform { ROTATION, TRANSLATE };

class Rotations {
enum Transformations { SCALE, ROTATION, TRANSLATE, TIMEROTATION, TIMETRANSLATE };


class TimeRotations {
public:
float time;
float x;
float y;
float z;

Rotations();
Rotations(float time, float x, float y, float z);
TimeRotations();
TimeRotations(float time, float x, float y, float z);

void applyRotation(float elapsed_time);
void applyTimeRotation(float elapsed_time);
};

class Translations {
class TimeTranslations {
public:
float time;
bool align;
std::vector<Point> curvePoints;
Point y_axis;

Translations();
Translations(float time, bool align, std::vector<Point> curve);
TimeTranslations();
TimeTranslations(float time, bool align, std::vector<Point> curve);

void applyTranslations(float elapsed_time);
void applyTimeTranslations(float elapsed_time);

void renderCatmullRomCurve();

Expand All @@ -42,4 +44,46 @@ class Translations {
std::array<float, 16> rotationMatrix(Point x, Point y, Point z);
};

class Scale {
public:
float x;
float y;
float z;

Scale();
Scale(float x, float y, float z);

void applyScale();
};

class Translate {
public:
float x;
float y;
float z;

Translate();
Translate(float x, float y, float z);

void applyTranslation();
};

class Rotation {
public:
float angle;
float x;
float y;
float z;

Rotation();
Rotation(float angle, float x, float y, float z);

void applyRotation();
};

glm::mat4 Scalematrix(float x, float y, float z);
glm::mat4 Rotationmatrix(float angle, float x, float y, float z);
glm::mat4 Translatematrix(float x, float y, float z);


#endif // CURVES_HPP
47 changes: 47 additions & 0 deletions engine/include/frustsum.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#ifndef FRUSTSUM_CPP
#define FRUSTSUM_CPP

#include <math.h>

#include <glm/glm.hpp>

#include "model.hpp"
#include "Camera.hpp"


struct Plane {

float distance = 0.f;
glm::vec3 normal = { 0.f, 1.f, 0.f };

Plane() = default;
Plane(const Plane& other) = default;
Plane(const glm::vec3& normal, float distance) : normal(normal), distance(distance) {}

float distanceToPoint(const glm::vec3& point) const {
return glm::dot(normal, point) + distance;
}


};

struct Frustsum {
Plane nearFace;
Plane farFace;
Plane rightFace;
Plane leftFace;
Plane topFace;
Plane bottomFace;

Frustsum() = default;
Frustsum(const Frustsum& other) = default;
Frustsum(const Plane& nearFace, const Plane& farFace, const Plane& rightFace, const Plane& leftFace, const Plane& topFace, const Plane& bottomFace)
: nearFace(nearFace), farFace(farFace), rightFace(rightFace), leftFace(leftFace), topFace(topFace), bottomFace(bottomFace) {}

Frustsum createFrustumFromCamera(const Camera& cam);

bool isInsideFrustsum(const Model& model);
};


#endif // FRUSTSUM_CPP
15 changes: 8 additions & 7 deletions engine/include/group.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ class Group {
public:
std::vector<Model> models;
std::vector<Group> subgroups;
glm::mat4 transformations = glm::mat4(1.0f);
std::vector<glm::mat4> static_transformations;

std::vector<Rotations> rotations;
std::vector<Translations> translates;
std::vector<TimeTransform> order;

std::vector<TimeRotations> rotations;
std::vector<TimeTranslations> translates;
std::vector<Transformations> order;

Group();
Group(std::vector<Model> models, std::vector<Group> subgroups,
glm::mat4 transformations, std::vector<Rotations> rotations,
std::vector<Translations> translates,
std::vector<TimeTransform> order); // Updated constructor
std::vector<glm::mat4> static_transformations, std::vector<TimeRotations> rotations,
std::vector<TimeTranslations> translates,
std::vector<Transformations> order); // Updated constructor

void translate(float x, float y, float z);

Expand Down
8 changes: 4 additions & 4 deletions engine/include/light.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ extern "C" {

enum LightType { DIRECTIONAL, POINT, SPOT };

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

Light createDirectionLight(glm::vec4 direction);

Expand All @@ -31,13 +31,13 @@ void setupLights(std::vector<Light> lights);

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

typedef struct {
struct Material{
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,
Expand Down
10 changes: 7 additions & 3 deletions engine/include/model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ extern "C" {
#endif
}

#include <stb_image/stb_image.h>

#include <algorithm>
#include <iostream>
#include <set>
Expand All @@ -26,7 +28,7 @@ std::vector<unsigned int> generateIBO(const std::vector<Vertex>& points,

class Model {
public:
std::string filename;
std::string filename, texture_filepath;
std::vector<Vertex> vbo;
std::vector<unsigned int> ibo;
int id;
Expand All @@ -36,13 +38,15 @@ class Model {
Model();
Model(std::string filename, std::vector<Vertex> points);

void setupModel();
void initModel();
void drawModel();
bool loadTexture();
void setupModel();

std::vector<Vertex> getPoints();

private:
GLuint _vbo, _ibo, _normals, _textures;
GLuint _vbo, _ibo, _normals, _textures, _texture_id;
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
85 changes: 76 additions & 9 deletions engine/src/curves.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,42 +63,42 @@ std::pair<Point, Point> catmollRomPosition(std::vector<Point> curve,
};
}

Rotations::Rotations() {
TimeRotations::TimeRotations() {
this->time = 0;
this->x = 0;
this->y = 0;
this->z = 0;
}

Rotations::Rotations(float time, float x, float y, float z) {
TimeRotations::TimeRotations(float time, float x, float y, float z) {
this->time = time;
this->x = x;
this->y = y;
this->z = z;
}

void Rotations::applyRotation(float elapsed_time) {
void TimeRotations::applyTimeRotation(float elapsed_time) {
if (this->time == 0) {
return;
}
float angle = 360 * (elapsed_time / this->time);
glRotatef(angle, this->x, this->y, this->z);
}

Translations::Translations() {
TimeTranslations::TimeTranslations() {
this->time = 0;
this->align = false;
this->y_axis = Point(0, 1, 0);
}

Translations::Translations(float time, bool align, std::vector<Point> curve) {
TimeTranslations::TimeTranslations(float time, bool align, std::vector<Point> curve) {
this->time = time;
this->align = align;
this->curvePoints = curve;
this->y_axis = Point(0, 1, 0);
}

std::pair<Point, Point> Translations::getLocation(float elapsed_time) {
std::pair<Point, Point> TimeTranslations::getLocation(float elapsed_time) {
int point_count = this->curvePoints.size();
float gt = elapsed_time / this->time;
float t = gt * point_count;
Expand All @@ -107,7 +107,7 @@ std::pair<Point, Point> Translations::getLocation(float elapsed_time) {
return catmollRomPosition(this->curvePoints, t);
}

std::array<float, 16> Translations::rotationMatrix(Point x, Point y, Point z) {
std::array<float, 16> TimeTranslations::rotationMatrix(Point x, Point y, Point z) {
return std::array<float, 16>{{
x.x,
x.y,
Expand All @@ -128,7 +128,8 @@ std::array<float, 16> Translations::rotationMatrix(Point x, Point y, Point z) {
}};
}

void Translations::applyTranslations(float elapsed_time) {

void TimeTranslations::applyTimeTranslations(float elapsed_time) {
if (this->time == 0) {
return;
}
Expand All @@ -152,7 +153,7 @@ void Translations::applyTranslations(float elapsed_time) {
}
}

void Translations::renderCatmullRomCurve() {
void TimeTranslations::renderCatmullRomCurve() {
// draw curve using line segments with GL_LINE_LOOP
float gt = 0.0;
const float NUM_STEPS = 100;
Expand All @@ -166,3 +167,69 @@ void Translations::renderCatmullRomCurve() {
}
glEnd();
}

Scale::Scale() {
this->x = 1;
this->y = 1;
this->z = 1;
}

Scale::Scale(float x, float y, float z) {
this->x = x;
this->y = y;
this->z = z;
}

glm::mat4 Scalematrix(float x, float y, float z){
glm::mat4 matrix = glm::mat4(1.0f);
matrix = glm::scale(matrix, glm::vec3(x, y, z));
return matrix;
}

void Scale::applyScale() { glScalef(this->x, this->y, this->z); }

Translate::Translate() {
this->x = 0;
this->y = 0;
this->z = 0;
}

Translate::Translate(float x, float y, float z) {
this->x = x;
this->y = y;
this->z = z;
}

glm::mat4 Translatematrix(float x, float y, float z){
glm::mat4 matrix = glm::mat4(1.0f);
matrix = glm::translate(matrix, glm::vec3(x, y, z));
return matrix;
}

void Translate::applyTranslation() { glTranslatef(this->x, this->y, this->z); }

Rotation::Rotation() {
this->angle = 0;
this->x = 0;
this->y = 0;
this->z = 0;
}

Rotation::Rotation(float angle, float x, float y, float z) {
this->angle = angle;
this->x = x;
this->y = y;
this->z = z;
}

glm::mat4 Rotationmatrix(float angle, float x, float y, float z){
glm::mat4 matrix = glm::mat4(1.0f);
matrix = glm::rotate(matrix, glm::radians(angle), glm::vec3(x, y, z));
return matrix;
}

void Rotation::applyRotation() {
float rad = this->angle * M_PI / 180;
glRotatef(rad, this->x, this->y, this->z);
}

Loading
Loading