Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glew.h>
- #include <GLFW/glfw3.h>
- #include <array>
- #include <string>
- #include <fstream>
- #include <sstream>
- #include "Log/Log.h"
- #define ASSERT(x) if (!(x)) __debugbreak();
- #define GLCall(x) GLClearError();\
- x;\
- ASSERT(GLLogCall(#x, __FILE__, __LINE__))
- static void GLClearError()
- {
- while (glGetError() != GL_NO_ERROR);
- }
- static bool GLLogCall(const char* function, const char* file, int line)
- {
- while (GLenum error = glGetError())
- {
- GUNN_CORE_ERROR("OpenGL Error: '{0}'\n{1}\nFILE: {2}\nLINE: {3} ", error, function, file, line);
- return false;
- }
- return true;
- }
- struct ShaderProgramSource
- {
- std::string VertexSource;
- std::string FragmentSource;
- };
- static ShaderProgramSource ParseShader(const std::string& filePath)
- {
- std::ifstream stream(filePath);
- enum class ShaderType
- {
- NONE = -1,
- VERTEX = 0,
- FRAGMENT = 1
- };
- std::string line;
- std::array<std::stringstream, 2> ss;
- ShaderType type = ShaderType::NONE;
- while (getline(stream, line))
- {
- if (line.find("#shader") != std::string::npos)
- {
- if (line.find("vertex") != std::string::npos)
- {
- type = ShaderType::VERTEX;
- }
- else if (line.find("fragment") != std::string::npos)
- {
- type = ShaderType::FRAGMENT;
- }
- }
- else
- {
- ss[(int)type] << line << "\n";
- }
- }
- return { ss[0].str(), ss[1].str() };
- }
- static unsigned int CompileShader(unsigned int type, const std::string& source)
- {
- GLCall(unsigned int id = glCreateShader(type));
- const char* src = source.c_str();
- GLCall(glShaderSource(id, 1, &src, nullptr));
- GLCall(glCompileShader(id));
- int result;
- GLCall(glGetShaderiv(id, GL_COMPILE_STATUS, &result));
- if (result == GL_FALSE)
- {
- int length;
- GLCall(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
- char* message = (char*)alloca(length * sizeof(char));
- GLCall(glGetShaderInfoLog(id, length, &length, message));
- GUNN_CORE_ERROR("Failed to compile {0} shader", (type == GL_VERTEX_SHADER ? "vertex" : "fragment"));
- GUNN_CORE_ERROR("{0}", message);
- GLCall(glDeleteShader(id));
- return 0;
- }
- return id;
- }
- static unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader)
- {
- GLCall(unsigned int program = glCreateProgram());
- GLCall(unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader));
- GLCall(unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader));
- GLCall(glAttachShader(program, vs));
- GLCall(glAttachShader(program, fs));
- GLCall(glLinkProgram(program));
- GLCall(glDetachShader(program, vs));
- GLCall(glDetachShader(program, fs));
- GLCall(glValidateProgram(program));
- GLCall(glDeleteShader(vs));
- GLCall(glDeleteShader(fs));
- return program;
- }
- int main()
- {
- GLFWwindow* window = nullptr;
- Gunn::Log::Init();
- /* Initialize the library */
- if (!glfwInit())
- {
- GUNN_CORE_FATAL("ERROR STARTING GLFW!");
- glfwDestroyWindow(window);
- window = nullptr;
- glfwTerminate();
- return EXIT_FAILURE;
- }
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- /* Create a windowed mode window and its OpenGL context */
- window = glfwCreateWindow(1280, 720, "OpenGL", NULL, NULL);
- if (window == nullptr)
- {
- GUNN_CORE_FATAL("ERROR CREATING GAME WINDOW!");
- glfwDestroyWindow(window);
- window = nullptr;
- glfwTerminate();
- return EXIT_FAILURE;
- }
- /* Make the window's context current */
- glfwMakeContextCurrent(window);
- glfwSwapInterval(1);
- if (glewInit() != GLEW_OK)
- {
- GUNN_CORE_FATAL("ERROR STARTING GLEW!");
- glfwDestroyWindow(window);
- window = nullptr;
- glfwTerminate();
- return EXIT_FAILURE;
- }
- GUNN_CORE_INFO("OpenGL Version {0}", glGetString(GL_VERSION));
- ////////////////////////////////////////////////////////////////
- std::array<GLfloat, 8> positions =
- {
- -0.5f, -0.5f,
- 0.5f, -0.5f,
- 0.5f, 0.5f,
- -0.5f, 0.5f
- };
- std::array<unsigned int, 6> indices =
- {
- 0, 1, 2,
- 2, 3, 0
- };
- unsigned int vao;
- GLCall(glGenVertexArrays(1, &vao));
- GLCall(glBindVertexArray(vao));
- unsigned int buffer;
- GLCall(glGenBuffers(1, &buffer));
- GLCall(glBindBuffer(GL_ARRAY_BUFFER, buffer));
- GLCall(glBufferData(GL_ARRAY_BUFFER, 4 * 2 * sizeof(float), positions.data(), GL_STATIC_DRAW));
- GLCall(glEnableVertexAttribArray(0));
- GLCall(glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0));
- unsigned int ibo;
- GLCall(glGenBuffers(1, &ibo));
- GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo));
- GLCall(glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(unsigned int), indices.data(), GL_STATIC_DRAW));
- ShaderProgramSource source = ParseShader("res/Shaders/Basic.shader");
- unsigned int shader = CreateShader(source.VertexSource, source.FragmentSource);
- GLCall(glUseProgram(shader));
- GLCall(int location = glGetUniformLocation(shader, "u_Color"));
- ASSERT(location != -1);
- GLCall(glUniform4f(location, 1.0f, 0.0f, 0.0f, 1.0f));
- GLCall(glBindVertexArray(0));
- GLCall(glUseProgram(0));
- GLCall(glBindBuffer(GL_ARRAY_BUFFER, 0));
- GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
- GLfloat r = 0.0f;
- GLfloat increment = 0.05f;
- /* Loop until the user closes the window */
- while (!glfwWindowShouldClose(window))
- {
- /* Render here */
- GLCall(glClear(GL_COLOR_BUFFER_BIT));
- GLCall(glUseProgram(shader));
- GLCall(glUniform4f(location, r, 3.0f, 4.0f, 1.0f));
- GLCall(glBindVertexArray(vao));
- GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo));
- /* Draw stuff here*/
- GLCall(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr));
- if (r > 1.0f)
- {
- increment = -0.05f;
- }
- else if (r < 0.0f)
- {
- increment = 0.05f;
- }
- r += increment;
- /* Swap front and back buffers */
- glfwSwapBuffers(window);
- /* Poll for and process events */
- glfwPollEvents();
- }
- GLCall(glDeleteProgram(shader));
- glfwDestroyWindow(window);
- glfwTerminate();
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement