6 done
This commit is contained in:
parent
38a7285c2b
commit
61c1ac5fb0
13
6/exercises/1/Makefile
Normal file
13
6/exercises/1/Makefile
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
CC=g++
|
||||||
|
CFLAGS= -Wall -g
|
||||||
|
|
||||||
|
LDLIBS = -lglfw
|
||||||
|
|
||||||
|
TARGET := test
|
||||||
|
|
||||||
|
build:
|
||||||
|
$(CC) $(CFLAGS) $(LDLIBS) src/*.c src/*.cpp -o $(TARGET)
|
||||||
|
|
||||||
|
.PHONY: clean
|
||||||
|
clean:
|
||||||
|
rm -rf $(BUILD_DIR)
|
1140
6/exercises/1/src/glad.c
Normal file
1140
6/exercises/1/src/glad.c
Normal file
File diff suppressed because it is too large
Load Diff
3611
6/exercises/1/src/include/glad.h
Normal file
3611
6/exercises/1/src/include/glad.h
Normal file
File diff suppressed because it is too large
Load Diff
114
6/exercises/1/src/include/shader.h
Normal file
114
6/exercises/1/src/include/shader.h
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
#ifndef SHADER_H
|
||||||
|
#define SHADER_H
|
||||||
|
|
||||||
|
#include "glad.h"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
class Shader
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// program ID
|
||||||
|
unsigned int ID;
|
||||||
|
|
||||||
|
// constructor reads and builds the shader
|
||||||
|
Shader(const char* vertexPath, const char* fragmentPath)
|
||||||
|
{
|
||||||
|
std::string vertexCode;
|
||||||
|
std::string fragmentCode;
|
||||||
|
std::ifstream vShaderFile;
|
||||||
|
std::ifstream fShaderFile;
|
||||||
|
|
||||||
|
// setup exceptions for bad files
|
||||||
|
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||||
|
fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||||
|
try {
|
||||||
|
// open files
|
||||||
|
vShaderFile.open(vertexPath);
|
||||||
|
fShaderFile.open(fragmentPath);
|
||||||
|
std::stringstream vShaderStream, fShaderStream;
|
||||||
|
|
||||||
|
// read file buffers into streams
|
||||||
|
vShaderStream << vShaderFile.rdbuf();
|
||||||
|
fShaderStream << fShaderFile.rdbuf();
|
||||||
|
|
||||||
|
// close file handlers
|
||||||
|
vShaderFile.close();
|
||||||
|
fShaderFile.close();
|
||||||
|
|
||||||
|
// convert stream into string
|
||||||
|
vertexCode = vShaderStream.str();
|
||||||
|
fragmentCode = fShaderStream.str();
|
||||||
|
} catch (std::ifstream::failure& e) {
|
||||||
|
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ:" << e.what() << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* vShaderCode = vertexCode.c_str();
|
||||||
|
const char* fShaderCode = fragmentCode.c_str();
|
||||||
|
|
||||||
|
unsigned int vertex, fragment;
|
||||||
|
int success;
|
||||||
|
char infoLog[512];
|
||||||
|
vertex = glCreateShader(GL_VERTEX_SHADER);
|
||||||
|
|
||||||
|
glShaderSource(vertex, 1, &vShaderCode, NULL);
|
||||||
|
glCompileShader(vertex);
|
||||||
|
|
||||||
|
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
|
||||||
|
if (!success) {
|
||||||
|
glGetShaderInfoLog(vertex, 512, NULL, infoLog);
|
||||||
|
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
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::FRAGMENT::COMPILATION_FAILED\n" << infoLog << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
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\n" << infoLog << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
// delete shaders, no longer needed
|
||||||
|
glDeleteShader(vertex);
|
||||||
|
glDeleteShader(fragment);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// use/activate the shader
|
||||||
|
void use()
|
||||||
|
{
|
||||||
|
glUseProgram(ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
// utility uniform functions
|
||||||
|
void setBool(const std::string &name, bool value) const
|
||||||
|
{
|
||||||
|
glUniform1i(glGetUniformLocation(ID, name.c_str()), (int) value);
|
||||||
|
}
|
||||||
|
void setInt(const std::string &name, int value) const
|
||||||
|
{
|
||||||
|
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
|
||||||
|
}
|
||||||
|
void setFloat(const std::string &name, float value) const
|
||||||
|
{
|
||||||
|
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
128
6/exercises/1/src/main.cpp
Normal file
128
6/exercises/1/src/main.cpp
Normal file
@ -0,0 +1,128 @@
|
|||||||
|
#include "include/glad.h"
|
||||||
|
#include "include/shader.h"
|
||||||
|
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
|
||||||
|
void processInput(GLFWwindow* window);
|
||||||
|
void setupGLFW(GLFWwindow** window);
|
||||||
|
|
||||||
|
float vertices[] = {
|
||||||
|
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom right
|
||||||
|
-0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left
|
||||||
|
0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, // top
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
bool wireframe = false;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
float timeValue;
|
||||||
|
float greenValue;
|
||||||
|
int vertexColorLocation;
|
||||||
|
|
||||||
|
GLFWwindow* window;
|
||||||
|
setupGLFW(&window);
|
||||||
|
|
||||||
|
Shader myShader("src/shaders/shader.vs", "src/shaders/shader.fs");
|
||||||
|
|
||||||
|
|
||||||
|
unsigned int VAO;
|
||||||
|
glGenVertexArrays(1, &VAO);
|
||||||
|
|
||||||
|
unsigned int VBO;
|
||||||
|
glGenBuffers(1, &VBO);
|
||||||
|
|
||||||
|
// bind Vertex Array Object
|
||||||
|
glBindVertexArray(VAO);
|
||||||
|
// copy our vertices into a buffer for opengl to use
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
// set the vertex attributes pointers
|
||||||
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*) 0);
|
||||||
|
glEnableVertexAttribArray(0);
|
||||||
|
|
||||||
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*) (3 * sizeof(float)));
|
||||||
|
glEnableVertexAttribArray(1);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
while(!glfwWindowShouldClose(window)) {
|
||||||
|
//input
|
||||||
|
processInput(window);
|
||||||
|
|
||||||
|
//rendering
|
||||||
|
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
|
/*
|
||||||
|
timeValue = glfwGetTime();
|
||||||
|
greenValue = (sin(timeValue) / 2.0f) + 0.5f;
|
||||||
|
vertexColorLocation = glGetUniformLocation(shaderProgram, "ourColor"); // finding attributes DOESNT require the program being used
|
||||||
|
if (vertexColorLocation == -1) {
|
||||||
|
std::cout << "Uniform value could not be located.\n";
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
myShader.use();
|
||||||
|
|
||||||
|
//glUniform4f(vertexColorLocation, 0.0f, greenValue, 0.0f, 1.0f); // but updating uniforms DOES requires the program to be used
|
||||||
|
|
||||||
|
glBindVertexArray(VAO);
|
||||||
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||||
|
glBindVertexArray(0); // unbinds the VAO so its ready to be bound again for the next render
|
||||||
|
|
||||||
|
// check and call events and swap buffers
|
||||||
|
glfwSwapBuffers(window);
|
||||||
|
glfwPollEvents();
|
||||||
|
}
|
||||||
|
|
||||||
|
glfwTerminate();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setupGLFW(GLFWwindow** window)
|
||||||
|
{
|
||||||
|
glfwInit();
|
||||||
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||||
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||||
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||||
|
|
||||||
|
*window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
|
||||||
|
if (window == NULL) {
|
||||||
|
std::cout << "Failed to create GLFW window\n";
|
||||||
|
glfwTerminate();
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
glfwMakeContextCurrent(*window);
|
||||||
|
|
||||||
|
if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) {
|
||||||
|
std::cout << "Failed to init GLAD\n";
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
glViewport(0, 0, 800, 600);
|
||||||
|
glfwSetFramebufferSizeCallback(*window, framebuffer_size_callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
|
||||||
|
{
|
||||||
|
glViewport(0, 0, width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
void processInput(GLFWwindow* window)
|
||||||
|
{
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||||
|
glfwSetWindowShouldClose(window, true);
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_ENTER) == GLFW_PRESS) // triggers wireframe mode
|
||||||
|
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_BACKSPACE) == GLFW_PRESS) // disables wireframe mode
|
||||||
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||||
|
|
||||||
|
}
|
9
6/exercises/1/src/shaders/shader.fs
Normal file
9
6/exercises/1/src/shaders/shader.fs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#version 330 core
|
||||||
|
|
||||||
|
out vec4 FragColor;
|
||||||
|
in vec3 ourColor;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
FragColor = vec4(ourColor, 1.0);
|
||||||
|
}
|
12
6/exercises/1/src/shaders/shader.vs
Normal file
12
6/exercises/1/src/shaders/shader.vs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#version 330 core
|
||||||
|
|
||||||
|
layout (location = 0) in vec3 aPos;
|
||||||
|
layout (location = 1) in vec3 aColor;
|
||||||
|
|
||||||
|
out vec3 ourColor;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
gl_Position = vec4(aPos * -1, 1.0);
|
||||||
|
ourColor = aColor;
|
||||||
|
}
|
BIN
6/exercises/1/test
Executable file
BIN
6/exercises/1/test
Executable file
Binary file not shown.
13
6/exercises/2/Makefile
Normal file
13
6/exercises/2/Makefile
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
CC=g++
|
||||||
|
CFLAGS= -Wall -g
|
||||||
|
|
||||||
|
LDLIBS = -lglfw
|
||||||
|
|
||||||
|
TARGET := test
|
||||||
|
|
||||||
|
build:
|
||||||
|
$(CC) $(CFLAGS) $(LDLIBS) src/*.c src/*.cpp -o $(TARGET)
|
||||||
|
|
||||||
|
.PHONY: clean
|
||||||
|
clean:
|
||||||
|
rm -rf $(BUILD_DIR)
|
1140
6/exercises/2/src/glad.c
Normal file
1140
6/exercises/2/src/glad.c
Normal file
File diff suppressed because it is too large
Load Diff
3611
6/exercises/2/src/include/glad.h
Normal file
3611
6/exercises/2/src/include/glad.h
Normal file
File diff suppressed because it is too large
Load Diff
114
6/exercises/2/src/include/shader.h
Normal file
114
6/exercises/2/src/include/shader.h
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
#ifndef SHADER_H
|
||||||
|
#define SHADER_H
|
||||||
|
|
||||||
|
#include "glad.h"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
class Shader
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// program ID
|
||||||
|
unsigned int ID;
|
||||||
|
|
||||||
|
// constructor reads and builds the shader
|
||||||
|
Shader(const char* vertexPath, const char* fragmentPath)
|
||||||
|
{
|
||||||
|
std::string vertexCode;
|
||||||
|
std::string fragmentCode;
|
||||||
|
std::ifstream vShaderFile;
|
||||||
|
std::ifstream fShaderFile;
|
||||||
|
|
||||||
|
// setup exceptions for bad files
|
||||||
|
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||||
|
fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||||
|
try {
|
||||||
|
// open files
|
||||||
|
vShaderFile.open(vertexPath);
|
||||||
|
fShaderFile.open(fragmentPath);
|
||||||
|
std::stringstream vShaderStream, fShaderStream;
|
||||||
|
|
||||||
|
// read file buffers into streams
|
||||||
|
vShaderStream << vShaderFile.rdbuf();
|
||||||
|
fShaderStream << fShaderFile.rdbuf();
|
||||||
|
|
||||||
|
// close file handlers
|
||||||
|
vShaderFile.close();
|
||||||
|
fShaderFile.close();
|
||||||
|
|
||||||
|
// convert stream into string
|
||||||
|
vertexCode = vShaderStream.str();
|
||||||
|
fragmentCode = fShaderStream.str();
|
||||||
|
} catch (std::ifstream::failure& e) {
|
||||||
|
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ:" << e.what() << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* vShaderCode = vertexCode.c_str();
|
||||||
|
const char* fShaderCode = fragmentCode.c_str();
|
||||||
|
|
||||||
|
unsigned int vertex, fragment;
|
||||||
|
int success;
|
||||||
|
char infoLog[512];
|
||||||
|
vertex = glCreateShader(GL_VERTEX_SHADER);
|
||||||
|
|
||||||
|
glShaderSource(vertex, 1, &vShaderCode, NULL);
|
||||||
|
glCompileShader(vertex);
|
||||||
|
|
||||||
|
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
|
||||||
|
if (!success) {
|
||||||
|
glGetShaderInfoLog(vertex, 512, NULL, infoLog);
|
||||||
|
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
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::FRAGMENT::COMPILATION_FAILED\n" << infoLog << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
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\n" << infoLog << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
// delete shaders, no longer needed
|
||||||
|
glDeleteShader(vertex);
|
||||||
|
glDeleteShader(fragment);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// use/activate the shader
|
||||||
|
void use()
|
||||||
|
{
|
||||||
|
glUseProgram(ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
// utility uniform functions
|
||||||
|
void setBool(const std::string &name, bool value) const
|
||||||
|
{
|
||||||
|
glUniform1i(glGetUniformLocation(ID, name.c_str()), (int) value);
|
||||||
|
}
|
||||||
|
void setInt(const std::string &name, int value) const
|
||||||
|
{
|
||||||
|
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
|
||||||
|
}
|
||||||
|
void setFloat(const std::string &name, float value) const
|
||||||
|
{
|
||||||
|
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
129
6/exercises/2/src/main.cpp
Normal file
129
6/exercises/2/src/main.cpp
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
#include "include/glad.h"
|
||||||
|
#include "include/shader.h"
|
||||||
|
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
|
||||||
|
void processInput(GLFWwindow* window);
|
||||||
|
void setupGLFW(GLFWwindow** window);
|
||||||
|
|
||||||
|
float vertices[] = {
|
||||||
|
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom right
|
||||||
|
-0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left
|
||||||
|
0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, // top
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
bool wireframe = false;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
float timeValue;
|
||||||
|
float greenValue;
|
||||||
|
int vertexColorLocation;
|
||||||
|
|
||||||
|
GLFWwindow* window;
|
||||||
|
setupGLFW(&window);
|
||||||
|
|
||||||
|
Shader myShader("src/shaders/shader.vs", "src/shaders/shader.fs");
|
||||||
|
|
||||||
|
|
||||||
|
unsigned int VAO;
|
||||||
|
glGenVertexArrays(1, &VAO);
|
||||||
|
|
||||||
|
unsigned int VBO;
|
||||||
|
glGenBuffers(1, &VBO);
|
||||||
|
|
||||||
|
// bind Vertex Array Object
|
||||||
|
glBindVertexArray(VAO);
|
||||||
|
// copy our vertices into a buffer for opengl to use
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
// set the vertex attributes pointers
|
||||||
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*) 0);
|
||||||
|
glEnableVertexAttribArray(0);
|
||||||
|
|
||||||
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*) (3 * sizeof(float)));
|
||||||
|
glEnableVertexAttribArray(1);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
while(!glfwWindowShouldClose(window)) {
|
||||||
|
//input
|
||||||
|
processInput(window);
|
||||||
|
|
||||||
|
//rendering
|
||||||
|
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
|
/*
|
||||||
|
timeValue = glfwGetTime();
|
||||||
|
greenValue = (sin(timeValue) / 2.0f) + 0.5f;
|
||||||
|
vertexColorLocation = glGetUniformLocation(shaderProgram, "ourColor"); // finding attributes DOESNT require the program being used
|
||||||
|
if (vertexColorLocation == -1) {
|
||||||
|
std::cout << "Uniform value could not be located.\n";
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
myShader.use();
|
||||||
|
myShader.setFloat("hOffset", 1.0f);
|
||||||
|
|
||||||
|
//glUniform4f(vertexColorLocation, 0.0f, greenValue, 0.0f, 1.0f); // but updating uniforms DOES requires the program to be used
|
||||||
|
|
||||||
|
glBindVertexArray(VAO);
|
||||||
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||||
|
glBindVertexArray(0); // unbinds the VAO so its ready to be bound again for the next render
|
||||||
|
|
||||||
|
// check and call events and swap buffers
|
||||||
|
glfwSwapBuffers(window);
|
||||||
|
glfwPollEvents();
|
||||||
|
}
|
||||||
|
|
||||||
|
glfwTerminate();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setupGLFW(GLFWwindow** window)
|
||||||
|
{
|
||||||
|
glfwInit();
|
||||||
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||||
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||||
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||||
|
|
||||||
|
*window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
|
||||||
|
if (window == NULL) {
|
||||||
|
std::cout << "Failed to create GLFW window\n";
|
||||||
|
glfwTerminate();
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
glfwMakeContextCurrent(*window);
|
||||||
|
|
||||||
|
if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) {
|
||||||
|
std::cout << "Failed to init GLAD\n";
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
glViewport(0, 0, 800, 600);
|
||||||
|
glfwSetFramebufferSizeCallback(*window, framebuffer_size_callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
|
||||||
|
{
|
||||||
|
glViewport(0, 0, width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
void processInput(GLFWwindow* window)
|
||||||
|
{
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||||
|
glfwSetWindowShouldClose(window, true);
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_ENTER) == GLFW_PRESS) // triggers wireframe mode
|
||||||
|
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_BACKSPACE) == GLFW_PRESS) // disables wireframe mode
|
||||||
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||||
|
|
||||||
|
}
|
9
6/exercises/2/src/shaders/shader.fs
Normal file
9
6/exercises/2/src/shaders/shader.fs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#version 330 core
|
||||||
|
|
||||||
|
out vec4 FragColor;
|
||||||
|
in vec3 ourColor;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
FragColor = vec4(ourColor, 1.0);
|
||||||
|
}
|
14
6/exercises/2/src/shaders/shader.vs
Normal file
14
6/exercises/2/src/shaders/shader.vs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#version 330 core
|
||||||
|
|
||||||
|
layout (location = 0) in vec3 aPos;
|
||||||
|
layout (location = 1) in vec3 aColor;
|
||||||
|
|
||||||
|
uniform float hOffset;
|
||||||
|
|
||||||
|
out vec3 ourColor;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
gl_Position = vec4(aPos.x + hOffset, aPos.yz, 1.0);
|
||||||
|
ourColor = aColor;
|
||||||
|
}
|
BIN
6/exercises/2/test
Executable file
BIN
6/exercises/2/test
Executable file
Binary file not shown.
13
6/exercises/3/Makefile
Normal file
13
6/exercises/3/Makefile
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
CC=g++
|
||||||
|
CFLAGS= -Wall -g
|
||||||
|
|
||||||
|
LDLIBS = -lglfw
|
||||||
|
|
||||||
|
TARGET := test
|
||||||
|
|
||||||
|
build:
|
||||||
|
$(CC) $(CFLAGS) $(LDLIBS) src/*.c src/*.cpp -o $(TARGET)
|
||||||
|
|
||||||
|
.PHONY: clean
|
||||||
|
clean:
|
||||||
|
rm -rf $(BUILD_DIR)
|
1140
6/exercises/3/src/glad.c
Normal file
1140
6/exercises/3/src/glad.c
Normal file
File diff suppressed because it is too large
Load Diff
3611
6/exercises/3/src/include/glad.h
Normal file
3611
6/exercises/3/src/include/glad.h
Normal file
File diff suppressed because it is too large
Load Diff
114
6/exercises/3/src/include/shader.h
Normal file
114
6/exercises/3/src/include/shader.h
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
#ifndef SHADER_H
|
||||||
|
#define SHADER_H
|
||||||
|
|
||||||
|
#include "glad.h"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
class Shader
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// program ID
|
||||||
|
unsigned int ID;
|
||||||
|
|
||||||
|
// constructor reads and builds the shader
|
||||||
|
Shader(const char* vertexPath, const char* fragmentPath)
|
||||||
|
{
|
||||||
|
std::string vertexCode;
|
||||||
|
std::string fragmentCode;
|
||||||
|
std::ifstream vShaderFile;
|
||||||
|
std::ifstream fShaderFile;
|
||||||
|
|
||||||
|
// setup exceptions for bad files
|
||||||
|
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||||
|
fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||||
|
try {
|
||||||
|
// open files
|
||||||
|
vShaderFile.open(vertexPath);
|
||||||
|
fShaderFile.open(fragmentPath);
|
||||||
|
std::stringstream vShaderStream, fShaderStream;
|
||||||
|
|
||||||
|
// read file buffers into streams
|
||||||
|
vShaderStream << vShaderFile.rdbuf();
|
||||||
|
fShaderStream << fShaderFile.rdbuf();
|
||||||
|
|
||||||
|
// close file handlers
|
||||||
|
vShaderFile.close();
|
||||||
|
fShaderFile.close();
|
||||||
|
|
||||||
|
// convert stream into string
|
||||||
|
vertexCode = vShaderStream.str();
|
||||||
|
fragmentCode = fShaderStream.str();
|
||||||
|
} catch (std::ifstream::failure& e) {
|
||||||
|
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ:" << e.what() << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* vShaderCode = vertexCode.c_str();
|
||||||
|
const char* fShaderCode = fragmentCode.c_str();
|
||||||
|
|
||||||
|
unsigned int vertex, fragment;
|
||||||
|
int success;
|
||||||
|
char infoLog[512];
|
||||||
|
vertex = glCreateShader(GL_VERTEX_SHADER);
|
||||||
|
|
||||||
|
glShaderSource(vertex, 1, &vShaderCode, NULL);
|
||||||
|
glCompileShader(vertex);
|
||||||
|
|
||||||
|
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
|
||||||
|
if (!success) {
|
||||||
|
glGetShaderInfoLog(vertex, 512, NULL, infoLog);
|
||||||
|
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
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::FRAGMENT::COMPILATION_FAILED\n" << infoLog << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
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\n" << infoLog << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
// delete shaders, no longer needed
|
||||||
|
glDeleteShader(vertex);
|
||||||
|
glDeleteShader(fragment);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// use/activate the shader
|
||||||
|
void use()
|
||||||
|
{
|
||||||
|
glUseProgram(ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
// utility uniform functions
|
||||||
|
void setBool(const std::string &name, bool value) const
|
||||||
|
{
|
||||||
|
glUniform1i(glGetUniformLocation(ID, name.c_str()), (int) value);
|
||||||
|
}
|
||||||
|
void setInt(const std::string &name, int value) const
|
||||||
|
{
|
||||||
|
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
|
||||||
|
}
|
||||||
|
void setFloat(const std::string &name, float value) const
|
||||||
|
{
|
||||||
|
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
128
6/exercises/3/src/main.cpp
Normal file
128
6/exercises/3/src/main.cpp
Normal file
@ -0,0 +1,128 @@
|
|||||||
|
#include "include/glad.h"
|
||||||
|
#include "include/shader.h"
|
||||||
|
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
|
||||||
|
void processInput(GLFWwindow* window);
|
||||||
|
void setupGLFW(GLFWwindow** window);
|
||||||
|
|
||||||
|
float vertices[] = {
|
||||||
|
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom right
|
||||||
|
-0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left
|
||||||
|
0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, // top
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
bool wireframe = false;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
float timeValue;
|
||||||
|
float greenValue;
|
||||||
|
int vertexColorLocation;
|
||||||
|
|
||||||
|
GLFWwindow* window;
|
||||||
|
setupGLFW(&window);
|
||||||
|
|
||||||
|
Shader myShader("src/shaders/shader.vs", "src/shaders/shader.fs");
|
||||||
|
|
||||||
|
|
||||||
|
unsigned int VAO;
|
||||||
|
glGenVertexArrays(1, &VAO);
|
||||||
|
|
||||||
|
unsigned int VBO;
|
||||||
|
glGenBuffers(1, &VBO);
|
||||||
|
|
||||||
|
// bind Vertex Array Object
|
||||||
|
glBindVertexArray(VAO);
|
||||||
|
// copy our vertices into a buffer for opengl to use
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
// set the vertex attributes pointers
|
||||||
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*) 0);
|
||||||
|
glEnableVertexAttribArray(0);
|
||||||
|
|
||||||
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*) (3 * sizeof(float)));
|
||||||
|
glEnableVertexAttribArray(1);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
while(!glfwWindowShouldClose(window)) {
|
||||||
|
//input
|
||||||
|
processInput(window);
|
||||||
|
|
||||||
|
//rendering
|
||||||
|
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
|
/*
|
||||||
|
timeValue = glfwGetTime();
|
||||||
|
greenValue = (sin(timeValue) / 2.0f) + 0.5f;
|
||||||
|
vertexColorLocation = glGetUniformLocation(shaderProgram, "ourColor"); // finding attributes DOESNT require the program being used
|
||||||
|
if (vertexColorLocation == -1) {
|
||||||
|
std::cout << "Uniform value could not be located.\n";
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
myShader.use();
|
||||||
|
|
||||||
|
//glUniform4f(vertexColorLocation, 0.0f, greenValue, 0.0f, 1.0f); // but updating uniforms DOES requires the program to be used
|
||||||
|
|
||||||
|
glBindVertexArray(VAO);
|
||||||
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||||
|
glBindVertexArray(0); // unbinds the VAO so its ready to be bound again for the next render
|
||||||
|
|
||||||
|
// check and call events and swap buffers
|
||||||
|
glfwSwapBuffers(window);
|
||||||
|
glfwPollEvents();
|
||||||
|
}
|
||||||
|
|
||||||
|
glfwTerminate();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setupGLFW(GLFWwindow** window)
|
||||||
|
{
|
||||||
|
glfwInit();
|
||||||
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||||
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||||
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||||
|
|
||||||
|
*window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
|
||||||
|
if (window == NULL) {
|
||||||
|
std::cout << "Failed to create GLFW window\n";
|
||||||
|
glfwTerminate();
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
glfwMakeContextCurrent(*window);
|
||||||
|
|
||||||
|
if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) {
|
||||||
|
std::cout << "Failed to init GLAD\n";
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
glViewport(0, 0, 800, 600);
|
||||||
|
glfwSetFramebufferSizeCallback(*window, framebuffer_size_callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
|
||||||
|
{
|
||||||
|
glViewport(0, 0, width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
void processInput(GLFWwindow* window)
|
||||||
|
{
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||||
|
glfwSetWindowShouldClose(window, true);
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_ENTER) == GLFW_PRESS) // triggers wireframe mode
|
||||||
|
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_BACKSPACE) == GLFW_PRESS) // disables wireframe mode
|
||||||
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||||
|
|
||||||
|
}
|
10
6/exercises/3/src/shaders/shader.fs
Normal file
10
6/exercises/3/src/shaders/shader.fs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#version 330 core
|
||||||
|
|
||||||
|
out vec4 FragColor;
|
||||||
|
in vec3 ourColor;
|
||||||
|
in vec3 vPos;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
FragColor = vec4(vPos, 1.0);
|
||||||
|
}
|
14
6/exercises/3/src/shaders/shader.vs
Normal file
14
6/exercises/3/src/shaders/shader.vs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#version 330 core
|
||||||
|
|
||||||
|
layout (location = 0) in vec3 aPos;
|
||||||
|
layout (location = 1) in vec3 aColor;
|
||||||
|
|
||||||
|
out vec3 ourColor;
|
||||||
|
out vec3 vPos;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
gl_Position = vec4(aPos, 1.0);
|
||||||
|
ourColor = aColor;
|
||||||
|
vPos = aPos;
|
||||||
|
}
|
BIN
6/exercises/3/test
Executable file
BIN
6/exercises/3/test
Executable file
Binary file not shown.
13
6/lecture/Makefile
Normal file
13
6/lecture/Makefile
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
CC=g++
|
||||||
|
CFLAGS= -Wall -g
|
||||||
|
|
||||||
|
LDLIBS = -lglfw
|
||||||
|
|
||||||
|
TARGET := test
|
||||||
|
|
||||||
|
build:
|
||||||
|
$(CC) $(CFLAGS) $(LDLIBS) src/*.c src/*.cpp -o $(TARGET)
|
||||||
|
|
||||||
|
.PHONY: clean
|
||||||
|
clean:
|
||||||
|
rm -rf $(BUILD_DIR)
|
1140
6/lecture/src/glad.c
Normal file
1140
6/lecture/src/glad.c
Normal file
File diff suppressed because it is too large
Load Diff
3611
6/lecture/src/include/glad.h
Normal file
3611
6/lecture/src/include/glad.h
Normal file
File diff suppressed because it is too large
Load Diff
114
6/lecture/src/include/shader.h
Normal file
114
6/lecture/src/include/shader.h
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
#ifndef SHADER_H
|
||||||
|
#define SHADER_H
|
||||||
|
|
||||||
|
#include "glad.h"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
class Shader
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// program ID
|
||||||
|
unsigned int ID;
|
||||||
|
|
||||||
|
// constructor reads and builds the shader
|
||||||
|
Shader(const char* vertexPath, const char* fragmentPath)
|
||||||
|
{
|
||||||
|
std::string vertexCode;
|
||||||
|
std::string fragmentCode;
|
||||||
|
std::ifstream vShaderFile;
|
||||||
|
std::ifstream fShaderFile;
|
||||||
|
|
||||||
|
// setup exceptions for bad files
|
||||||
|
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||||
|
fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||||
|
try {
|
||||||
|
// open files
|
||||||
|
vShaderFile.open(vertexPath);
|
||||||
|
fShaderFile.open(fragmentPath);
|
||||||
|
std::stringstream vShaderStream, fShaderStream;
|
||||||
|
|
||||||
|
// read file buffers into streams
|
||||||
|
vShaderStream << vShaderFile.rdbuf();
|
||||||
|
fShaderStream << fShaderFile.rdbuf();
|
||||||
|
|
||||||
|
// close file handlers
|
||||||
|
vShaderFile.close();
|
||||||
|
fShaderFile.close();
|
||||||
|
|
||||||
|
// convert stream into string
|
||||||
|
vertexCode = vShaderStream.str();
|
||||||
|
fragmentCode = fShaderStream.str();
|
||||||
|
} catch (std::ifstream::failure& e) {
|
||||||
|
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ:" << e.what() << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* vShaderCode = vertexCode.c_str();
|
||||||
|
const char* fShaderCode = fragmentCode.c_str();
|
||||||
|
|
||||||
|
unsigned int vertex, fragment;
|
||||||
|
int success;
|
||||||
|
char infoLog[512];
|
||||||
|
vertex = glCreateShader(GL_VERTEX_SHADER);
|
||||||
|
|
||||||
|
glShaderSource(vertex, 1, &vShaderCode, NULL);
|
||||||
|
glCompileShader(vertex);
|
||||||
|
|
||||||
|
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
|
||||||
|
if (!success) {
|
||||||
|
glGetShaderInfoLog(vertex, 512, NULL, infoLog);
|
||||||
|
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
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::FRAGMENT::COMPILATION_FAILED\n" << infoLog << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
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\n" << infoLog << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
// delete shaders, no longer needed
|
||||||
|
glDeleteShader(vertex);
|
||||||
|
glDeleteShader(fragment);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// use/activate the shader
|
||||||
|
void use()
|
||||||
|
{
|
||||||
|
glUseProgram(ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
// utility uniform functions
|
||||||
|
void setBool(const std::string &name, bool value) const
|
||||||
|
{
|
||||||
|
glUniform1i(glGetUniformLocation(ID, name.c_str()), (int) value);
|
||||||
|
}
|
||||||
|
void setInt(const std::string &name, int value) const
|
||||||
|
{
|
||||||
|
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
|
||||||
|
}
|
||||||
|
void setFloat(const std::string &name, float value) const
|
||||||
|
{
|
||||||
|
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
128
6/lecture/src/main.cpp
Normal file
128
6/lecture/src/main.cpp
Normal file
@ -0,0 +1,128 @@
|
|||||||
|
#include "include/glad.h"
|
||||||
|
#include "include/shader.h"
|
||||||
|
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
|
||||||
|
void processInput(GLFWwindow* window);
|
||||||
|
void setupGLFW(GLFWwindow** window);
|
||||||
|
|
||||||
|
float vertices[] = {
|
||||||
|
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom right
|
||||||
|
-0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left
|
||||||
|
0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, // top
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
bool wireframe = false;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
float timeValue;
|
||||||
|
float greenValue;
|
||||||
|
int vertexColorLocation;
|
||||||
|
|
||||||
|
GLFWwindow* window;
|
||||||
|
setupGLFW(&window);
|
||||||
|
|
||||||
|
Shader myShader("src/shaders/shader.vs", "src/shaders/shader.fs");
|
||||||
|
|
||||||
|
|
||||||
|
unsigned int VAO;
|
||||||
|
glGenVertexArrays(1, &VAO);
|
||||||
|
|
||||||
|
unsigned int VBO;
|
||||||
|
glGenBuffers(1, &VBO);
|
||||||
|
|
||||||
|
// bind Vertex Array Object
|
||||||
|
glBindVertexArray(VAO);
|
||||||
|
// copy our vertices into a buffer for opengl to use
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
// set the vertex attributes pointers
|
||||||
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*) 0);
|
||||||
|
glEnableVertexAttribArray(0);
|
||||||
|
|
||||||
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*) (3 * sizeof(float)));
|
||||||
|
glEnableVertexAttribArray(1);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
while(!glfwWindowShouldClose(window)) {
|
||||||
|
//input
|
||||||
|
processInput(window);
|
||||||
|
|
||||||
|
//rendering
|
||||||
|
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
|
/*
|
||||||
|
timeValue = glfwGetTime();
|
||||||
|
greenValue = (sin(timeValue) / 2.0f) + 0.5f;
|
||||||
|
vertexColorLocation = glGetUniformLocation(shaderProgram, "ourColor"); // finding attributes DOESNT require the program being used
|
||||||
|
if (vertexColorLocation == -1) {
|
||||||
|
std::cout << "Uniform value could not be located.\n";
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
myShader.use();
|
||||||
|
|
||||||
|
//glUniform4f(vertexColorLocation, 0.0f, greenValue, 0.0f, 1.0f); // but updating uniforms DOES requires the program to be used
|
||||||
|
|
||||||
|
glBindVertexArray(VAO);
|
||||||
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||||
|
glBindVertexArray(0); // unbinds the VAO so its ready to be bound again for the next render
|
||||||
|
|
||||||
|
// check and call events and swap buffers
|
||||||
|
glfwSwapBuffers(window);
|
||||||
|
glfwPollEvents();
|
||||||
|
}
|
||||||
|
|
||||||
|
glfwTerminate();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setupGLFW(GLFWwindow** window)
|
||||||
|
{
|
||||||
|
glfwInit();
|
||||||
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||||
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||||
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||||
|
|
||||||
|
*window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
|
||||||
|
if (window == NULL) {
|
||||||
|
std::cout << "Failed to create GLFW window\n";
|
||||||
|
glfwTerminate();
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
glfwMakeContextCurrent(*window);
|
||||||
|
|
||||||
|
if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) {
|
||||||
|
std::cout << "Failed to init GLAD\n";
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
glViewport(0, 0, 800, 600);
|
||||||
|
glfwSetFramebufferSizeCallback(*window, framebuffer_size_callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
|
||||||
|
{
|
||||||
|
glViewport(0, 0, width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
void processInput(GLFWwindow* window)
|
||||||
|
{
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||||
|
glfwSetWindowShouldClose(window, true);
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_ENTER) == GLFW_PRESS) // triggers wireframe mode
|
||||||
|
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_BACKSPACE) == GLFW_PRESS) // disables wireframe mode
|
||||||
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||||
|
|
||||||
|
}
|
9
6/lecture/src/shaders/shader.fs
Normal file
9
6/lecture/src/shaders/shader.fs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#version 330 core
|
||||||
|
|
||||||
|
out vec4 FragColor;
|
||||||
|
in vec3 ourColor;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
FragColor = vec4(ourColor, 1.0);
|
||||||
|
}
|
12
6/lecture/src/shaders/shader.vs
Normal file
12
6/lecture/src/shaders/shader.vs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#version 330 core
|
||||||
|
|
||||||
|
layout (location = 0) in vec3 aPos;
|
||||||
|
layout (location = 1) in vec3 aColor;
|
||||||
|
|
||||||
|
out vec3 ourColor;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
gl_Position = vec4(aPos, 1.0);
|
||||||
|
ourColor = aColor;
|
||||||
|
}
|
BIN
6/lecture/test
Executable file
BIN
6/lecture/test
Executable file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user