Skip to content

Commit

Permalink
abstracted clear color and backfaceculling
Browse files Browse the repository at this point in the history
  • Loading branch information
BarthPaleologue committed Sep 26, 2023
1 parent e87a938 commit 2feed9b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
7 changes: 7 additions & 0 deletions core/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ Engine::Engine(int windowWidth, int windowHeight, const char *name = "Feather Pr
engine->mouseX = xpos;
engine->mouseY = ypos;
});

setClearColor(0.1f, 0.1f, 0.1f, 1.0f);

glDepthFunc(GL_LESS); // Specify the depth test for the z-buffer
glEnable(GL_DEPTH_TEST); // Enable the z-buffer test in the rasterization
glEnable(GL_BLEND);
glCullFace(GL_BACK); // Specifies the faces to cull (here the ones pointing away from the camera)
}

Engine::~Engine() = default;
4 changes: 4 additions & 0 deletions core/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class Engine {
return (float) glfwGetTime();
}

void setClearColor(float r, float g, float b, float a) {
glClearColor(r, g, b, a);
}

void start() {
while (!glfwWindowShouldClose(window)) {
onExecuteLoopObservable.notifyObservers();
Expand Down
6 changes: 6 additions & 0 deletions core/materials/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ void Material::compile() {

void Material::bind() {
glUseProgram(_program);

if (_isBackFaceCullingEnabled) {
glEnable(GL_CULL_FACE); // Enables face culling (based on the orientation defined by the CW/CCW enumeration).
} else {
glDisable(GL_CULL_FACE);
}
}

void Material::setMat4(const char *uniformName, const glm::mat4 *matrix) const {
Expand Down
23 changes: 17 additions & 6 deletions core/materials/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,34 @@

class Material {
public:
explicit Material(const char* shaderFolder);
explicit Material(const char *shaderFolder);

void compile();

virtual void bind();

virtual void unbind();

void setMat4(const char* uniformName, const glm::mat4 * matrix) const;
void setVec3(const char* uniformName, const glm::vec3 * vector) const;
void setInt(const char* uniformName, int integer) const;
void setMat4(const char *uniformName, const glm::mat4 *matrix) const;

void setVec3(const char *uniformName, const glm::vec3 *vector) const;

void bindTexture(const char* uniformName, Texture *texture, int id) const;
void setInt(const char *uniformName, int integer) const;

void setDefine(const char* defineName);
void bindTexture(const char *uniformName, Texture *texture, int id) const;

void setDefine(const char *defineName);

bool isBackFaceCullingEnabled() const {
return _isBackFaceCullingEnabled;
}

private:
GLuint _program;
std::string _vertexShaderCode;
std::string _fragmentShaderCode;

bool _isBackFaceCullingEnabled = true;
};


Expand Down
9 changes: 1 addition & 8 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,7 @@ int main() {
CelestialBody moon("moon", 0.25, 5, 5, 2, &moonMat, scene);

CelestialBody mars("mars", 0.35, 10, 12, 13, &marsMat, scene);

glCullFace(GL_BACK); // Specifies the faces to cull (here the ones pointing away from the camera)
glEnable(GL_CULL_FACE); // Enables face culling (based on the orientation defined by the CW/CCW enumeration).
glDepthFunc(GL_LESS); // Specify the depth test for the z-buffer
glEnable(GL_DEPTH_TEST); // Enable the z-buffer test in the rasterization
glEnable(GL_BLEND);
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);


CelestialBody *currentTarget = &sun;

engine.onKeyPressObservable.add([&currentTarget, &sun, &earth, &moon, &engine](int key) {
Expand Down

0 comments on commit 2feed9b

Please sign in to comment.