将位图文件放在此代码的同一目录中。将此文件命名为“a.bmp”
代码=>
#include<GL/glut.h>
#include<stdio.h>
#include<stdlib.h>
typedef struct __attribute__((__packed__)) {
unsigned short type;
unsigned long size;
unsigned short reserved1;
unsigned short reserved2;
unsigned long offsetbits;
} BITMAPFILEHEADER1;
typedef struct __attribute__((__packed__)) {
unsigned long size;
unsigned long width;
unsigned long height;
unsigned short planes;
unsigned short bitcount;
unsigned long compression;
unsigned long sizeimage;
long xpelspermeter;
long ypelspermeter;
unsigned long colorsused;
unsigned long colorsimportant;
} BITMAPINFOHEADER1;
typedef struct {
unsigned char blue;
unsigned char green;
unsigned char red;
} SINGLE_PIXEL1;
void display()
{
FILE *fp;
unsigned char p;
int x=0,y=0,c=0;
float r,g,b;
BITMAPFILEHEADER1 bitmp;
BITMAPINFOHEADER1 bitm;
glClearColor(1.0,1.0,1.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
fp = fopen("a.bmp","rb");//Filename is given
fread(&bitmp,14,1,fp);
fread(&bitm,40,1,fp);
gluOrtho2D(0.0,bitm.width,0.0,bitm.height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0,0,bitm.width,bitm.height);
glBegin(GL_POINTS);
int cnt=0;
int bi,gi,ri;
unsigned char tp1,tp2,tp3;
while(!feof(fp))
{
fread(&p,1,1,fp);
//b = p/255.0;
tp1=p;
fread(&p,1,1,fp);
//g = p/255.0;
tp2=p;
fread(&p,1,1,fp);
//r = p/255.0;
tp3=p;
//glColor3f(r,g,b);
glColor3ub(tp1,tp2,tp3);
glVertex2i(x++,y);
if(x == bitm.width)
{
x = 0;
y++;
}
}
glEnd();
glFlush();
fclose(fp);
}
int main(int argc, char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(800,800);
glutInitWindowPosition(100,150);
glutCreateWindow("BITMAP");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
将代码放在名为“bit.cpp”的文件中。 然后编译这个“bit.cpp”文件:
g++ bit.cpp -o bit -lglut -lGL -lGLU
并运行:
./bit
您将在过剩窗口中看到黑白(灰色)图像。
我如何绘制彩色像素? glColor3ub(tp1,tp2,tp3);
这应该为顶点设置颜色。不应该吗?但它不是绘制彩色像素,为什么?
如何在过剩窗口中获得彩色图像?
任何答案都将受到高度赞赏。
提前致谢。