• 【学习日志】2022.11.03 LearnOpenGL----DAY1


     

     

     

     

     

     

     

    Draw Cube

    1. #include <iostream>
    2. #include <glad/gl.h>
    3. #define GLFW_INCLUDE_NONE
    4. #include <GLFW/glfw3.h>
    5. #include <glm/glm.hpp>
    6. #include<glm/gtc/matrix_transform.hpp>
    7. #include<glm/gtx/transform2.hpp>
    8. #include<glm/gtx/euler_angles.hpp>
    9. #include <stdlib.h>
    10. #include <stdio.h>
    11. #include "VertexData.h"
    12. #include "ShaderSource.h"
    13. using namespace std;
    14. static void error_callback(int error, const char* description)
    15. {
    16. fprintf(stderr, "Error: %s\n", description);
    17. }
    18. GLFWwindow* window;
    19. GLuint vertex_shader, fragment_shader, program;
    20. GLint mvp_location, vpos_location, vcol_location;
    21. //初始化OpenGL
    22. void init_opengl()
    23. {
    24. cout<<"init opengl"<<endl;
    25. //设置错误回调
    26. glfwSetErrorCallback(error_callback);
    27. if (!glfwInit())
    28. exit(EXIT_FAILURE);
    29. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
    30. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
    31. //创建窗口
    32. window = glfwCreateWindow(960, 640, "Simple example", NULL, NULL);
    33. if (!window)
    34. {
    35. glfwTerminate();
    36. exit(EXIT_FAILURE);
    37. }
    38. glfwMakeContextCurrent(window);
    39. gladLoadGL(glfwGetProcAddress);
    40. glfwSwapInterval(1);
    41. }
    42. //编译、链接Shader
    43. void compile_shader()
    44. {
    45. //创建顶点Shader
    46. vertex_shader = glCreateShader(GL_VERTEX_SHADER);
    47. //指定Shader源码
    48. glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
    49. //编译Shader
    50. glCompileShader(vertex_shader);
    51. //获取编译结果
    52. GLint compile_status=GL_FALSE;
    53. glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &compile_status);
    54. if (compile_status == GL_FALSE)
    55. {
    56. GLchar message[256];
    57. glGetShaderInfoLog(vertex_shader, sizeof(message), 0, message);
    58. cout<<"compile vs error:"<<message<<endl;
    59. }
    60. //创建片段Shader
    61. fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
    62. //指定Shader源码
    63. glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
    64. //编译Shader
    65. glCompileShader(fragment_shader);
    66. //获取编译结果
    67. compile_status=GL_FALSE;
    68. glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &compile_status);
    69. if (compile_status == GL_FALSE)
    70. {
    71. GLchar message[256];
    72. glGetShaderInfoLog(fragment_shader, sizeof(message), 0, message);
    73. cout<<"compile fs error:"<<message<<endl;
    74. }
    75. //创建GPU程序
    76. program = glCreateProgram();
    77. //附加Shader
    78. glAttachShader(program, vertex_shader);
    79. glAttachShader(program, fragment_shader);
    80. //Link
    81. glLinkProgram(program);
    82. //获取编译结果
    83. GLint link_status=GL_FALSE;
    84. glGetProgramiv(program, GL_LINK_STATUS, &link_status);
    85. if (link_status == GL_FALSE)
    86. {
    87. GLchar message[256];
    88. glGetProgramInfoLog(program, sizeof(message), 0, message);
    89. cout<<"link error:"<<message<<endl;
    90. }
    91. }
    92. int main(void)
    93. {
    94. init_opengl();
    95. compile_shader();
    96. //获取shader属性ID
    97. mvp_location = glGetUniformLocation(program, "u_mvp");
    98. vpos_location = glGetAttribLocation(program, "a_pos");
    99. vcol_location = glGetAttribLocation(program, "a_color");
    100. while (!glfwWindowShouldClose(window))
    101. {
    102. float ratio;
    103. int width, height;
    104. glm::mat4 model,view, projection, mvp;
    105. //获取画面宽高
    106. glfwGetFramebufferSize(window, &width, &height);
    107. ratio = width / (float) height;
    108. glViewport(0, 0, width, height);
    109. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    110. glClearColor(49.f/255,77.f/255,121.f/255,1.f);
    111. //坐标系变换
    112. glm::mat4 trans = glm::translate(glm::vec3(0,0,0)); //不移动顶点坐标;
    113. static float rotate_eulerAngle=0.f;
    114. rotate_eulerAngle+=1;
    115. glm::mat4 rotation = glm::eulerAngleYXZ(glm::radians(rotate_eulerAngle), glm::radians(rotate_eulerAngle), glm::radians(rotate_eulerAngle)); //使用欧拉角旋转;
    116. glm::mat4 scale = glm::scale(glm::vec3(2.0f, 2.0f, 2.0f)); //缩放;
    117. model = trans*scale*rotation;
    118. view = glm::lookAt(glm::vec3(0, 0, 10), glm::vec3(0, 0,0), glm::vec3(0, 1, 0));
    119. projection=glm::perspective(glm::radians(60.f),ratio,1.f,1000.f);
    120. mvp=projection*view*model;
    121. //指定GPU程序(就是指定顶点着色器、片段着色器)
    122. glUseProgram(program);
    123. glEnable(GL_DEPTH_TEST);
    124. glEnable(GL_CULL_FACE);//开启背面剔除
    125. //启用顶点Shader属性(a_pos),指定与顶点坐标数据进行关联
    126. glEnableVertexAttribArray(vpos_location);
    127. glVertexAttribPointer(vpos_location, 3, GL_FLOAT, false, sizeof(glm::vec3), kPositions);
    128. //启用顶点Shader属性(a_color),指定与顶点颜色数据进行关联
    129. glEnableVertexAttribArray(vcol_location);
    130. glVertexAttribPointer(vcol_location, 3, GL_FLOAT, false, sizeof(glm::vec4), kColors);
    131. //上传mvp矩阵
    132. glUniformMatrix4fv(mvp_location, 1, GL_FALSE, &mvp[0][0]);
    133. //上传顶点数据并进行绘制
    134. glDrawArrays(GL_TRIANGLES, 0, 6*6);
    135. glfwSwapBuffers(window);
    136. glfwPollEvents();
    137. }
    138. glfwDestroyWindow(window);
    139. glfwTerminate();
    140. exit(EXIT_SUCCESS);
    141. }

  • 相关阅读:
    批量生成图片的数据增强脚本
    Seata之AT模式原理详解(三)
    【Crypto | CTF】RSA打法 集合
    Go学习笔记 -- 函数和高阶函数
    webpack使用 三 优化环境配置
    【PyQt5系列】修改计数器实现控制
    【python基础】生成式、装饰器、高阶函数
    持续学习的综述: 理论、方法与应用
    CLR C#--引用类型和值类型
    寒气难抵,跨境电商年底仍有一批卖家要出局!
  • 原文地址:https://blog.csdn.net/Angelloveyatou/article/details/127673720