Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // CPPTutorial.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
- #include <iostream>
- #include <Windows.h>
- #include <fstream>
- #include <string>
- #include <vector>
- #include <glad/glad.h>
- #include <GLFW/glfw3.h>
- #include <bitset>
- #include <Shader.h>
- GLuint LoadShader(const char* vertexFile, const char* fragmentFile);
- // Define this function so it can be seen in main()
- void framebuffer_size_callback(GLFWwindow* window, int width, int height);
- int processInput(GLFWwindow* window);
- std::string readFile(const char* filePath);
- const char* vertexShaderSourceLocation = "VertexShader.vert";
- const char* fragmentShaderSourceLocation = "FragmentShader.frag";
- const char* vertexShaderSource;
- const char* fragmentShaderSource;
- /**"#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";
- = "#version 330 core\n"
- "out vec4 FragColor;\n"
- "void main() {\n"
- "FragColor = vec4(0.5f, 0.0f, 1.0f, 1.0f);} ";
- **/
- int main()
- {
- glfwInit();
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
- if (window == NULL)
- {
- std::cout << "Failed to create GLFW window" << std::endl;
- glfwTerminate();
- return -1;
- }
- glfwMakeContextCurrent(window);
- if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
- {
- std::cout << "Failed to initialize GLAD" << std::endl;
- return -1;
- }
- glViewport(0, 0, 800, 600);
- glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
- // Initialisation done. Let's draw something:
- float vertices[] = {
- // 3-pos // 4-col
- 0.0f, 1.0f, 0.0f, 0.6f, 0.0f, 0.6f, 1.0f,
- -0.5f, 0.0f, 0.0f, 1.0f, 0.5f, 0.0f, 1.0f,
- 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f,
- };
- unsigned int indices[] = {
- 0, 1, 2,
- 1, 2, 3
- };
- // ***** Pass Vertex Information
- unsigned int VBO; // Vertex buffer object
- glGenBuffers(1, &VBO); // Generate uid for this buffer
- glBindBuffer(GL_ARRAY_BUFFER, VBO); // Bind our buffer to GL_STREAM_DRAW, setting it as the current working buffer of this type.
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STREAM_DRAW); // Copy our vertex data into the buffer memory. Static draw shows it is set once and used repeatedly
- //Load Shader program in !!
- GLuint shaderProgram = LoadShader(vertexShaderSourceLocation, fragmentShaderSourceLocation);
- // Specify that this is the program we want to use from now on.
- glUseProgram(shaderProgram);
- //Shader ourShader("FragmentShader.frag", "VertexShader.vert");
- // Tell OpenGL how to interpret our vertex data:
- // 0 is the location of the position attribute, specified in vertex shader. vec3. float. not normalized. stride is distance between consecutive vertex attributes. offset of 0 as it is at the start of the buffer.
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
- glEnableVertexAttribArray(0);
- // EBO for storing the order in which we render vertices.
- unsigned int EBO;
- glGenBuffers(1, &EBO);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STREAM_DRAW);
- // Generate a Vertex Array Object to store our rendering procedure.
- unsigned int VAO;
- glGenVertexArrays(1, &VAO);
- // 1. bind Vertex Array Object, Element Buffer Object
- glBindVertexArray(VAO);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
- // 2. copy our vertices array in a buffer for OpenGL to use
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
- // 2. copy our vertex indices in a buffer for OpenGL to use
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
- glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, sizeof(indices), indices);
- // 3. then set our vertex attributes pointers:
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(float), (void*)0); // Pos vec3
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 7 * sizeof(float), (void*)(3* sizeof(float))); // Col vec4
- glEnableVertexAttribArray(1);
- vertices[0] = 0.4f;
- // Wireframes
- glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
- while (!glfwWindowShouldClose(window))
- {
- int movement = processInput(window);
- if(movement != 0)
- std::cout << "movement: " << std::bitset<8>(movement) << std::endl;
- std::cout << vertices[1] << std::endl;
- if (movement && 1UL)
- {
- //vertices[1] += 0.1f;
- //vertices[4] += 0.1f;
- //vertices[7] += 0.1f;
- }
- // clear colorbuffer
- glClearColor(0.6f, 0.0f, 0.6f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT);
- glUseProgram(shaderProgram);
- float timeValue = glfwGetTime();
- float colorValue = (sin(timeValue*3) / 2.0f) + 0.5f;
- std::cout << colorValue << std::endl;
- //float colorVec[4] = { colorValue, (colorValue / 2.0f), 0.0f, 1.0f };
- //ourShader.setVec4("vertexColor", colorValue, colorValue / 2.0f, 0.0f, 1.0f);
- int vertexColorLocation = glGetUniformLocation(shaderProgram, "variableColor");
- glUniform4f(vertexColorLocation, colorValue, colorValue/2.0f, 0.0f, 1.0f);
- /**
- glUniform1f(glGetUniformLocation(shaderProgram, "horizOffset"), colorValue);
- glBindVertexArray(VAO);
- glDrawArrays(GL_TRIANGLES, 0, 3);**/
- glUniform1f(glGetUniformLocation(shaderProgram, "horizOffset"), colorValue);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
- // 2. copy our vertices array in a buffer for OpenGL to use
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
- // 2. copy our vertex indices in a buffer for OpenGL to use
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
- glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, sizeof(indices), indices);
- // 3. then set our vertex attributes pointers:
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(float), (void*)0); // Pos vec3
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 7 * sizeof(float), (void*)(3 * sizeof(float))); // Col vec4
- glEnableVertexAttribArray(1);
- glDrawArrays(GL_TRIANGLES, 0, 3);
- glfwSwapBuffers(window);
- glfwPollEvents();
- }
- glfwTerminate(); // Properly clean all of GLFW's allocated resources
- return 0;
- }
- void framebuffer_size_callback(GLFWwindow* window, int width, int height)
- {
- glViewport(0, 0, width, height);
- std::cout << "Resized to " << width << ", " << height << std::endl;
- }
- int processInput(GLFWwindow* window)
- {
- if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
- glfwSetWindowShouldClose(window, true);
- unsigned int output = 0;
- if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS)
- output = output | 1UL;
- if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS)
- output = output | 2UL;
- if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS)
- output = output | 4UL;
- if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS)
- output = output | 8UL;
- return output;
- }
- std::string readFile(const char* filePath) {
- std::string content;
- std::ifstream fileStream(filePath, std::ios::in);
- if (!fileStream.is_open()) {
- std::cerr << "Could not read file " << filePath << ". File does not exist." << std::endl;
- return "";
- }
- std::string line = "";
- while (!fileStream.eof()) {
- std::getline(fileStream, line);
- content.append(line + "\n");
- }
- fileStream.close();
- return content;
- }
- GLuint LoadShader(const char* vertexPath, const char* fragmentPath)
- {
- // 1. retrieve the vertex/fragment source code from filePath
- std::string vertexCode;
- std::string fragmentCode;
- std::ifstream vShaderFile;
- std::ifstream fShaderFile;
- // ensure ifstream objects can throw exceptions:
- vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
- fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
- try
- {
- // open files
- vShaderFile.open(vertexPath);
- fShaderFile.open(fragmentPath);
- std::stringstream vShaderStream, fShaderStream;
- // read file's buffer contents into streams
- vShaderStream << vShaderFile.rdbuf();
- fShaderStream << fShaderFile.rdbuf();
- // close file handlers
- vShaderFile.close();
- fShaderFile.close();
- // convert stream into string
- vertexCode = vShaderStream.str();
- fragmentCode = fShaderStream.str();
- }
- catch (std::ifstream::failure e)
- {
- std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl;
- }
- const char* vShaderCode = vertexCode.c_str();
- const char* fShaderCode = fragmentCode.c_str();
- // 2. compile shaders
- unsigned int vertex, fragment;
- int success;
- char infoLog[512];
- // vertex Shader
- vertex = glCreateShader(GL_VERTEX_SHADER);
- glShaderSource(vertex, 1, &vShaderCode, NULL);
- glCompileShader(vertex);
- // print compile errors if any
- glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
- if (!success)
- {
- glGetShaderInfoLog(vertex, 512, NULL, infoLog);
- std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
- };
- // similiar for Fragment Shader
- fragment = glCreateShader(GL_FRAGMENT_SHADER);
- glShaderSource(fragment, 1, &fShaderCode, NULL);
- glCompileShader(fragment);
- // print compile errors if any
- glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
- if (!success)
- {
- glGetShaderInfoLog(fragment, 512, NULL, infoLog);
- std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
- };
- // shader Program
- GLuint ID = glCreateProgram();
- glAttachShader(ID, vertex);
- glAttachShader(ID, fragment);
- glLinkProgram(ID);
- // print linking errors if any
- glGetProgramiv(ID, GL_LINK_STATUS, &success);
- if (!success)
- {
- glGetProgramInfoLog(ID, 512, NULL, infoLog);
- std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
- }
- // delete the shaders as they're linked into our program now and no longer necessary
- glDeleteShader(vertex);
- glDeleteShader(fragment);
- return ID;
- }
Add Comment
Please, Sign In to add comment