Skip to content

Commit

Permalink
Code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
joelvaz0x01 committed Dec 23, 2023
1 parent aaea384 commit 53fd289
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 41 deletions.
30 changes: 10 additions & 20 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,24 @@ file(GLOB SRC_SOLAR_SYSTEM
add_executable(${SOLAR_SYSTEM} ${SRC_SOLAR_SYSTEM})
target_link_libraries(${SOLAR_SYSTEM} ${ALL_LIBS})

# create directory for shaders on ${CMAKE_SOURCE_DIR}/bin
# copy shaders to ${CMAKE_SOURCE_DIR}/bin/shaders directory
# POST_BUILD is to override shaders directory
if(WIN32)
add_custom_command(TARGET ${SOLAR_SYSTEM} PRE_BUILD COMMAND ${CMAKE_COMMAND} -E make_directory $<TARGET_FILE_DIR:${SOLAR_SYSTEM}>/shaders)
add_custom_command(
TARGET ${SOLAR_SYSTEM} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/src/shaders $<TARGET_FILE_DIR:${SOLAR_SYSTEM}>/shaders
)
elseif(UNIX AND NOT APPLE)
file(MAKE_DIRECTORY ${CMAKE_SOURCE_DIR}/bin/shaders)
file(COPY ${CMAKE_SOURCE_DIR}/src/shaders DESTINATION ${CMAKE_SOURCE_DIR}/bin/)
elseif(APPLE)
file(MAKE_DIRECTORY ${CMAKE_SOURCE_DIR}/bin/shaders)
makeLink(${CMAKE_SOURCE_DIR}/src/shaders ${CMAKE_SOURCE_DIR}/bin/shaders ${SOLAR_SYSTEM})
endif(WIN32)

# copy shaders to ${CMAKE_SOURCE_DIR}/bin/shaders directory
foreach(SHADER ${SHADERS})
if(WIN32)
add_custom_command(
TARGET ${SOLAR_SYSTEM} PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${SHADER} $<TARGET_FILE_DIR:${SOLAR_SYSTEM}>/shaders
)
elseif(UNIX AND NOT APPLE)
file(COPY ${SHADER} DESTINATION ${CMAKE_SOURCE_DIR}/bin/)
elseif(APPLE)
get_filename_component(SHADER_NAME ${SHADER} NAME)
makeLink(${SHADER} ${CMAKE_SOURCE_DIR}/bin/${SHADER_NAME} ${SOLAR_SYSTEM})
endif(WIN32)
endforeach(SHADER)

# copy resources to ${CMAKE_SOURCE_DIR}/bin/resources directory
# POST_BUILD is to override resources directory
if(WIN32)
add_custom_command(
TARGET ${SOLAR_SYSTEM} PRE_BUILD
TARGET ${SOLAR_SYSTEM} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/resources $<TARGET_FILE_DIR:${SOLAR_SYSTEM}>/resources
)
elseif(UNIX AND NOT APPLE)
Expand Down
41 changes: 23 additions & 18 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ int main() {
loadTexture("resources/textures/neptune.jpg")
};

// load moon texture
// load earth's moon texture
unsigned int moonTexture = loadTexture("resources/textures/moon.jpg");

// load saturn's ring texture
Expand Down Expand Up @@ -141,6 +141,7 @@ int main() {
// light properties (sun)
glm::vec3 sunPosition = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 sunLightColor = glm::vec3(1.0f, 1.0f, 1.0f);
glm::mat4 sunModel = glm::mat4(1.0f);

while (!glfwWindowShouldClose(window)) {
double currentFrame = glfwGetTime();
Expand All @@ -165,10 +166,10 @@ int main() {
sun.setVec3("color", lightColor);
sun.setMat4("projection", projection);
sun.setMat4("view", view);
glm::mat4 sunModel = glm::translate(glm::mat4(1.0f), sunPosition);
sunModel = glm::translate(glm::mat4(1.0f), sunPosition);
sunModel = glm::rotate(sunModel, (float) glfwGetTime() * 0.1f, glm::vec3(0.0f, 1.0f, 0.0f));
sun.setMat4("model", sunModel);
bindTexture(sunTexture, 0);
bindTexture(sunTexture);
renderSphere();

// planet properties
Expand All @@ -179,30 +180,29 @@ int main() {
planet.setVec3("light.ambient", ambientColor);
planet.setVec3("light.diffuse", diffuseColor);
planet.setVec3("light.specular", lightColor);
planet.setFloat("material.shininess", 0.4f);

// render planets
for (unsigned int i = 0; i < planetCount; i++) {
planetModel[i] = planetCreator(
planetProp[i].translation, // translation around the sun
planetProp[i].translation, // translation around the sun (translation velocity)
planetProp[i].distance, // distance from the sun
planetProp[i].rotation, // rotation around its own axis
planetProp[i].rotation, // rotation around its own axis (rotation velocity)
planetProp[i].scale, // scale of the planet
sunModel[3] // center of the model (contains [x, y, z])
sunModel[3] // center of the model (contains the exact position of the sun)
);
planet.setMat4("model", planetModel[i]);
bindTexture(planetTextures[i], 0);
bindTexture(planetTextures[i]);
renderSphere();
if (planetProp[i].name == "earth") { // render moon
glm::mat4 moonModel = planetCreator(
5.0f, // translation around the earth
5.0f, // translation around the earth (translation velocity)
0.3f, // distance from the earth
2.0f, // rotation around its own axis
2.0f, // rotation around its own axis (rotation velocity)
0.05f, // scale of the planet
planetModel[i][3] // center of the model (contains [x, y, z])
planetModel[i][3] // center of the model (contains the exact position of the earth)
);
planet.setMat4("model", moonModel);
bindTexture(moonTexture, 0);
bindTexture(moonTexture);
renderSphere();
}
}
Expand All @@ -212,7 +212,11 @@ int main() {
glfwPollEvents();
}

glfwTerminate();
// de-allocate all resources
glDeleteBuffers(1, &sphereVAO);
delete[] planetModel;

glfwTerminate(); // clear all previously allocated GLFW resources
return 0;
}

Expand Down Expand Up @@ -291,9 +295,9 @@ void renderSphere() {
glGenBuffers(1, &vbo);
glGenBuffers(1, &ebo);

std::vector<glm::vec3> positions;
std::vector<glm::vec2> uv;
std::vector<glm::vec3> normals;
std::vector<glm::vec3> positions; // vertices
std::vector<glm::vec2> uv; // texture coordinates
std::vector<glm::vec3> normals; // normals
std::vector<unsigned int> indices;

const unsigned int STEP = 64; // increase to improve shape quality
Expand Down Expand Up @@ -359,6 +363,7 @@ void renderSphere() {
data.push_back(uv[i].y);
}
}

glBindVertexArray(sphereVAO);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), &data[0], GL_STATIC_DRAW);
Expand Down Expand Up @@ -451,8 +456,8 @@ unsigned int loadTexture(char const *path) {
* @param textureUnit: texture unit to bind
* @return nullptr
*/
void bindTexture(unsigned int texture, unsigned int textureUnit) {
glActiveTexture(GL_TEXTURE + textureUnit);
void bindTexture(unsigned int texture) {
glActiveTexture(GL_TEXTURE);
glBindTexture(GL_TEXTURE_2D, texture);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ unsigned int loadTexture(const char *path);

void renderSphere();

void bindTexture(unsigned int texture, unsigned int textureUnit);
void bindTexture(unsigned int texture);

glm::mat4 planetCreator(float translation, float distance, float rotation, float scale, glm::vec3 centerModel);

Expand Down
3 changes: 1 addition & 2 deletions src/shaders/planetFragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ out vec4 FragColor;
struct Material {
sampler2D diffuse;
sampler2D specular;
float shininess;
};

struct Light {
Expand Down Expand Up @@ -41,7 +40,7 @@ void main()
// specular
vec3 viewDir = normalize(viewPos - FragPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
float spec = max(dot(viewDir, reflectDir), 0.0);
vec3 specular = light.specular * spec * texture(material.specular, TexCoords).rgb;

vec3 result = ambient + diffuse + specular;
Expand Down

0 comments on commit 53fd289

Please sign in to comment.