Opengl 性能问题,

Opengl Performance issues ,

所以我正在用 cpp 中的 openGl 制作一个游戏(类似于乒乓球),当我尝试 运行 这个程序时,图像渲染太慢了,我觉得有些东西占用了很大的空间占用我的 GPU 内存, (到目前为止,我已经完成了球拍的绘制和移动,效果很好,但当按下按钮让球开始运动时,问题就出现了,此后一切都变慢了) 在这里我附上了我的部分代码 指出我的代码中的任何缺陷

    #include<stdlib.h>
    #include<GL/glut.h>
    #include<windows.h>
    #include<iostream>
    //#include<sstream>
    #include<stdio.h>
    #include<ctime>
    #include<string.h>






    typedef double point[3];
    point a,b,c,d;
    static float xmove=0.0;
     static int release=0;
    static  float ii=0.00;

    static int reac_top;


    using namespace std;
     //GLvoid player1_box();
    GLfloat xangle=0.0;
    float changeDir=1;// positive direction
    double vv[4][3]={   {0.5,0.2,0.0},{ 0.5,0.25,0.0},{0.7,0.25,0.0},{0.7,0.2,0.0}};


    void renderoutline()
    {
        glBegin(GL_LINE_LOOP);

        glColor3f(1,0,0);
        glVertex2f(0.0,0.0);
        glColor3f(1,0,0);
        glVertex2f(0,1.2);
        glColor3f(1,0,0);
        glVertex2f(1.2,1.2);
        glColor3f(1,0,0);
        glVertex2f(1.2,0);

        glEnd();
    }

    void ball()
    {
        glBegin(GL_POLYGON);
        glColor3f(0,0,1);
        glVertex3f(0.6,0.25,0.0);
        glColor3f(0,0,1);
        glVertex3f(0.6,0.28,0.0);
        glColor3f(0,0,1);
        glVertex3f(0.62,0.28,0.0);
        glColor3f(0,0,1);
        glVertex3f(0.62,0.25,0.0);

            glEnd();
    }
    void renderbox1( point a , point b , point c, point d)
    {

        glBegin(GL_POLYGON);
        glColor3f(0,0,0);
        glVertex3dv(a);
        glColor3f(0,0,0);
    glVertex3dv(b);
    glColor3f(0,0,0);
    glVertex3dv(c);
    glColor3f(0,0,0);
    glVertex3dv(d);
    glEnd();
    }


    void keyboard(unsigned char key,int x,int y)
    {

    switch(key){

        case 'a':xangle-=0.1;

                ++xmove;

                while(xmove>5)
                {
                    xmove=-5;

                    xangle=0.5;
                    continue;
                }
                break;
        case 'd':xangle+=0.1;
                --xmove;
                while(xmove<-5)
                {
                    xmove=5;
                    xangle =-0.5;
                    continue;
                }
                break;
        case 'r': 

            release=1;
            //printf("Inside switch r");
            break;
        }
    glutPostRedisplay();

    }







    void display()
    {
        Sleep(2);

    glClear(GL_COLOR_BUFFER_BIT);

    glPushMatrix();
    glLineWidth(3.0);
    glLoadIdentity();

    glTranslatef(xangle,0,0);
    renderbox1(vv[0],vv[1],vv[2],vv[3]); // To draw the paddle 
    glPopMatrix();
    //printf("x move in disp:%1f \n",xmove);
    renderoutline();

    glPushMatrix();
    glTranslatef(0,ii,0);
    ball();                             //To draw the ball
    glPopMatrix();

    glFlush();


    }
    void moveBall(){            //glutidlefunction

        if(release==1)
        {               //Release becomes one when the key 'r' is pressed (glutkeyboardfunc)
        Sleep(500);

         if(ii>=0&&ii<=0.9 )
         {
        reac_top=false;
        ii=ii+0.05;printf("1\n");//The ball which was intially on the paddle starts moving till it reaches the top edge of thescreen 
         } 

    else if(ii>=0.9)
    {
                                //when the boll reaches top 
        ii=ii-0.05;
        reac_top=3;
        printf("2\n");

    }
        else if( (ii<0.9)&& (reac_top==3)&&(ii>0.05))   //The balls journey back to the paddle 
        {
            printf("3\n");
            ii=ii-0.05;

        }   
    release=1;
        }

    }


    void reshape(int w, int h)
    {
        glMatrixMode(GL_PROJECTION);  
        glLoadIdentity();
        glOrtho(0, 1.2, 0, 1.2,-10.0,10.0); 
        glMatrixMode(GL_MODELVIEW);
        glViewport(0,0,w,h);  
        //glutPostRedisplay();
    //Use the whole window for rendering
    }

    int main(int argc ,char **argv)
    {
        //clock_t begin= clock();
        char n1,n2;
        int ack;
    /*printf("The object of Tic Tac Toe is to get three in a row. \n . Players alternate placing Xs and Os(Traingles and Boxes in this version) \n on the game board until either opponent has three \n in a row or all nine squares are filled(Which would be a drawObviously). \n");
    printf("\n Press 1 if acknowledged \n");
    scanf("%d",&ack);*/
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowSize(1000,500);
    glutInitWindowPosition(200,0);

    glutCreateWindow("3d Gasket");
    glutDisplayFunc(display);

    glutKeyboardFunc(keyboard);
    glutReshapeFunc(reshape);
    glutIdleFunc(moveBall);
    glClearColor(1,1,1,1);
    glutMainLoop();
    //clock_t end=clock();
    }
  • 您正在使用 ,使用 C++ 风格(stdlib.h -> cstdlib)headers.
  • 每次通过 display() 应用您的 projection/modelview 矩阵而不是重塑回调,有助于防止恼人的矩阵堆栈问题。
  • 不要 Sleep(),使用 timer/idle 回调 glutPostRedisplay()framerate-independent movement的加分(可以用glutGet(GLUT_ELAPSED_TIME)来计算delta-t)。
  • 没有 Sleep() 就没有理由将 windows.h 拖入其中。

总计:

#include <GL/glut.h>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <cstring>

typedef double point[3];
point a,b,c,d;
static float xmove=0.0;
static int release=0;
static  float ii=0.00;

static int reac_top;

using namespace std;

GLfloat xangle=0.0;
float changeDir=1;// positive direction
double vv[4][3]={   {0.5,0.2,0.0},{ 0.5,0.25,0.0},{0.7,0.25,0.0},{0.7,0.2,0.0}};

void renderoutline()
{
    glBegin(GL_LINE_LOOP);

    glColor3f(1,0,0);
    glVertex2f(0.0,0.0);
    glColor3f(1,0,0);
    glVertex2f(0,1.2);
    glColor3f(1,0,0);
    glVertex2f(1.2,1.2);
    glColor3f(1,0,0);
    glVertex2f(1.2,0);

    glEnd();
}

void ball()
{
    glBegin(GL_POLYGON);
    glColor3f(0,0,1);
    glVertex3f(0.6,0.25,0.0);
    glColor3f(0,0,1);
    glVertex3f(0.6,0.28,0.0);
    glColor3f(0,0,1);
    glVertex3f(0.62,0.28,0.0);
    glColor3f(0,0,1);
    glVertex3f(0.62,0.25,0.0);

    glEnd();
}

void renderbox1( point a , point b , point c, point d)
{
    glBegin(GL_POLYGON);
    glColor3f(0,0,0);
    glVertex3dv(a);
    glColor3f(0,0,0);
    glVertex3dv(b);
    glColor3f(0,0,0);
    glVertex3dv(c);
    glColor3f(0,0,0);
    glVertex3dv(d);
    glEnd();
}

void keyboard(unsigned char key,int x,int y)
{
    switch(key)
    {
    case 'a':xangle-=0.1;
        ++xmove;
        while(xmove>5)
        {
            xmove=-5;
            xangle=0.5;
            continue;
        }
        break;

    case 'd':xangle+=0.1;
        --xmove;
        while(xmove<-5)
        {
            xmove=5;
            xangle =-0.5;
            continue;
        }
        break;

    case 'r': 
        release=1;
        break;
    }
}

void display()
{
    glClearColor(1,1,1,1);
    glClear(GL_COLOR_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);  
    glLoadIdentity();
    glOrtho(0, 1.2, 0, 1.2,-10.0,10.0); 

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glPushMatrix();
    glLineWidth(3.0);
    glLoadIdentity();

    glTranslatef(xangle,0,0);
    renderbox1(vv[0],vv[1],vv[2],vv[3]); // To draw the paddle 
    glPopMatrix();
    renderoutline();

    glPushMatrix();
    glTranslatef(0,ii,0);
    ball();
    glPopMatrix();

    glFlush();
}

void moveBall()
{
    if(release==1)
    {
        //Release becomes one when the key 'r' is pressed (glutkeyboardfunc)
        if(ii>=0&&ii<=0.9 )
        {
            reac_top=false;
            ii=ii+0.05;printf("1\n");//The ball which was intially on the paddle starts moving till it reaches the top edge of thescreen 
        } 

        else if(ii>=0.9)
        {
            //when the boll reaches top 
            ii=ii-0.05;
            reac_top=3;
            printf("2\n");

        }
        else if( (ii<0.9)&& (reac_top==3)&&(ii>0.05))   //The balls journey back to the paddle 
        {
            printf("3\n");
            ii=ii-0.05;

        }   
        release=1;
    }
}

void timer( int value )
{
    moveBall();

    glutTimerFunc( 16, timer, 0 );
    glutPostRedisplay();
}

int main(int argc ,char **argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowSize(1000,500);
    glutInitWindowPosition(200,0);
    glutCreateWindow("3d Gasket");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutTimerFunc( 0, timer, 0 );
    glutMainLoop();
}