手动生成mipmaps

3

原始帖子:
https://computergraphics.stackexchange.com/questions/4793/how-can-i-generate-mipmaps-manually
这是个愚蠢的错误,我忘记设置所有纹理坐标了 :P

学习OpenGL相当困难。 根据我对mipmaps的理解,我可以手动设置每个mipmap级别,这样当我的纹理看起来更大或更小时,我可以改变使用的图像。

我尝试了以下代码,但无论三角形的大小如何,它总是使用第一个mipmap级别。为什么?如果三角形更接近其他任何mipmap级别,为什么不使用它们?

我真的很讨厌在我的问题中放置一堆代码,因为我迫使其他人浪费时间分析代码而不是放置最相关的代码片段,但我不知道问题出在哪里,也没有其他选择。抱歉 :(

#include #define GLEW_STATIC #include #include const GLchar* VERTEX_SHADER_SOURCE = "#version 330 core \n" "layout (location = 0) in vec2 position; \n" "layout (location = 1) in vec2 myTexCoord; \n" " \n" "out vec2 TexCoord; \n" " \n" "void main() \n" "{ \n" " gl_Position = vec4(position,0.0f, 1.0f);\n" " TexCoord = myTexCoord; \n" "} \n";
const GLchar* FRAGMENT_SHADER_SOURCE = "#version 330 core \n" "in vec2 TexCoord; \n" " \n" "out vec4 color; \n" " \n" "uniform sampler2D ourTexture; \n" " \n" "void main() \n" "{ \n" " color = texture(ourTexture, TexCoord);\n" "} \n";
int main() { //Change the value of size by 1,2,4,8,16,32,64,128 or 256 and see how the color of the triangle doesn't change. Why does it happen? GLint size = 128;
//INIT STUFFS glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); GLFWwindow* window = glfwCreateWindow(size,size, "", nullptr, nullptr); glfwMakeContextCurrent(window); glewExperimental = GL_TRUE; glewInit(); glViewport(0, 0,size,size);
GLuint texture; glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture);
//Our image for the mipmap with odd level and green color //The dimensions are 256 x 256 and is multiplied by 3 because I defined R,G and B colors unsigned char imageArray[256*256*3]; for(int i = 0; i < 256*256*3; i++){ if((i+2)%3 == 0) imageArray[i] = 255; else imageArray[i] = 0; }
//Our image for the mipmap with pair level and red color unsigned char imageArray2[256*256*3]; for(int i = 0; i < 256*256*3; i++){ if(i%3 == 0) imageArray2[i] = 255; else imageArray2[i] = 0; }
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 8);
//All mipmap levels defined by hand glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, imageArray); glTexImage2D(GL_TEXTURE_2D, 1, GL_RGB, 128, 128, 0, GL_RGB, GL_UNSIGNED_BYTE, imageArray2); glTexImage2D(GL_TEXTURE_2D, 2, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, imageArray); glTexImage2D(GL_TEXTURE_2D, 3, GL_RGB, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, imageArray2); glTexImage2D(GL_TEXTURE_2D, 4, GL_RGB, 16, 16, 0, GL_RGB, GL_UNSIGNED_BYTE, imageArray); glTexImage2D(GL_TEXTURE_2D, 5, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, imageArray2); glTexImage2D(GL_TEXTURE_2D, 6, GL_RGB, 4, 4, 0, GL_RGB, GL_UNSIGNED_BYTE, imageArray); glTexImage2D(GL_TEXTURE_2D, 7, GL_RGB, 2, 2

1
好像只有一个纹理坐标集,而不是三个。 - pleluron
@pleluron 你是对的,那是我的错误。请将其发布为答案。 - Adrian
1个回答

3
你的mipmap设置看起来没问题。但我发现你有这样一个代码块:
  GLfloat vertices[] = {
        -1.0f, -1.0f, 1.0f, -1.0f, 0.0f, 1.0f,  //vertex coordinates
        0.0f, 0.0f                              //Triangle's texture coordinates
  };

看起来你还有2个顶点缺少UV信息。此外,除非你希望完全控制过滤技术等,否则不要费劲手动生成mipmap。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接