• Opengl实例7:glm(0.9.8.5)库 +矩阵旋转+课后作业


    重点:
    需要采用glm库进行矩阵的功能操作。

    矩阵进行操作的时候是存在顺序的:缩放->旋转->位移

    y'=y*缩放*旋转*位移

    位移*旋转*缩放*y =y'

    采用右向左读这个乘法。在组合矩阵时,先进行缩放操作,然后是旋转,最后才是位移,否则它们会(消极地)互相影响。比如,如果你先位移再缩放,位移的向量也会同样被缩放(译注:比如向某方向移动2米,2米也许会被缩放成1米)!

    乘法顺序

    glm-0.9.8.5

    链接:https://pan.baidu.com/s/1GsaewfRIDH5XnQSLweT93A?pwd=ay5h 
    提取码:ay5h

    结果:

     

    代码:

    vertex.txt

    1. #version 330 core
    2. layout (location = 0) in vec3 aPos;
    3. layout (location = 1) in vec3 aColor;
    4. layout (location = 2) in vec2 aTexCoord;
    5. out vec3 ourColor;
    6. out vec2 TexCoord;
    7. uniform mat4 transform;
    8. void main()
    9. {
    10. gl_Position = transform * vec4(aPos, 1.0f);
    11. ourColor = aColor;
    12. TexCoord = aTexCoord;
    13. }

     fragment.txt

    1. #version 330 core
    2. out vec4 FragColor;
    3. in vec3 ourColor;
    4. in vec2 TexCoord;
    5. uniform sampler2D texture1;
    6. uniform sampler2D texture2;
    7. uniform float factor;
    8. void main()
    9. {
    10. FragColor = mix(texture(texture1, TexCoord), texture(texture2, vec2(-TexCoord.x,TexCoord.y)), abs(sin(factor*0.2)));
    11. }

     main.cpp

    1. #include <glad/glad.h>
    2. #include <GLFW/glfw3.h>
    3. #include <glm/glm.hpp>
    4. #include <glm/gtc/matrix_transform.hpp>
    5. #include <glm/gtc/type_ptr.hpp>
    6. #include "Shader.h"
    7. #include <iostream>
    8. #define STB_IMAGE_IMPLEMENTATION
    9. #include "stb_image.h"
    10. void framebuffer_size_callback(GLFWwindow* window, int width, int height);
    11. void processInput(GLFWwindow* window);
    12. // settings
    13. const unsigned int SCR_WIDTH = 800;
    14. const unsigned int SCR_HEIGHT = 600;
    15. int main()
    16. {
    17. // glfw: initialize and configure
    18. // ------------------------------
    19. glfwInit();
    20. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    21. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    22. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    23. #ifdef __APPLE__
    24. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    25. #endif
    26. // glfw window creation
    27. // --------------------
    28. GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
    29. if (window == NULL)
    30. {
    31. std::cout << "Failed to create GLFW window" << std::endl;
    32. glfwTerminate();
    33. return -1;
    34. }
    35. glfwMakeContextCurrent(window);
    36. glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
    37. // glad: load all OpenGL function pointers
    38. // ---------------------------------------
    39. if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    40. {
    41. std::cout << "Failed to initialize GLAD" << std::endl;
    42. return -1;
    43. }
    44. // build and compile our shader zprogram
    45. // ------------------------------------
    46. Shader ourShader(".//text//vertex.txt", ".//text//fragment.txt");
    47. // set up vertex data (and buffer(s)) and configure vertex attributes
    48. // ------------------------------------------------------------------
    49. float vertices[] = {
    50. // positions // colors // texture coords
    51. 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
    52. 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
    53. -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
    54. -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
    55. };
    56. unsigned int indices[] = {
    57. 0, 1, 3, // first triangle
    58. 1, 2, 3 // second triangle
    59. };
    60. unsigned int VBO, VAO, EBO;
    61. glGenVertexArrays(1, &VAO);
    62. glGenBuffers(1, &VBO);
    63. glGenBuffers(1, &EBO);
    64. glBindVertexArray(VAO);
    65. glBindBuffer(GL_ARRAY_BUFFER, VBO);
    66. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    67. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    68. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
    69. // position attribute
    70. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
    71. glEnableVertexAttribArray(0);
    72. // color attribute
    73. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
    74. glEnableVertexAttribArray(1);
    75. // texture coord attribute
    76. glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
    77. glEnableVertexAttribArray(2);
    78. // load and create a texture
    79. // -------------------------
    80. unsigned int texture1, texture2;
    81. // texture 1
    82. // ---------
    83. glGenTextures(1, &texture1);
    84. glBindTexture(GL_TEXTURE_2D, texture1);
    85. // set the texture wrapping parameters
    86. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method)
    87. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    88. // set texture filtering parameters
    89. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    90. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    91. // load image, create texture and generate mipmaps
    92. int width, height, nrChannels;
    93. //在图像加载时帮助我们翻转y轴
    94. stbi_set_flip_vertically_on_load(true); // tell stb_image.h to flip loaded texture's on the y-axis.
    95. // The FileSystem::getPath(...) is part of the GitHub repository so we can find files on any IDE/platform; replace it with your own image path.
    96. unsigned char* data = stbi_load(".//resource//container.jpg", &width, &height, &nrChannels, 0);
    97. if (data)
    98. {
    99. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
    100. glGenerateMipmap(GL_TEXTURE_2D);
    101. }
    102. else
    103. {
    104. std::cout << "Failed to load texture" << std::endl;
    105. }
    106. stbi_image_free(data);
    107. 设置边缘颜色值
    108. //float borderColor[] = { 1.0f, 1.0f, 0.0f, 1.0f };
    109. //glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
    110. // texture 2
    111. // ---------
    112. glGenTextures(1, &texture2);
    113. glBindTexture(GL_TEXTURE_2D, texture2);
    114. // set the texture wrapping parameters
    115. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method)
    116. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    117. // set texture filtering parameters
    118. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    119. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    120. // load image, create texture and generate mipmaps
    121. data = stbi_load(".//resource//awesomeface.png", &width, &height, &nrChannels, 0);
    122. if (data)
    123. {
    124. // note that the awesomeface.png has transparency and thus an alpha channel, so make sure to tell OpenGL the data type is of GL_RGBA
    125. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
    126. glGenerateMipmap(GL_TEXTURE_2D);
    127. }
    128. else
    129. {
    130. std::cout << "Failed to load texture" << std::endl;
    131. }
    132. stbi_image_free(data);
    133. // tell opengl for each sampler to which texture unit it belongs to (only has to be done once)
    134. // -------------------------------------------------------------------------------------------
    135. ourShader.use(); // don't forget to activate/use the shader before setting uniforms!
    136. // either set it manually like so:
    137. glUniform1i(glGetUniformLocation(ourShader.ID, "texture1"), 0);
    138. // or set it via the texture class
    139. ourShader.setInt("texture2", 1);
    140. float factor = 0.0;
    141. // render loop
    142. // -----------
    143. while (!glfwWindowShouldClose(window))
    144. {
    145. // input
    146. // -----
    147. processInput(window);
    148. // render
    149. // ------
    150. glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    151. glClear(GL_COLOR_BUFFER_BIT);
    152. // bind textures on corresponding texture units
    153. glActiveTexture(GL_TEXTURE0);
    154. glBindTexture(GL_TEXTURE_2D, texture1);
    155. glActiveTexture(GL_TEXTURE1);
    156. glBindTexture(GL_TEXTURE_2D, texture2);
    157. // render container
    158. ourShader.use();
    159. factor = glfwGetTime();
    160. ourShader.setFloat("factor", factor);
    161. //旋转缩放矩阵
    162. glm::mat4 trans;
    163. GLM希望它的角度是弧度制的,绕z轴旋转
    164. trans = glm::rotate(trans, (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f));
    165. //进行平移
    166. trans = glm::translate(trans, glm::vec3(0.5f, -0.5f, 0.0f));
    167. unsigned int transformLoc = glGetUniformLocation(ourShader.ID, "transform");
    168. glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(trans));
    169. glBindVertexArray(VAO);
    170. glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
    171. // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
    172. // -------------------------------------------------------------------------------
    173. glfwSwapBuffers(window);
    174. glfwPollEvents();
    175. }
    176. // optional: de-allocate all resources once they've outlived their purpose:
    177. // ------------------------------------------------------------------------
    178. glDeleteVertexArrays(1, &VAO);
    179. glDeleteBuffers(1, &VBO);
    180. glDeleteBuffers(1, &EBO);
    181. // glfw: terminate, clearing all previously allocated GLFW resources.
    182. // ------------------------------------------------------------------
    183. glfwTerminate();
    184. return 0;
    185. }
    186. // process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
    187. // ---------------------------------------------------------------------------------------------------------
    188. void processInput(GLFWwindow* window)
    189. {
    190. if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
    191. glfwSetWindowShouldClose(window, true);
    192. }
    193. // glfw: whenever the window size changed (by OS or user resize) this callback function executes
    194. // ---------------------------------------------------------------------------------------------
    195. void framebuffer_size_callback(GLFWwindow* window, int width, int height)
    196. {
    197. // make sure the viewport matches the new window dimensions; note that width and
    198. // height will be significantly larger than specified on retina displays.
    199. glViewport(0, 0, width, height);
    200. }

  • 相关阅读:
    通讯录管理系统-C++课程设计
    Akshare获取分红数据
    云打印api搭建,云打印api怎么对接?
    如何优雅编写测试用例
    MySQL如何在不知道密码的情况下知道并修改密码
    win10 配置 oh-my-posh
    【图自动编码器】基础介绍 及 基于PyTorch的图自动编码器实例代码 | MLP应用于节点级别和图级别的任务实例(附实例代码+数据集)
    DataSheet专业名词解读——每天10个专业名词(1)23.9.18 (NXP)MPC5604B/C
    YOLO算法改进5【中阶改进篇】:添加SENet注意力机制
    MacBook Pro 耗电严重的终极解决办法2022年
  • 原文地址:https://blog.csdn.net/qq_38409301/article/details/126897461