下载本文示例代码
本节讲解像素操作。 例14:本例在窗口绘制一个三角形,然后利用glCopyPixel函数拷贝了五次该图形,并将这五个三角形放置在窗口的不同位置。
#include
#include /*设置背景色*/void myInit(void){ glClearColor(1.0,1.0,1.0,1.0);}/*绘制彩色三角形*/void triangle(void){ glBegin(GL_TRIANGLES); glColor3f(1.0,1.0,0.0); glVertex2f(10.0,10.0); glColor3f(0.0,0.3,1.0); glVertex2f(20.0,30.0); glColor3f(1.0,0.0,0.0); glVertex2f(30.0,10.0); glEnd();}void myDisplay(){ int i; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); glTranslatef(10.0,100.0,0.0); triangle(); glPopMatrix(); glPushMatrix(); for(i = 0;i < 5;i ) { glRasterPos2i(20 i*30,10 i*5);//为像素操作指定光栅位置 glCopyPixels(50,200,500,500,GL_COLOR);//在缓存中拷贝像素 } glPopMatrix(); glFlush();}void myReshape(int w,int h){ glViewport(0,0,(GLsizei)w,(GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if( w <= h) gluOrtho2D(0.0,150,0.0,150.0*(GLfloat)h/(GLfloat)w); else gluOrtho2D(0.0,150*(GLfloat)w/(GLfloat)h,0.0,150.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity();}int main(int argc,char ** argv){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH); glutInitWindowSize(500,400); glutInitWindowPosition(100,100); glutCreateWindow(" copy "); myInit(); glutReshapeFunc(myReshape); glutDisplayFunc(myDisplay); glutMainLoop(); return 0;} ·glRasterPos在像素操作中指定光栅位置。同样后面的数字表示坐标维数,2表示坐标为x、y,3表示、,y、z。数字后面的字母表示参数类型。最后带V表示参数为指针。 ·void glCopyPixels(GLint x, Glint y, GLsizei width, GLsizei height, GLenum type)函数将像素拷贝到缓存中。 x,y指定被拷贝像素的右下角坐标。 width,heigth指定被拷贝像素矩形区域的大小。 type指定拷贝数值的类型。值为颜色、深度或膜板值。本例中为颜色。 例15:此例在窗口左下角写单词FILE。
#include #include /*字母F*/GLubyte f_rasters[12] = {0xc0,0xc0,0xc0,0xc0,0xc0,0xfc,0xfc,0xc0,0xc0,0xc0,0xff,0xff};/*字母I*/GLubyte i_rasters[12] = {0xff,0xff,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0xff,0xff};/*字母L*/GLubyte l_rasters[12] = {0xff,0xff,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0};/*字母E*/GLubyte e_rasters[12] = {0xff,0xff,0xc0,0xc0,0xc0,0xff,0xff,0xc0,0xc0,0xc0,0xff,0xff};void myInit(void){ glPixelStorei(GL_UNPACK_ALIGNMENT,1);//设置像素存储模式 glClearColor(1.0,1.0,1.0,1.0);//设置背景为白色}void myDisplay(){ glClear(GL_COLOR_BUFFER_BIT); glColor3f(0.0,0.0,0.0);//设置像素颜色为黑色 glRasterPos2i(20,20);//为像素指定位置 /*绘制位图*/ glBitmap(8,12,0.0,0.0,14.0,0.0,f_rasters); glBitmap(8,12,0.0,0.0,14.0,0.0,i_rasters); glBitmap(8,12,0.0,0.0,14.0,0.0,l_rasters); glBitmap(8,12,0.0,0.0,14.0,0.0,e_rasters); glFlush();}void myReshape(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); glLoadIdentity();}int main(int argc,char ** argv){ /*初始化*/ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(500,400); glutInitWindowPosition(100,100); /*创建窗口*/ glutCreateWindow(" BitMap"); /*绘制图形*/ myInit(); glutReshapeFunc(myReshape); glutDisplayFunc(myDisplay); glutMainLoop();//进入GLUT事件处理循环 return 0;} ·glBitmap(GLsizei width,GLsizei height,GLfloat xorig,GLfloat yorig,GLfloat xmove,GLfloat ymove,const GLubyte *bitmap)函数绘制一个位图。 width,height分别指定位图图像的宽度和高度。 xorig,yorig位图图像的原点位置。原点为位图的左下角。向右和向上为坐标轴的正向。 xmove,ymove绘制完位图后x,y相对于当前光栅的位移。 bitmap位图图像的地址。 你可以改变此函数的参数,观察不同大小不同位置的效果。
本节讲解像素操作。 例14:本例在窗口绘制一个三角形,然后利用glCopyPixel函数拷贝了五次该图形,并将这五个三角形放置在窗口的不同位置。
#include #include /*设置背景色*/void myInit(void){ glClearColor(1.0,1.0,1.0,1.0);}/*绘制彩色三角形*/void triangle(void){ glBegin(GL_TRIANGLES); glColor3f(1.0,1.0,0.0); glVertex2f(10.0,10.0); glColor3f(0.0,0.3,1.0); glVertex2f(20.0,30.0); glColor3f(1.0,0.0,0.0); glVertex2f(30.0,10.0); glEnd();}void myDisplay(){ int i; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); glTranslatef(10.0,100.0,0.0); triangle(); glPopMatrix(); glPushMatrix(); for(i = 0;i < 5;i ) { glRasterPos2i(20 i*30,10 i*5);//为像素操作指定光栅位置 glCopyPixels(50,200,500,500,GL_COLOR);//在缓存中拷贝像素 } glPopMatrix(); glFlush();}void myReshape(int w,int h){ glViewport(0,0,(GLsizei)w,(GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if( w <= h) gluOrtho2D(0.0,150,0.0,150.0*(GLfloat)h/(GLfloat)w); else gluOrtho2D(0.0,150*(GLfloat)w/(GLfloat)h,0.0,150.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity();}int main(int argc,char ** argv){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH); glutInitWindowSize(500,400); glutInitWindowPosition(100,100); glutCreateWindow(" copy "); myInit(); glutReshapeFunc(myReshape); glutDisplayFunc(myDisplay); glutMainLoop(); return 0;} ·glRasterPos在像素操作中指定光栅位置。同样后面的数字表示坐标维数,2表示坐标为x、y,3表示、,y、z。数字后面的字母表示参数类型。最后带V表示参数为指针。 ·void glCopyPixels(GLint x, Glint y, GLsizei width, GLsizei height, GLenum type)函数将像素拷贝到缓存中。 x,y指定被拷贝像素的右下角坐标。 width,heigth指定被拷贝像素矩形区域的大小。 type指定拷贝数值的类型。值为颜色、深度或膜板值。本例中为颜色。 例15:此例在窗口左下角写单词FILE。
#include #include /*字母F*/GLubyte f_rasters[12] = {0xc0,0xc0,0xc0,0xc0,0xc0,0xfc,0xfc,0xc0,0xc0,0xc0,0xff,0xff};/*字母I*/GLubyte i_rasters[12] = {0xff,0xff,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0xff,0xff};/*字母L*/GLubyte l_rasters[12] = {0xff,0xff,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0};/*字母E*/GLubyte e_rasters[12] = {0xff,0xff,0xc0,0xc0,0xc0,0xff,0xff,0xc0,0xc0,0xc0,0xff,0xff};void myInit(void){ glPixelStorei(GL_UNPACK_ALIGNMENT,1);//设置像素存储模式 glClearColor(1.0,1.0,1.0,1.0);//设置背景为白色}void myDisplay(){ glClear(GL_COLOR_BUFFER_BIT); glColor3f(0.0,0.0,0.0);//设置像素颜色为黑色 glRasterPos2i(20,20);//为像素指定位置 /*绘制位图*/ glBitmap(8,12,0.0,0.0,14.0,0.0,f_rasters); glBitmap(8,12,0.0,0.0,14.0,0.0,i_rasters); glBitmap(8,12,0.0,0.0,14.0,0.0,l_rasters); glBitmap(8,12,0.0,0.0,14.0,0.0,e_rasters); glFlush();}void myReshape(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); glLoadIdentity();}int main(int argc,char ** argv){ /*初始化*/ glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(500,400); glutInitWindowPosition(100,100); /*创建窗口*/ glutCreateWindow(" BitMap"); /*绘制图形*/ myInit(); glutReshapeFunc(myReshape); glutDisplayFunc(myDisplay); glutMainLoop();//进入GLUT事件处理循环 return 0;} ·glBitmap(GLsizei width,GLsizei height,GLfloat xorig,GLfloat yorig,GLfloat xmove,GLfloat ymove,const GLubyte *bitmap)函数绘制一个位图。 width,height分别指定位图图像的宽度和高度。 xorig,yorig位图图像的原点位置。原点为位图的左下角。向右和向上为坐标轴的正向。 xmove,ymove绘制完位图后x,y相对于当前光栅的位移。 bitmap位图图像的地址。 你可以改变此函数的参数,观察不同大小不同位置的效果。
下载本文示例代码
OpenGL编程轻松入门之像素操作OpenGL编程轻松入门之像素操作OpenGL编程轻松入门之像素操作OpenGL编程轻松入门之像素操作OpenGL编程轻松入门之像素操作OpenGL编程轻松入门之像素操作OpenGL编程轻松入门之像素操作OpenGL编程轻松入门之像素操作OpenGL编程轻松入门之像素操作OpenGL编程轻松入门之像素操作OpenGL编程轻松入门之像素操作OpenGL编程轻松入门之像素操作OpenGL编程轻松入门之像素操作OpenGL编程轻松入门之像素操作OpenGL编程轻松入门之像素操作
阅读(188) | 评论(0) | 转发(0) |