-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleGLmain.cpp
120 lines (97 loc) · 2.77 KB
/
simpleGLmain.cpp
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
// simpleGLmain.cpp (Rob Farber)
// includes
#include <GL/glut.h>
#include <GL/freeglut.h>
#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
// The user must create the following routines:
// CUDA methods
extern void initCuda();
extern void runCuda();
extern void renderCuda(int);
// callbacks
extern void display();
extern void keyboard(unsigned char key, int x, int y);
extern void spkeyboard(int key, int x, int y);
extern void mouse(int button, int state, int x, int y);
extern void motion(int x, int y);
// GLUT specific variables
unsigned int window_width = 1024;
unsigned int window_height = 1024;
// Forward declarations of GL functionality
bool initGL(int argc, char** argv);
struct timeval start, end;
// Simple method to display the Frames Per Second in the window title
void computeFPS()
{
static int fpsCount=0;
static int fpsLimit=100;
fpsCount++;
if (fpsCount == fpsLimit) {
char fps[256];
long seconds = end.tv_sec - start.tv_sec;
long useconds = end.tv_usec - start.tv_usec;
long utime = ((seconds) * 1000000 + useconds);
double ifps = 1.0 /utime;//1.f / (mtime / 1000.f);
ifps = ifps * (1000000);// 1/usec -> 1/sec
sprintf(fps, "Cuda GL Interop Wrapper: %3.4f fps, Total Time: %3.4fms", ifps, (utime/(float)1000));
glutSetWindowTitle(fps);
fpsCount = 0;
}
}
void fpsDisplay()
{
gettimeofday( &start, NULL );
display();
gettimeofday( &end, NULL );
computeFPS();
}
// Main program
int main(int argc, char** argv)
{
if( argc > 1 )
{
int width = atoi(argv[1]);
int height = atoi(argv[2]);
if( width > 100 && height > 100 )
{
window_width = width;
window_height = height;
}
}
if (false == initGL(argc, argv)) {
return false;
}
initCuda();
// register callbacks
glutDisplayFunc(fpsDisplay);
glutKeyboardFunc(keyboard);
glutSpecialFunc(spkeyboard);
glutMouseFunc(mouse);
glutPassiveMotionFunc(motion);
// start rendering mainloop
glutMainLoop();
}
bool initGL(int argc, char **argv)
{
//Steps 1-2: create a window and GL context (also register callbacks)
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(window_width, window_height);
glutCreateWindow("Cuda GL Interop Demo (adapted from NVIDIA's simpleGL");
glutDisplayFunc(fpsDisplay);
glutKeyboardFunc(keyboard);
glutMotionFunc(motion);
// Step 3: Setup our viewport and viewing modes
glViewport(0, 0, window_width, window_height);
glClearColor(0.0, 0.0, 0.0, 1.0);
glDisable(GL_DEPTH_TEST);
// set view matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f);
return true;
}