File opperations to convert text into OBJECTS
This commit is contained in:
parent
c59c838696
commit
f84e739a7f
@ -83,9 +83,18 @@ file_content w_read_file(FILE *file)
|
||||
line_start = i + 1;
|
||||
}
|
||||
}
|
||||
__result.lines = lines;
|
||||
return __result;
|
||||
}
|
||||
|
||||
void w_close_file(FILE *file)
|
||||
{
|
||||
if (file != NULL)
|
||||
{
|
||||
fclose(file);
|
||||
}
|
||||
}
|
||||
|
||||
file_content init_file_content()
|
||||
{
|
||||
file_content r;
|
||||
|
||||
@ -13,6 +13,7 @@ typedef struct
|
||||
|
||||
FILE *w_open_file(const char *filepath);
|
||||
file_content w_read_file(FILE *file);
|
||||
void w_close_file(FILE *file);
|
||||
|
||||
file_content init_file_content();
|
||||
|
||||
|
||||
55
include/vector/obj_utils.cpp
Normal file
55
include/vector/obj_utils.cpp
Normal file
@ -0,0 +1,55 @@
|
||||
#include "vec_utils.hpp"
|
||||
#include "obj_utils.hpp"
|
||||
|
||||
vertex3 vertex3::operator+(const vertex3 &other) const
|
||||
{
|
||||
return vertex3(x + other.x, y + other.y, z + other.z, w + other.w);
|
||||
}
|
||||
vertex3 vertex3::operator-(const vertex3 &other) const
|
||||
{
|
||||
return vertex3(x - other.x, y - other.y, z - other.z, w - other.w);
|
||||
}
|
||||
vertex3 vertex3::operator*(float scalar) const
|
||||
{
|
||||
return vertex3(x * scalar, y * scalar, z * scalar, w * scalar);
|
||||
}
|
||||
vertex3 vertex3::operator/(float scalar) const
|
||||
{
|
||||
if (scalar == 0)
|
||||
{
|
||||
fprintf(stderr, "Error: Division by zero in vertex3::operator/.\n");
|
||||
return vertex3(0, 0, 0, 1); // Return a default vertex if division fails
|
||||
}
|
||||
return vertex3(x / scalar, y / scalar, z / scalar, w / scalar);
|
||||
}
|
||||
float vertex3::dot(const vertex3 &other) const
|
||||
{
|
||||
return x * other.x + y * other.y + z * other.z + w * other.w;
|
||||
}
|
||||
vertex3 vertex3::cross(const vertex3 &other) const
|
||||
{
|
||||
return vertex3(
|
||||
y * other.z - z * other.y,
|
||||
z * other.x - x * other.z,
|
||||
x * other.y - y * other.x);
|
||||
}
|
||||
vertex3 vertex3::normalize() const
|
||||
{
|
||||
float length = sqrt(x * x + y * y + z * z + w * w);
|
||||
if (length == 0)
|
||||
{
|
||||
fprintf(stderr, "Error: Normalization of zero vector in vertex3::normalize.\n");
|
||||
return vertex3(0, 0, 0, 1); // Return a default vertex if normalization fails
|
||||
}
|
||||
return vertex3(x / length, y / length, z / length, w / length);
|
||||
}
|
||||
// Function to convert vertex3 to vec3
|
||||
vec3 vertex3_to_vec3(const vertex3 &v)
|
||||
{
|
||||
return vec3(v.getX(), v.getY(), v.getZ());
|
||||
}
|
||||
// Function to convert vec3 to vertex3
|
||||
vertex3 vec3_to_vertex3(const vec3 &v)
|
||||
{
|
||||
return vertex3(v.getX(), v.getY(), v.getZ(), 1.0f); // w is set to 1 for vertex3
|
||||
}
|
||||
92
include/vector/obj_utils.hpp
Normal file
92
include/vector/obj_utils.hpp
Normal file
@ -0,0 +1,92 @@
|
||||
#ifndef OBJ_UTILS_HPP
|
||||
#define OBJ_UTILS_HPP
|
||||
|
||||
#include "vector/vec_utils.hpp"
|
||||
|
||||
class vertex3
|
||||
{
|
||||
private:
|
||||
float x, y, z;
|
||||
float w;
|
||||
|
||||
public:
|
||||
vertex3() : x(0), y(0), z(0), w(1) {}
|
||||
vertex3(float x, float y, float z, float w = 1.0f) : x(x), y(y), z(z), w(w) {}
|
||||
|
||||
float getX() const { return x; }
|
||||
float getY() const { return y; }
|
||||
float getZ() const { return z; }
|
||||
float getW() const { return w; }
|
||||
|
||||
void setX(float val) { this->x = val; }
|
||||
void setY(float val) { this->y = val; }
|
||||
void setZ(float val) { this->z = val; }
|
||||
void setW(float val) { this->w = val; }
|
||||
|
||||
vertex3 operator+(const vertex3 &other) const;
|
||||
vertex3 operator-(const vertex3 &other) const;
|
||||
vertex3 operator*(float scalar) const;
|
||||
vertex3 operator/(float scalar) const;
|
||||
|
||||
float dot(const vertex3 &other) const;
|
||||
vertex3 cross(const vertex3 &other) const;
|
||||
vertex3 normalize() const;
|
||||
};
|
||||
|
||||
class cube
|
||||
{
|
||||
private:
|
||||
vertex3 *vertices;
|
||||
vertex3 center;
|
||||
float size;
|
||||
|
||||
public:
|
||||
cube()
|
||||
{
|
||||
vertices = (vertex3 *)malloc(8 * sizeof(vertex3));
|
||||
if (!vertices)
|
||||
{
|
||||
fprintf(stderr, "Error: Memory allocation failed for cube vertices.\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
~cube()
|
||||
{
|
||||
if (vertices)
|
||||
{
|
||||
free(vertices);
|
||||
vertices = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void setVertex(int index, const vertex3 &v)
|
||||
{
|
||||
if (index < 0 || index >= 8)
|
||||
{
|
||||
fprintf(stderr, "Error: Index out of bounds in cube::setVertex.\n");
|
||||
return;
|
||||
}
|
||||
vertices[index] = v;
|
||||
}
|
||||
|
||||
vertex3 getVertex(int index) const
|
||||
{
|
||||
if (index < 0 || index >= 8)
|
||||
{
|
||||
fprintf(stderr, "Error: Index out of bounds in cube::getVertex.\n");
|
||||
return vertex3();
|
||||
}
|
||||
return vertices[index];
|
||||
}
|
||||
vertex3 getCenter() const
|
||||
{
|
||||
return center;
|
||||
}
|
||||
float getSize() const
|
||||
{
|
||||
return size;
|
||||
}
|
||||
};
|
||||
|
||||
#endif // !OBJ_UTILS_HPP
|
||||
@ -11,18 +11,27 @@ class vec3
|
||||
{
|
||||
private:
|
||||
float x, y, z;
|
||||
float orientation;
|
||||
float scale;
|
||||
float rotation;
|
||||
|
||||
public:
|
||||
vec3() : x(0), y(0), z(0) {}
|
||||
vec3(float x, float y, float z) : x(x), y(y), z(z) {}
|
||||
vec3() : x(0), y(0), z(0), orientation(0), scale(1), rotation(0) {}
|
||||
vec3(float x, float y, float z) : x(x), y(y), z(z), orientation(0), scale(1), rotation(0) {}
|
||||
|
||||
float getX() const { return x; }
|
||||
float getY() const { return y; }
|
||||
float getZ() const { return z; }
|
||||
float getOrientation() const { return orientation; }
|
||||
float getScale() const { return scale; }
|
||||
float getRotation() const { return rotation; }
|
||||
|
||||
void setX(float val) { this->x = val; }
|
||||
void setY(float val) { this->y = val; }
|
||||
void setZ(float val) { this->z = val; }
|
||||
void setOrientation(float val) { this->orientation = val; }
|
||||
void setScale(float val) { this->scale = val; }
|
||||
void setRotation(float val) { this->rotation = val; }
|
||||
|
||||
vec3 operator+(const vec3 &other) const;
|
||||
vec3 operator-(const vec3 &other) const;
|
||||
|
||||
@ -2,22 +2,19 @@
|
||||
|
||||
// Include the vector utilities header file
|
||||
#include "vector/vec_utils.hpp"
|
||||
#include "vector/obj_utils.hpp"
|
||||
#include "io/io_file_op.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
file_content file_info = w_read_file(w_open_file("./CUBE.obj"));
|
||||
|
||||
for (size_t i = 0; i < (u_int32_t)file_info.num_lines; i++)
|
||||
if (file_info.lines == NULL || file_info.num_lines <= 0)
|
||||
{
|
||||
printf("Line %04zu: \t", i);
|
||||
for (size_t j = 0; file_info.lines[i][j] != '\0'; j++)
|
||||
{
|
||||
printf("%c", file_info.lines[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
fprintf(stderr, "Error: Could not read file or file is empty.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
w_close_file(w_open_file("./CUBE.obj"));
|
||||
free(file_info.lines);
|
||||
return 0;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user