From 32d9ad1dea75b067109ed12f2d32a952280cef3b Mon Sep 17 00:00:00 2001 From: plane000 Date: Sun, 16 Sep 2018 14:02:14 +0100 Subject: [PATCH] Initial commit --- .gitignore | 6 +- OpenGL.sln | 31 ++++++++ OpenGL/OpenGL.vcxproj | 136 ++++++++++++++++++++++++++++++++++ OpenGL/OpenGL.vcxproj.filters | 25 +++++++ OpenGL/display.cpp | 48 ++++++++++++ OpenGL/display.h | 19 +++++ OpenGL/main.cpp | 33 +++++++++ OpenGL/mesh.cpp | 29 ++++++++ OpenGL/mesh.h | 21 ++++++ OpenGL/shader.cpp | 79 ++++++++++++++++++++ OpenGL/shader.h | 20 +++++ 11 files changed, 443 insertions(+), 4 deletions(-) create mode 100644 OpenGL.sln create mode 100644 OpenGL/OpenGL.vcxproj create mode 100644 OpenGL/OpenGL.vcxproj.filters create mode 100644 OpenGL/display.cpp create mode 100644 OpenGL/display.h create mode 100644 OpenGL/main.cpp create mode 100644 OpenGL/mesh.cpp create mode 100644 OpenGL/mesh.h create mode 100644 OpenGL/shader.cpp create mode 100644 OpenGL/shader.h diff --git a/.gitignore b/.gitignore index 3e759b7..56089aa 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,5 @@ -## Ignore Visual Studio temporary files, build results, and -## files generated by popular Visual Studio add-ons. -## -## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore +OpenGL/lib/ +OpenGL/include/ # User-specific files *.suo diff --git a/OpenGL.sln b/OpenGL.sln new file mode 100644 index 0000000..c964feb --- /dev/null +++ b/OpenGL.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27428.2043 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "OpenGL", "OpenGL\OpenGL.vcxproj", "{BE30292B-9C31-474C-AC8C-E1BFA61BD1A1}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {BE30292B-9C31-474C-AC8C-E1BFA61BD1A1}.Debug|x64.ActiveCfg = Debug|x64 + {BE30292B-9C31-474C-AC8C-E1BFA61BD1A1}.Debug|x64.Build.0 = Debug|x64 + {BE30292B-9C31-474C-AC8C-E1BFA61BD1A1}.Debug|x86.ActiveCfg = Debug|Win32 + {BE30292B-9C31-474C-AC8C-E1BFA61BD1A1}.Debug|x86.Build.0 = Debug|Win32 + {BE30292B-9C31-474C-AC8C-E1BFA61BD1A1}.Release|x64.ActiveCfg = Release|x64 + {BE30292B-9C31-474C-AC8C-E1BFA61BD1A1}.Release|x64.Build.0 = Release|x64 + {BE30292B-9C31-474C-AC8C-E1BFA61BD1A1}.Release|x86.ActiveCfg = Release|Win32 + {BE30292B-9C31-474C-AC8C-E1BFA61BD1A1}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {EB95C8C7-F8BE-43A6-947B-4728EA586F29} + EndGlobalSection +EndGlobal diff --git a/OpenGL/OpenGL.vcxproj b/OpenGL/OpenGL.vcxproj new file mode 100644 index 0000000..f58419a --- /dev/null +++ b/OpenGL/OpenGL.vcxproj @@ -0,0 +1,136 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {BE30292B-9C31-474C-AC8C-E1BFA61BD1A1} + OpenGL + 10.0.16299.0 + + + + Application + true + v141 + MultiByte + + + Application + false + v141 + true + MultiByte + + + Application + true + v141 + MultiByte + + + Application + false + v141 + true + MultiByte + + + + + + + + + + + + + + + + + + + + + + + Level3 + Disabled + true + true + .\include;%(AdditionalIncludeDirectories) + + + .\lib;%(AdditionalLibraryDirectories) + glew32.lib;glew32s.lib;SDL2.lib;SDL2main.lib;SDL2test.lib;OpenGL32.lib;%(AdditionalDependencies) + + + + + Level3 + Disabled + true + true + + + + + Level3 + MaxSpeed + true + true + true + true + + + true + true + + + + + Level3 + MaxSpeed + true + true + true + true + + + true + true + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/OpenGL/OpenGL.vcxproj.filters b/OpenGL/OpenGL.vcxproj.filters new file mode 100644 index 0000000..d64752d --- /dev/null +++ b/OpenGL/OpenGL.vcxproj.filters @@ -0,0 +1,25 @@ + + + + + + + + + + + {3e9ef4e5-bcd4-4161-8243-90e67d85dc43} + + + + + headers + + + headers + + + headers + + + \ No newline at end of file diff --git a/OpenGL/display.cpp b/OpenGL/display.cpp new file mode 100644 index 0000000..61ba2a8 --- /dev/null +++ b/OpenGL/display.cpp @@ -0,0 +1,48 @@ +#include +#include + +#include "display.h" + +Display::Display(int width, int height, const std::string& title) { + SDL_Init(SDL_INIT_VIDEO); + + 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); + + m_window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL); + m_glContext = SDL_GL_CreateContext(m_window); + + GLenum status = glewInit(); + if (status != GLEW_OK) { + std::cerr << "GLEW Failed to initialize" << std::endl; + } + + m_isClosed = false; + std::cout << "Display initialized and gl context successfuly created" << std::endl; +} + +void Display::Update() { + SDL_GL_SwapWindow(m_window); + + SDL_Event e; + + while (SDL_PollEvent(&e)) { + if (e.type == SDL_QUIT) { + m_isClosed = true; + } + } +} + +bool Display::isClosed() { + return m_isClosed; +} + +Display::~Display() { + SDL_GL_DeleteContext(m_glContext); + SDL_DestroyWindow(m_window); + SDL_Quit(); +} diff --git a/OpenGL/display.h b/OpenGL/display.h new file mode 100644 index 0000000..45a5ab2 --- /dev/null +++ b/OpenGL/display.h @@ -0,0 +1,19 @@ +#pragma once + +#include +#include + +class Display { +public: + Display(int width, int height, const std::string& title); + + void Update(); + bool isClosed(); + + virtual ~Display(); +private: + SDL_Window* m_window; + SDL_GLContext m_glContext; + + bool m_isClosed; +}; diff --git a/OpenGL/main.cpp b/OpenGL/main.cpp new file mode 100644 index 0000000..c9fb4a9 --- /dev/null +++ b/OpenGL/main.cpp @@ -0,0 +1,33 @@ +#include +#include + +#include "display.h" +#include "mesh.h" +#include "shader.h" + +#undef main + +int main(int argc, char** argv) { + Display display(800, 600, "Crumpet Engine"); + glClearColor(0.1f, 0.45f, 0.9f, 1.0f); + + GLfloat vertices[] = { + -0.5f, -0.5f, 0.0f, + 0.5f, -0.5f, 0.0f, + 0.0f, 0.5f, 0.0f + }; + + Mesh mesh(vertices, sizeof(vertices) / sizeof(vertices[0])); + Shader shader; + + while(!display.isClosed()) { + glClear(GL_COLOR_BUFFER_BIT); + + shader.Bind(); + mesh.Draw(); + + display.Update(); + } + + return 0; +} diff --git a/OpenGL/mesh.cpp b/OpenGL/mesh.cpp new file mode 100644 index 0000000..8aa54a1 --- /dev/null +++ b/OpenGL/mesh.cpp @@ -0,0 +1,29 @@ +#include "mesh.h" + +Mesh::Mesh(GLfloat *vertices, unsigned int numVerticies) { + m_drawCount = numVerticies; + + glGenVertexArrays(1, &m_VAO); + glBindVertexArray(m_VAO); + + glGenBuffers(NUM_BUFFERS, m_VBO); + glBindBuffer(GL_ARRAY_BUFFER, m_VBO[POSITION_VB]); + glBufferData(GL_ARRAY_BUFFER, numVerticies * sizeof(vertices[0]), vertices, GL_STATIC_DRAW); + + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); + glEnableVertexAttribArray(0); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); + std::cout << "Mesh loaded successfully" << std::endl; +} + +void Mesh::Draw() { + glBindVertexArray(m_VAO); + glDrawArrays(GL_TRIANGLES, 0, m_drawCount); + glBindVertexArray(0); +} + +Mesh::~Mesh() { + glDeleteVertexArrays(1, &m_VAO); +} diff --git a/OpenGL/mesh.h b/OpenGL/mesh.h new file mode 100644 index 0000000..e1a371e --- /dev/null +++ b/OpenGL/mesh.h @@ -0,0 +1,21 @@ +#pragma once + +#include +#include + +class Mesh { +public: + Mesh(GLfloat *vertices, unsigned int numVerticies); + void Draw(); + virtual ~Mesh(); +private: + enum { + POSITION_VB, + NUM_BUFFERS + }; + + unsigned int m_VAO; + unsigned int m_VBO[NUM_BUFFERS]; + + unsigned int m_drawCount; +}; diff --git a/OpenGL/shader.cpp b/OpenGL/shader.cpp new file mode 100644 index 0000000..233ca6f --- /dev/null +++ b/OpenGL/shader.cpp @@ -0,0 +1,79 @@ +#include "shader.h" + +const char *vertexShaderSource = "#version 330 core\n" +"layout (location = 0) in vec3 aPos;\n" +"void main()\n" +"{\n" +" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n" +"}\0"; +const char *fragmentShaderSource = "#version 330 core\n" +"out vec4 FragColor;\n" +"void main()\n" +"{\n" +" FragColor = vec4(0.25f, 1.0f, 0.49f, 1.0f);\n" +"}\n\0"; + +Shader::Shader() { + m_program = glCreateProgram(); + m_shaders[0] = CreateShader(vertexShaderSource, GL_VERTEX_SHADER); + m_shaders[1] = CreateShader(fragmentShaderSource, GL_FRAGMENT_SHADER); + + for (unsigned int i = 0; i < NUM_SHADERS; i++) { + glAttachShader(m_program, m_shaders[i]); + std::cout << "Shader successfully attatched" << std::endl; + } + + glLinkProgram(m_program); + + int success; + char infoLog[512]; + glGetProgramiv(m_program, GL_LINK_STATUS, &success); + if (!success) { + glGetProgramInfoLog(m_program, 512, NULL, infoLog); + std::cout << "Shader program linking failed" << infoLog << std::endl; + } + else { + std::cout << "Shader(s) successfully linked" << std::endl; + std::cout << "Shader program successfully loadedd" << std::endl; + } +} + +GLuint Shader::CreateShader(const std::string& text, GLenum shaderType) { + GLuint shader = glCreateShader(shaderType); + + if (shader == 0) { + std::cout << "Error creating shader" << std::endl; + } + + const GLchar* sourceString[1]; + sourceString[0] = text.c_str(); + + glShaderSource(shader, 1, sourceString, NULL); + glCompileShader(shader); + + int success; + char infoLog[512]; + glGetShaderiv(shader, GL_COMPILE_STATUS, &success); + if (!success) { + glGetShaderInfoLog(shader, 512, NULL, infoLog); + std::cout << "Shader compilation failed" << infoLog << std::endl; + } + else { + std::cout << "Shader successfully compiled" << std::endl; + } + + return shader; +} + +void Shader::Bind() { + glUseProgram(m_program); +} + +Shader::~Shader() { + for (unsigned int i = 0; i < NUM_SHADERS; i++) { + glDetachShader(m_program, m_shaders[i]); + glDeleteShader(m_shaders[i]); + } + + glDeleteProgram(m_program); +} diff --git a/OpenGL/shader.h b/OpenGL/shader.h new file mode 100644 index 0000000..511fd7a --- /dev/null +++ b/OpenGL/shader.h @@ -0,0 +1,20 @@ +#pragma once + +#include +#include +#include + +class Shader { +public: + Shader(); + void Bind(); + virtual ~Shader(); +private: + static const unsigned int NUM_SHADERS = 2; + GLuint CreateShader(const std::string& text, GLenum shaderType); + + GLuint m_program; + GLuint m_shaders[NUM_SHADERS]; + // 0 = vertex, 1 = fragment +}; +