Advertisement
Guest User

GLFW3 Tutorial Code

a guest
Jun 27th, 2013
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. #include <GLFW\glfw3.h>
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. static void error_callback(int error, const char* description)
  7. {
  8.     fputs(description, stderr);
  9. }
  10.  
  11. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  12. {
  13.     if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  14.         glfwSetWindowShouldClose(window, GL_TRUE);
  15. }
  16.  
  17. int main(void)
  18. {
  19.     GLFWwindow* window;
  20.  
  21.     glfwSetErrorCallback(error_callback);
  22.  
  23.     if(!glfwInit())
  24.         exit(EXIT_FAILURE);
  25.  
  26.     window = glfwCreateWindow(800, 600, "Test Window, I Guess", NULL, NULL);
  27.     if(!window)
  28.     {
  29.         glfwTerminate();
  30.         exit(EXIT_FAILURE);
  31.     }
  32.  
  33.     glfwMakeContextCurrent(window);
  34.  
  35.     glfwSetKeyCallback(window, key_callback);
  36.  
  37.     while(!glfwWindowShouldClose(window))
  38.     {
  39.         float ratio;
  40.         int width, height;
  41.  
  42.         glfwGetFramebufferSize(window, &width, &height);
  43.         ratio = width / (float)height;
  44.  
  45.         glViewport(0, 0, width, height);
  46.         glClear(GL_COLOR_BUFFER_BIT);
  47.  
  48.         glMatrixMode(GL_PROJECTION);
  49.         glLoadIdentity();
  50.         glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f);
  51.         glMatrixMode(GL_MODELVIEW);
  52.  
  53.         glLoadIdentity();
  54.         glRotatef((float)glfwGetTime() * 50.f, 0.f, 0.f, 1.f);
  55.  
  56.         glBegin(GL_TRIANGLES);
  57.         glColor3f(1.f, 0.f, 0.f);
  58.         glVertex3f(-0.6f, -0.4f, 0.f);
  59.         glColor3f(0.f, 1.f, 0.f);
  60.         glVertex3f(0.6f, -0.4f, 0.f);
  61.         glColor3f(0.f, 0.f, 1.f);
  62.         glVertex3f(0.f, 0.6f, 0.f);
  63.         glEnd();
  64.  
  65.         glfwSwapBuffers(window);
  66.         glfwPollEvents();
  67.     }
  68.  
  69.     glfwDestroyWindow(window);
  70.  
  71.     glfwTerminate();
  72.     exit(EXIT_SUCCESS);
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement