c++ - Opengl 性能问题,

标签 c++ opengl lag

所以我在 cpp 中使用 openGl 制作游戏(类似于乒乓球),当我尝试运行该程序时,图像渲染速度太慢,我觉得某些东西对我的 GPU 内存造成了巨大的损失, (到目前为止,我已经完成了 Racket 的绘制和运动,效果很好,但当按下按钮让球开始运动时,问题就出现了,此后一切都变慢了) 在这里我附上了我的部分代码 指出我的代码中的任何不足

    #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) 头文件。
  • 每次通过 display() 应用您的投影/模型 View 矩阵而不是 reshape 回调,有助于防止恼人的矩阵堆栈问题。
  • 不要Sleep(),使用计时器/空闲回调到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();
}

关于c++ - Opengl 性能问题,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29130381/

相关文章:

c++ - 返回 *this 作为引用是否安全?

c++ - 从标准输入中捕获字符,而无需等待按下回车键

c++ - 为什么 case 语句只接受常量?

c++ - OpenGL 调用锁定/卡住

java - 为什么我添加图像时出现延迟?

C++ #ifndef TOKEN #define TOKEN

c++ - 在OpenGL程序中绘制彩色四边形作为背景

c++ - 在 2D 中仅旋转一个四边形

java - 选项卡布局 : Lag when switching fragments

r - dplyr 内部 mutate 滞后函数的奇怪行为