Shader exercises complete!

This commit is contained in:
Nigel Barink 2022-02-11 22:24:15 +01:00
parent 2e529beafa
commit 58cca1db34
6 changed files with 180 additions and 81 deletions

View File

@ -3,7 +3,7 @@ CFLAGS = -std=c++17 -O2
LDFLAGS = -lglfw3 -lGL -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi LDFLAGS = -lglfw3 -lGL -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi
OUT_NAME = LearnOpenGL OUT_NAME = LearnOpenGL
build: src/main.cpp build: src/main.cpp src/shader.h
mkdir -p build/ mkdir -p build/
$(CC) $(CFLAGS) -Ilib/GLAD/include -Ilib/glfw/include -o build/$(OUT_NAME) lib/GLAD/src/glad.c src/main.cpp $(LDFLAGS) $(CC) $(CFLAGS) -Ilib/GLAD/include -Ilib/glfw/include -o build/$(OUT_NAME) lib/GLAD/src/glad.c src/main.cpp $(LDFLAGS)

11
shader.fs Normal file
View File

@ -0,0 +1,11 @@
#version 460 core
out vec4 FragColor;
in vec4 ourPosition;
in vec3 ourColor;
void main()
{
FragColor = ourPosition;// vec4(ourColor, 1.0f);
}

15
shader.vs Normal file
View File

@ -0,0 +1,15 @@
#version 460 core
layout (location=0) in vec3 aPos;
layout (location=1) in vec3 aColor;
uniform float HorizontalOffset;
out vec3 ourColor;
out vec4 ourPosition;
void main(){
gl_Position = vec4(-aPos.x + HorizontalOffset, aPos.yz , 1.0);
ourColor = aColor;
ourPosition = gl_Position;
}

View File

@ -1,8 +1,11 @@
#include <stdio.h> #include <stdio.h>
#include <cmath>
#include <glad/glad.h> #include <glad/glad.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include "shader.h"
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);
@ -41,82 +44,20 @@ int main() {
glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
// TODO: Load from disk Shader shader("shader.vs", "shader.fs");
const char* vertextShaderSource =
"#version 460 core\n"
"layout (location=0) in vec3 aPos;\n"
"void main(){\n"
"gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\n";
unsigned int vertexShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertextShaderSource, NULL);
glCompileShader(vertexShader);
int success;
char infolog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if(!success){
glGetShaderInfoLog(vertexShader, 512, NULL, infolog);
printf ( "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n %s", infolog);
}
//TODO: load from disk
const char* fragmentShaderSource =
"#version 330 core\n"
"out vec4 FragColor;\n"
"void main(){\n"
"FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\n";
unsigned int fragmentShader;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if(!success){
glGetShaderInfoLog(fragmentShader, 512 ,NULL,infolog);
printf("ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n %s", infolog);
}
unsigned int shaderProgram;
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if(!success) {
glGetProgramInfoLog(shaderProgram, 512, NULL, infolog);
printf("ERROR::SHADER_PROGRAM::LINKING_FAILED\n %s", infolog);
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
float vertices[] = { float vertices[] = {
0.5f, 0.5f, 0.0f, // top, right 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom right
0.5f, -0.5f, 0.0f, // bottom right -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left
-0.5f, -0.5f, 0.0f, // bottom left 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f // top
-0.5f, 0.5f, 0.0f // top left
}; };
unsigned int indices[] = { unsigned int indices[] = {
0, 1,3, // first triangle 0, 1,2 // first triangle
1,2,3 // second triangle
}; };
// Vertex buffer objects!! // Vertex buffer objects!!
@ -133,9 +74,12 @@ int main() {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// Position attribute
glVertexAttribPointer(0,3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); glVertexAttribPointer(0,3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
// Color attribute
glVertexAttribPointer(1,3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3* sizeof(float)));
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
@ -150,11 +94,15 @@ int main() {
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram);
shader.use();
shader.setFloat("HorizontalOffset", .3);
glBindVertexArray(VAO); glBindVertexArray(VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glDrawElements(GL_TRIANGLES, 6,GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, 6,GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
glfwSwapBuffers(window); glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();
@ -162,7 +110,6 @@ int main() {
glDeleteVertexArrays(1, &VAO); glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO); glDeleteBuffers(1, &VBO);
glDeleteProgram(shaderProgram);
glfwTerminate(); glfwTerminate();

133
src/shader.h Normal file
View File

@ -0,0 +1,133 @@
#pragma once
#include <glad/glad.h>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
class Shader
{
public:
unsigned int ID;// Program ID
// Read and build the shader upon construction
Shader(const char* vertexPath, const char* fragmentPath);
// Activate the shader
void use();
void setBool(const std::string &name, bool value)const;
void setInt(const std::string &name, int value)const;
void setFloat(const std::string &name, float value)const;
};
Shader::Shader(const char* vertextPath, const char* fragmentPath)
{
// retrieve the vertex / fragment source code from filepath
std::string vertexCode;
std::string fragmentCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile;
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try{
vShaderFile.open(vertextPath);
fShaderFile.open(fragmentPath);
std::stringstream vShaderStream,fShaderStream;
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
vShaderFile.close();
fShaderFile.close();
vertexCode = vShaderStream.str();
std::cout << "VertexShaderSource:\n\n" << vertexCode << std::endl;
fragmentCode = fShaderStream.str();
std::cout << "FragmentShaderSource:\n\n" << fragmentCode << std::endl;
}
catch(std::ifstream::failure e){
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl << e.what() << std::endl ;
}
const char* vShaderCode = vertexCode.c_str();
const char* fShaderCode = fragmentCode.c_str();
// Compile Shaders
unsigned int vertex, fragment;
int success;
char infoLog[512];
// vertex shader
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShaderCode, NULL);
glCompileShader(vertex);
// print any compile errors if there are any
glGetShaderiv(vertex,GL_COMPILE_STATUS, &success);
if(!success)
{
glGetShaderInfoLog(vertex, 512, NULL, infoLog);
std::cout << "ERROR::SHADER:::VERTEX::COMPILATION_FAILED" << std::endl << infoLog << std::endl;
}
// fragment shader
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, NULL);
glCompileShader(fragment);
glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
if(!success){
glGetShaderInfoLog(fragment, 512, NULL, infoLog);
std::cout << "ERROR::SHADER:::VERTEX::COMPILATION_FAILED" << std::endl << infoLog << std::endl;
}
// Shader Program
ID = glCreateProgram();
glAttachShader(ID, vertex);
glAttachShader(ID, fragment);
glLinkProgram(ID);
glGetProgramiv(ID, GL_LINK_STATUS, &success);
if(!success){
glGetProgramInfoLog(ID, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED" << std::endl << infoLog << std::endl;
}
// delete shaders as they're now linked into our program and no longer necessary
glDeleteShader(vertex);
glDeleteShader(fragment);
}
void Shader::use(){
glUseProgram(ID);
}
void Shader::setBool(const std::string &name, bool value) const{
glUniform1i(glGetUniformLocation(ID, name.c_str()), (int) value );
}
void Shader::setInt(const std::string &name, int value) const{
glUniform1i(glGetUniformLocation(ID, name.c_str()), value );
}
void Shader::setFloat(const std::string &name, float value) const{
glUniform1f(glGetUniformLocation(ID, name.c_str()), value );
}

View File

@ -1,7 +0,0 @@
#version 460 core
layout (location=0) in vec3 aPos;
void main(){
gl_Position = vec4(aPos.x, aPos.y, aPos.z 1.0);
}