Advertisement
DrAungWinHtut

opengltestslo.c

Dec 30th, 2022
1,110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.92 KB | None | 0 0
  1. 1. Set `$PROJECT_DIR` as project directory
  2. 2. Download [GLFW Source package](https://github.com/glfw/glfw/releases/download/3.3.8/glfw-3.3.8.zip)
  3. 3. Extract `glfw-3.3.8.zip` folder to `$PROJECT_DIR`
  4. 4. `mkdir -p $PROJECT_DIR/glfw-3.3.8/build`
  5. 5. `cd $PROJECT_DIR/glfw-3.3.8/build`
  6. 6. `cmake ..`
  7. 7. `cmake --build .`
  8. 8. `mkdir -p $PROJECT_DIR/References/Include`
  9. 9. `mkdir -p $PROJECT_DIR/References/Libs`
  10. 10. `ln -s src/libglfw3.a $PROJECT_DIR/References/Libs/`
  11. 11. `ln -s ../include/GLFW $PROJECT_DIR/References/Include/`
  12. 12. `cd $PROJECT_DIR`
  13. 13. `mkdir -p GLAD`
  14. 14. Generate [GLAD](https://glad.dav1d.de/#language=c&specification=gl&api=gl%3D3.3&api=gles1%3Dnone&api=gles2%3Dnone&api=glsc2%3Dnone&profile=core&loader=on) and download `glad.zip`
  15. 15. Extract `glad.zip` to `$PROJECT_DIR/GLAD/`
  16. 16. `ln -s GLAD/include/glad References/Include/`
  17. 17. `ln -s GLAD/include/KHR References/Include/`
  18. 18. `mkdir -p HelloWindow/src/`
  19. 19. `cp GLAD/src/glac.c HelloWindow/src/`
  20.  
  21. ```CMake
  22. # $PROJECT_DIR/HelloWindow/CMakeLists.txt
  23. cmake_minimum_required(VERSION 3.0)
  24.  
  25. project(HelloWindow)
  26.  
  27. file(GLOB_RECURSE sources src/*.c src/*.cpp src/*.h src/*.hpp)
  28. add_executable(HelloWindow ${sources})
  29.  
  30. target_include_directories(HelloWindow PRIVATE ${CMAKE_SOURCE_DIR}/../References/Include)
  31. target_link_libraries(HelloWindow
  32.   ${CMAKE_SOURCE_DIR}/../References/Libs/libglfw3.a
  33.   GL
  34.   X11
  35.   pthread
  36.   Xrandr
  37.   Xi
  38.   dl
  39. )
  40. ```
  41. ```cpp
  42. // $PROJECT_DIR/HelloWindow/src/main.cpp
  43. #include <glad/glad.h>
  44. #include <GLFW/glfw3.h>
  45.  
  46. #include <iostream>
  47.  
  48. void framebuffer_size_callback(GLFWwindow *window, int width, int height)
  49. {
  50.   glViewport(0, 0, width, height);
  51. }
  52.  
  53. void processInput(GLFWwindow *window)
  54. {
  55.   if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
  56.     glfwSetWindowShouldClose(window, true);
  57. }
  58.  
  59. int main()
  60. {
  61.   glfwInit();
  62.   glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  63.   glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  64.   glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  65.   // glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  66.  
  67.   GLFWwindow *window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
  68.   if (window == NULL)
  69.   {
  70.     std::cout << "Failed to create GLFW window" << std::endl;
  71.     glfwTerminate();
  72.     return -1;
  73.   }
  74.   glfwMakeContextCurrent(window);
  75.  
  76.   if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
  77.   {
  78.     std::cout << "Failed to initialize GLAD" << std::endl;
  79.     return -1;
  80.   }
  81.  
  82.   glViewport(0, 0, 800, 600);
  83.  
  84.   glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  85.  
  86.   while (!glfwWindowShouldClose(window))
  87.   {
  88.     processInput(window);
  89.  
  90.     glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
  91.     glClear(GL_COLOR_BUFFER_BIT);
  92.  
  93.     glfwSwapBuffers(window);
  94.     glfwPollEvents();
  95.   }
  96.  
  97.   glfwTerminate();
  98.   return 0;
  99. }
  100. ```
  101. 20. `mkdir -p HelloWindow/build`
  102. 21. `cd HelloWindow/build`
  103. 22. `cmake .. && cmake --build .`
  104. 23. `./HelloWindow`
Tags: opengl
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement