问题描述
我遇到的问题与 OpenGl太黑了答案对我不起作用.我试图显示图像,这要归功于将表面转换为纹理,结果太暗了:
I've quite the same problem than this OpenGl goes too dark but the answer doesn't work for me. I'm trying to display a image thanks to a surface converted to a texture and the result is too damn dark:
原文:
openGL之后
左边是原稿,右边是OpenGl img.
On the left is the original, on the right the OpenGl img.
这是我的代码:
void TexturedRect::draw(int scroll){
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D, _texture);
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS); //Begining the cube's drawing
int x = this->getX();
int y = this->getY();
int w = this->getWidth();
int h = this->getHeight();
int z = this->getZ();
/*
(0,0) ------ (1,0)
| |
| |
(0,1) ------ (1,1)
*/
glTexCoord3i(0, 0, 1);glVertex3i(x + scroll, y, z);
glTexCoord3i(_tv, 0, 1);glVertex3i(x + w * _tv + scroll, y, z);
glTexCoord3i(_tv, _tu, 1);glVertex3i(x + w * _tv + scroll, y + h * _tu, z);
glTexCoord3i(0, _tu, 1);glVertex3i(x + scroll, y + h * _tu, z);
glEnd();
glDisable(GL_TEXTURE_2D);
}
void TexturedRect::createTextureFromSurface()
{
SDL_Surface * surface = IMG_Load(filename.toStdString().c_str());
// get the number of channels in the SDL surface
GLint nbOfColors = surface->format->BytesPerPixel;
GLenum textureFormat = 0;
switch (nbOfColors) {
case 1:
textureFormat = GL_ALPHA;
break;
case 3: // no alpha channel
if (surface->format->Rmask == 0x000000ff)
textureFormat = GL_RGB;
else
textureFormat = GL_BGR;
break;
case 4: // contains an alpha channel
if (surface->format->Rmask == 0x000000ff)
textureFormat = GL_RGBA;
else
textureFormat = GL_BGRA;
break;
default:
qDebug() << "Warning: the image is not truecolor...";
break;
}
glEnable( GL_TEXTURE_2D );
// Have OpenGL generate a texture object handle for us
glGenTextures( 1, &_texture );
// Bind the texture object
glBindTexture( GL_TEXTURE_2D, _texture );
// Edit the texture object's image data using the information SDL_Surface gives us
glTexImage2D( GL_TEXTURE_2D, 0, nbOfColors, surface->w, surface->h, 0,
textureFormat, GL_UNSIGNED_BYTE, surface->pixels );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
推荐答案
您可能在代码中的其他位置设置了一些状态,当您希望禁用此状态以绘制此四边形时,这些状态仍处于启用状态.
You probably have some state set elsewhere in your code that is still enabled when you want it disabled for drawing this quad.
尝试将以下内容放在glBindTexture(GL_TEXTURE_2D,_texture)之后;在您的绘制代码中(重要的是,它是在draw方法中完成的,而不是在createTextureFromSurface方法中完成的):
Try putting the following after glBindTexture(GL_TEXTURE_2D, _texture); in your draw code (it's important that it's done in the draw method and not the createTextureFromSurface method):
glDisable(GL_BLEND);
glDisable(GL_LIGHTING);
glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE )
glColor3f(1.0f, 1.0f, 1.0f);
如果这行得通,您可以一次将它们注释掉,以找出导致您的问题的状态.在绘制需要此四边形的对象时,需要对其进行禁用.
If this works you can comment them out one at a time to figure out which state was causing your problem. Any state you disable for drawing this quad will need re-enabled when drawing the object that required it.
这篇关于OpenGL纹理形成SDLSurface太暗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!