[关闭]
@markheng 2016-03-12T04:42:31.000000Z 字数 1784 阅读 3266

OpenGL GLFW库画图流程

计算机图形学 OpenGL-Tutorial OpenGL教程


参考地址

http://www.opengl-tutorial.org/ OpenGL-tutorial
有中文版的但是中文版的代码部分有些注释有遗漏

代码流程

头文件

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. // Include GLEW
  4. #include <GL/glew.h>
  5. // Include GLFW
  6. #include <glfw3.h>
  7. GLFWwindow* window;
  8. // Include GLM
  9. #include <glm/glm.hpp>
  10. //matrix calculate header
  11. #include <glm/gtc/matrix_transform.hpp>
  12. using namespace glm;
  13. //loading GLSL files
  14. #include <common/shader.hpp>

main函数

  1. int main( void )
  2. {
  3. // Initialise GLFW
  4. if( !glfwInit() )
  5. {
  6. fprintf( stderr, "Failed to initialize GLFW\n" );
  7. getchar();
  8. return -1;
  9. }
  10. glfwWindowHint(GLFW_SAMPLES, 4);
  11. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  12. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  13. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
  14. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  15. // Open a window and create its OpenGL context
  16. window = glfwCreateWindow( 1024, 768, "Tutorial 01", NULL, NULL);
  17. if( window == NULL ){
  18. 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" );
  19. getchar();
  20. glfwTerminate();
  21. return -1;
  22. }
  23. glfwMakeContextCurrent(window);
  24. // Initialize GLEW
  25. if (glewInit() != GLEW_OK) {
  26. fprintf(stderr, "Failed to initialize GLEW\n");
  27. getchar();
  28. glfwTerminate();
  29. return -1;
  30. }
  31. // Ensure we can capture the escape key being pressed below
  32. glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
  33. // Dark blue background
  34. glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
  35. do{
  36. // Clear the screen. It's not mentioned before Tutorial 02, but it can cause flickering, so it's there nonetheless.
  37. glClear( GL_COLOR_BUFFER_BIT );
  38. // Draw nothing, see you in tutorial 2 !
  39. // Swap buffers
  40. glfwSwapBuffers(window);
  41. glfwPollEvents();
  42. } // Check if the ESC key was pressed or the window was closed
  43. while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
  44. glfwWindowShouldClose(window) == 0 );
  45. // Close OpenGL window and terminate GLFW
  46. glfwTerminate();
  47. return 0;
  48. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注