/* Shoot in the direction of attack_vector */ static void pilot_shoot(struct Pilot *pilot) { double xoff,yoff; xoff = pilot->attack_vector.x * (pilot->walker.physics.radius+1); yoff = pilot->attack_vector.y * (pilot->walker.physics.radius+1); add_projectile(make_bullet ( pilot->walker.physics.x + xoff, pilot->walker.physics.y + yoff, addVectors(pilot->walker.physics.vel, multVector(pilot->attack_vector,10.0)))); pilot->weap_cooloff = 7; }
void PLYObject::draw() { // setup default material glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular); glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, shininess); glColor3fv(diffuse); // set lighting if enabled // Otherwise, compute colors if (light) { glEnable(GL_LIGHTING); } else { glDisable(GL_LIGHTING); for (int v=0; v<nv; v++) { // Instead of this, use the Phong illumination equation to find the color // Phong equation with attenuation factor att // If = (As*Am) + ((Al*Am) + Id + Is) * att // Get ModelView float M[16]; Matrix4f modelViewMatrix; glGetFloatv(GL_MODELVIEW_MATRIX, M); for (int i = 0; i < 3; i++) { modelViewMatrix[i][3] = 0.0; modelViewMatrix[3][i] = 0.0; for (int j = 0; j < 3; j++) { modelViewMatrix[i][j] = M[i*4+j]; } } modelViewMatrix[3][3] = 1.0; // ModelView is already here in a Matrix4f for operations Vector3f vVertex; multVector(vVertex, modelViewMatrix, vertices[v]); Vector4f lightSource; copy(lightSource, light_pos); //glGetLightfv(GL_LIGHT0, GL_POSITION, lightSource); Vector3f lightDir; sub(lightDir, lightSource, vVertex); float d = length(lightDir); // Get parameters for the ambient part float Al[4] = {0.0f, 0.0f, 0.0f, 0.0f }; glGetLightfv( GL_LIGHT0, GL_AMBIENT, Al ); Vector4f As = {0.0f, 0.0f, 0.0f, 0.0f }; glGetFloatv( GL_LIGHT_MODEL_AMBIENT, As ); float Dl[4] = {0.0f, 0.0f, 0.0f, 0.0f }; glGetLightfv( GL_LIGHT0, GL_DIFFUSE, Dl ); float Sl[4] = {0.0f, 0.0f, 0.0f, 0.0f }; glGetLightfv( GL_LIGHT0, GL_SPECULAR, Sl ); Vector4f Am = {0.0f, 0.0f, 0.0f, 0.0f }; copy(Am, ambient); float Dm[4] = {0.0f, 0.0f, 0.0f, 0.0f }; copy(Dm, diffuse); float Sm[4] = {0.0f, 0.0f, 0.0f, 0.0f }; copy(Sm, specular); float f = shininess[0]; // copy shininess float constantAttenuation = 0.0f; glGetLightfv( GL_LIGHT0, GL_CONSTANT_ATTENUATION, &constantAttenuation); float linearAttenuation = 0.0f; glGetLightfv( GL_LIGHT0, GL_LINEAR_ATTENUATION , &linearAttenuation ); float quadraticAttenuation = 0.0f; glGetLightfv( GL_LIGHT0, GL_QUADRATIC_ATTENUATION , &quadraticAttenuation); float att = (constantAttenuation + (linearAttenuation*d) + (quadraticAttenuation*d*d) ); // Start the phong equation with the ambient component Vector4f temp1; multVectors4(temp1, As, Am); Vector4f temp2; multVectors4(temp2, Al, Am); scale4(temp2, att); Vector4f final_color; //final_color = (As * Am) + (Al * Am)*att; add(final_color, temp1, temp2); // Calculate lambertTerm Vector3f N; normalizeVector(N, normals[v]); Vector3f L; normalizeVector(L, lightDir); float lambertTerm = dotProd(N,L); if (lambertTerm > 0.0){ // diffuse component multVectors4(temp1,Dl,Dm); scale4(temp1,lambertTerm); add(final_color, final_color, temp1); // specular component Vector3f E; normalizeVector(E,viewer_pos); Vector3f R; scale4(temp1,lambertTerm*2,N); sub(R, temp1, L); // reflect(-L,N) float dotProduct = dotProd(R,E); float specular_factor; if(dotProduct > 0.0){ specular_factor = pow(dotProduct, f); } else { specular_factor = 0; // pow(0.0, f); } multVectors4(temp1, Sl, Sm); scale4(temp1, specular_factor*att, temp1); add(final_color, final_color, temp1); // printf("DM are %f , %f and %f", final_color[0], final_color[1], final_color[2]); } //Colors [v][i] should take the colors of final_color, how? colors[v][0] = final_color[0]*255; colors[v][1] = final_color[1]*255; colors[v][2] = final_color[2]*255; } } // Now do the actual drawing of the model glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glBegin(GL_TRIANGLES); for (int i = 0; i < nf; i++) { for (int j = 0; j < 3; j++) { glColor3ubv((GLubyte*)colors[faces[i][j]]); glNormal3fv(normals[faces[i][j]]); // vertex normal glVertex3fv(vertices[faces[i][j]]); // vertex coordinates } } glEnd(); glDisable(GL_POLYGON_OFFSET_FILL); if (hascolor) glDisable(GL_COLOR_MATERIAL); }