Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glew.h>
- #include <GLFW/glfw3.h>
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <sstream>
- #define ASSERT(x) if(!(x)) __debugbreak();
- #ifdef DEBUG
- #define GLCall(x) GLClearError(); x; ASSERT(GLLogCall(...))
- #else
- #define GLCall(x) x
- #endif
- static void GLClearError() {
- while (glGetError() != GL_NO_ERROR); // clear all the errors
- }
- static void GLCheckError() {
- while (GLenum error = glGetError()) {
- std::cout << "[OpenGL_Error] (" << error << ")" << std::endl;
- }
- }
- // returns true if no error
- static bool GLLogCall(const char* function, const char* file, int line) {
- if (GLenum error = glGetError()) {
- std::cout << "[OpenGL_Error] (" << error << ")" << std::endl;
- std::cout << function << " " << file << ":" << line << std::endl;
- 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::stringstream ss[2];
- ShaderType type = ShaderType::NONE;
- while (std::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';
- }
- }
- std::cout << "here" << std::endl;
- //return { ss[(int)ShaderType::VERTEX].str(), ss[(int)ShaderType::FRAGMENT].str() };
- return { ss[0].str(), ss[1].str() };
- }
- static unsigned int compileShader(unsigned int type, const std::string& source) {
- unsigned int id = glCreateShader(type);
- const char* src = source.c_str();
- glShaderSource(id, 1, &src, nullptr);
- glCompileShader(id);
- // error handling
- int result;
- glGetShaderiv(id, GL_COMPILE_STATUS, &result);
- if (result == GL_FALSE) {
- int length;
- glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
- char* message = (char*)alloca(length * sizeof(char));
- glGetShaderInfoLog(id, length, &length, message);
- std::cout << "Failed to compile " << ((type == GL_VERTEX_SHADER) ? "vertex" : "fragment") << " shader! " << std::endl;
- std::cout << message << std::endl;
- glDeleteShader(id);
- return 0;
- }
- return id;
- }
- static unsigned int createShader(const std::string& vertexShader, const std::string& fragmentShader) {
- unsigned int program = glCreateProgram();
- unsigned int vs = compileShader(GL_VERTEX_SHADER, vertexShader);
- unsigned int fs = compileShader(GL_FRAGMENT_SHADER, fragmentShader);
- glAttachShader(program, vs);
- glAttachShader(program, fs);
- glLinkProgram(program);
- glValidateProgram(program);
- glDeleteShader(vs);
- glDeleteShader(fs);
- return program;
- }
- int main(void)
- {
- GLFWwindow* window;
- /* Initialize the library */
- if (!glfwInit())
- return -1;
- /* Create a windowed mode window and its OpenGL context */
- window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
- if (!window)
- {
- glfwTerminate();
- return -1;
- }
- /* Make the window's context current */
- glfwMakeContextCurrent(window);
- if (glewInit() != GLEW_OK) {
- std::cout << "Error!" << std::endl;
- }
- std::cout << glGetString(GL_VERSION) << std::endl;
- float positions[12] = {
- -0.5f, -0.5f, // 0
- 0.5f, -0.5f, // 1
- 0.5f, 0.5f, // 2
- -0.5f, 0.5f, // 3
- };
- unsigned int indices[6] = {
- 0, 1, 2,
- 2, 3, 0
- };
- unsigned int buffer;
- GLCall(glGenBuffers(1, &buffer));
- GLCall(glBindBuffer(GL_ARRAY_BUFFER, buffer));
- GLCall(glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW));
- GLCall(glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0));
- GLCall(glEnableVertexAttribArray(0));
- unsigned int ibo;
- GLCall(glGenBuffers(1, &ibo));
- GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo));
- GLCall(glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW));
- shaderProgramSource src = parseShader("Basic.shader");
- std::cout << "VERTEX" << std::endl;
- std::cout << src.vertexSource << std::endl;
- std::cout << "FRAGMENT" << std::endl;
- std::cout << src.fragmentSource << std::endl;
- unsigned int shader = createShader(src.vertexSource, src.fragmentSource);
- GLCall(glUseProgram(shader));
- int location = GLCall(glGetUniformLocation(shader, "u_Color"));
- ASSERT(location != -1);
- GLCall(glUniform4f(location, 0.2f, 0.3f, 0.8f, 0.7f));
- /* Loop until the user closes the window */
- while (!glfwWindowShouldClose(window))
- {
- /* Render here */
- glClear(GL_COLOR_BUFFER_BIT);
- //glDrawArrays(GL_TRIANGLES, 0, 6);
- //GLClearError();
- GLCall(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr));
- /*GLCheckError();
- ASSERT(GLLogCall());*/
- /* Swap front and back buffers */
- glfwSwapBuffers(window);
- /* Poll for and process events */
- glfwPollEvents();
- }
- glDeleteProgram(shader);
- glfwTerminate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement