Skip to content

Instantly share code, notes, and snippets.

@ashtonx
Created February 23, 2018 22:02
Show Gist options
  • Save ashtonx/6c45bf03e4cf482f011fdfe9a833d477 to your computer and use it in GitHub Desktop.
Save ashtonx/6c45bf03e4cf482f011fdfe9a833d477 to your computer and use it in GitHub Desktop.
while loop
while (!glfwWindowShouldClose(window))
{
// per frame logic
// ---------------
float currentFrame = static_cast<float>(glfwGetTime());
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
// input
// -----
processInput(window);
// render
// ------
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Activate shader
lightingShader.use();
lightingShader.setVec3("viewPosition", camera.Position);
lightingShader.setFloat("material.shininess", 32.0f);
//Directional light
lightingShader.setVec3("directLight.direction", -0.2f, -1.0f, -0.3f);
lightingShader.setVec3("directLight.ambient", 0.05f, 0.05f, 0.05f);
lightingShader.setVec3("directLight.diffuse", 0.4f, 0.4f, 0.4f);
lightingShader.setVec3("directLight.specular", 0.5f, 0.5f, 0.5f);
// point lights
for (int i = 0; i < 4; ++i)
{
std::string light = "pointLights["+std::to_string(i)+"].";
lightingShader.setVec3(light+"position", pointLightPositions[i]);
lightingShader.setVec3(light+"ambient", 0.05f, 0.05f, 0.05f);
lightingShader.setVec3(light+"diffuse", 0.8f, 0.8f, 0.8f);
lightingShader.setVec3(light+"specular", 1.0f, 1.0f, 1.0f);
lightingShader.setFloat(light+"constant", 1.0f);
lightingShader.setFloat(light+"linear", 0.09f);
lightingShader.setFloat(light+"quadratic", 0.032f);
}
// Projections
glm::mat4 projection = glm::perspective(
glm::radians(camera.Zoom),
static_cast<float>(WIN_WIDTH) / static_cast<float>(WIN_HEIGHT),
0.1f,
100.0f);
lightingShader.setMat4("projection", projection);
// camera/VIEW transformation
glm::mat4 view = camera.GetViewMatrix();
lightingShader.setMat4("view", view);
// world transformation
glm::mat4 model;
lightingShader.setMat4("model", model);
//bind Difuse & spec map
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, diffuseMap);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, specularMap);
// render containers
glBindVertexArray(cubeVAO);
for(unsigned int i = 0; i<10; ++i)
{
glm::mat4 model;
model = glm::translate(model, cubePositions[i]);
float angle = static_cast<float>(20.0f * (i*0.5f));
model = glm::rotate(model, glm::radians(angle),
glm::vec3(1.0f, 0.3f, 0.5f));
lightingShader.setMat4("model", model);
glm::mat3 normalModelMatrix = glm::transpose(glm::inverse(model));
lightingShader.setMat3("normalModel", normalModelMatrix);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
// Lamp
lampShader.use();
lampShader.setMat4("projection", projection);
lampShader.setMat4("view", view);
lampShader.setVec4("lightColor",glm::vec4(1.0f,1.0f, 1.0f ,1.0f));
glBindVertexArray(lightVAO);
for (int i = 0; i<4 ; ++i)
{
model = glm::mat4();
model = glm::translate(model, pointLightPositions[i]);
model = glm::scale(model, glm::vec3(0.2f));
lampShader.setMat4("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
// check and call events and swap the buffers
glfwSwapBuffers(window);
glfwPollEvents();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment