学习opengl之为立方体六个面贴上不同的纹理图片

您所在的位置:网站首页 opengl添加纹理 学习opengl之为立方体六个面贴上不同的纹理图片

学习opengl之为立方体六个面贴上不同的纹理图片

2023-08-26 03:11| 来源: 网络整理| 查看: 265

效果

相比于立方体自转,这一博客增添了 为立方体的不同面添加不同纹理图片的操作,不再是六个面单一纹理了。 在这里插入图片描述

工具

glad、glfw(这个无所谓)、glm(这个也主要是为了和glad配合,使用eigen也行)

思路

主要是要理解着色器(shader)的原理,它要求所有出现在其中的变量具有相同的维度,比如顶点的个数要和表示纹理坐标的坐标数量相同。

每次使用glDrawArrays(GL_TRIANGLES, 0, 4 * 3);时调用部分顶点。 需要设置glDrawArrays的第三个参数需要设置glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(63 * sizeof(float)))的最后一个参数,即VBO bufferdata中的数据起点 使用glActiveTexture和glBindTexture激活着色器中正确的纹理图片 在这里都激活GL_TEXTURE0,但用时绑定不同的texture纹理图片ID glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture1); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(0 * sizeof(float))); glEnableVertexAttribArray(0); // texture coord attribute glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float))); glEnableVertexAttribArray(1); glDrawArrays(GL_TRIANGLES, 0, 4 * 3);

这里激活了GL_TEXTURE0,同时将texture1所索引的图片设置为GL_TEXTURE0,在使用不同纹理图片时,修改当前绑定的纹理即可。

代码

main.cpp

#pragma execution_character_set("utf-8") #include #include #define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" #include #include #include #include "shader_m.h" #include #include using namespace std; void framebuffer_size_callback(GLFWwindow* window, int width, int height); void processInput(GLFWwindow* window); // settings const unsigned int SCR_WIDTH = 400; const unsigned int SCR_HEIGHT = 400; // camera glm::vec3 cameraPos = glm::vec3(5.f, 5.f, 5.0f); glm::vec3 cameraFront = glm::vec3(-5.0f, -5.0f, -5.0f); glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f); float deltaalpha = 0.f; float deltabeta = 0.f; float deltatheta = 0.f; //左右转 glm::mat4 currentModel = glm::mat4(1.0f); // timing float deltaTime = 0.0f; // time between current frame and last frame float lastFrame = 0.0f; int main() { // glfw: initialize and configure // ------------------------------ glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); #ifdef __APPLE__ glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X #endif // glfw window creation // -------------------- std::cout std::cout glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(1.0f,0.f,0.f), glm::vec3(0.f,1.f,0.0f), glm::vec3(-1.f,0.f,0.f), glm::vec3(0.f,-1.f,0.0f), glm::vec3(1.f,1.f,0.f), glm::vec3(-1.f,1.f,0.0f), glm::vec3(-1.f,-1.f,0.f), glm::vec3(1.f,-1.f,0.0f), glm::vec3(0.0f,0.0f,1.f), glm::vec3(1.0f,0.f,1.f), glm::vec3(0.f,1.f,1.0f), glm::vec3(-1.f,0.f,1.f), glm::vec3(0.f,-1.f,1.0f), glm::vec3(1.f,1.f,1.f), glm::vec3(-1.f,1.f,1.0f), glm::vec3(-1.f,-1.f,1.f), glm::vec3(1.f,-1.f,1.0f), glm::vec3(0.0f,0.0f,-1.f), glm::vec3(1.0f,0.f,-1.f), glm::vec3(0.f,1.f,-1.0f), glm::vec3(-1.f,0.f,-1.f), glm::vec3(0.f,-1.f,-1.0f), glm::vec3(1.f,1.f,-1.f), glm::vec3(-1.f,1.f,-1.0f), glm::vec3(-1.f,-1.f,-1.f), glm::vec3(1.f,-1.f,-1.0f) }; unsigned int VBO, VAO; glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // position attribute glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(6*5*sizeof(float))); glEnableVertexAttribArray(0); // texture coord attribute glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(33 * sizeof(float))); glEnableVertexAttribArray(1); // load and create a texture // ------------------------- unsigned int texture1, texture2, texture3; // texture 1 // --------- glGenTextures(1, &texture1); glBindTexture(GL_TEXTURE_2D, texture1); // set the texture wrapping parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // set texture filtering parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // load image, create texture and generate mipmaps int width, height, nrChannels; stbi_set_flip_vertically_on_load(true); // tell stb_image.h to flip loaded texture's on the y-axis. unsigned char* data = stbi_load("wall.jpg", &width, &height, &nrChannels, 0); if (data) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); glGenerateMipmap(GL_TEXTURE_2D); } else { std::cout std::cout std::cout


【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3