Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glew.h>
- #include <GLFW/glfw3.h>
- #include <glm/glm.hpp>
- #include <memory>
- #include <vector>
- #include <iostream>
- #include <stdlib.h>
- #include <vector>
- using namespace std;
- using namespace glm;
- #include "../common/shader.hpp"
- GLFWwindow *window;
- class Vertices {
- private:
- const uint32_t vertParts;
- const uint32_t primitiveSize;
- GLuint vbo;
- vector<GLfloat> verts;
- uint32_t pointer;
- public:
- Vertices(const uint32_t vertParts, const uint32_t primitiveSize, const uint32_t primitives);
- ~Vertices(void);
- void PutBufferData(const GLenum bufferType);
- void SetAttribPointer(const uint16_t attrib, GLboolean normalized);
- void DelAttribPointer(const uint16_t attrib);
- void Bind(void);
- Vertices& operator<<(GLfloat val);
- };
- Vertices::Vertices(const uint32_t vertParts, const uint32_t primitiveSize, const uint32_t primitives)
- : vertParts{vertParts}, primitiveSize{primitiveSize}, verts(primitives * vertParts * primitiveSize) {
- glGenBuffers(1, &vbo);
- }
- Vertices::~Vertices() {
- glDeleteBuffers(1, &vbo);
- }
- void Vertices::PutBufferData(const GLenum bufferType) {
- glBufferData(GL_ARRAY_BUFFER, verts.size(), verts.data(), bufferType);
- }
- void Vertices::SetAttribPointer(const uint16_t attrib, GLboolean normalized) {
- glEnableVertexAttribArray(attrib);
- glVertexAttribPointer(attrib, primitiveSize, GL_FLOAT, normalized, sizeof(GLfloat) * vertParts, nullptr);
- }
- void Vertices::DelAttribPointer(const uint16_t attrib) {
- glDisableVertexAttribArray(attrib);
- }
- void Vertices::Bind() {
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
- }
- Vertices& Vertices::operator<<(GLfloat val) {
- verts[pointer] = val;
- ++pointer;
- return *this;
- }
- int main(void) {
- // Initialise GLFW
- if (!glfwInit()) {
- fprintf(stderr, "Failed to initialize GLFW\n");
- getchar();
- return -1;
- }
- glfwWindowHint(GLFW_SAMPLES, 4);
- glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT,
- GL_TRUE); // To make MacOS happy; should not be needed
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- // Open a window and create its OpenGL context
- window = glfwCreateWindow(1024, 768, "Playground", nullptr, nullptr);
- if (window == nullptr) {
- fprintf(stderr,
- "Failed to open GLFW window. If you have an Intel GPU, they are "
- "not 3.3 compatible. Try the 2.1 version of the tutorials.\n");
- getchar();
- glfwTerminate();
- return -1;
- }
- glfwMakeContextCurrent(window);
- // Initialize GLEW
- // IMPORTANT: if this is not enable this might create SegmentationFault
- glewExperimental = true; // For core profile
- if (glewInit() != GLEW_OK) {
- fprintf(stderr, "Failed to initialize GLEW\n");
- getchar();
- glfwTerminate();
- return -1;
- }
- // Ensure we can capture the escape key being pressed below
- glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
- // GL code starts
- // Dark blue background
- glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
- GLuint vao;
- glGenVertexArrays(1, &vao);
- glBindVertexArray(vao);
- Shader shader("shader.vert", "shader.frag");
- static const uint32_t triangles = 1;
- Vertices vbo(3, 3, triangles);
- Vertices colors(3, 3, triangles);
- vbo.Bind();
- vbo << -0.5f << -0.5f << 0.0f;
- vbo << 0.5f << -0.5f << 0.0f;
- vbo << 0.0f << 0.5f << 0.0f;
- vbo.PutBufferData(GL_STATIC_DRAW);
- colors.Bind();
- colors << 1.0f << 0.0f << 0.0f;
- colors << 0.0f << 1.0f << 0.0f;
- colors << 0.0f << 0.0f << 1.0f;
- colors.PutBufferData(GL_STATIC_DRAW);
- GLint colorLoc = shader.GetLocation("color");
- do {
- glClear(GL_COLOR_BUFFER_BIT);
- shader.Use();
- float time = glfwGetTime();
- float color = sin(time) * 0.2f;
- glUniform4f(colorLoc, color, color + 0.5f, color, 1.0f);
- vbo.Bind();
- vbo.SetAttribPointer(0, GL_FALSE);
- colors.Bind();
- colors.SetAttribPointer(1, GL_FALSE);
- glDrawArrays(GL_TRIANGLES, 0, triangles * 3);
- vbo.DelAttribPointer(0);
- colors.DelAttribPointer(1);
- // Swap buffers
- glfwSwapBuffers(window);
- glfwPollEvents();
- } while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
- glfwWindowShouldClose(window) == 0);
- glDeleteVertexArrays(1, &vao);
- // GL code ends
- // Close OpenGL window and terminate GLFW
- glfwTerminate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement