Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <GL/glew.h>
- #include <GLFW/glfw3.h>
- GLFWwindow * window;
- const GLuint default_window_width = 800;
- const GLuint default_window_height = 600;
- const GLchar * window_title = "An OpenGL + GLFW3 Experiment";
- void open(void);
- void update(void);
- void render(void);
- void close(void);
- void keyboard(GLFWwindow* window, int key, int scancode, int action, int mode);
- GLuint shaderProgram, vertexShaderObject, fragmentShaderObject;
- GLuint vertexBufferObject, vertexArrayObject, indexBufferObject;
- int main(void)
- {
- open();
- while (! glfwWindowShouldClose(window))
- {
- update();
- render();
- }
- close();
- exit(EXIT_SUCCESS);
- }
- void open(void)
- {
- if (! glfwInit())
- {
- printf("Glfw init failure\n");
- exit(EXIT_FAILURE);
- }
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
- glfwWindowHint(GLFW_DECORATED, GL_TRUE);
- window = glfwCreateWindow
- (
- default_window_width,
- default_window_height,
- "DEFUALT_WINDOW_NAME",
- NULL,
- NULL
- );
- glfwSetWindowTitle(window, window_title);
- if (NULL == window)
- {
- printf("Window creation failure\n");
- glfwTerminate();
- exit(EXIT_FAILURE);
- }
- glfwMakeContextCurrent(window);
- glewExperimental = GL_TRUE;
- if (glewInit() != GLEW_OK)
- {
- printf("Glew init failure\n");
- exit(EXIT_FAILURE);
- }
- glfwSetKeyCallback(window, keyboard);
- glfwSwapInterval(1); // V-Sync 0=off, 1=60fps, 2=30fps?
- // Set OpenGL viewport
- GLint width, height;
- glfwGetFramebufferSize(window, &width, &height);
- glViewport(0, 0, width, height);
- // Vertex shader object
- const GLchar * vertexShaderObjectSource = "#version 330 core\n"
- "layout (location = 0) in vec3 position;\n"
- "void main()\n"
- "{\n"
- "gl_Position = vec4(position.x, position.y, position.z, 1.0);\n"
- "}\0";
- vertexShaderObject = glCreateShader(GL_VERTEX_SHADER);
- glShaderSource(vertexShaderObject, 1, &vertexShaderObjectSource, NULL);
- glCompileShader(vertexShaderObject);
- // Fragment shader object
- const GLchar* fragmentShaderObjectSource = "#version 330 core\n"
- "out vec4 color;\n"
- "void main()\n"
- "{\n"
- "color = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
- "}\n\0";
- fragmentShaderObject = glCreateShader(GL_FRAGMENT_SHADER);
- glShaderSource(fragmentShaderObject, 1, &fragmentShaderObjectSource, NULL);
- glCompileShader(fragmentShaderObject);
- // Link the shaders
- shaderProgram = glCreateProgram();
- glAttachShader(shaderProgram, vertexShaderObject);
- glAttachShader(shaderProgram, fragmentShaderObject);
- glLinkProgram(shaderProgram);
- glDeleteShader(vertexShaderObject);
- glDeleteShader(fragmentShaderObject);
- GLfloat vertices[] = {
- 0.5f, 0.5f, 0.0f, // Top Right
- 0.5f, -0.5f, 0.0f, // Bottom Right
- -0.5f, -0.5f, 0.0f, // Bottom Left
- -0.5f, 0.5f, 0.0f // Top Left
- };
- GLuint indices[] = {
- // Start from 0!
- 0, 1, 3, // First triangle
- 1, 2, 3 // Second triangle
- };
- // Generate a unique ID for the vertext array object variable "vertexArrayObject"
- glGenVertexArrays(1, &vertexArrayObject);
- glGenBuffers(1, &vertexBufferObject);
- glGenBuffers(1, &indexBufferObject);
- glBindVertexArray(vertexArrayObject);
- glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferObject);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
- glEnableVertexAttribArray(0);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindVertexArray(0);
- }
- void update(void)
- {
- glfwPollEvents();
- fflush(stdout);
- }
- void render(void)
- {
- glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT);
- glUseProgram(shaderProgram);
- glBindVertexArray(vertexArrayObject);
- glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
- glBindVertexArray(0);
- glfwSwapBuffers(window);
- }
- void close(void)
- {
- glfwTerminate();
- }
- void keyboard(GLFWwindow* window, int key, int scancode, int action, int mode)
- {
- if (key == GLFW_KEY_UNKNOWN)
- {
- printf("Key unknown\n");
- }
- else if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
- {
- glfwSetWindowShouldClose(window, GL_TRUE);
- }
- else if (key == GLFW_KEY_W || key == GLFW_KEY_UP)
- {
- if (action == GLFW_PRESS)
- printf("↑/W press\n");
- else if (action == GLFW_RELEASE)
- printf("↑/W release\n");
- }
- else if (key == GLFW_KEY_A || key == GLFW_KEY_LEFT)
- {
- if (action == GLFW_PRESS)
- printf("←/A press\n");
- else if (action == GLFW_RELEASE)
- printf("←/A release\n");
- }
- else if (key == GLFW_KEY_S || key == GLFW_KEY_DOWN)
- {
- if (action == GLFW_PRESS)
- printf("↓/S press\n");
- else if (action == GLFW_RELEASE)
- printf("↓/S release\n");
- }
- else if (key == GLFW_KEY_D || key == GLFW_KEY_RIGHT)
- {
- if (action == GLFW_PRESS)
- printf("→/D press\n");
- else if (action == GLFW_RELEASE)
- printf("→/D release\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement