C++:从 openGL 中去除莫尔效应

标签 c++ opengl shader glut antialiasing

我画的图案有详细的像素,它们经常面对波纹效果。我不擅长着色,我不确定这个问题是否会被着色器解决。我还没有找到任何基本的、可理解的和完整的着色器示例。大多数教程网站从中间开始一个程序,省略了包含的头文件!

这是我的代码的 MWE。是否有可能减轻或消除其中的莫尔效应?

#include <cmath>
#include <vector>

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

const int w=640,h=480;
float cam_angle=0.0f;

void init()
{
    GLfloat LightAmbient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
    GLfloat LightDiffuse[] = { 0.5f, 0.5f, 0.5f, 1.0f };
    GLfloat LightPosition[] = { 5.0f, 5.0f, -10.0f, 1.0f };

    glClearColor(0.0, 0.0, 0.0, 0.0);
    glShadeModel(GL_SMOOTH);
    glEnable(GL_DEPTH_TEST);
    glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
    glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
    glLightfv(GL_LIGHT1, GL_POSITION, LightPosition);
    glEnable(GL_LIGHT1);
}

void onDisplay()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective (90, float(w)/float(h), 0.01, 10000);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    const double pi=3.1415926;
    double cam_x=2.0*cos(pi/4.0)*cos(cam_angle);;
    double cam_y=2.0*sin(pi/4.0)*cos(cam_angle);;
    double cam_z=2.0*sin(cam_angle);
    gluLookAt(cam_x, cam_y, cam_z,  0, 0, 0, 0, 0, 1);

    struct Point3D
    {
        double x, y, z;
        unsigned char r, g, b, a=255;
    };

    for(double r=0.5;r<=1.0;r+=0.03)
    {
        std::vector<Point3D> points;
        for(int i=0;i<1000;i++)
        {
            double theta=double(i)/1000.0*pi*2.0;
            Point3D p;
            p.x=r*sin(theta);
            p.y=r*cos(theta);
            p.z=r;
            p.r=128;
            p.g=200;
            p.b=50;
            points.push_back(p);
        }
    // draw
        glPushMatrix();
        glColor3ub(255,255,255);
        glEnableClientState( GL_VERTEX_ARRAY );
        glEnableClientState( GL_COLOR_ARRAY );
        glVertexPointer(3, GL_DOUBLE, sizeof(Point3D), &points[0].x );
        glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(Point3D), &points[0].r );
        // glPointSize( 3.0 );
        glLineWidth(2.0);
        glDrawArrays( GL_LINE_STRIP, 0, int(points.size()) );
        glDisableClientState( GL_VERTEX_ARRAY );
        glDisableClientState( GL_COLOR_ARRAY );
        glPopMatrix();
    }

    glFlush();
    glutSwapBuffers();
}

void Timer(int /*value*/)
{
    cam_angle+=0.01f;
    glutPostRedisplay();
    // 100 milliseconds
    glutTimerFunc(100, Timer, 0);
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitWindowSize (w, h);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("my window");
    glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_BLEND);

    init();
    glutDisplayFunc(onDisplay);
    Timer(0);
    glutMainLoop();

    return 0;
}

Moire effect

最佳答案

如果您使用 Multisampling , 则结果将得到显着改善:

glutSetOption(GLUT_MULTISAMPLE, 8);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE);

没有GLUT_MULTISAMPLE:
enter image description here

使用GLUT_MULTISAMPLE:
enter image description here


另请参阅 GLUT + OpenGL multisampling anti aliasing (MSAA) 的答案

关于C++:从 openGL 中去除莫尔效应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46386807/

相关文章:

c++ - Legacy-C C++ 并入

c++ - 通过函数指针定义 "main"

opengl - 如何在 PyOpenGL 中将纹理复制到 pbo 中?

c++ - 支持 CUDA 统一内存系统分配器的操作系统版本?

c++ - 如何避免 typedef?

c++ - 绕错误的轴旋转

OpenGL - 自动生成 glDrawArrays 的索引/步长参数

c++ - 具有多个着色器程序的 OpenGL 多纹理

matrix - 三.js shader 点光源位置

opengl - GLSL - 将坐标传递给片段着色器?