diff --git a/CMakeLists.txt b/CMakeLists.txt index 1c6d077..e4e63c9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 $/shaders) + add_custom_command( + TARGET ${SOLAR_SYSTEM} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/src/shaders $/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} $/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 $/resources ) elseif(UNIX AND NOT APPLE) diff --git a/src/main.cpp b/src/main.cpp index 3e84147..766bf05 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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 @@ -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(); @@ -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 @@ -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(); } } @@ -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; } @@ -291,9 +295,9 @@ void renderSphere() { glGenBuffers(1, &vbo); glGenBuffers(1, &ebo); - std::vector positions; - std::vector uv; - std::vector normals; + std::vector positions; // vertices + std::vector uv; // texture coordinates + std::vector normals; // normals std::vector indices; const unsigned int STEP = 64; // increase to improve shape quality @@ -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); @@ -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); } diff --git a/src/main.h b/src/main.h index 72c38c7..762ddf8 100644 --- a/src/main.h +++ b/src/main.h @@ -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); diff --git a/src/shaders/planetFragment.glsl b/src/shaders/planetFragment.glsl index 91a8bfe..ca22124 100644 --- a/src/shaders/planetFragment.glsl +++ b/src/shaders/planetFragment.glsl @@ -4,7 +4,6 @@ out vec4 FragColor; struct Material { sampler2D diffuse; sampler2D specular; - float shininess; }; struct Light { @@ -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;