Decreasing GL graphics quality to increase performance

I am running some openglgl code on a raspi 3. I am getting 6 fps in some of my tests, and need to optimise for performance. I have a height map with 98 polygons. This is rendered using GL_TRIANGLE_STRIP, all polygons as viewed from above are "front" rotation, and from below are "back". I have optimised it so that "back" polygons are rendered in POINT mode, with is far less taxing on the pi's GPU, as polygons that are a the back of a mountain are rendered in point mode.

Using my current configuration, I can place my view point from the top of the "mountain" in my height map, thus rendering all 98 polygons as they are all "front" polygons. 6 fps, very shaky when they are solid. Does up to 10 fps, acceptable, when they are line. I need 10 fps minimum.

I need more optimisation. Having the front polygons as LINE mode would be acceptable, but my method means that flat tiles behind mountains are still visible as they are still "front" orientation.

There are two options:

1. Make all front polygons line mode, but all polygons behind need to be invisible or point (point is near enough)

2. Make all front polygons solid mode, but something needs to be done to increase speed. I have tried GL_POLYGON_STIPPLE as an experiment, but this decreases fps.

I am sure there are other ways a non-beginner can think of. Is there a way to make my POINT polygons not rendered at all? This might add a bit of speed. I am not using lighting/shaders/anything impressive, this is part of a secondary school competition.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
  ///TODO: add comments

#include <GL/glut.h>

#include <SDL.h>

#include <SDL_keysym.h>

#include <SDL/SDL.h>

#include <iostream>

#include <string.h>

#include <cstdlib>

//#include <math.h>

#include <cmath>

#include <unistd.h>

#include <stdio.h>

#include <ctype.h>

#include "kbhit.h" /* http://linux-sxs.org/programming/kbhit.html */

#include <termios.h>

#include <unistd.h>   // for read()

#define PI 3.141519265368979323846264338327950288

#include <wiringPi.h>


#define deg 180 * PI

#define CLOCKS_PER_SEC 1000000

using namespace std;

//SDL_Event event;

double m = 0;

double ti = 0;

double two = 0;

double d = 0;

//#include <conio>
double x = -3;
double y = -5;
double z = -4;

double YAW = 0;
double PITCH = 90;
double ROLL = 0;

int64_t program_count = 0;
int iter_c = 0;
int rep_count = 0;

double overtime = 0;

double fps = 10;

std::string t = "p";


const int ys = 8;
const int xs = 8;

int ya = 0;
int xa = 0;

int yTriangles = 0;

double landMass [ys] [xs]= {
    {0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0},

    {0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0},

    {0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0},

    {0.0,0.0,1.5,2.0,1.5,0.0,0.0,0.0},

    {0.0,1.0,2.0,4.0,2.0,1.0,0.0,0.0},

    {0.0,0.0,1.5,2.0,1.5,0.0,0.0,0.0},

    {0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0},

    {0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0},
};

void display() { ///everything looped happens in here

  for(;;){

    glClear(GL_COLOR_BUFFER_BIT);

    /*glColor3f(1.0, 1.0, 0.0);
    glutWireTorus(0.5, 3, 10, 10);
    glTranslated(1,0,0);
    /*glutSolidTorus(0.5, 3, 10, 10);
    glTranslated(0,1,0);
    /*glutWireTorus(0.5, 3, 10, 10);
    glTranslated(1,0,0);
    glutWireTorus(0.5, 3, 6, 6);
    glTranslated(0,1,0);
    glutWireTorus(0.5, 3, 6, 6);
    glTranslated(1,0,0);
    glutWireTorus(0.5, 3, 6, 6);
    glTranslated(0,1,0);*/

    //glColor3f(1.0, 0.0, 0.0);

    /*glBegin(GL_LINES);
    glColor3f(1, 0, 0); glVertex3f(0, 0, 0); glVertex3f(10, 0, 0);
    glColor3f(0, 1, 0); glVertex3f(0, 0, 0); glVertex3f(0, 10, 0);
    glColor3f(0, 0, 1); glVertex3f(0, 0, 0); glVertex3f(0, 0, 10);
    glEnd();*/

    for(yTriangles = 0; yTriangles < ys-1; yTriangles++){
        glBegin(GL_TRIANGLE_STRIP);
        for(xa = 0; xa < xs; xa++){
            for(ya = yTriangles; ya < yTriangles + 2; ya++){
                double doubley = ya;
                double doublex = xa;
                glColor3d(0.01, 0.1,abs(1 / (pow(pow(-landMass[ya][xa] - y, 2)+pow(-ya-z,2)+pow(-xa-x,2),0.6))));
                glVertex3d(xa, landMass[ya] [xa], ya);
                std::cout << "1";
            }
        }
        glEnd();
    }
    std::cout << "\n";


    /*glBegin(GL_TRIANGLE_STRIP);

    glVertex3d(landMass[ya][xa],xa,ya);
    glVertex3d(0,0,1);
    glVertex3d(0,1,0);
    glVertex3d(0,1,1);
    glVertex3d(0,2,0);
    glVertex3d(0,2,1);
    glVertex3d(0,3,0);
    glVertex3d(0,3,1);
    glVertex3d(0,4,0);
    glVertex3d(0,4,1);

    glEnd();

    glBegin(GL_TRIANGLE_STRIP);

    glVertex3d(1,0,0);
    glVertex3d(1,0,1);
    glVertex3d(1,1,0);
    glVertex3d(1,1,1);
    glVertex3d(1,2,0);
    glVertex3d(1,2,1);
    glVertex3d(1,3,0);
    glVertex3d(1,3,1);
    glVertex3d(1,4,0);
    glVertex3d(1,4,1);

    glEnd();*/

    ///GLUTSWAPBUFFERS GOES HERE IF MISBEHAVING
    glutSwapBuffers();
    /*t = getchar();
    if(t == "w"){
      z = z + 0.2;
    }
    if(t == "s"){
      z = z - 0.2;
    }
    if(t == "a"){
      x = x - 0.2;
    }
    if(t == "d"){
      x = x + 0.2;
    }
    if(t == "f"){
      y = y + 0.2;
    }
    if(t == "c"){
      y = y - 0.2;
    }
    if(t == "p"){
      YAW++;
    }
    if(t == "o"){
      YAW--;
    }
    if(t == "8"){
      PITCH++;
    }
    if(t == "2"){
      PITCH--;
    }
    if(t == "6"){
      ROLL++;
    }
    if(t == "4"){
      ROLL--;
    }
    if(t ==""){
      ;
    }*/
    ROLL++;

    ti = ti + 0.1;


    //x = sin(ti / 360);
    //z = cos(ti / 360);
    //YAW = ti;

    glLoadIdentity();
    //PITCH = PITCH + 0.7;
    //YAW++;
    //ROLL = ROLL + 1.3;
    glRotatef(ROLL,0,0,1);
    glRotatef(PITCH,1,0,0);
    glRotatef(YAW,0,1,0);
    glTranslatef(x, y, z);

    std::cout << "\nYAW: " << YAW
    /*<< "\nPITCH: " << PITCH
    << "\nROLL: " << ROLL
    << "\nx: " << x
    << "\ny: " << y
    << "\nz: " << z*/
    << "\novertime: " << overtime
    << "\nclock: " << CLOCKS_PER_SEC
    << "\nIterationsPerSecond: " << fps;
    overtime = 0;
    //---------------------
        iter_c = iter_c + (CLOCKS_PER_SEC / fps);
        //rep_count++;
        while(clock() < iter_c){
            overtime++;
        }
        if(overtime > 50 && fps < 60){
            fps = fps + 0.1;
        }
        if(overtime < 50){
            fps = fps - 0.1;
        }

    //-----------------------------------------------------------------------

  }
}

// Sets up global attributes like clear color and drawing color, and sets up
// the desired projection and modelview matrices.
void init() {

  glClearColor(0.0, 0.0, 0.0, 1.0);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(100, 4.0/3.0, 0.1, 40);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  /*GL_ENABLE_BIT(*///GL_POLYGON_STIPPLE;//);
  glPolygonMode(GL_FRONT, /*GL_LINE*/GL_FILL); ///Makes all polygons
  ///Wireframe, greatly increases fps
  glPolygonMode(GL_BACK, /*GL_LINE*/GL_POINT);
  gluLookAt(10, 10, 10, 0, 0, 0, 0, 1, 0);
}

// Initializes GLUT, the display mode, and main window; registers callbacks;
// does application initialization; enters the main event loop.
int main(int argc, char** argv) {
  SDL_Init(SDL_INIT_EVERYTHING);
  init_keyboard();
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  glutInitWindowPosition(0, 0);
  glutInitWindowSize(800, 500);
  glutCreateWindow("ChambersFlight");
  glutDisplayFunc(display);
  /*void glutKeyboardFunc(void (*func)(unsigned char key,
                                   int x, int y));*/
  //glutKeyboardFunc(processNormalKeys);
  init();

  glutMainLoop();

}


The code is messy, IDK if it is of any help, my question is more related to decreasing the quality of graphics rather than the structure of my program.
OK, I have worked out that my timer section is faulty, I got some "recycled" code off of the internet. If any other beginners view this, do not make my mistake. Instead of a for(;;){} around display, put glutPostRedisplay(); at the end of your glutDisplayFunc() function. Secondly my timing was terrible, I am actually getting 18 fps!!!!! Which is excellent, I never expected it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
///TODO: add comments

/**Thanks to:
ALI BADEREDDIN for timing code
**/

#include <GL/glut.h>

#include <SDL.h>

#include <SDL_keysym.h>

#include <SDL/SDL.h>

#include <iostream>

#include <string.h>

#include <cstdlib>

//#include <math.h>

#include <cmath>

#include <unistd.h>

#include <stdio.h>

#include <ctype.h>

#include "kbhit.h" /* http://linux-sxs.org/programming/kbhit.html */

#include <termios.h>

#include <unistd.h>   // for read()

#define PI 3.141519265368979323846264338327950288

#include <wiringPi.h>


#define deg 180 * PI

#define CLOCKS_PER_SEC 1000000

using namespace std;

//SDL_Event event;

double m = 0;

double ti = 0;

double two = 0;

double d = 0;

//#include <conio>
double x = -3;
double y = -5;
double z = -4;

double YAW = 0;
double PITCH = 90;
double ROLL = 0;

int64_t program_count = 0;
int iter_c = 0;
int rep_count = 0;

double overtime = 0;

int fps = 10;

std::string t = "p";


const int ys = 8;
const int xs = 8;

int ya = 0;
int xa = 0;

int yTriangles = 0;

double landMass [ys] [xs]= {
    {0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0},

    {0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0},

    {0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0},

    {0.0,0.0,1.5,2.0,1.5,0.0,0.0,0.0},

    {0.0,1.0,2.0,4.0,2.0,1.0,0.0,0.0},

    {0.0,0.0,1.5,2.0,1.5,0.0,0.0,0.0},

    {0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0},

    {0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0},
};

int frameCount = 0;
int currentTime = 0;
int previousTime = 0;

void calculateFPS()
{

    //  Increase frame count
    frameCount++;

    //  Get the number of milliseconds since glutInit called
    //  (or first call to glutGet(GLUT ELAPSED TIME)).
    currentTime = glutGet(GLUT_ELAPSED_TIME);

    //  Calculate time passed
    int timeInterval = currentTime - previousTime;

    if(timeInterval > 1000)
    {
        //  calculate the number of frames per second
        fps = frameCount / (timeInterval / 1000.0f);
std::cout << fps;
        //  Set time
        previousTime = currentTime;

        //  Reset frame count
        frameCount = 0;
    }

}



void display() { ///everything looped happens in here

  //for(;;){

    glClear(GL_COLOR_BUFFER_BIT);

    for(yTriangles = 0; yTriangles < ys-1; yTriangles++){
        glBegin(GL_TRIANGLE_STRIP);
        for(xa = 0; xa < xs; xa++){
            for(ya = yTriangles; ya < yTriangles + 2; ya++){
                double doubley = ya;
                double doublex = xa;
                glColor3d(0.01, 0.1,abs(1 / (pow(pow(-landMass[ya][xa] - y, 2)+pow(-ya-z,2)+pow(-xa-x,2),0.6))));
                glVertex3d(xa, landMass[ya] [xa], ya);
                //std::cout << "1";
            }
        }
        glEnd();
    }
    std::cout << "\n";


    ///GLUTSWAPBUFFERS GOES HERE IF MISBEHAVING
    glutSwapBuffers();
    t = getchar();
    if(t == "w"){
      z = z + 0.2;
    }
    if(t == "s"){
      z = z - 0.2;
    }
    if(t == "a"){
      x = x - 0.2;
    }
    if(t == "d"){
      x = x + 0.2;
    }
    if(t == "f"){
      y = y + 0.2;
    }
    if(t == "c"){
      y = y - 0.2;
    }
    if(t == "p"){
      YAW++;
    }
    if(t == "o"){
      YAW--;
    }
    if(t == "8"){
      PITCH++;
    }
    if(t == "2"){
      PITCH--;
    }
    if(t == "6"){
      ROLL++;
    }
    if(t == "4"){
      ROLL--;
    }
    if(t ==""){
      ;
    }
    //ROLL++; ///FOR TESTING FPS

    glLoadIdentity();

    glRotatef(ROLL,0,0,1);
    glRotatef(PITCH,1,0,0);
    glRotatef(YAW,0,1,0);
    glTranslatef(x, y, z);

    /*std::cout << "\nYAW: " << YAW
    << "\nPITCH: " << PITCH
    << "\nROLL: " << ROLL
    << "\nx: " << x
    << "\ny: " << y
    << "\nz: " << z
    << "\novertime: " << overtime
    << "\nclock: " << CLOCKS_PER_SEC
    << "\nIterationsPerSecond: " << fps;*/

    calculateFPS();
    glutPostRedisplay();
}


// Sets up global attributes like clear color and drawing color, and sets up
// the desired projection and modelview matrices.
void init() {

  glClearColor(0.0, 0.0, 0.0, 1.0);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(100, 4.0/3.0, 0.1, 5);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  /*GL_ENABLE_BIT(*///GL_POLYGON_STIPPLE;//);
  glPolygonMode(GL_FRONT, /*GL_LINE*/GL_FILL); ///Makes all polygons
  ///Wireframe, greatly increases fps
  glPolygonMode(GL_BACK, /*GL_LINE*/GL_POINT);
  gluLookAt(10, 10, 10, 0, 0, 0, 0, 1, 0);
}

// Initializes GLUT, the display mode, and main window; registers callbacks;
// does application initialization; enters the main event loop.
int main(int argc, char** argv) {
  SDL_Init(SDL_INIT_EVERYTHING);
  init_keyboard();
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  glutInitWindowPosition(0, 0);
  glutInitWindowSize(800, 500);
  glutCreateWindow("ChambersFlight");
  glutDisplayFunc(display);
  /*void glutKeyboardFunc(void (*func)(unsigned char key,
                                   int x, int y));*/
  //glutKeyboardFunc(processNormalKeys);
  init();

  glutMainLoop();

}


I am still interested in optimisation though, some of the positions and movement bring this down to 9 fps.
Topic archived. No new replies allowed.