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
/* * Progetto di Grafica 2008 - Gnesa Gianni (Gnix) * * Il nostro lavoro consisteva nello studio delle luci/ombre su di un * modello come ad esempio una sedia. * */ #include <math.h> #include <stdio.h> #include <stdlib.h> #include <GL/glut.h> // Variabili globali double rx = 0.0; double ry = 0.0; float l[] = { 70.0, 20.0, -40.0 }; // Coordinate della sorgente di luce float n[] = { 0.0, -1.0, 0.0 }; // Normal vector for the plane float e[] = { 0.0, -60.0, 0.0 }; // Point of the plane // Prototipi void help(); // Questa funzione e' chiamata ogni volta che l'oggetto deve essere disegnato // (una volta per disegnare l'oggetto e una volta per la sua ombra). void draw() { // Schienale glPushMatrix(); glTranslatef(-20.0,0.0,0.0); glScalef(0.1,1.0,1.0); glutSolidCube(40.0); glPopMatrix(); // Piano dove ti siedi glPushMatrix(); glTranslatef(0.0,-18.0,0.0); glScalef(1.0,0.1,1.0); glutSolidCube(40.0); glPopMatrix(); // (guardando la sedia da davanti) gamba posteriore sinistra glPushMatrix(); glTranslatef(-20.0,-40.0,-18.0); glScalef(0.1,1.0,0.1); glutSolidCube(40.0); glPopMatrix(); // (guardando la sedia da davanti) gamba posteriore destra glPushMatrix(); glTranslatef(-20.0,-40.0,18.0); glScalef(0.1,1.0,0.1); glutSolidCube(40.0); glPopMatrix(); // (guardando la sedia da davanti) gamba anteriore destra glPushMatrix(); glTranslatef(18.0,-40.0,18.0); glScalef(0.1,1.0,0.1); glutSolidCube(40.0); glPopMatrix(); // (guardando la sedia da davanti) gamba anteriore sinistra glPushMatrix(); glTranslatef(18.0,-40.0,-18.0); glScalef(0.1,1.0,0.1); glutSolidCube(40.0); glPopMatrix(); // Teapot glPushMatrix(); glTranslatef(60.0,-53.0,-30.0); glRotatef(180.0,0.0,1.0,0.0); glutSolidTeapot(10.0); glPopMatrix(); } // Questa funzione moltiplica la matrice modelview corrente con una // matrice di proiezione calcolata. // // l e' la posizione della sorgente di luce // e e' il punto nel piano dove sara' proiettata l'ombra // n e' il vettore normale del piano void glShadowProjection(float * l, float * e, float * n) { float d, c; float mat[16]; d = n[0]*l[0] + n[1]*l[1] + n[2]*l[2]; c = e[0]*n[0] + e[1]*n[1] + e[2]*n[2] - d; // Creazione della matrice di proiezione mat[0] = l[0]*n[0]+c; mat[4] = n[1]*l[0]; mat[8] = n[2]*l[0]; mat[12] = -l[0]*c-l[0]*d; mat[1] = n[0]*l[1]; mat[5] = l[1]*n[1]+c; mat[9] = n[2]*l[1]; mat[13] = -l[1]*c-l[1]*d; mat[2] = n[0]*l[2]; mat[6] = n[1]*l[2]; mat[10] = l[2]*n[2]+c; mat[14] = -l[2]*c-l[2]*d; mat[3] = n[0]; mat[7] = n[1]; mat[11] = n[2]; mat[15] = -d; // Moltiplica matrice glMultMatrixf(mat); } // Questa funzione si occupa di disegnare la scena void render() { glClearColor(0.36,0.25,0.20,0.0); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glLightfv(GL_LIGHT0, GL_POSITION, l); glDisable(GL_CULL_FACE); glDisable(GL_LIGHTING); // Disegna sorgente di luce glColor3f(1.0,1.0,0.0); glPointSize(5.0); glBegin(GL_POINTS); glVertex3f(l[0],l[1],l[2]); glEnd(); glPointSize(1.0); // Disegna il piano in cui sara' proiettata l'ombra. Attenzione: alla // coordinata y del piano viene sottratto 0.1 in modo che il piano si // trovi appena sotto l'ombra, per evitare rischi di rappresentazione // dovuti al Z-Buffer. glColor3f(0.55,0.27,0.07); // chocolate4 X 139;69;19 8B4513 glBegin(GL_QUADS); glNormal3f(0.0,1.0,0.0); glVertex3f(-1300.0,e[1]-0.1, 1300.0); glVertex3f( 1300.0,e[1]-0.1, 1300.0); glVertex3f( 1300.0,e[1]-0.1,-1300.0); glVertex3f(-1300.0,e[1]-0.1,-1300.0); glEnd(); // Disegna la scena glPushMatrix(); glRotatef(ry,0,1,0); glRotatef(rx,1,0,0); glEnable(GL_LIGHTING); glColor3f(0.55,0.28,0.15); draw(); glPopMatrix(); // Disegna l'ombra glPushMatrix(); glShadowProjection(l,e,n); glRotatef(ry,0,1,0); glRotatef(rx,1,0,0); glDisable(GL_LIGHTING); glColor3f(0.36,0.2,0.09); // baker's chocolate N 92;51;23 5C3317 draw(); glPopMatrix(); glutSwapBuffers(); } // Gestione keyboard void keyboard(unsigned char c, int a, int b) { if ( c==27 ) exit(0); else if ( c=='s' ) l[1]-=5.0; else if ( c=='w' ) l[1]+=5.0; else if ( c=='a' ) l[0]-=5.0; else if ( c=='d' ) l[0]+=5.0; else if ( c=='q' ) l[2]-=5.0; else if ( c=='e' ) l[2]+=5.0; else if ( c=='h' ) help(); glutPostRedisplay(); } // Visualizza l'help void help() { printf("========================================================\n"); printf(" Progetto di Grafica 2008 - Gnesa Gianni (Gnix)\n"); printf("--------------------------------------------------------\n\n"); printf(" w Sposta la sorgente di luce in alto\n"); printf(" s Sposta la sorgente di luce in basso\n"); printf(" a Sposta la sorgente di luce a sinistra\n"); printf(" d Sposta la sorgente di luce a destra\n"); printf(" q Sposta la sorgente di luce avanti\n"); printf(" e Sposta la sorgente di luce indietro\n"); printf(" h Visualizza Help\n\n"); printf("========================================================\n"); } // Rifai il viewport se si modifca la dimensione dell'immagine void resize(int w, int h) { glViewport(0, 0, w, h); } // Programma principale int main(int argc, char * argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutCreateWindow("Progetto di Grafica 2008 - Gnesa Gianni (Gnix)"); glutReshapeFunc(resize); glutReshapeWindow(700,700); glutKeyboardFunc(keyboard); glutDisplayFunc(render); glEnable(GL_NORMALIZE); glEnable(GL_LIGHTING); glEnable(GL_COLOR_MATERIAL); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHT0); glEnable(GL_TEXTURE_2D); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0f, 1.0, 1.0, 400.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0, 0.0, -150.0); help(); glutMainLoop(); return 0; }
Refactorings
No refactoring yet !
This code draw a chair and his shadow.