Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <GL/glew.h>
- #include <GLFW/glfw3.h>
- #include <glm/glm.hpp>
- using namespace std;
- void error_callback(int error, const char *description);
- void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods);
- void framebuffer_size_callback(GLFWwindow *window, int w, int h);
- GLFWwindow *window = NULL;
- int width, height;
- void init_glfw();
- const GLfloat vertex_data[] = {
- -0.75f, -0.75f, 0.0f, 1.0f,
- 0.0f, 0.75f, 0.0f, 1.0f,
- 0.75f, -0.75f, 0.0f, 1.0f
- };
- int main()
- {
- init_glfw();
- GLuint positionBO;
- glGenBuffers(1, &positionBO);
- glBindBuffer(GL_ARRAY_BUFFER, positionBO);
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_data), vertex_data, GL_STATIC_DRAW);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- while(!glfwWindowShouldClose(window)) {
- glClear(GL_COLOR_BUFFER_BIT);
- glViewport(0, 0, width, height);
- glBindBuffer(GL_ARRAY_BUFFER, positionBO);
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
- glDrawArrays(GL_TRIANGLES, 0, 3);
- glfwSwapBuffers(window);
- glfwPollEvents();
- }
- glfwDestroyWindow(window);
- glfwTerminate();
- return 0;
- }
- void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
- {
- if (key == GLFW_KEY_ESCAPE and action == GLFW_PRESS)
- glfwSetWindowShouldClose(window, GL_TRUE);
- }
- void init_glfw()
- {
- glfwSetErrorCallback(error_callback);
- if (!glfwInit())
- exit(EXIT_FAILURE);
- window = glfwCreateWindow(640, 480, "OpenGL", NULL, NULL);
- if (!window) {
- glfwTerminate();
- exit(EXIT_FAILURE);
- }
- glfwMakeContextCurrent(window);
- glewInit();
- glfwSetKeyCallback(window, key_callback);
- glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
- glfwGetFramebufferSize(window, &width, &height);
- glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
- }
- void framebuffer_size_callback(GLFWwindow *window, int w, int h)
- {
- cout << "Resized to: " << w << "x" << h << endl;
- width = w;
- height = h;
- //glViewport(0, 0, width, height);
- }
- void error_callback(int error, const char *description)
- {
- cerr << description;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement