Opengl性能问题,

Opengl Performance issues ,

本文关键字:问题 性能 Opengl      更新时间:2023-10-16

所以我在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("1n");//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("2n");
    }
        else if( (ii<0.9)&& (reac_top==3)&&(ii>0.05))   //The balls journey back to the paddle 
        {
            printf("3n");
            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++,请使用c++样式(stdlib.h->cstdlib)标头
  • 每次通过display()而不是整形回调应用投影/模型视图矩阵,有助于防止恼人的矩阵堆栈问题
  • 不要Sleep(),使用定时器/空闲回调glutPostRedisplay()。与帧速率无关的移动的加分(您可以使用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("1n");//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("2n");
        }
        else if( (ii<0.9)&& (reac_top==3)&&(ii>0.05))   //The balls journey back to the paddle 
        {
            printf("3n");
            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();
}