OpenGL編程輕鬆入門之像素操作

來源:互聯網
上載者:User

本節講解像素操作。

例14:本例在視窗繪製一個三角形,然後利用glCopyPixel函數拷貝了五次該圖形,並將這五個三角形放置在視窗的不同位置。

#include <stdlib.h>
#include <GL/glut.h>
/*設定背景色*/
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 <stdlib.h>
#include <GL/glut.h>

/*字母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位元影像映像的地址。

  你可以改變此函數的參數,觀察不同大小不同位置的效果。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.