OpenGL学习十一:抗锯齿

启用抗锯齿
还是以glEnable来启用抗锯齿,可以根据不同图形进行处理
1. GL_POINT_SMOOTH 点
2. GL_LINE_SMOOTH 线
3. GL_POLYGON_SMOOTH 多边形

抗锯齿质量
效果越好,那么计算机速度就越慢
glHint用于对点,线,多边形的抗锯齿程度进行设置
1. GL_DONT_CARE 放弃,应该是系统默认吧
2. GL_FASTEST 速度优先
3. GL_NICEST 图形显示质量优先
点抗锯齿(RGBA模式)
glPointSize(10);
glEnable (GL_POINT_SMOOTH);//启动点抗锯齿
glHint (GL_POINT_SMOOTH, GL_NICEST); //图像渲染质量

左图1:不启动抗锯齿
右图2:启动抗锯齿
  OpenGL学习十一:抗锯齿 OpenGL学习十一:抗锯齿
线抗锯齿

glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable (GL_LINE_SMOOTH);
glHint (GL_LINE_SMOOTH, GL_NICEST);
glLineWidth (10);

右图1:不启动抗锯齿
右图2:启动抗锯齿,不启用混合
右图2:启动抗锯齿,启用混合
OpenGL学习十一:抗锯齿OpenGL学习十一:抗锯齿  

多重采样
多重采样的技巧使用额外的颜色,深度和模板信息,对OPENGL图元进行抗锯齿处理
1.创建一个多重采样的窗口
   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB |
GLUT_MULTISAMPLE
);
2.查询状态变量GL_SAMPLE_BUFFERS
   glGetIntegerv (GL_SAMPLE_BUFFERS, buf);
3.查询状态变量GL_SAMPLE
  glGetIntegerv (GL_SAMPLES, sbuf);
如果buf=1 sbuf>1 就可以使用多重采样
4.启动多重采样
glEnable(GLUT_MULTISAMPLE)

说明:多边形抗锯齿做好采用多重采样进行抗锯齿

 

#include "header.h"
static float rotAngle = 0.;


void init(void)
{
   GLfloat values[2];
   glGetFloatv (GL_LINE_WIDTH_GRANULARITY, values);
   printf ("GL_LINE_WIDTH_GRANULARITY value is %3.1f\n", values[0]);

   glGetFloatv (GL_LINE_WIDTH_RANGE, values);
   printf ("GL_LINE_WIDTH_RANGE values are %3.1f %3.1f\n",
      values[0], values[1]);

   glEnable (GL_LINE_SMOOTH);
   glEnable (GL_BLEND);
   glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   glHint (GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
   glLineWidth (1.5);

   glEnable (GL_POINT_SMOOTH);
   glHint (GL_POINT_SMOOTH_HINT, GL_NICEST);
   glPointSize(100);
   glClearColor(0.0, 0.0, 0.0, 0.0);
}


void display(void)
{
   glClear(GL_COLOR_BUFFER_BIT);


   glPushMatrix();
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(0,600,0,600,-10,10);
   glColor3f (1.0, 1.0, 1.0);
   glBegin (GL_LINES);
	 glVertex2f (0, 300);
     glVertex2f (600, 300);
   glEnd ();
   glBegin (GL_LINES);
     glVertex2f (300, 600);
     glVertex2f (300, 0);
   glEnd ();
   glPopMatrix();

   glEnable (GL_LINE_SMOOTH);
   glEnable (GL_BLEND);

   glPushMatrix();
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   
   glOrtho(-150,450,-450,150,-10,10);
   glPushMatrix();
     glRotatef(rotAngle, 0.0, 0.0, 1);
     glColor3f (0.0, 1.0, 0.0);
     glBegin (GL_LINES);
      glVertex2f (-100, -100);
      glVertex2f (100, 100);
   glEnd ();
   glPopMatrix();
   glPushMatrix();
	 glRotatef(-rotAngle, 0.0, 0.0, 1);
     glColor3f (0.0, 0.0, 1.0);
     glBegin (GL_LINES);
       glVertex2f (-100,100);
       glVertex2f (100, -100);
     glEnd ();
   glPopMatrix();
   glPopMatrix();


   glDisable(GL_LINE_SMOOTH);
   glDisable (GL_BLEND);
 
   glPushMatrix();
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();

   glOrtho(-450,150,-450,150,-10,10);
   glPushMatrix();
     glRotatef(rotAngle, 0.0, 0.0, 1);
     glColor3f (0.0, 1.0, 0.0);
     glBegin (GL_LINES);
       glVertex2f (-100, -100);
       glVertex2f (100, 100);
     glEnd ();
   glPopMatrix();
   glPushMatrix();
     glRotatef(-rotAngle, 0.0, 0.0, 1);
     glColor3f (0.0, 0.0, 1.0);
     glBegin (GL_LINES);
       glVertex2f (-100,100);
       glVertex2f (100, -100);
     glEnd ();
   glPopMatrix();
   glPopMatrix();

   glEnable (GL_POINT_SMOOTH);
   glPushMatrix();
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();

   glOrtho(-450,150,-150,450,-10,10);
   glColor3f (0.0,0.0, 1.0);
   glBegin (GL_POINTS);
     glVertex2f(0,0);
   glEnd ();
   glPopMatrix();

   glDisable (GL_POINT_SMOOTH);
   glPushMatrix();
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(-150,450,-150,450,-10,10);
   glColor3f (0.0, 1.0, 0.0);
   glBegin (GL_POINTS);
     glVertex2f(0,0);
   glEnd ();
   glPopMatrix();



   glFlush();
}

void reshape(int w, int h)
{
   glViewport(0, 0, w, h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(0,600,0,600,-10,10);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
}

void keyboard(unsigned char key, int x, int y)
{
   switch (key) {
      case 'r':
      case 'R':
         rotAngle += 20.;
         if (rotAngle >= 360.) rotAngle = 0.;
         glutPostRedisplay();	
         break;
      case 27:
         exit(0);
         break;
      default:
         break;
    }
}


int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
   glutInitWindowSize (600, 600);
   glutCreateWindow (argv[0]);
   init();
   glutReshapeFunc (reshape);
   glutKeyboardFunc (keyboard);
   glutDisplayFunc (display);
   glutMainLoop();
   return 0;
}

 

你可能感兴趣的:(OpenGL)