多重采样 多重采样的技巧使用额外的颜色,深度和模板信息,对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;
}
|