最近使用glcolortable()函数,运行是出现异常,网上查找也有人遇到类似问题,但是都没有给出解决方法。
#include "Glee.h"
#include <GL/glut.h>
#include<stdio.h>
#include <stdlib.h>
GLubyte pixels[64][64][3];
void init(void)
{
int i,j,c;
GLubyte colorTable[256][3];
//pixels = readImage("leeds.bin", &width, &height);
for (i = 0; i < 64; i++) {
for (j = 0; j < 64; j++) {
c = ((((i&0x8)==0)||((j&0x8))==0))*255;
pixels[i][j][0] = (GLubyte) c;
pixels[i][j][1] = (GLubyte) c;
pixels[i][j][2] = (GLubyte) c;
}
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glClearColor(0.0, 0.0, 0.0, 0.0);
/* Set up an inverse color table */
for ( i = 0; i < 256; ++i ) {
colorTable[i][0] = 255 - i;
colorTable[i][1] = 255 - i;
colorTable[i][2] = 255 - i;
}
glColorTable(GL_COLOR_TABLE, GL_RGB, 256, GL_RGB, GL_UNSIGNED_BYTE,colorTable);// 出错位置
glEnable(GL_COLOR_TABLE);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glRasterPos2i( 1, 1 );
glDrawPixels(64, 64, GL_RGB, GL_UNSIGNED_BYTE, pixels );
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho (0, w, 0, h, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
在实验了各种方法都没解决问题。
红宝书中230页有样一句段话:
尽管图像处理自己一直是一个OpenGL扩展,但其功能在OpenGL 3.0中废弃了,并且从OpenGL3.1规范中删除了。
开始怀疑是opengl版本问题。于是测试版本
const char* verstr = (const char*)glGetString( GL_VERSION );
出错机器是opengl 2.1,没有错误的机器是opengl3.1版本。
同样glGenvertexArrays在2.1版本中也出错,在3.1版本中没有问题。也有人这么解决
http://blog.csdn.net/niexiao2008/article/details/8898171
,在glewInit()在添加
glewExperimental = GL_TRUE;
但我在2.1版本中测试没有通过。
出错机器的版本号2.1.0-Build 8.15.10.1840 没有错误的版本号3.1 NVIDIA 188.98,所以应该显卡对opengl版本支持不一样的。
主要原因见: