OpenGL半球贴图
#version 330 core out vec4 FragColor; in vec2 tex_coord; uniform sampler2D tex; void main() { //FragColor = vec4(1.0,0.635,0.345,1.0); FragColor = texture(tex, tex_coord); }
#version 330 core layout (location = 0) in vec3 aPos; uniform mat4 face_matrix; out vec2 tex_coord; void main() { float dist = 2*sqrt(pow(aPos.x,2)+pow(aPos.y,2)+pow(aPos.z,2)); vec4 vpos = face_matrix *vec4(aPos.x/dist,aPos.y/dist,aPos.z/dist,1.0);//顶点坐标旋转 tex_coord = vec2((aPos.x+1)/2 ,(aPos.z+1)/2); // -1~1 移动到0~1;((*+1)/2),写错了可能会有黑条(不要去乘MVP) gl_Position = vpos; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | #include <glad/glad.h> #include <GLFW/glfw3.h> #include <shader.h> #include <iostream> #include <math.h> #include <vector> #include <glm/glm.hpp> #include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/type_ptr.hpp> #include <windows.h> #define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" const unsigned int screen_width = 780; const unsigned int screen_height = 780; const GLfloat PI = 3.14159265358979323846f; //将球横纵划分成X*Y的网格 const int Y_SEGMENTS = 40; const int X_SEGMENTS = 40; ; unsigned int testureID = 99; glm::mat4 pos_matrix, face_matrix, projection; GLuint pos_matrix_idx, face_matrix_idx,projID; int main() { // 初始化GLFW glfwInit(); // 初始化GLFW glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // OpenGL版本为3.3,主次版本号均设为3 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 使用核心模式(无需向后兼容性) glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // 如果使用的是Mac OS X系统,需加上这行 glfwWindowHint(GLFW_RESIZABLE, 0); // 不可改变窗口大小 // 创建窗口(宽、高、窗口名称) auto window = glfwCreateWindow(screen_width, screen_height, "Sphere" , nullptr, nullptr); if (window == nullptr) { // 如果窗口创建失败,输出Failed to Create OpenGL Context std::cout << "Failed to Create OpenGL Context" << std::endl; glfwTerminate(); return -1; } glfwMakeContextCurrent(window); // 将窗口的上下文设置为当前线程的主上下文 // 初始化GLAD,加载OpenGL函数指针地址的函数 if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { std::cout << "Failed to initialize GLAD" << std::endl; return -1; } // 指定当前视口尺寸(前两个参数为左下角位置,后两个参数是渲染窗口宽、高) glViewport(0, 0, screen_width, screen_height); Shader shader( "vertexShader.glsl" , "fragmentShader.glsl" ); //加载,编译着色器 //加载图片 //加载图片----------------------------------------------------------------- shader.use(); glGenTextures(1, &testureID); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, testureID); //为bind的纹理设置环绕,过滤方式 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //stbi是一个图片载入的开源组件,文件名,宽高,通道数,你期望的通道数 int W, H, desired_channels; //注意图片是24位的 unsigned char * data = stbi_load( "./21.jpg" , &W, &H, &desired_channels, 0); if (data) { //数据生成纹理;根据指定的参数,把输入数据生成一张2D纹理 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, W, H, 0, GL_RGB, GL_UNSIGNED_BYTE, data); //生成mipmap数组 glUniform1i(glGetUniformLocation(shader.ID, "tex" ), 0); //这句话一定要写,不然没法采样 glGenerateMipmap(GL_TEXTURE_2D); } stbi_image_free(data); //----------------------------------------------------------- std::vector< float > sphereVertices; // Y_SEGMENTS 和 X_SEGMENTS 分别表 示将α 和β 分割了多少份, //y 和 x 分别表示是第几份,以此进行遍历, xSegment*2.0f*PI 即β 角,ySegment*PI 即α 角。 for ( int y = -(Y_SEGMENTS / 2); y <= 0; y++) { for ( int x = 0; x <= X_SEGMENTS; x++) { float xSegment = ( float )x / ( float )X_SEGMENTS; float ySegment = ( float )y / ( float )Y_SEGMENTS; float xPos = std::cos(xSegment * 2.0f * PI) * std::cos(ySegment * PI); float yPos = std::sin(ySegment * PI); float zPos = std::sin(xSegment * 2.0f * PI) * std::cos(ySegment * PI); sphereVertices.push_back(xPos); sphereVertices.push_back(yPos); sphereVertices.push_back(zPos); } } // 生成球的三角形索引;六个点两个三角; std::vector< int > sphereIndices; for ( int i = 0; i < Y_SEGMENTS/2 ; i++) { for ( int j = 0; j < X_SEGMENTS; j++) { sphereIndices.push_back(i * (X_SEGMENTS + 1) + j); sphereIndices.push_back((i + 1) * (X_SEGMENTS + 1) + j); sphereIndices.push_back((i + 1) * (X_SEGMENTS + 1) + j + 1); sphereIndices.push_back(i * (X_SEGMENTS + 1) + j); sphereIndices.push_back((i + 1) * (X_SEGMENTS + 1) + j + 1); sphereIndices.push_back(i * (X_SEGMENTS + 1) + j + 1); } } // 球 unsigned int VBO, VAO; glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); //生成并绑定球体的VAO和VBO glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); // 将顶点数据绑定至当前默认的缓冲中 glBufferData(GL_ARRAY_BUFFER, sphereVertices.size() * sizeof ( float ), &sphereVertices[0], GL_STATIC_DRAW); GLuint element_buffer_object; //EBO glGenBuffers(1, &element_buffer_object); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, element_buffer_object); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sphereIndices.size() * sizeof ( int ), &sphereIndices[0], GL_STATIC_DRAW); // 设置顶点属性指针 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof ( float ), ( void *)0); glEnableVertexAttribArray(0); // 解绑VAO和VBO glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); // pos_matrix_idx = glGetUniformLocation(shader.ID, "pos_matrix"); //projID = glGetUniformLocation(shader.ID, "projection"); glm::mat4 view = glm::mat4(1.0f); ////平移 //pos_matrix = glm::translate(view, glm::vec3(0.0f, 0.00f, 0.0f)); //glUniformMatrix4fv(pos_matrix_idx, 1, GL_FALSE, &pos_matrix[0][0]); ////旋转 //face_matrix = glm::rotate(view, glm::radians(30.0f), glm::vec3(0.0f, 1.0f, 0.0f)); //glUniformMatrix4fv(face_matrix_idx, 1, GL_FALSE, &face_matrix[0][0]); ////s视角 //projection = glm::perspective((float)glfwGetTime(), (float)screen_width / (float)screen_height, 0.1f, 10.0f); //glUniformMatrix4fv(projID, 1, GL_FALSE, &projection[0][0]); //------------------------------------------------------------------------------- int i = 0; // 渲染循环 while (!glfwWindowShouldClose(window)) { // 清空颜色缓冲 glClearColor(0.0f, 0.3f, 0.2f, 1.0f); i = (i++)% 12; //旋转 face_matrix = glm::rotate(view, glm::radians(30.0f*i), glm::vec3(1.0f, 0.0f, 0.0f)); face_matrix_idx = glGetUniformLocation(shader.ID, "face_matrix" ); glUniformMatrix4fv(face_matrix_idx, 1, GL_FALSE, &face_matrix[0][0]); shader.use(); //绘制球 //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glClearDepth(1.0); glEnable(GL_DEPTH_TEST); glBindVertexArray(VAO); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBindBuffer(GL_ARRAY_BUFFER, VAO); glDrawElements(GL_TRIANGLES,(X_SEGMENTS)*(Y_SEGMENTS/2) * 6, GL_UNSIGNED_INT, (GLvoid *)(0)); glBindVertexArray(0); glfwSwapBuffers(window); glfwPollEvents(); Sleep(2000); //1s } // 删除VAO和VBO,EBO glDeleteVertexArrays(1, &VAO); glDeleteBuffers(1, &VBO); glDeleteBuffers(1, &element_buffer_object); // 清理所有的资源并正确退出程序 glfwTerminate(); return 0; } |
半球贴图:旋转过程截图
原图:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2021-12-29 debugview使用
2017-12-29 freetype教程网址
2017-12-29 编译器发展
2017-12-29 静态与动态库文件
2017-12-29 makefile文件操作大全