diff --git a/C++/Floyd-Steinberg diffusion/Lenna.png b/C++/Floyd-Steinberg diffusion/Lenna.png new file mode 100644 index 0000000..59ef68a Binary files /dev/null and b/C++/Floyd-Steinberg diffusion/Lenna.png differ diff --git a/C++/Floyd-Steinberg diffusion/output.png b/C++/Floyd-Steinberg diffusion/output.png index 41d3b1c..8ba421a 100644 Binary files a/C++/Floyd-Steinberg diffusion/output.png and b/C++/Floyd-Steinberg diffusion/output.png differ diff --git a/OpenGL/playground/CMakeFiles/output.dir/CXX.includecache b/OpenGL/playground/CMakeFiles/output.dir/CXX.includecache index b4e38db..638a373 100644 --- a/OpenGL/playground/CMakeFiles/output.dir/CXX.includecache +++ b/OpenGL/playground/CMakeFiles/output.dir/CXX.includecache @@ -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 +- + diff --git a/OpenGL/playground/CMakeFiles/output.dir/depend.internal b/OpenGL/playground/CMakeFiles/output.dir/depend.internal index 52442da..1111bf5 100644 --- a/OpenGL/playground/CMakeFiles/output.dir/depend.internal +++ b/OpenGL/playground/CMakeFiles/output.dir/depend.internal @@ -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 diff --git a/OpenGL/playground/CMakeFiles/output.dir/depend.make b/OpenGL/playground/CMakeFiles/output.dir/depend.make index f190c88..6fca7df 100644 --- a/OpenGL/playground/CMakeFiles/output.dir/depend.make +++ b/OpenGL/playground/CMakeFiles/output.dir/depend.make @@ -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 diff --git a/OpenGL/playground/CMakeFiles/output.dir/src/main.cpp.o b/OpenGL/playground/CMakeFiles/output.dir/src/main.cpp.o index 384b0d7..e94916f 100644 Binary files a/OpenGL/playground/CMakeFiles/output.dir/src/main.cpp.o and b/OpenGL/playground/CMakeFiles/output.dir/src/main.cpp.o differ diff --git a/OpenGL/playground/CMakeFiles/output.dir/src/object.cpp.o b/OpenGL/playground/CMakeFiles/output.dir/src/object.cpp.o index d1207f3..373ee98 100644 Binary files a/OpenGL/playground/CMakeFiles/output.dir/src/object.cpp.o and b/OpenGL/playground/CMakeFiles/output.dir/src/object.cpp.o differ diff --git a/OpenGL/playground/output b/OpenGL/playground/output index 30c3b86..ff39a86 100644 Binary files a/OpenGL/playground/output and b/OpenGL/playground/output differ diff --git a/OpenGL/playground/resources/shaders/simple.frag b/OpenGL/playground/resources/shaders/simple.frag index 8b13789..2dfb50e 100644 --- a/OpenGL/playground/resources/shaders/simple.frag +++ b/OpenGL/playground/resources/shaders/simple.frag @@ -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); +} diff --git a/OpenGL/playground/resources/shaders/simple.vert b/OpenGL/playground/resources/shaders/simple.vert index 8b13789..f4d9384 100644 --- a/OpenGL/playground/resources/shaders/simple.vert +++ b/OpenGL/playground/resources/shaders/simple.vert @@ -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); +} diff --git a/OpenGL/playground/src/init.h b/OpenGL/playground/src/init.h deleted file mode 100644 index 20ed85c..0000000 --- a/OpenGL/playground/src/init.h +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef SRC_INIT_H_ -#define SRC_INIT_H_ - -// General includes -#include - -// GL includes -#if _WIN32 -#include -#else -#include -#endif - -// Custom includes -#include - -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 << "----- �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 diff --git a/OpenGL/playground/src/main.cpp b/OpenGL/playground/src/main.cpp index f54cf51..ced4ce1 100644 --- a/OpenGL/playground/src/main.cpp +++ b/OpenGL/playground/src/main.cpp @@ -20,11 +20,17 @@ // Custom includes #include -#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 << "----- �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 vertices; std::vector normals; std::vector 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); } diff --git a/OpenGL/playground/src/shader.cpp b/OpenGL/playground/src/shader.cpp new file mode 100644 index 0000000..353f258 --- /dev/null +++ b/OpenGL/playground/src/shader.cpp @@ -0,0 +1,124 @@ +#include "shader.h" + +#include +#include + +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(t)), + std::istreambuf_iterator()); + 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); +} diff --git a/OpenGL/playground/src/shader.h b/OpenGL/playground/src/shader.h new file mode 100644 index 0000000..7152154 --- /dev/null +++ b/OpenGL/playground/src/shader.h @@ -0,0 +1,38 @@ +#ifndef SRC_SHADER_H_ +#define SRC_SHADER_H_ + +#include +#include + +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