void vecNormalize(Vec v) { Flt len = vecLength(v); if (len == 0.0) { vecMake(0,0,1,v); return; } len = 1.0 / len; v[0] *= len; v[1] *= len; v[2] *= len; }
/* --------------- Platform Class --------------- */ Platform::Platform(int width, int height, int x, int y, int tex) { body.type = RECTANGLE; id = PLATFORM; tex_id = tex; vecMake(x,y,body.center); vecCopy(body.center, prevPosition); vecZero(velocity); body.width = width; body.height = height; textureWidth = 15; // !!!!! need to merge with game's texture data !!!!! textureHeight = 15; horizontalTiles = width / textureWidth; verticalTiles = height / textureHeight; rgb[0] = 90; rgb[1] = 140; rgb[2] = 90; }
void DrawGLScene5() { Vec v1,v2,v3,v4,v5,norm; glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); glLoadIdentity(); glLightfv(GL_LIGHT0, GL_POSITION,LightPosition); glTranslatef(-1.5f,0.0f,-6.0f); glRotatef(rtri,0.0f,1.0f,0.0f); glColor3f(0.8f,0.0f,0.0f); glBegin(GL_TRIANGLES); //Notice the process here... //1. build verts //2. make 2 vectors //3. find cross product. that's the normal //4. normalize it to a length of 1 vecMake( 0.0f, 1.0f, 0.0f, v1); vecMake(-1.0f,-1.0f, 1.0f, v2); vecMake( 1.0f,-1.0f, 1.0f, v3); vecSub(v2,v1,v4); vecSub(v3,v1,v5); vecCrossProduct(v4,v5,norm); vecNormalize(norm); glNormal3fv(norm); glVertex3fv(v1); glVertex3fv(v2); glVertex3fv(v3); vecMake( 0.0f, 1.0f, 0.0f,v1); vecMake( 1.0f,-1.0f, 1.0f,v2); vecMake( 1.0f,-1.0f,-1.0f,v3); vecSub(v2,v1,v4); vecSub(v3,v1,v5); vecCrossProduct(v4,v5,norm); vecNormalize(norm); glNormal3fv(norm); glVertex3fv(v1); glVertex3fv(v2); glVertex3fv(v3); vecMake( 0.0f, 1.0f, 0.0f,v1); vecMake( 1.0f,-1.0f,-1.0f,v2); vecMake(-1.0f,-1.0f,-1.0f,v3); vecSub(v2,v1,v4); vecSub(v3,v1,v5); vecCrossProduct(v4,v5,norm); vecNormalize(norm); glNormal3fv(norm); glVertex3fv(v1); glVertex3fv(v2); glVertex3fv(v3); vecMake( 0.0f, 1.0f, 0.0f,v1); vecMake(-1.0f,-1.0f,-1.0f,v2); vecMake(-1.0f,-1.0f, 1.0f,v3); vecSub(v2,v1,v4); vecSub(v3,v1,v5); vecCrossProduct(v4,v5,norm); vecNormalize(norm); glNormal3fv(norm); glVertex3fv(v1); glVertex3fv(v2); glVertex3fv(v3); glEnd(); glLoadIdentity(); glLightfv(GL_LIGHT0, GL_POSITION, LightPosition); glTranslatef(1.5f,0.0f,-7.0f); glRotatef(rquad,1.0f,1.0f,1.0f); glColor3f(0.0f,0.5f,1.0f); glBegin(GL_QUADS); //top //notice the normal being set glNormal3f( 0.0f, 1.0f, 0.0f); glVertex3f( 1.0f, 1.0f,-1.0f); glVertex3f(-1.0f, 1.0f,-1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // bottom of cube glNormal3f( 0.0f, -1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 1.0f); glVertex3f(-1.0f,-1.0f, 1.0f); glVertex3f(-1.0f,-1.0f,-1.0f); glVertex3f( 1.0f,-1.0f,-1.0f); // front of cube glNormal3f( 0.0f, 0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); glVertex3f(-1.0f,-1.0f, 1.0f); glVertex3f( 1.0f,-1.0f, 1.0f); // back of cube. glNormal3f( 0.0f, 0.0f, -1.0f); glVertex3f( 1.0f,-1.0f,-1.0f); glVertex3f(-1.0f,-1.0f,-1.0f); glVertex3f(-1.0f, 1.0f,-1.0f); glVertex3f( 1.0f, 1.0f,-1.0f); // left of cube glNormal3f( -1.0f, 0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); glVertex3f(-1.0f, 1.0f,-1.0f); glVertex3f(-1.0f,-1.0f,-1.0f); glVertex3f(-1.0f,-1.0f, 1.0f); // Right of cube glNormal3f( 1.0f, 0.0f, 0.0f); glVertex3f( 1.0f, 1.0f,-1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); glVertex3f( 1.0f,-1.0f, 1.0f); glVertex3f( 1.0f,-1.0f,-1.0f); glEnd(); rtri+=2.0f; rquad-=1.0f; }
void init_ship(void) { Object *o; nobjects=0; int ncolors = 0; char ts[200], *ptr; int nverts=0; nfaces = 0; Vec *vert = new Vec[3000]; iVec *face = new iVec[3000]; Vec *color = new Vec[3000]; FILE *fin = fopen("ship.txt", "r"); if (!fin) printf("file not open\n"); if (fin) { while(1) { if (feof(fin)) break; fgets(ts,100,fin); ptr = ts; if (*ptr == 'v') { ptr = findSpace(ptr)+1; vert[nverts][0] = atof(ptr);// * 200.0; ptr = findSpace(ptr)+1; vert[nverts][1] = atof(ptr);// * 200.0; ptr = findSpace(ptr)+1; vert[nverts][2] = atof(ptr);// * 200.0; nverts++; continue; } if (*ptr == 'f') { ptr = findSpace(ptr)+1; face[nfaces][0] = atoi(ptr); ptr = findSpace(ptr)+1; face[nfaces][1] = atoi(ptr); ptr = findSpace(ptr)+1; face[nfaces][2] = atoi(ptr); nfaces++; } if (*ptr == 'c') { ptr = findSpace(ptr)+1; color[ncolors][0] = atof(ptr); ptr = findSpace(ptr)+1; color[ncolors][1] = atof(ptr); ptr = findSpace(ptr)+1; color[ncolors][2] = atof(ptr); ncolors++; } } fclose(fin); } //Vec mv; for (int i=0; i<nfaces; i++) { o = &object[nobjects]; o->type = TYPE_TRIANGLE; int f; f=face[i][0]-1; vecMake(vert[f][0],vert[f][1],vert[f][2],o->tri[1]); f=face[i][1]-1; vecMake(vert[f][0],vert[f][1],vert[f][2],o->tri[0]); f=face[i][2]-1; vecMake(vert[f][0],vert[f][1],vert[f][2],o->tri[2]); vecMake(color[i][0],color[i][1],color[i][2], o->color); //vecMake(0.0, 0.0, 0.0, mv); for (int j=0; j<3; j++) { //move(mv, o->tri[j]); scale(0.45, o->tri[j]); } getTriangleNormal(o->tri, o->norm); //change the color based on normal //if (o->norm[2] > 0.5 || o->norm[2] < -0.5) { // vecMake(.1,.1,.1, o->color); //} nobjects++; } delete vert; delete face; delete color; }