OpenGL绘图质量有问题,绘制出的颜色跟文件里的颜色不一样
原来用OpenGL做程序时一直觉得颜色不对,就做了个测试程序: 我载入一个bmp文件,以贴图的方式按1:1的大小贴在屏幕上,
我把混合,光照,纹理过滤,反样全都关闭了,但是仔细看屏幕上的图片与bmp里的图片对比,发现有细微差别,就是有些颜色不对(这些细微差别不仔细看看不出来)
为什么OpenGL绘制的图片颜色会变掉?我不想让它变,我想要bmp里的图片一模一样的颜色,请问我该怎么处理???
(后来我用Win32 API来创建OpenGL,发现还是无法解决这个问题)
附效果图:
" border="0" />
测试代码:
#include <gl\glut.h>
#include <gl\glaux.h>
#pragma comment(lib, "glaux.lib")
// 读取bmp文件
GLuint LoadTexture(LPCTSTR path)
{
GLuint texture = 0;
AUX_RGBImageRec *bmp = auxDIBImageLoad(path);
if(!bmp) return 0;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, 3, bmp->sizeX, bmp->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, bmp->data);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
free(bmp->data);
free(bmp);
return texture;
}
void OnInit()
{
LoadTexture("buttons.bmp");
glDisable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glDisable(GL_POINT_SMOOTH);
glDisable(GL_CULL_FACE);
glEnable(GL_TEXTURE_2D);
}
// 绘制
void OnDisplay()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(0, 0);
glTexCoord2f(1, 0);
glVertex2f(1024, 0);
glTexCoord2f(1, 1);
glVertex2f(1024, 128);
glTexCoord2f(0, 1);
glVertex2f(0, 128);
glEnd();
glutSwapBuffers();
}
// 当窗口大小改变时
void OnReshape(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0f, width, 0.0f, height); // 创建2D视矩阵
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("测试程序");
OnInit(); // 初始化
glutDisplayFunc(OnDisplay);
glutReshapeFunc(OnReshape);
glutIdleFunc(OnDisplay);
glutMainLoop();
return 0;
}
#include <gl\glaux.h>
#pragma comment(lib, "glaux.lib")
// 读取bmp文件
GLuint LoadTexture(LPCTSTR path)
{
GLuint texture = 0;
AUX_RGBImageRec *bmp = auxDIBImageLoad(path);
if(!bmp) return 0;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, 3, bmp->sizeX, bmp->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, bmp->data);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
free(bmp->data);
free(bmp);
return texture;
}
void OnInit()
{
LoadTexture("buttons.bmp");
glDisable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glDisable(GL_POINT_SMOOTH);
glDisable(GL_CULL_FACE);
glEnable(GL_TEXTURE_2D);
}
// 绘制
void OnDisplay()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(0, 0);
glTexCoord2f(1, 0);
glVertex2f(1024, 0);
glTexCoord2f(1, 1);
glVertex2f(1024, 128);
glTexCoord2f(0, 1);
glVertex2f(0, 128);
glEnd();
glutSwapBuffers();
}
// 当窗口大小改变时
void OnReshape(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0f, width, 0.0f, height); // 创建2D视矩阵
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("测试程序");
OnInit(); // 初始化
glutDisplayFunc(OnDisplay);
glutReshapeFunc(OnReshape);
glutIdleFunc(OnDisplay);
glutMainLoop();
return 0;
}