Inbuilt polygon clipping in OpenGL

本小妞迷上赌 提交于 2021-02-11 15:27:38

问题


Can someone help me in understanding why below code is showing triangle in front of the Quad? For gluLookAt(10, 10, 60, 0, 0, 0, 0, 1, 0) and glOrtho(-30, 30, -30, 30, 0, 90)?

Expected Output: Because the 'z' value of the triangle is less than the quad and observer is at 60 unit in the +ive 'z' axis. Thus, the rectangle should had been visible as a whole and only a few parts of the triangle would had been visible.

glBegin(GL_QUADS);
    glColor3f(1, 1, 1); 
    glVertex3f(10, 10, 15); glVertex3f(20, 10, 15); glVertex3f(20, 20, 15); glVertex3f(10, 20, 15);
glEnd(); 

glBegin(GL_TRIANGLES);
    glColor3f(0, 1, 0);

    glVertex3f(0, 0, 0);
    glVertex3f(22, 14, 0);
    glVertex3f(16, 22, 0);
glEnd();

回答1:


First of all, your question is totally unrelated to clipping - Clipping means calculating the intersection of the primitves against some clip area or clip volume.

The triangle does appear because you draw it after the quad. By default, OpenGL will not apply any visibility algorithms. However, OpenGL does support the Z buffer algorithm in form of the depth test.




回答2:


So finally I have got answer to this. It required few lines of code to be added.

  1. Adding below 3 lines in the initialization step

    glClearDepth(1.0f); glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST);

  2. Initializing display mode to add depth information (GLUT_DEPTH ) as below

    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB);

Below is the complete code for displaying a rectangle behind which is a traingle.

#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
#include<iostream>

using namespace std;

void FPS(void);

GLint gFramesPerSecond = 0;
int TIMER_SPEED = 10000;

float i = 40;
void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    gluLookAt(10, 10, i-=.2, 10, 10, -10, 0, 1, 0);

    glBegin(GL_LINES);
        glColor3f(1, 0, 0);
        glVertex3f(0, 0, 0); glVertex3f(20, 0, 0);

        glColor3f(0, 1, 0);
        glVertex3f(0, 0, 0); glVertex3f(0, 20, 0);

        glColor3f(0, 0, 1);
        glVertex3f(0, 0, 0); glVertex3f(0, 0, 50);
    glEnd();

    glBegin(GL_QUADS);
        glColor3f(1, 1, 1); 
        glVertex3f(10, 10, 10); glVertex3f(20, 10, 15); glVertex3f(20, 20, 15); glVertex3f(10, 20, 15);
    glEnd(); 

    glBegin(GL_TRIANGLES);
        glColor3f(0, 1, 0);

        glVertex3f(12, 12, 20);
        glVertex3f(22, 14, 12);
        glVertex3f(16, 22, 12);
    glEnd();
    glutSwapBuffers();
}

void timer(int v)
{
    static GLfloat u = 0.0;
    u += 0.01;
    glutTimerFunc(TIMER_SPEED/60.0, timer, ++v);
    display();
}

void Init(void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClearDepth(1.0f);
    glDepthFunc(GL_LESS);
    glEnable(GL_DEPTH_TEST);
}

void Resize(int width, int height)
{
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-15, 30, -15, 30, 15, 40);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(640, 480);
    glutInitWindowPosition(200, 200);
    glutCreateWindow("3D Object in OpenGL");

   glutKeyboardFunc(myKeyboard);
    Init();
    glutDisplayFunc(display);
    glutReshapeFunc(Resize);
    glutTimerFunc(100, timer, 0);
    glutMainLoop();
    return 0;
}


来源:https://stackoverflow.com/questions/37071539/inbuilt-polygon-clipping-in-opengl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!