Blame view

rts/rts_glutRenderWindow.h 3.14 KB
ebb721c7   David Mayerich   new repository fo...
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
  #include <GL/glew.h>
  #include <GL/glut.h>
  #include "rtsQuaternion.h"
  #include "rtsCamera.h"
  
  float d_angle = 0.05;
  
  rtsCamera rts_glut_camera;
  unsigned int mouse_x;
  unsigned int mouse_y;
  int mouse_button;
  
  
  //user display function pointer
  void (*UserDisplay)(void);
  
  
  
  /*void KeyboardFunction(unsigned char key, int x, int y)
  {
  	if(key == 27)
  		exit(0);
  
  }*/
  
  /*void SpecialKeys(int key, int x, int y)
  {
  	rtsQuaternion<float> new_rotation;
  
  	if(key == GLUT_KEY_UP)
  		rts_glut_camera.OrbitFocus(0, d_angle);
  	if(key == GLUT_KEY_DOWN)
  		rts_glut_camera.OrbitFocus(0, -d_angle);
  	if(key == GLUT_KEY_LEFT)
  		rts_glut_camera.OrbitFocus(d_angle, 0.0);
  	if(key == GLUT_KEY_RIGHT)
  		rts_glut_camera.OrbitFocus(-d_angle, 0.0);
  }*/
  
  void MouseDrag(int x, int y)
  {
  	int dx = x - mouse_x;
  	int dy = y - mouse_y;
  
  	if(mouse_button == GLUT_LEFT_BUTTON)
  		rts_glut_camera.OrbitFocus(-dx*0.01, dy*0.01);
  	else if(mouse_button == GLUT_MIDDLE_BUTTON)
  		rts_glut_camera.Zoom(dy*rts_glut_camera.getFOV()/60.0);
  
  	mouse_x = x;
  	mouse_y = y;
  
  }
  
  void MouseMove(int x, int y)
  {
  	mouse_x = x;
  	mouse_y = y;
  }
  
  void MouseClick(int button, int state, int x, int y)
  {
  	if(state == GLUT_DOWN)
  		mouse_button = button;
  
  
  }
  
  void IdleFunction()
  {
  	glutPostRedisplay();
  }
  
  void RenderCamera()
  {
  	//set viewport
  	glMatrixMode(GL_PROJECTION);
  	glLoadIdentity();
  	glViewport(0, 0, glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
  
  	//compute the aspect ratio
  	float aspect_ratio = (float)glutGet(GLUT_WINDOW_WIDTH)/(float)glutGet(GLUT_WINDOW_HEIGHT);
  	gluPerspective(rts_glut_camera.getFOV(), aspect_ratio, 0.01, 10.0);
  
  	//render the camera
  	glMatrixMode(GL_MODELVIEW);
  	glLoadIdentity();
  
  	point3D<float> camera_position = rts_glut_camera.getPosition();
  	vector3D<float> camera_up = rts_glut_camera.getUp();
  	gluLookAt(camera_position.x,
  			  camera_position.y,
  			  camera_position.z,
  			  0.0, 0.0, 0.0,
  			  camera_up.x,
  			  camera_up.y,
  			  camera_up.z);
  }
  
  void rts_glutInitialize(const char* WindowName, int width = 1024, int height = 768)
  {
  	char *myargv [1];
  	int myargc=1;
  	myargv [0]=strdup ("AppName");
  	glutInit(&myargc, myargv);
  
  	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
  	glutInitWindowSize(width, height);
  	glutInitWindowPosition(200,0);
  	glutCreateWindow(WindowName);
  
  	//initialize GLEW
  	GLenum err = glewInit();
  	if(err != GLEW_OK)
  		fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
  	if (!glewIsSupported("GL_VERSION_2_0"))
  	{
  		printf("OpenGL 2.0 not supported\n");
  		//exit(1);
  	}
  	glEnable(GL_DEPTH_TEST);
  
  	glClearColor(1.0, 1.0, 1.0, 0.0);
  
  	rts_glut_camera.setPosition(0.0, 0.0, -1.0);
  	rts_glut_camera.setFOV(60);
  	rts_glut_camera.LookAt(0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  }
  
  void DisplayFunction()
  {
  	RenderCamera();
  	UserDisplay();
  	glutSwapBuffers();
  }
  
  void rts_glutStart(void (*display_func)(void))
  {
  	//glutSpecialFunc(SpecialKeys);
  	//glutKeyboardFunc(KeyboardFunction);
  	glutDisplayFunc(DisplayFunction);
  	glutMotionFunc(MouseDrag);
  	glutMouseFunc(MouseClick);
  	glutPassiveMotionFunc(MouseMove);
  	UserDisplay = display_func;
  	glutIdleFunc(IdleFunction);
  
  
  
  	//glutReshapeFunc(ReshapeFunction);
  	//glutMouseFunc(MouseFunction);
  	//glutMotionFunc(MotionFunction);
  	glutMainLoop();
  
  }