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
| #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
/*
* Window properties
*/
#define WINDOW_WIDTH 500
#define WINDOW_HEIGHT 500
#define WINDOW_X 100
#define WINDOW_Y 100
#define WINDOW_TITLE "RGB-ColorCube"
/*
* Perspective properties
*/
#define FOV_ANGLE 45
#define CENTER_X 0.0
#define CENTER_Y 0.0
#define CENTER_Z 0.0
#define VIEWER_X 0.0
#define VIEWER_Y 0.0
#define VIEWER_Z -5
#define UP_X 1.0
#define UP_Y 0.0
#define UP_Z 0.0
#define CLIPPLANE_NEAR 1.0
#define CLIPPLANE_FAR 20.0
#define ROTATION_SPEED 2.0
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
GLfloat rot_x = 0.0, rot_y = 0.0;
GLfloat saved_x, saved_y;
/*
* Material colors used for shading
*/
GLfloat red[4] = {.8, 0.0, 0.0, 1.0};
GLfloat white[4] = {.8, .8, .8, 1.0};
/*
* Function prototypes
*/
void usage(void);
void draw_scene(void);
void draw_object(void);
void init(int argc, char **argv, void (*draw)(void));
void save_position(int button, int state, int x, int y);
struct point get_coords(double a, double b);
void vertex(double a, double b);
void rotate(int x, int y);
void set_color(int angle);
int main(int argc, char **argv) {
/*
* Init OpenGL and enter the event loop
*/
init(argc, argv, draw_scene);
return 0;
}
/*
* Handle rotations and buffer swapping and call the function draw_object
* which does the actual drawing
*/
void draw_scene(void) {
static GLfloat old_rot_matrix[16];
static int initialized = 0;
GLfloat new_rot_matrix[16];
/* calculate new rotation matrix */
glPushMatrix();
glLoadIdentity();
glRotatef(rot_x, 1.0, 0.0, 0.0);
glRotatef(rot_y, 0.0, 1.0, 0.0);
glGetFloatv(GL_MODELVIEW_MATRIX, new_rot_matrix);
glPopMatrix();
/* calculate total rotation */
glPushMatrix();
glLoadIdentity();
glMultMatrixf(new_rot_matrix);
if (initialized) {
glMultMatrixf(old_rot_matrix);
}
glGetFloatv(GL_MODELVIEW_MATRIX, old_rot_matrix);
initialized = 1;
glPopMatrix();
glPushMatrix();
glMultMatrixf(old_rot_matrix);
draw_object();
glPopMatrix();
glFlush();
glutSwapBuffers();
}
/*
* Initialize the OpenGL machine
*/
void init(int argc, char **argv, void (*draw)(void)) {
GLfloat light0_pos[] = {100.0, 100.0, 100.0, 1.0};
GLfloat light0_color[] = {1.0, 1.0, 1.0, 1.0};
GLfloat ambient_light[] = {0.8, 0.8, 0.8, 1.0};
glutInit(&argc, argv);
/* Create a window */
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutInitWindowPosition(WINDOW_X, WINDOW_Y);
glutCreateWindow(WINDOW_TITLE);
glClearColor(0.0, 0.0, 0.0, 0.0);
/* Set up some light sources */
glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_color);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient_light);
glEnable(GL_LIGHT0);
/* glEnable(GL_LIGHTING); */
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
/* Create a viewing frustum */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(FOV_ANGLE, WINDOW_WIDTH/WINDOW_HEIGHT, CLIPPLANE_NEAR,
CLIPPLANE_FAR);
gluLookAt(VIEWER_X, VIEWER_Y, -VIEWER_Z, CENTER_X, CENTER_Y, CENTER_Z,
UP_X, UP_Y, UP_Z);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(VIEWER_X, VIEWER_Y, VIEWER_Z);
glutDisplayFunc(draw);
glutMouseFunc(save_position);
glutMotionFunc(rotate);
glutMainLoop();
/* Not reached */
}
/*
* Save the position of the mouse pointer where the bottun press occured
*/
void save_position(int button, int state, int x, int y) {
if (state == GLUT_DOWN) {
saved_x = x;
saved_y = y;
}
}
/*
* Calculate the angle the object has rotated by since the last update
*/
void rotate(int x, int y) {
rot_y = (GLfloat)(x - saved_x) * ROTATION_SPEED;
rot_x = (GLfloat)(y - saved_y) * ROTATION_SPEED;
saved_x = x;
saved_y = y;
glutPostRedisplay();
}
/*
* Draw a object
*/
void draw_object(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUAD_STRIP);
glNormal3f(0,0,1);
glColor3f(0,0,0);
glVertex3f(-0.5,-0.5,-0.5);
glColor3f(0,1,0);
glVertex3f(-0.5,0.5,-0.5);
glColor3f(1,0,0);
glVertex3f(0.5,-0.5,-0.5);
glColor3f(1,1,0);
glVertex3f(0.5,0.5,-0.5);
glNormal3f(1,0,0);
glColor3f(1,0,1);
glVertex3f(0.5,-0.5,0.5);
glColor3f(1,1,1);
glVertex3f(0.5,0.5,0.5);
glNormal3f(0,0,1);
glColor3f(0,0,1);
glVertex3f(-0.5,-0.5,0.5);
glColor3f(0,1,1);
glVertex3f(-0.5,0.5,0.5);
glNormal3f(-1,0,0);
glColor3f(0,0,0);
glVertex3f(-0.5,-0.5,-0.5);
glColor3f(0,1,0);
glVertex3f(-0.5,0.5,-0.5);
glEnd();
glBegin(GL_QUADS);
glNormal3f(0,1,0);
glColor3f(0,1,0);
glVertex3f(-0.5,0.5,-0.5);
glColor3f(0,1,1);
glVertex3f(-0.5,0.5,0.5);
glColor3f(1,1,1);
glVertex3f(0.5,0.5,0.5);
glColor3f(1,1,0);
glVertex3f(0.5,0.5,-0.5);
glEnd();
glBegin(GL_QUADS);
glNormal3f(0,-1,0);
glColor3f(0,0,0);
glVertex3f(-0.5,-0.5,-0.5);
glColor3f(0,0,1);
glVertex3f(-0.5,-0.5,0.5);
glColor3f(1,0,1);
glVertex3f(0.5,-0.5,0.5);
glColor3f(1,0,0);
glVertex3f(0.5,-0.5,-0.5);
glEnd();
} |
Partager