Feladat - Megvilágított kocka készítése
A feladat olyan OpenGL program létrehozása, mely egy kockát jelenít meg, a következő módon:
- a kocka legyen olyan anyagból, amely főleg a sárga ambiens és diffúz fényt veri vissza,
- az anyag meghatározásakor használj színkövetést,
- csak a kocka egyik oldalát kell megvilágítani, mivel a másik nem látszik,
- a használt fény egy sötétebb szürke ambiens és egy világosabb szürke diffúz fényt tartalmazzon, közép szürke spekuláris komponenssel.
A kocka forgatható legyen az x és az y tengelye körül a szokásos módon.
Az alábbi eljárásokat kell használni a normálisoknál:
- normális kiszámítása:
void calcNormal(float vertices[3][3], float out[3]) { float v1[3], v2[3]; static const int x = 0; static const int y = 1; static const int z = 2;
// Calculate two vectors from the three points v1[x] = vertices[0][x] - vertices[1][x]; v1[y] = vertices[0][y] - vertices[1][y]; v1[z] = vertices[0][z] - vertices[1][z];
v2[x] = vertices[1][x] - vertices[2][x]; v2[y] = vertices[1][y] - vertices[2][y]; v2[z] = vertices[1][z] - vertices[2][z];
// Take the cross product of the two vectors to get // the normal vector which will be stored in out out[x] = v1[y]*v2[z] - v1[z]*v2[y]; out[y] = v1[z]*v2[x] - v1[x]*v2[z]; out[z] = v1[x]*v2[y] - v1[y]*v2[x];
// Normalize the vector (shorten length to one) ReduceToUnit(out); }
- normális normalizálása:
void ReduceToUnit(float vector[3]) { float length;
// Calculate the length of the vector length = (float)sqrt((vector[0]*vector[0]) + (vector[1]*vector[1]) + (vector[2]*vector[2]));
// Keep the program from blowing up by providing an exceptable // value for vectors that may calculated too close to zero. if(length == 0.0f) length = 1.0f;
// Dividing each element by the length will result in a // unit normal vector. vector[0] /= length; vector[1] /= length; vector[2] /= length; }
- normális kirajzolása:
void drawNormalFront(float vertex[3], float normal[3]) { glColor3f(0.0, 0.0, 1.0); glBegin(GL_LINES); glVertex3fv(vertex); glVertex3f(vertex[0]+5.0*normal[0], vertex[1]+5.0*normal[1], vertex[2]+5.0*normal[2]); glEnd(); }
Tippek:
- használd a Segédanyag részből letölthető alapZH.c nevű forráskódot kiindulópontként.