forked from cirosantilli/cpp-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
triangle_rotate.c
117 lines (98 loc) · 3.09 KB
/
triangle_rotate.c
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
/*
Animation.
C11 and C++11 have a nanosecond clock, and previous versions only go down to seconds:
http://stackoverflow.com/questions/361363/how-to-measure-time-in-milliseconds-using-ansi-c/36095407#36095407
So GLUT, `glutGet(GLUT_ELAPSED_TIME);` looks like the best option for C portability.
To make things smoother, we also need:
glutIdleFunction();
glutSwapBuffers();
glutInitDisplayMode(GLUT_DOUBLE);
We always draw to the back buffer, and `glutSwapBuffers` likely only swaps once per vsync.
Also have a look at `glfwSwapInterval(1);`.
TODO: why use glutIdleFunction() ? Why not do it directly on the `display`?
This setup seems to always lead to one idle per display.
See also:
http://gamedev.stackexchange.com/questions/8623/a-good-way-to-build-a-game-loop-in-opengl?lq=1
## FPS
- http://gamedev.stackexchange.com/questions/83159/simple-framerate-counter
- http://stackoverflow.com/questions/28530798/how-to-make-a-basic-fps-counter
- http://stackoverflow.com/questions/5627803/how-to-calculate-fps-in-opengl
- http://stackoverflow.com/questions/8779936/what-is-the-correct-way-to-calculate-the-fps-given-that-gpus-have-a-task-queue-a
Here we just count display calls, but not exaclty how many frames were rendered.
*/
#include <assert.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include "common_glut.h"
/* Degrees per second. */
static const GLfloat speed = 45.0f;
static GLfloat angle = 0.0f;
static int old_t;
static int debug;
static int last_func = 1;
static void display(void) {
if (debug) {
puts("display");
assert(last_func == 1);
last_func = 0;
}
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
glRotatef(angle, 0.0f, 0.0f, 1.0f);
glTranslatef(0.0, -1.0/sqrt(3.0), 0.0);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, sqrt(3.0), 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, 0.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
glEnd();
glPopMatrix();
glutSwapBuffers();
common_glut_fps_print();
}
static void reshape(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2.0, 2.0, -2.0, 2.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}
void idle(void) {
int t;
if (debug) {
puts("idle");
assert(last_func == 0);
last_func = 1;
}
t = glutGet(GLUT_ELAPSED_TIME);
angle += speed * (t - old_t) / 1000.0;
angle = fmod(angle, 360.0);
old_t = t;
glutPostRedisplay();
}
void init(void) {
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
common_glut_fps_init();
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
if (argc > 1)
debug = 1;
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glutMainLoop();
return EXIT_SUCCESS;
}