1

该项目编译并运行,但窗口中没有任何内容。

它应该显示一些文本和一个带有一些点的区域。 谁能帮我解决这个问题?

这是我的代码:

#include <stdio.h>
#include <windows.h>       
#include <GL/gl.h>         
#include <glut.h>          

#define KEY_ESCAPE 27

#define PROJECTION_WIDTH 150
#define NUMBER_OF_FLIES 50

float xpos[NUMBER_OF_FLIES];
float ypos[NUMBER_OF_FLIES];


typedef struct {
    int width;
    int height;
    char* title;
    float field_of_view_angle;
    float z_near;
    float z_far;
} glutWindow;

glutWindow win;

// To display the text
void renderBitmapString(float x, float y, void *font, const char *string)
{
    const char *c;
    glRasterPos3f(x, y, 0);
    for (c = string; *c != '\0'; c++)
    {
        glutBitmapCharacter(font, *c);
    }
}

void displayText()
{

    glPushMatrix();
    //glTranslatef(1.0, 0.50, 0.5);
        //const int font = (int)GLUT_BITMAP_9_BY_15;
        const int font = (int)GLUT_BITMAP_HELVETICA_12;
        glColor3f(0.0f, 0.0f, 0.0f);                                // black color
        renderBitmapString(-1.7, 1.3, (void *)font, "1. Load Measured projections");
        renderBitmapString(-1.7, 1.2, (void *)font, "2. Set the position of flies, i.e. create initial guess");
        renderBitmapString(-1.7, 1.1, (void *)font, "3. Compute projections from the current reconstruction");
        renderBitmapString(-1.7, 1, (void *)font, "4. Compute the population's perfomance, i.e. the global fitness");
        renderBitmapString(-1.7, 0.9, (void *)font, "5. Select a random fly (fly_to_kill)");
        renderBitmapString(-1.7, 0.8, (void *)font, "6. Remove the fly's contribution");
        renderBitmapString(-1.7, 0.7, (void *)font, "7. Compute the population's performance without the selected fly");
        renderBitmapString(-1.7, 0.6, (void *)font, "8. Compare the performances, i.e. compute the fly's local fitness");
        renderBitmapString(-1.7, 0.5, (void *)font, "9. If the local fitness is equal to or greater than the selection threshold,");
        renderBitmapString(-1.5, 0.4, (void *)font, "then go to Step 10, ");
        renderBitmapString(-1.5, 0.3, (void *)font, "else go to Step 11");
        renderBitmapString(-1.7, 0.2, (void *)font, "10. Restore the fly's contribution, then go to Step 5");
        renderBitmapString(-1.7, 0.1, (void *)font, "11. Select a genetic operator");
        renderBitmapString(-1.7, 0.0, (void *)font, "12. If the genetic operator is mutation,");
        renderBitmapString(-1.5, -0.1, (void *)font, "then go to Step 13, ");
        renderBitmapString(-1.5, -0.2, (void *)font, "else go to Step 19 ");
        renderBitmapString(-1.7, -0.3, (void *)font, "13. Select a random fly (fly_to_reproduce)");
        renderBitmapString(-1.7, -0.4, (void *)font, "14. Remove the fly's contribution");
        renderBitmapString(-1.7, -0.5, (void *)font, "15. Compute the population's performance without the selected fly");
        renderBitmapString(-1.7, -0.6, (void *)font, "16. Compare the performances, i.e. compute the fly's local fitness");
        renderBitmapString(-1.7, -0.7, (void *)font, "17. Restore the fly's contribution'");
        renderBitmapString(-1.7, -0.8, (void *)font, "18. If the local fitness is smaller than the selection threshold,");
        renderBitmapString(-1.5, -0.9, (void *)font, "then go to Step 13, ");
        renderBitmapString(-1.5, -1, (void *)font, "else go to Step 20 ");
        renderBitmapString(-1.7, -1.1, (void *)font, "19. Replace fly_to_kill by a new fly with a random position, go to Step 22");
        renderBitmapString(-1.7, -1.2, (void *)font, "20. Replace fly_to_kill by a new fly based on fly_to_reproduce");
        renderBitmapString(-1.7, -1.3, (void *)font, "21. Add the fly's contribution to the population");
        renderBitmapString(-1.7, -1.4, (void *)font, "22. If stop the reconstruction,");
        renderBitmapString(-1.5, -1.5, (void *)font, "then go to Step 23, ");
        renderBitmapString(-1.5, -1.6, (void *)font, "else go to Step 4 ");
        renderBitmapString(-1.7, -1.7, (void *)font, "23. Extract good flies");

        // the bottom left
        renderBitmapString(-3.9, -0.9, (void *)font, "Iterative paradigm for tomography reconstruction");
        renderBitmapString(-3.9, -1, (void *)font, "(click here to view the flowchart in full resolution)");
        // the top right
        renderBitmapString(1.8, 1.3, (void *)font, "Basic Fly algorithm for tomography reconstruction");
        renderBitmapString(1.8, 1.2, (void *)font, "(click here to view the flowchart in full resolution)");

    glPopMatrix();
    glutSwapBuffers();
}

void displayFlies()
{
    glPushMatrix();
        //glTranslatef(0.0, 0.50, 0.0);
        glColor3f(0.0f, 0.0f, 1.0f);       //blue color

        glPushAttrib(GL_POINT_BIT);
            glPointSize(5.0);

            // drawing the data
            for (int i = 0; i < NUMBER_OF_FLIES; i++){
                int DX = xpos[i];
                int DY = ypos[i];
                glPushMatrix();
                    //glTranslatef(- 0.85 + DX * 0.014, -1.2 + DY * 0.0135, 0.0);

                    glBegin(GL_POINTS); // drawing points 
                    glVertex3f(DX, DY, 0.0f);
                    glEnd();

                glPopMatrix();
            }

        glPopAttrib();

        // draw The display Area
        glBegin(GL_LINE_LOOP);             //start drawing a line loop
        glVertex3f(0.65f, 0.25f, 0.0f);    //left of window
        glVertex3f(0.65f, -1.20f, 0.0f);   //bottom of window
        glVertex3f(-0.85f, -1.20f, 0.0f);  //right of window
        glVertex3f(-0.85f, 0.25f, 0.0f);   //top of window
        glEnd();                           //end drawing of line loop
    glPopMatrix();
    glutSwapBuffers();
}

void displayProjection()
{
    glPushMatrix();
    //glTranslatef(1.20, 0.50, 0.0);

        // The upper rectangle
        glBegin(GL_LINE_LOOP);             //start drawing a line loop
        glVertex3f(0.65f, 0.60f, 0.0f);    //left of window
        glVertex3f(0.65f, 0.27f, 0.0f);    //bottom of window
        glVertex3f(-0.85f, 0.27f, 0.0f);   //right of window
        glVertex3f(-0.85f, 0.60f, 0.0f);   //top of window
        glEnd();

        //The right rectangle
        glBegin(GL_LINE_LOOP);             //start drawing a line loop
        glVertex3f(0.67f, 0.25f, 0.0f);    //left of window
        glVertex3f(0.67f, -1.20f, 0.0f);   //bottom of window
        glVertex3f(0.99f, -1.20f, 0.0f);  //right of window
        glVertex3f(0.99f, 0.25f, 0.0f);   //top of window
        glEnd();                           //end drawing of line loop

        // draw Horizontal RED lines
        glBegin(GL_LINES);
        glColor3f(1.0f, 0.0f, 0.0f);
        glVertex3f(-0.25f, 0.60f, 0.0f);
        glVertex3f(-0.25f, 0.27f, 0.0f);
        glVertex3f(0.1f, 0.27f, 0.0f);
        glVertex3f(0.1f, 0.60f, 0.0f);
        glEnd();

        // draw Upper RED lines
        glBegin(GL_LINES);
        glVertex3f(0.46f, 0.18f, 0.59f);  // origin of the Right line 
        glVertex3f(0.59f, 0.29f, -0.11f);  // ending point of the Right line

        glVertex3f(-0.37f, 0.18f, 0.59f);  // origin of the Left line
        glVertex3f(-0.33f, 0.29f, -0.11f);  // ending point of the Left line

        glVertex3f(0.37f, 0.40f, 0.92f);  // origin of the Upper line
        glVertex3f(0.40f, 0.49f, 0.50f);  // ending point of the Upper line
        glEnd();


        // draw Horizontal RED lines
        glBegin(GL_LINES);
        glVertex3f(-0.06f, 0.47f, -1.50f);    // Origin The Upper Horizontal line
        glVertex3f(-0.06f, -0.23f, -1.50f);   // Ending point of The Upper Horizontal line

        glVertex3f(-0.06f, -0.63f, -1.50f);   // Origin The bottom Horizontal line
        glVertex3f(-0.06f, -1.40f, -1.50f);   // Ending point of The bottom Horizontal line

        glVertex3f(0.36f, -0.23f, -1.50f);   // Origin The right Horizontal line
        glVertex3f(0.36f, -0.63f, -1.50f);   // Ending point of The right Horizontal line

        glVertex3f(0.78f, -0.30f, 0.21f);  // origin of the upper vartical line
        glVertex3f(0.95f, -0.29f, -0.11f);  // ending point of the upper vartical line

        glVertex3f(0.78f, -0.60f, 0.21f);  // origin of the bottom vartical line
        glVertex3f(0.95f, -0.61f, -0.11f);  // ending point of the bottom vartical line


        glEnd();

    glPopMatrix();

}


// intialization function
void initialize()
{
    glMatrixMode(GL_PROJECTION);                                            // select projection matrix
    glViewport(0, 0, win.width, win.height);                                // set the viewport
    glMatrixMode(GL_PROJECTION);                                            // set matrix mode
    glLoadIdentity();                                                       // reset projection matrix
    GLfloat aspect = (GLfloat)win.width / win.height;
    //gluPerspective(win.field_of_view_angle, aspect, win.z_near, win.z_far);   // set up a perspective projection matrix
    gluOrtho2D(0, win.width, 0, win.height);
    glMatrixMode(GL_MODELVIEW);                                             // specify which matrix is the current matrix
    glClearDepth(1.0f);                                                     // specify the clear value for the depth buffer
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);                      // specify implementation-specific hints
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);                               // specify clear values for the color buffers"background"

}

// keyboard control function
void keyboard(unsigned char key, int mousePositionX, int mousePositionY)
{
    switch (key)
    {
    case KEY_ESCAPE:
        exit(0);
        break;

    default:
        break;
    }
}

// the main function
int main(int argc, char **argv)
{
    // set window values
    win.width = 1350;
    win.height = 690;
    win.title = " The Project ";
    win.field_of_view_angle = 45;
    win.z_near = 1.0f;
    win.z_far = 500.0f;
    //initialing the data
    for (int i = 0; i < NUMBER_OF_FLIES; i++){
        xpos[i] = rand() % PROJECTION_WIDTH;
        ypos[i] = rand() % PROJECTION_WIDTH;
    }
    // initialize and run program
    glutInit(&argc, argv);                                      // GLUT initialization
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);   // Display Mode
    glutInitWindowSize(win.width, win.height);                  // set window size
    glutCreateWindow(win.title);                                // create Window
    glutDisplayFunc(displayText);                                   // register Display Function
    glutIdleFunc(displayText);                                      // register Idle Function
    glutDisplayFunc(displayFlies);                                  // register Display Function
    glutIdleFunc(displayFlies);
    glutDisplayFunc(displayProjection);                                 // register Display Function
    glutIdleFunc(displayProjection);
    glutKeyboardFunc(keyboard);                                 // register Keyboard Handler
    initialize();
    glutMainLoop();                                             // run GLUT mainloop
    return 0;
}
4

1 回答 1

1

您拥有三种不同的显示功能,但glutDisplayFuncglutIdleFunc使用您设置的最新功能,因此displayTextdisplayFlies未使用。

最后一个,displayProjection,最后需要一行:

glutSwapBuffers();

此外,您可能需要设置一些灯来照亮您绘制的内容。

顺便说一句,注释掉另外两个,您可以 test displayFlies,它呈现一堆蓝点,并且displayText,它写一些文本(黑色背景上的黑色,几行相互叠加),以及。

最好先尝试一些教程,例如NeHe ... 是的,正如其他评论员所说,你可能最好学习着色器 :)

于 2015-07-12T20:33:57.430 回答