简体   繁体   English

打开gl窗口不显示?

[英]open gl window not showing?

#include <iostream>
#include <stdio.h>
#include <GL/glut.h>
#include <windows.h>
#include <math.h>

using namespace std;
void display(void) {
    float cx = 200, cy = 200, rad = 50;
    float startx = cx - rad;
    float starty = cy;
    int x = startx, y = starty;

    glColor3f(1.0f, 0.0f, 0.0f);
    glBegin(GL_POINTS);
    glVertex2f((x - 250.f) / 250.f, (250.f - y) / 250.f);

    while (x < cx + rad) {    
        x++;
        y = cy - sqrt(rad * rad - (x - cx) * (x - cx));
        // y = sqrt(rad*rad - (x-cx)*(x-cx))-cy;
        glVertex2f((x - 250.f) / 250.f, (250.f - y) / 250.f);
        cout << x << " " << y << endl;
    }

    glFlush();    
    glEnd();
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(500, 500);
    gebtutInitWindowPosition(100, 100);
    glutCreateWindow("CpViewer");
    glClearColor(1.0, 1.0, 1.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glutDisplayFunc(display);
    glutMainLoop();

    return 0;
}

I am trying to implement an algo to drawn a circle but im not getting any output. 我正在尝试实现一种算法来画一个圆,但是我没有得到任何输出。 i am also not able to see the opengl graphics window it appears transparent . 我也无法看到显示透明的opengl图形窗口。 what's wrong with this code.?? 这段代码怎么了?

You are requesting a double-buffered window with that GLUT_DOUBLE flag. 您正在请求带有该GLUT_DOUBLE标志的双缓冲窗口。 But you never swap the buffers. 但是,您永远不会交换缓冲区。 So what happens is that you draw all the time into the back buffer, and the front buffer is just undefined and nothing will ever be shown. 因此,发生的事情是您一直将其拉入后缓冲区,而前缓冲区只是未定义的,因此不会显示任何内容。

You should add a glutSwapBuffer() call when you finished drawing a frame, typically at the very end of your display() function. 完成绘制框架后,通常应在display()函数的最后,添加glutSwapBuffer()调用。 And while you're at it: Remove that glFlush() call. 当您使用它时:删除该glFlush()调用。 You put it inside an glBegin()/glEnd() block, where it will not work at all, and you won't need it. 您将其放在glBegin()/glEnd()块中,该块根本不起作用,并且您将不需要它。 Also make sure that the glutSwapBuffers() call is added after the glEnd() . 还要确保在glEnd() 之后添加了glutSwapBuffers()调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM