OpenGL 渲染纹理全白
OpenGL renders texture all white
我正在尝试将.png图像渲染为纹理。但是,渲染的只是一个白色正方形。
我给我的纹理一个叫做texID的唯一int ID,将像素数据读入缓冲区"图像"(在.h文件中声明)。我加载我的像素缓冲区,做我所有的OpenGL工作,并将该像素缓冲区绑定到OpenGL的纹理上。 然后我使用 glDrawElements 绘制它。
此外,当调用构造器时,我以 32x32 的大小初始化纹理,因此我怀疑它与两个大小的幂问题有关。
任何人都可以看到我的 OpenGL GL_TEXTURE_2D设置中的任何错误,这些错误可能会给我一个白色方块。
#include "Texture.h"
Texture::Texture(int width, int height, string filename)
{
const char* fnPtr = filename.c_str(); //our image loader accepts a ptr to a char, not a string
printf(fnPtr);
w = width; //give our texture a width and height, the reason that we need to pass in the width and height values manually
h = height;//UPDATE, these MUST be P.O.T.
unsigned error = lodepng::decode(image,w,h,fnPtr);//lodepng's decode function will load the pixel data into image vector
//display any errors with the texture
if(error)
{
cout << "ndecoder error " << error << ": " << lodepng_error_text(error) <<endl;
}
for(int i = 0; i<image.size(); i++)
{
printf("%i,", image.at(i));
}
printf("nImage size is %i", image.size());
//image now contains our pixeldata. All ready for OpenGL to do its thing
//let's get this texture up in the video memory
texGLInit();
}
void Texture::texGLInit()
{
//WHERE YOU LEFT OFF: glGenTextures isn't assigning an ID to textures. it stays at zero the whole time
//i believe this is why it's been rendering white
glGenTextures(1, &textures);
printf("ntexture = %u", textures);
glBindTexture(GL_TEXTURE_2D, textures);//evrything we're about to do is about this texture
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
//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_MAG_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
//glDisable(GL_COLOR_MATERIAL);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,w,h,0, GL_RGBA, GL_UNSIGNED_BYTE, &image);
//we COULD free the image vectors memory right about now.
}
void Texture::draw(point centerPoint, point dimensions)
{
glEnable(GL_TEXTURE_2D);
printf("nDrawing block at (%f, %f)",centerPoint.x, centerPoint.y);
glBindTexture(GL_TEXTURE_2D, textures);//bind the texture
//create a quick vertex array for the primitive we're going to bind the texture to
printf("TexID = %u",textures);
GLfloat vArray[8] =
{
centerPoint.x-(dimensions.x/2), centerPoint.y-(dimensions.y/2),//bottom left i0
centerPoint.x-(dimensions.x/2), centerPoint.y+(dimensions.y/2),//top left i1
centerPoint.x+(dimensions.x/2), centerPoint.y+(dimensions.y/2),//top right i2
centerPoint.x+(dimensions.x/2), centerPoint.y-(dimensions.y/2)//bottom right i3
};
//create a quick texture array (we COULD create this on the heap rather than creating/destoying every cycle)
GLfloat tArray[8] =
{
0.0f,0.0f, //0
0.0f,1.0f, //1
1.0f,1.0f, //2
1.0f,0.0f //3
};
//and finally.. the index array...remember, we draw in triangles....(and we'll go CW)
GLubyte iArray[6] =
{
0,1,2,
0,2,3
};
//Activate arrays
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
//Give openGL a pointer to our vArray and tArray
glVertexPointer(2, GL_FLOAT, 0, &vArray[0]);
glTexCoordPointer(2, GL_FLOAT, 0, &tArray[0]);
//Draw it all
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, &iArray[0]);
//glDrawArrays(GL_TRIANGLES,0,6);
//Disable the vertex arrays
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_TEXTURE_2D);
//done!
/*glBegin(GL_QUADS);
glTexCoord2f(0.0f,0.0f);
glVertex2f(centerPoint.x-(dimensions.x/2), centerPoint.y-(dimensions.y/2));
glTexCoord2f(0.0f,1.0f);
glVertex2f(centerPoint.x-(dimensions.x/2), centerPoint.y+(dimensions.y/2));
glTexCoord2f(1.0f,1.0f);
glVertex2f(centerPoint.x+(dimensions.x/2), centerPoint.y+(dimensions.y/2));
glTexCoord2f(1.0f,0.0f);
glVertex2f(centerPoint.x+(dimensions.x/2), centerPoint.y-(dimensions.y/2));
glEnd();*/
}
Texture::Texture(void)
{
}
Texture::~Texture(void)
{
}
我还将包括主类的 init,在此之前我做了更多的 OGL 设置。
void init(void)
{
printf("n......Hello Guy. n....nInitilising");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,XSize,0,YSize);
glEnable(GL_TEXTURE_2D);
myBlock = new Block(0,0,offset);
glClearColor(0,0.4,0.7,1);
glLineWidth(2); // Width of the drawing line
glMatrixMode(GL_MODELVIEW);
glDisable(GL_DEPTH_TEST);
printf("nInitialisation Complete");
}
更新:添加我第一次设置 OpenGL 窗口的主函数。
int main(int argc, char** argv)
{
glutInit(&argc, argv); // GLUT Initialization
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE); // Initializing the Display mode
glutInitWindowSize(800,600); // Define the window size
glutCreateWindow("Gem Miners"); // Create the window, with caption.
printf("n========== McLeanTech Systems =========nBecoming Sentientn...n...n....nKILLnHUMANnRACE n");
init(); // All OpenGL initialization
//-- Callback functions ---------------------
glutDisplayFunc(display);
glutKeyboardFunc(mykey);
glutSpecialFunc(processSpecialKeys);
glutSpecialUpFunc(processSpecialUpKeys);
//glutMouseFunc(mymouse);
glutMainLoop(); // Loop waiting for event
}
以下是纹理变为白色的常用清单:
-
OpenGL 上下文已创建并在加载纹理时绑定到当前线程?
-
使用
glGenTextures
分配的纹理 ID ? -
是否允许将参数
format
和internal format
glTex[Sub]Image…
有效的 OpenGL 令牌作为此函数的输入? -
是否正在使用 mipmap?
-
是:提供所有 mipmap 图层 – 最佳设置
glTexParameteri
GL_TEXTURE_BASE_LEVEL和GL_TEXTURE_MAX_LEVEL,以及GL_TEXTURE_MIN_LOD和GL_TEXTURE_MAX_LOG。 -
否:通过将
glTexParameteri
GL_TEXTURE_MIN_FILTER设置为 GL_NEAREST 或 GL_LINEAR 来关闭 mipmap 过滤。
-
相关文章:
- OpenGL大的3D纹理(>2GB)非常慢
- OpenGL将纹理四边形渲染为(0,0)
- OpenGL 4.3 错误地将第 4 个纹理坐标映射到与第 3 个纹理坐标相同的位置
- Qt OpenGL 渲染到纹理性能问题
- 使用 C++在 OpenGL 中对 3D 多边形进行纹理处理
- 无法获取要在 OpenGL ES2 中显示的 RGB 纹理
- 在 OpenGL 中加载纹理C++
- 添加新纹理OpenGL时,模型消失了
- 初始化多个纹理 OpenGL、SDL C++ 时出现问题
- 将多个纹理 OpenGL 绑定到不同的四边形
- 如何加载纹理Opengl
- 读写一个纹理(OpenGL)
- 渲染时加载纹理 (OpenGL)
- 如何绑定纹理openGL的一部分
- 将两个图像堆栈的非电源加载到 3D 纹理 OpenGL C++
- 我的精灵在纹理opengl之间切换
- 使用深度纹理OpenGL/GLSL的深度遮挡(3.3/330)
- 加载多纹理opengl 2.0
- 将位置写入纹理OpenGL/GLSL
- 为纹理OpenGL C++加载多个.bmp文件时出现问题