Skip to content

Commit

Permalink
Merge branch 'jp/textures' of github.com:JulioJPinto/CG-Project into …
Browse files Browse the repository at this point in the history
…jp/textures
  • Loading branch information
gramosomi committed May 22, 2024
2 parents f8c0e48 + 8a7d9e0 commit 1c14ccc
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 37 deletions.
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
29 changes: 3 additions & 26 deletions engine/src/frustsum.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,4 @@
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "frustsum.hpp"

// Frustum createFrustumFromCamera(const Camera& cam, float aspect, float fovY,
// float zNear,
// float zFar)
// {
// Frustum frustum;
// const float halfVSide = zFar * tanf(fovY * .5f);
// const float halfHSide = halfVSide * aspect;
// const glm::vec3 frontMultFar = zFar * cam.Front;

// frustum.nearFace = { cam.Position + zNear * cam.Front, cam.Front };
// frustum.farFace = { cam.Position + frontMultFar, -cam.Front };
// frustum.rightFace = { cam.Position,
// glm::cross(frontMultFar - cam.Right * halfHSide,
// cam.Up) };
// frustum.leftFace = { cam.Position,
// glm::cross(cam.Up,frontMultFar + cam.Right *
// halfHSide) };
// frustum.topFace = { cam.Position,
// glm::cross(cam.Right, frontMultFar - cam.Up *
// halfVSide) };
// frustum.bottomFace = { cam.Position,
// glm::cross(frontMultFar + cam.Up * halfVSide,
// cam.Right) };

// return frustum;
// }
28 changes: 17 additions & 11 deletions engine/src/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,23 @@ bool Model::loadTexture() {

// Determine the correct format
GLenum format;
if (num_channels == 1)
format = GL_RED;
else if (num_channels == 3)
format = GL_RGB;
else if (num_channels == 4)
format = GL_RGBA;
else {
std::cerr << "Unsupported number of channels: " << num_channels
<< std::endl;
stbi_image_free(image_data);
return false;
switch (num_channels) {
case 1:
format = GL_RED;
break;
case 3:
format = GL_RGB;
break;
case 4:
format = GL_RGBA;
break;
default: {
// Free image data before returning
stbi_image_free(image_data);
std::cerr << "Unsupported number of channels: " << num_channels
<< std::endl;
return false;
}
}

// Upload data to GPU
Expand Down
40 changes: 40 additions & 0 deletions scripts/channels_formats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from PIL import Image
import sys

def check_image_format(image_path):
try:
with Image.open(image_path) as img:
mode = img.mode
format = img.format
size = img.size

if mode == "L":
num_channels = 1 # Grayscale
opengl_format = "GL_RED"
elif mode == "LA":
num_channels = 2 # Grayscale + Alpha
opengl_format = "GL_RG"
elif mode == "RGB":
num_channels = 3 # RGB
opengl_format = "GL_RGB"
elif mode == "RGBA":
num_channels = 4 # RGBA
opengl_format = "GL_RGBA"
else:
num_channels = len(mode) # Other modes like "CMYK"
opengl_format = "Unknown or not directly supported"

print(f"Image Format: {format}")
print(f"Image Size: {size}")
print(f"Number of Channels: {num_channels}")
print(f"OpenGL Format: {opengl_format}")

except IOError:
print(f"Cannot open image file: {image_path}")

if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python check_image_format.py <image_path>")
else:
image_path = sys.argv[1]
check_image_format(image_path)

0 comments on commit 1c14ccc

Please sign in to comment.