T
T
Type Programmer2019-12-25 18:38:22
JavaScript
Type Programmer, 2019-12-25 18:38:22

The program does not load either the processor or the video card by 100%, what should I do (openGL)?

I wanted to make such a mini benchmark on OpenGL.
The code

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <cstdlib>
#include <windows.h>
#include <math.h>

     double size = 5;
     const int X = 250, Y = 250;
     GLfloat light_position[]={0.0,2.0,0.0,0.0};
     GLfloat light_global[]={1,1,1,1};
     GLfloat white_light[]={1,1,1,1};
     GLfloat mat_color[]={1,1,1,1};
     GLfloat mat_specular[]={0.7,0.7,0.7,1};
     GLfloat mat_shininess[]={60.0};
     double angle = 0,angle_global = 0;


class Checker{
private:
    double x,y,z;
public:
    void Draw(){
    glPushMatrix();
    glTranslatef(x,y,z);
    glRotatef(-90,1,0,0);
    glutSolidCone(3,5,10,10);
    glPopMatrix();
    }

    void Set(double x, double y, double z){
    Checker::x = x; Checker::y = y; Checker::y = y;
    }
};

void draw_game_board(){
  glPushMatrix();
  glScalef(1,2,1);
  glTranslatef(-size*(X-1)/2,0,size*Y/2);
  for( int y = 0; y < Y; y++ ){
    angle = 0 + angle_global;
    for(int x = 0; x < X; x++ ){
            mat_specular[0] *= -1;
            mat_specular[1] *= -1;
            mat_specular[2] *= -1;
            glPushMatrix();
            glTranslatef(0,sin(angle)*20,0);
            angle += 0.15;
            glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_specular);
        glutSolidCube(size);
        glPopMatrix();
        glTranslatef(size,0,0);
    }
            mat_specular[0] *= -1;
            mat_specular[1] *= -1;
            mat_specular[2] *= -1;
    glTranslatef(-size*X,0,-size);
  }
  glPopMatrix();
}

void display(){
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  draw_game_board();
  glPushMatrix();
  glPopMatrix();
  glutSwapBuffers();
  glutPostRedisplay();
  angle_global += 0.05;
}

void Init(){
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
         glLightModelfv(GL_LIGHT_MODEL_AMBIENT,light_global);
         glLightfv(GL_LIGHT0,GL_POSITION,light_position);
         glLightfv(GL_LIGHT0,GL_DIFFUSE,white_light);
         glLightfv(GL_LIGHT0,GL_SPECULAR,white_light);
    glClearDepth(1);
    glClearColor(0.3,1,0.5,0);
  glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(90.f, 1.f, 0.1f, 1000.f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(500, 500, 500, 0, 0, 0, 0, 1, 0);
         glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_color);
         glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
         glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
}

int main(int argc, char** argv){
    srand(4541);
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
  glutInitWindowPosition(80, 80);
  glutInitWindowSize(1024, 800);
  glViewport(0,0,1024,800);
  glutCreateWindow("Learn OpenGL");
  glutDisplayFunc(display);
  Init();
  glutMainLoop();
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Somewhere Intech, 2019-02-23
@freelancer

let small = dates.loads[item].files && dates.loads[item].files.length > 0 ? dates.loads[item].files[0].small : null

T
tsarevfs, 2019-12-25
@MegaCraZy6

100% CPU usage usually means all cores are loaded. In your case, the application is single-threaded and can only load one.
It is quite likely that even one core will wait most of the time to send data to the video card and will not be loaded at 100.
GPU load is an even more complex concept. There may already be hundreds of cores and the load distribution on them depends on a bunch of parameters.
There are profiler programs that can show you some additional information about your program.
For NVIDIA there is https://developer.nvidia.com/nsight-systems
With their help you can see what exactly the program is doing most of the time.
Vsync also intervenes here, which limits the fps to the refresh rate of the monitor. So most of the time there is waiting.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question