-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.c
More file actions
163 lines (121 loc) · 3.36 KB
/
camera.c
File metadata and controls
163 lines (121 loc) · 3.36 KB
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
/*
* Final project, CMPS 160, Fall 1999
* Ben Gertzfield <ben@cse.ucsc.edu>
*
* This program is released under the GNU GPL, version 2 or later.
*/
/* Main routines */
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <gtk/gtk.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glx.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include "prog.h"
struct camera *gkos_first_camera;
struct camera *gkos_cur_camera;
static unsigned long cam_id = 0;
/*
* Create a new camera and add it to the camera list.
*/
struct camera *gkos_new_camera(void)
{
struct camera *camera, *temp;
camera = (struct camera *) malloc(sizeof(struct camera));
if (camera == NULL) { /* malloc failed :( */
fprintf(stderr, "Cannot allocate memory for light %li, exiting\n",
cam_id);
exit(1);
}
camera->id = cam_id++;
camera->rot_angle.x = camera->rot_angle.y = camera->rot_angle.z =
camera->zoom = 0.0;
camera->fovy = 45.0;
camera->near_clip = 1.0;
camera->far_clip = 30.0;
camera->prev = camera->next = NULL;
gkos_cur_camera = camera;
if (gkos_first_camera == NULL) {
gkos_first_camera = camera;
return camera;
}
temp = gkos_first_camera;
while (temp->next != NULL)
temp = temp->next;
temp->next = camera;
camera->prev = temp;
return camera;
}
void gkos_pop_camera(void)
{
struct camera *first, *next;
if (gkos_first_camera == NULL)
return;
first = gkos_first_camera;
next = gkos_first_camera->next;
if (gkos_cur_camera == first)
gkos_cur_camera = next;
free(first);
gkos_first_camera = next;
}
/*
* Rotate the given camera about dx, dy, and dz.
*/
void gkos_rotate_camera(struct camera *camera, int x0, int y0, int z0,
int x, int y, int z)
{
int dx, dy, dz;
dx = x0 - x;
dy = y0 - y;
dz = z0 - z;
camera->rot_angle.x -= dx;
camera->rot_angle.y += dy;
camera->rot_angle.z -= dz;
if (camera->rot_angle.x > 360.0)
camera->rot_angle.x -= 360.0;
if (camera->rot_angle.x < 0.0)
camera->rot_angle.x += 360.0;
if (camera->rot_angle.y > 360.0)
camera->rot_angle.y -= 360.0;
if (camera->rot_angle.y < 0.0)
camera->rot_angle.y += 360.0;
if (camera->rot_angle.z > 360.0)
camera->rot_angle.z -= 360.0;
if (camera->rot_angle.z < 0.0)
camera->rot_angle.z += 360.0;
}
/*
* Zoom the camera in. We munge dz to be divided by 5, this is
* totally arbitrary.
*/
void gkos_zoom_camera(struct camera *camera, int z0, int z)
{
camera->zoom += (GLfloat) (z0 - z) / 5.0;
}
void gkos_setup_projection(struct camera *camera)
{
if (camera == NULL) {
fprintf(stderr, "setup_projection(): camera is null!\n");
return;
}
glMatrixMode(GL_PROJECTION); /* Modify the projection matrix */
glLoadIdentity(); /* Initialize to the identity matrix */
gluPerspective(camera->fovy, gkos_winw / gkos_winh,
camera->near_clip, camera->far_clip);
/* Set our camera up */
gluLookAt(0.0, 0.0, 20.0, 0.0, 0.0, -100.0,
0.0, 1.0, 0.0);
/* Move to where the current camera is */
glTranslatef(0.0, 0.0, -camera->zoom);
/* Rotate as the current camera is rotated */
glRotatef(camera->rot_angle.x, 1.0, 0.0, 0.0);
glRotatef(camera->rot_angle.y, 0.0, 1.0, 0.0);
glRotatef(camera->rot_angle.z, 0.0, 0.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}
// Local variables:
// c-file-style:"cc-mode"
// End: