Shaders
This commit is contained in:
BIN
C++/Floyd-Steinberg diffusion/Lenna.png
Normal file
BIN
C++/Floyd-Steinberg diffusion/Lenna.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 463 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 349 KiB After Width: | Height: | Size: 608 KiB |
@@ -32,11 +32,29 @@ map
|
||||
Windows.h
|
||||
-
|
||||
|
||||
/home/ben/Programming/Languages/Examples/OpenGL/playground/src/object.cpp
|
||||
/home/ben/Programming/Languages/Examples/OpenGL/playground/src/main.cpp
|
||||
chrono
|
||||
-
|
||||
vector
|
||||
-
|
||||
glad/glad.h
|
||||
-
|
||||
SDL.h
|
||||
-
|
||||
SDL2/SDL.h
|
||||
-
|
||||
glm/glm.hpp
|
||||
-
|
||||
glm/gtc/matrix_transform.hpp
|
||||
-
|
||||
glm/gtc/type_ptr.hpp
|
||||
-
|
||||
logger.h
|
||||
-
|
||||
timers.h
|
||||
/home/ben/Programming/Languages/Examples/OpenGL/playground/src/timers.h
|
||||
object.h
|
||||
/home/ben/Programming/Languages/Examples/OpenGL/playground/src/object.h
|
||||
fstream
|
||||
-
|
||||
|
||||
/home/ben/Programming/Languages/Examples/OpenGL/playground/src/object.h
|
||||
vector
|
||||
@@ -56,3 +74,13 @@ glm/gtc/type_ptr.hpp
|
||||
logger.h
|
||||
-
|
||||
|
||||
/home/ben/Programming/Languages/Examples/OpenGL/playground/src/timers.h
|
||||
chrono
|
||||
-
|
||||
SDL.h
|
||||
-
|
||||
SDL2/SDL.h
|
||||
-
|
||||
logger.h
|
||||
-
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@ CMakeFiles/output.dir/src/main.cpp.o
|
||||
./include/KHR/khrplatform.h
|
||||
./include/glad/glad.h
|
||||
./include/logger.h
|
||||
/home/ben/Programming/Languages/Examples/OpenGL/playground/src/init.h
|
||||
/home/ben/Programming/Languages/Examples/OpenGL/playground/src/main.cpp
|
||||
/home/ben/Programming/Languages/Examples/OpenGL/playground/src/object.h
|
||||
/home/ben/Programming/Languages/Examples/OpenGL/playground/src/timers.h
|
||||
|
||||
@@ -8,7 +8,6 @@ CMakeFiles/output.dir/src/glad.c.o: src/glad.c
|
||||
CMakeFiles/output.dir/src/main.cpp.o: ./include/KHR/khrplatform.h
|
||||
CMakeFiles/output.dir/src/main.cpp.o: ./include/glad/glad.h
|
||||
CMakeFiles/output.dir/src/main.cpp.o: ./include/logger.h
|
||||
CMakeFiles/output.dir/src/main.cpp.o: src/init.h
|
||||
CMakeFiles/output.dir/src/main.cpp.o: src/main.cpp
|
||||
CMakeFiles/output.dir/src/main.cpp.o: src/object.h
|
||||
CMakeFiles/output.dir/src/main.cpp.o: src/timers.h
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1 +1,13 @@
|
||||
#version 330
|
||||
|
||||
// in vec3 Colour;
|
||||
// in vec2 TexCoord;
|
||||
out vec4 outColour;
|
||||
|
||||
uniform vec3 triangleColour;
|
||||
|
||||
uniform sampler2D tex;
|
||||
|
||||
void main() {
|
||||
outColour = vec4(0.23, 0.58, 0.12, 1.0);
|
||||
}
|
||||
|
||||
@@ -1 +1,18 @@
|
||||
#version 330
|
||||
|
||||
in vec3 position;
|
||||
// in vec2 texcoord;
|
||||
// in vec3 colour;
|
||||
|
||||
// out vec3 Colour;
|
||||
// out vec2 TexCoord;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 proj;
|
||||
|
||||
void main() {
|
||||
// Colour = colour;
|
||||
// TexCoord = texcoord;
|
||||
gl_Position = proj * view * model * vec4(position, 1.0);
|
||||
}
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
#ifndef SRC_INIT_H_
|
||||
#define SRC_INIT_H_
|
||||
|
||||
// General includes
|
||||
#include <chrono>
|
||||
|
||||
// GL includes
|
||||
#if _WIN32
|
||||
#include <SDL.h>
|
||||
#else
|
||||
#include <SDL2/SDL.h>
|
||||
#endif
|
||||
|
||||
// Custom includes
|
||||
#include <logger.h>
|
||||
|
||||
bool init(Logger& logger, SDL_Window* window, SDL_GLContext glContext) {
|
||||
std::cout << "-----------------------------" << std::endl;
|
||||
std::cout << "----- OpenGL Playground -----" << std::endl;
|
||||
std::cout << "-------- Version 1.0 --------" << std::endl;
|
||||
std::cout << "----- <20>Benjamin Kyd 2019 ----" << std::endl;
|
||||
std::cout << "-----------------------------" << std::endl;
|
||||
std::cout << std::endl;
|
||||
|
||||
SDL_Init(SDL_INIT_EVERYTHING);
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
|
||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5);
|
||||
|
||||
window = SDL_CreateWindow("OpenGL Playground V1.0",
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
1280, 720,
|
||||
SDL_WINDOW_OPENGL);
|
||||
glContext = SDL_GL_CreateContext(window);
|
||||
SDL_GL_SetSwapInterval(0);
|
||||
|
||||
gladLoadGLLoader(SDL_GL_GetProcAddress);
|
||||
|
||||
logger << LOGGER_INFO << "OpenGL and SDL initialized" << LOGGER_ENDL;
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -20,11 +20,17 @@
|
||||
|
||||
// Custom includes
|
||||
#include <logger.h>
|
||||
#include "init.h"
|
||||
#include "timers.h"
|
||||
#include "object.h"
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
std::cout << "-----------------------------" << std::endl;
|
||||
std::cout << "----- OpenGL Playground -----" << std::endl;
|
||||
std::cout << "-------- Version 1.0 --------" << std::endl;
|
||||
std::cout << "----- <20>Benjamin Kyd 2019 ----" << std::endl;
|
||||
std::cout << "-----------------------------" << std::endl;
|
||||
std::cout << std::endl;
|
||||
|
||||
// Get global variables ready
|
||||
Logger logger;
|
||||
SDL_Window* window = nullptr;
|
||||
@@ -32,11 +38,38 @@ int main(int argc, char** argv) {
|
||||
bool isWindowOpen = false;
|
||||
|
||||
// Initialize SDL and OpenGL
|
||||
isWindowOpen = init(logger, window, glContext);
|
||||
// isWindowOpen = init(logger, window, glContext);
|
||||
SDL_Init(SDL_INIT_EVERYTHING);
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
|
||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5);
|
||||
|
||||
window = SDL_CreateWindow("OpenGL Playground V1.0",
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
1280, 720,
|
||||
SDL_WINDOW_OPENGL);
|
||||
glContext = SDL_GL_CreateContext(window);
|
||||
SDL_GL_SetSwapInterval(0);
|
||||
|
||||
gladLoadGLLoader(SDL_GL_GetProcAddress);
|
||||
std::vector<glm::vec4> vertices;
|
||||
std::vector<glm::vec3> normals;
|
||||
std::vector<GLushort> elements;
|
||||
isWindowOpen = true;
|
||||
logger << LOGGER_INFO << "OpenGL and SDL initialized" << LOGGER_ENDL;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
LoadOBJ(logger, "./resources/dragon.obj", vertices, normals, elements);
|
||||
|
||||
SDL_Event event;
|
||||
@@ -53,6 +86,11 @@ int main(int argc, char** argv) {
|
||||
if (event.type == SDL_QUIT)
|
||||
isWindowOpen = false;
|
||||
|
||||
|
||||
// Clear
|
||||
const float clear[] = {0.1f, 0.45f, 0.9f, 1.0f};
|
||||
glClearBufferfv(GL_COLOR, 0, clear);
|
||||
glClear(GL_DEPTH_BUFFER_BIT);
|
||||
// Swap GL frame buffers
|
||||
SDL_GL_SwapWindow(window);
|
||||
}
|
||||
|
||||
124
OpenGL/playground/src/shader.cpp
Normal file
124
OpenGL/playground/src/shader.cpp
Normal file
@@ -0,0 +1,124 @@
|
||||
#include "shader.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
Shader::Shader() {
|
||||
m_program = glCreateProgram();
|
||||
}
|
||||
|
||||
Shader& Shader::use() {
|
||||
glUseProgram(m_program);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Shader& Shader::link() {
|
||||
glLinkProgram(m_program);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Shader& Shader::attatch() {
|
||||
glAttachShader(m_program, m_vert);
|
||||
glAttachShader(m_program, m_frag);
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::string readShader(std::string source) {
|
||||
std::ifstream t(source);
|
||||
std::string shaderCode((std::istreambuf_iterator<char>(t)),
|
||||
std::istreambuf_iterator<char>());
|
||||
return shaderCode;
|
||||
}
|
||||
|
||||
Shader& Shader::load(GLenum type, std::string sourceLoc) {
|
||||
const char* source = readShader(sourceLoc).c_str();
|
||||
|
||||
if (type == GL_VERTEX_SHADER) {
|
||||
m_vertLoc = sourceLoc;
|
||||
m_vertSource = (std::string)source;
|
||||
m_vert = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(m_vert, 1, &source, NULL);
|
||||
glCompileShader(m_vert);
|
||||
|
||||
glGetShaderiv(m_vert, GL_COMPILE_STATUS, &m_status);
|
||||
if (m_status == GL_FALSE) {
|
||||
char buf[512];
|
||||
glGetShaderInfoLog(m_vert, 512, NULL, buf);
|
||||
std::cerr << buf << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "Vertex shader at '" << sourceLoc << "' compiled..." << std::endl;
|
||||
} else if (type == GL_FRAGMENT_SHADER) {
|
||||
m_fragLoc = sourceLoc;
|
||||
m_fragSource = (std::string)source;
|
||||
m_frag = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(m_frag, 1, &source, NULL);
|
||||
glCompileShader(m_frag);
|
||||
|
||||
glGetShaderiv(m_frag, GL_COMPILE_STATUS, &m_status);
|
||||
if (m_status == GL_FALSE) {
|
||||
char buf[512];
|
||||
glGetShaderInfoLog(m_frag, 512, NULL, buf);
|
||||
std::cerr << buf << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "Vertex shader at '" << sourceLoc << "' compiled..." << std::endl;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Shader& Shader::load(std::string sourceLoc) {
|
||||
m_vertLoc = sourceLoc + ".vert";
|
||||
m_fragLoc = sourceLoc + ".frag";
|
||||
|
||||
m_vertSource = readShader(m_vertLoc);
|
||||
m_fragSource = readShader(m_fragLoc);
|
||||
|
||||
m_vert = glCreateShader(GL_VERTEX_SHADER);
|
||||
m_frag = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
|
||||
const char* vertSource = m_vertSource.c_str();
|
||||
glShaderSource(m_vert, 1, &vertSource, NULL);
|
||||
glCompileShader(m_vert);
|
||||
|
||||
const char* fragSource = m_fragSource.c_str();
|
||||
glShaderSource(m_frag, 1, &fragSource, NULL);
|
||||
glCompileShader(m_frag);
|
||||
|
||||
glGetShaderiv(m_vert, GL_COMPILE_STATUS, &m_status);
|
||||
if (m_status == GL_FALSE) {
|
||||
char buf[512];
|
||||
glGetShaderInfoLog(m_vert, 512, NULL, buf);
|
||||
std::cerr << buf << std::endl;
|
||||
}
|
||||
|
||||
glGetShaderiv(m_frag, GL_COMPILE_STATUS, &m_status);
|
||||
if (m_status == GL_FALSE) {
|
||||
char buf[512];
|
||||
glGetShaderInfoLog(m_frag, 512, NULL, buf);
|
||||
std::cerr << buf << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "Vertex shader at '" << m_vertLoc << "' compiled..." << std::endl;
|
||||
std::cout << "Fragment shader at '" << m_fragLoc << "' compiled..." << std::endl;
|
||||
return *this;
|
||||
}
|
||||
|
||||
GLuint Shader::getProgram() {
|
||||
return m_program;
|
||||
}
|
||||
|
||||
GLuint Shader::getVertex() {
|
||||
return m_vert;
|
||||
}
|
||||
|
||||
GLuint Shader::getFragment() {
|
||||
return m_frag;
|
||||
}
|
||||
|
||||
Shader::~Shader() {
|
||||
glDeleteProgram(m_program);
|
||||
glDeleteShader(m_vert);
|
||||
glDeleteShader(m_frag);
|
||||
}
|
||||
38
OpenGL/playground/src/shader.h
Normal file
38
OpenGL/playground/src/shader.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#ifndef SRC_SHADER_H_
|
||||
#define SRC_SHADER_H_
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <string>
|
||||
|
||||
class Shader {
|
||||
public:
|
||||
Shader();
|
||||
|
||||
Shader& use();
|
||||
Shader& link();
|
||||
Shader& attatch();
|
||||
Shader& load(GLenum type, std::string sourceLoc);
|
||||
Shader& load(std::string sourceLoc);
|
||||
|
||||
GLuint getProgram();
|
||||
GLuint getVertex();
|
||||
GLuint getFragment();
|
||||
|
||||
virtual ~Shader();
|
||||
private:
|
||||
Shader(Shader const &) = delete;
|
||||
Shader & operator=(Shader const &) = delete;
|
||||
|
||||
GLuint m_program;
|
||||
GLuint m_vert;
|
||||
GLuint m_frag;
|
||||
|
||||
std::string m_vertSource;
|
||||
std::string m_fragSource;
|
||||
std::string m_vertLoc;
|
||||
std::string m_fragLoc;
|
||||
|
||||
GLint m_status;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user