Directional light implementation
This commit is contained in:
		
							
								
								
									
										35
									
								
								src/main.cpp
									
									
									
									
									
								
							
							
						
						
									
										35
									
								
								src/main.cpp
									
									
									
									
									
								
							@ -20,7 +20,7 @@ Camera camera(glm::vec3(0.0f, 0.0f, 8.0f));
 | 
			
		||||
float lastX = 400, lastY = 300;
 | 
			
		||||
bool firstMouse  = true;
 | 
			
		||||
 | 
			
		||||
glm::vec3 lightpos(1.2f, 1.0f, 2.0f);
 | 
			
		||||
glm::vec3 lightpos(-0.2f, -1.0f, -0.3f);
 | 
			
		||||
 | 
			
		||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
 | 
			
		||||
   glViewport(0,0, width, height);
 | 
			
		||||
@ -136,6 +136,19 @@ float vertices[] = {
 | 
			
		||||
    -0.5f,  0.5f, -0.5f,  0.0f,  1.0f,  0.0f,  0.0f, 1.0f
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
glm::vec3 cubePositions[] = {
 | 
			
		||||
    glm::vec3( 0.0f,  0.0f,  0.0f),
 | 
			
		||||
    glm::vec3( 2.0f,  5.0f, -15.0f),
 | 
			
		||||
    glm::vec3(-1.5f, -2.2f, -2.5f),
 | 
			
		||||
    glm::vec3(-3.8f, -2.0f, -12.3f),
 | 
			
		||||
    glm::vec3( 2.4f, -0.4f, -3.5f),
 | 
			
		||||
    glm::vec3(-1.7f,  3.0f, -7.5f),
 | 
			
		||||
    glm::vec3( 1.3f, -2.0f, -2.5f),
 | 
			
		||||
    glm::vec3( 1.5f,  2.0f, -2.5f),
 | 
			
		||||
    glm::vec3( 1.5f,  0.2f, -1.5f),
 | 
			
		||||
    glm::vec3(-1.3f,  1.0f, -1.5f)
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
   unsigned int VBO, VAO;
 | 
			
		||||
   glGenVertexArrays(1, &VAO);
 | 
			
		||||
   glGenBuffers(1, &VBO);
 | 
			
		||||
@ -233,9 +246,7 @@ while(!glfwWindowShouldClose(window))
 | 
			
		||||
   
 | 
			
		||||
   float orbital_speed = .4 ; 
 | 
			
		||||
 | 
			
		||||
   // move light around 
 | 
			
		||||
   lightpos.x = 1.0 + sin(glfwGetTime()) * 2.0f;
 | 
			
		||||
   lightpos.y = sin(glfwGetTime() / 2.0f) * 1.0f;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
   
 | 
			
		||||
   // draw cubes
 | 
			
		||||
@ -248,7 +259,7 @@ while(!glfwWindowShouldClose(window))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
   shader.setVec3("light.position", lightpos);
 | 
			
		||||
   shader.setVec3("light.direction", lightpos);
 | 
			
		||||
 | 
			
		||||
   shader.setVec3("light.ambient", glm::vec3(0.2f, 0.2f, 0.2f));
 | 
			
		||||
   shader.setVec3("light.diffuse", glm::vec3(0.5f, 0.5f, 0.5f));
 | 
			
		||||
@ -271,8 +282,18 @@ while(!glfwWindowShouldClose(window))
 | 
			
		||||
   glActiveTexture(GL_TEXTURE1);
 | 
			
		||||
   glBindTexture(GL_TEXTURE_2D, specularMap);
 | 
			
		||||
 | 
			
		||||
   glBindVertexArray(VAO);
 | 
			
		||||
   glDrawArrays(GL_TRIANGLES, 0, 36);
 | 
			
		||||
   for( unsigned int i = 0; i < 10; i++ ){
 | 
			
		||||
      glm::mat4 model = glm::mat4(1.0f);
 | 
			
		||||
      model = glm::translate(model , cubePositions[i]);
 | 
			
		||||
      float angle = 20.0f * i;
 | 
			
		||||
      model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
 | 
			
		||||
      shader.setMat4("model", model);
 | 
			
		||||
 | 
			
		||||
      glBindVertexArray(VAO);
 | 
			
		||||
      glDrawArrays(GL_TRIANGLES, 0, 36);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
   }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user