【问题标题】:C++ opengl intersecting glScissorC++ opengl 相交 glScissor
【发布时间】:2022-11-27 10:51:13
【问题描述】:

我正在从事的一个项目涉及我使用 glScissor,在某些情况下我需要在一个区域上执行两次(或更多)剪刀,目的是只渲染两个剪刀框中的内容。

我遇到的问题是第二个剪刀盒只是覆盖了前一个,这意味着只使用最后一个盒子集而不是两个。

我尝试了现有的解决方案,例如设置 scissor1、推送矩阵、启用 scissor_test、设置 scissor2、禁用 scissor_test、popmatrix、禁用 scissor_test。如这里所建议的:glScissor() call inside another glScissor() 我无法让这些产生任何区别,我也尝试过 glPushAttrib 而不是矩阵,但仍然没有区别。

这是我为剪刀测试编写的示例程序,它由 g++ 编译并使用 freeglut,剪刀发生在 display() 中:

/*
Compile: g++ .\scissor.cpp -lglu32 -lfreeglut -lopengl32

*/

#include <GL/gl.h>//standard from mingw, already in glut.h - header library
#include <GL/glu.h>//standard from mingw, already in glut.h - utility library
#include <GL/glut.h>//glut/freeglut - more utilities, utility tool kit

void display();
void reshape(int, int);
void timer(int);

void init(){
    glClearColor(0, 0, 0, 1);
}

int main(int argc, char **argv){
    glutInit(&argc, argv);//init glut
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);//init display mode, add double buffer mode

    //init window
    glutInitWindowPosition(200, 100);//if not specified, it will display in a random spot
    glutInitWindowSize(500, 500);//size


    //create window
    glutCreateWindow("Window 1");

    //give glut a function pointer so it can call that function later
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutTimerFunc(0, timer, 0);//call certain function after a specified amount of time
    init();


    glutMainLoop();//once this loop runs your program has started running, when the loop ends the program terminates

}



float xPos = -10;
int state = 1;//1 = right, -1 = left

//our rendering happens here
void display(){
    //clear previous frame
    glClear(GL_COLOR_BUFFER_BIT);//pass in flag of frame buffer
    //draw next frame below
    glLoadIdentity();//reset rotations, transformations, ect. (resets coordinate system)
    //we are using a model view matrix by default

    //TEST
    glEnable(GL_SCISSOR_TEST);
    glScissor(0, 0, 100, 1000);
    
    glPushMatrix();
    glEnable(GL_SCISSOR_TEST);
    glScissor(50, 0, 1000, 1000);
    
    //assuming both scissors intersect, we should only see the square between 50 and 100 pixels
    

    //draw
    
    glBegin(GL_QUADS);//every set of 3 verticies is a triangle
    //GL_TRIANGLES = 3 points
    //GL_QUADS = 4 points
    //GL_POLYGON = any amount of points
    
    glVertex2f(xPos, 1);//the 2 is the amount of args we pass in, the f means theyr floats
    glVertex2f(xPos, -1);
    glVertex2f(xPos+2, -1);
    glVertex2f(xPos+2, 1);

    glEnd();//tell opengl your done drawing verticies
    
    
    glDisable(GL_SCISSOR_TEST);
    glPopMatrix();
    glDisable(GL_SCISSOR_TEST);


    //display frame buffer on screen
    //glFlush();
    glutSwapBuffers();//if double buffering, call swap buffers instead of flush
}




//gets called when window is reshaped
void reshape(int width, int hight){
    //set viewport and projection
    //viewport is a rectangle where everything is drawn, like its the window
    glViewport(0, 0, width, hight);

    //matrix modes: there is model view and projection, projection has depth
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();//reset current matrix after changing matrix mode
    gluOrtho2D(-10, 10, -10, 10);//specify 2d projection, set opengl's coordinate system
    glMatrixMode(GL_MODELVIEW);//change back to model view


}


//this like makes a loop
void timer(int a){
    glutPostRedisplay();//opengl will call the display function the next time it gets the chance
    glutTimerFunc(1000/60, timer, 0);

    //update positions and stuff
    //this can be done here or in the display function
    switch(state){
        case 1:
            if(xPos < 8)
                xPos += 0.15;
            else
                state = -1;
            break;
        case -1:
            if(xPos > -10)
                xPos -= 0.15;
            else
                state = 1;
            break;
    }
}

我尝试了以下示例解决方案,例如 push/pop matrix/attrib,但无法正常工作

【问题讨论】:

    标签: c++ opengl


    【解决方案1】:

    没有第一个或第二个剪刀盒。只有剪刀盒。你可以改变剪刀框,该更改将影响后续渲染。但在任何时候,都只有一个。

    您想要的是使用模板缓冲区丢弃通过将某些值渲染到模板缓冲区而定义的区域之外的片段。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多