Lighting: Basic color

Added a light source
removed texturing
removed drawing multiple instances
of the same cube
This commit is contained in:
Nigel Barink 2022-02-13 13:19:54 +01:00
parent c8bde6881f
commit 06b979dfaa
6 changed files with 134 additions and 175 deletions

7
lightsource.fs Normal file
View File

@ -0,0 +1,7 @@
#version 460 core
out vec4 FragColor;
void main()
{
FragColor = vec4(1.0);
}

14
lightsource.vs Normal file
View File

@ -0,0 +1,14 @@
#version 460 core
layout (location=0) in vec3 aPos;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main(){
gl_Position = projection * view * model * vec4(aPos , 1.0);
}

View File

@ -1,12 +1,11 @@
#version 460 core #version 460 core
out vec4 FragColor; out vec4 FragColor;
uniform vec3 objectColor;
uniform vec3 lightColor;
in vec2 TexCoord;
uniform sampler2D texture1;
uniform sampler2D texture2;
void main() void main()
{ {
FragColor = mix(texture(texture1, TexCoord) , texture(texture2, vec2(-TexCoord.x , TexCoord.y)), 0.2); FragColor = vec4(lightColor * objectColor, 1.0);
} }

View File

@ -1,18 +1,15 @@
#version 460 core #version 460 core
layout (location=0) in vec3 aPos; layout (location=0) in vec3 aPos;
layout (location=2) in vec3 aColor;
layout (location=1) in vec2 aTexCoord;
uniform float HorizontalOffset;
uniform mat4 model; uniform mat4 model;
uniform mat4 view; uniform mat4 view;
uniform mat4 projection; uniform mat4 projection;
out vec2 TexCoord;
void main(){ void main(){
gl_Position = projection * view * model * vec4(aPos , 1.0); gl_Position = projection * view * model * vec4(aPos , 1.0);
TexCoord = aTexCoord;
} }

View File

@ -16,10 +16,12 @@
float deltaTime = 0.0f; // Time between current frame and last frame float deltaTime = 0.0f; // Time between current frame and last frame
float lastFrame = 0.0f; // Time of last frame float lastFrame = 0.0f; // Time of last frame
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f)); Camera camera(glm::vec3(0.0f, 0.0f, 8.0f));
float lastX = 400, lastY = 300; float lastX = 400, lastY = 300;
bool firstMouse = true; bool firstMouse = true;
glm::vec3 lightpos(1.2f, 1.0f, 2.0f);
void framebuffer_size_callback(GLFWwindow* window, int width, int height) { void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
glViewport(0,0, width, height); glViewport(0,0, width, height);
} }
@ -86,56 +88,54 @@ int main() {
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
Shader shader("shader.vs", "shader.fs"); Shader lightshader("lightsource.vs", "lightsource.fs");
Shader shader ("shader.vs", "shader.fs");
float vertices[] = {
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
float vertices[] = { 0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, -0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.5f, -0.5f, -0.5f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.5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, VAO; unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO); glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO); glGenBuffers(1, &VBO);
@ -146,150 +146,92 @@ int main() {
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// position attribute // position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
// texture coord attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
unsigned int lightVAO;
glGenVertexArrays(1, &lightVAO);
glBindVertexArray(lightVAO);
int width, height, nrChannels; glBindBuffer(GL_ARRAY_BUFFER, VBO);
unsigned int texture1;
glGenTextures(1, &texture1);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);
glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE, 3* sizeof(float), (void*) 0);
glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glEnableVertexAttribArray(0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); #pragma endregion
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
unsigned char* data = stbi_load("Textures/container.jpg", &width, &height, &nrChannels,0);
if(data){
std::cout<< width << " x " << height << std::endl;
glTexImage2D(GL_TEXTURE_2D,0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
} else{ glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
std::cout << "Failed to load texture!" << std::endl; glfwSetCursorPosCallback(window, mouse_callback);
} glfwSetScrollCallback(window, scroll_callback);
glEnable(GL_DEPTH_TEST);
while(!glfwWindowShouldClose(window))
{
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
processInput(window);
stbi_image_free(data); glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
int width2, height2, nrChannels2; glm::mat4 model = glm::mat4(1.0f);
unsigned int texture2; glm::mat4 view = glm::mat4(1.0f);
glGenTextures(1, &texture2); glm::mat4 projection = glm::mat4(1.0f);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);
glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
stbi_set_flip_vertically_on_load(true);
unsigned char* data2 = stbi_load("Textures/awesomeface.png", &width2, &height2, &nrChannels2,0);
if(data2){
std::cout<< width2 << " x " << height2 << std::endl;
glTexImage2D(GL_TEXTURE_2D,0, GL_RGB, width2, height2, 0, GL_RGBA, GL_UNSIGNED_BYTE, data2);
glGenerateMipmap(GL_TEXTURE_2D);
} else{
std::cout << "Failed to load texture!" << std::endl;
}
stbi_image_free(data2); view = camera.GetViewMatrix();
projection = glm::perspective(glm::radians(camera.Zoom), (float)800 / (float)600, 0.1f, 100.0f);
// draw cubes
shader.use(); shader.use();
shader.setInt("texture1",0); shader.setVec3("objectColor", glm::vec3( 1.0f, 0.5, 0.31f));
shader.setInt("texture2",1); shader.setVec3("lightColor",glm::vec3( 1.0f, 1.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)
};
shader.setMat4("view", view);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); shader.setMat4("projection", projection);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
glEnable(GL_DEPTH_TEST);
while(!glfwWindowShouldClose(window)) model = glm::mat4(1.0);
{ shader.setMat4("model", model);
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
processInput(window); glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);
glActiveTexture(GL_TEXTURE1); // draw lights
glBindTexture(GL_TEXTURE_2D, texture2); lightshader.use();
lightshader.setMat4("view", view);
lightshader.setMat4("projection", projection);
model = glm::mat4(1.0f);
model = glm::translate(model, lightpos);
model = glm::scale(model, glm::vec3(0.2f));
lightshader.setMat4("model", model);
// create transformations glBindVertexArray(lightVAO);
glm::mat4 model = glm::mat4(1.0f); // make sure to initialize matrix to identity matrix first glDrawArrays(GL_TRIANGLES, 0, 36);
glm::mat4 view = glm::mat4(1.0f);
glm::mat4 projection = glm::mat4(1.0f);
model = glm::rotate(model, (float)glfwGetTime(), glm::vec3(0.5f, 1.0f, 0.0f));
// Pass a matrix to the shader
view = camera.GetViewMatrix();
shader.setMat4("view", view);
projection = glm::perspective(glm::radians(camera.Zoom), (float)800 / (float)600, 0.1f, 100.0f);
shader.setMat4("projection", projection); glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glBindVertexArray(VAO); glfwTerminate();
for(unsigned int i = 0; i < 10; i++){ return 0;
glm::mat4 model = glm::mat4(1.0);
model = glm::translate(model, cubePositions[i]);
float angle = 20.0f * i;
model = glm::rotate(model, (float)glfwGetTime() * glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
shader.setMat4("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glfwTerminate();
return 0;
} }

View File

@ -21,7 +21,7 @@ class Shader
void setInt(const std::string &name, int value)const; void setInt(const std::string &name, int value)const;
void setFloat(const std::string &name, float value)const; void setFloat(const std::string &name, float value)const;
void setMat4(const std::string &name, glm::mat4 value)const; void setMat4(const std::string &name, glm::mat4 value)const;
void setVec3(const std::string &name, glm::vec3 value) const ;
}; };
@ -50,13 +50,9 @@ Shader::Shader(const char* vertextPath, const char* fragmentPath)
fShaderFile.close(); fShaderFile.close();
vertexCode = vShaderStream.str(); vertexCode = vShaderStream.str();
std::cout << "VertexShaderSource:\n\n" << vertexCode << std::endl;
fragmentCode = fShaderStream.str(); fragmentCode = fShaderStream.str();
std::cout << "FragmentShaderSource:\n\n" << fragmentCode << std::endl;
} }
catch(std::ifstream::failure e){ catch(std::ifstream::failure e){
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl << e.what() << std::endl ; std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl << e.what() << std::endl ;
@ -135,4 +131,8 @@ void Shader::setFloat(const std::string &name, float value) const{
void Shader::setMat4(const std::string &name, glm::mat4 value)const{ void Shader::setMat4(const std::string &name, glm::mat4 value)const{
glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &value[0][0] ); glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &value[0][0] );
}
void Shader::setVec3(const std::string &name, glm::vec3 value)const{
glUniform3fv(glGetUniformLocation(ID, name.c_str()), 1, &value.x);
} }