void Camera::yawRight(void) { float tempVec[3]; rotateAroundVec(forwardVec, upVec, -cameraTurnSpeed, tempVec); vectorCopy(forwardVec, tempVec); rotateAroundVec(rightVec, upVec, -cameraTurnSpeed, tempVec); vectorCopy(rightVec, tempVec); }
void scene::runNPC(float movSpd, float rotSpd) { mesh *pMesh; mesh *wayPoint; for (mMeshList.moveFirst(); !mMeshList.eof(); mMeshList.moveNext()) { pMesh = mMeshList.getData(); if (pMesh->mName != NULL) if (strcmp(pMesh->mName, "npc") == 0) { if (pMesh->mTarget == NULL) { pMesh->mTarget = new float[3]; setMotion((long) pMesh, 1, false); moveMesh( (long) pMesh, MOV_VEL, 0, 0, randEx(movSpd * 0.5, movSpd) ); moveMesh( (long) pMesh, MOV_ANGVEL, randEx(rotSpd * 0.5, rotSpd), randEx(rotSpd * 0.5, rotSpd), randEx(rotSpd * 0.5, rotSpd) ); wayPoint = (mesh *) findWaypoint((long) pMesh); if (wayPoint != NULL) { vectorCopy(pMesh->mTarget, wayPoint->mOrigin); pMesh->mNext = wayPoint; } } else { wayPoint = pMesh->mNext; if ( vectorDist(pMesh->mOrigin, wayPoint->mOrigin) < pMesh->mRadius + wayPoint->mRadius ) { wayPoint = wayPoint->mNext; if (wayPoint != NULL) { vectorCopy(pMesh->mTarget, wayPoint->mOrigin); pMesh->mNext = wayPoint; } } } } } }
bool checkPointMeshCollision(float *position, Mesh *mesh, float *normal, float *contactpoint) { float maxdist = 0; bool planecollision = false; int i; for (i = 0; i < mesh->polygoncount; i++) { class Polygon *polygon = &mesh->polygons[i]; float dist = distanceFromPlane(position, polygon->planenormal, polygon->planedistance); if (dist < 0) { bool directcollision = true; /*for (j = 0; j < polygon->vertexcount; j++){ float *p1 = polygon->vertices[j]->position; float *p2 = polygon->vertices[(j+1)%polygon->vertexcount]->position; float *p3 = polygon->vertices[(j+2)%polygon->vertexcount]->position; float v1[3], v2[3]; vectorSub(v1, p2, p1); //Collision for polygon surface vectorSub(v2, p3, p2); float t1[3]; vectorProject(t1, v2, v1); float norm[3]; vectorSub(norm, v2, t1); vectorNormalize(norm); //Collision for polygon edges float newpoint[3]; vectorSub(newpoint, position, p1); float dist2 = vectorDot(newpoint, norm); if (dist2 < 0) directcollision = false; }*/ if (directcollision) { if (dist > maxdist || !planecollision) { vectorCopy(normal, polygon->planenormal); maxdist = dist; planecollision = true; } } } else { return false; } } if (planecollision) { vectorCopy(contactpoint, position); } else { return false; } return true; }
void Camera::transformWithMouse(float theta, float phi, float *forward, float *up, float *right) { float tempForward[3], tempUp[3], tempRight[3], tempVec[3]; rotateAroundVec(forwardVec, upVec, theta, tempVec); rotateAroundVec(rightVec, upVec, theta, tempRight); rotateAroundVec(tempVec, tempRight, phi, tempForward); rotateAroundVec(upVec, tempRight, phi, tempUp); vectorCopy(forward, tempForward); vectorCopy(right, tempRight); vectorCopy(up, tempUp); }
void ConjugateGradient3( float **A, float *b, float *x, int N, int iterations) { float r[N]; float r2[N]; float p[N]; float q[N]; //float beta[N]; //float alpha[N]; float beta; float alpha; float temp[N]; // initialization of residual vector: r_0 matrixVectorProduct( A, x, temp, N); vectorDifference( b, temp, r, N); // initialization of p_0 vectorCopy( r, p, N); for( int i=0; i<iterations; i++){ // q_k matrixVectorProduct( A, p, q, N); alpha = dotProduct( r, r, N); if(i>0){ beta = alpha / dotProduct( r2, r2, N); vectorScale( p, beta, p, N); vectorSum( r, p, p, N); } alpha /= dotProduct( p, q, N); // next x vectorScale( p, alpha, temp, N); vectorSum( x, temp, x, N); // old r vectorCopy( r, r2, N); // next r vectorScale( q, -alpha, temp, N); vectorSum( r, temp, r, N); //float *pointer ; } }
void addCollision(Object *source, Object *target, float *normal, float *contactpoint) { if (contactcount == MAXCONTACTS) { printf("Too many contacts!\n"); return; } Contact *contact = &contacts[contactcount++]; contact->object1 = source; contact->object2 = target; vectorCopy(contact->normal, normal); vectorCopy(contact->position, contactpoint); }
// points the camera at the given point in 3d space void Camera::pointAt(float* targetVec) { float tempVec[3]; float up[3] = { 0.0f, 0.0f, 1.0f }; forwardVec[0] = targetVec[0] - position[0]; forwardVec[1] = targetVec[1] - position[1]; forwardVec[2] = targetVec[2] - position[2]; normalizeVec(forwardVec); rotateAroundVec(forwardVec, up, -1.57079632679f, tempVec); tempVec[2] = 0; normalizeVec(tempVec); vectorCopy(rightVec, tempVec); rotateAroundVec(forwardVec, rightVec, 1.57079632679f, tempVec); vectorCopy(upVec, tempVec); }
/** * Vector a is rotated along each axis by the number of degrees specified by that component in the rotation vector. * This means that if you pass the rotation vector (45, 90, 0), vector a will be rotated 45 degrees along the * i axis, 90 degrees along the j axis, and 0 degrees along the k axis. */ DLLEX Vector *vectorRotate(Vector *a, Vector *rotation) { Vector *rotated = vectorCopy(a); double *i = &(rotated->components[0]), // declare pointers to save typing, and avoid having to reassign each component by hand *j = &(rotated->components[1]), // even if it means more memory references because I'm lazy. *k = &(rotated->components[2]); double iRot = rotation->components[0] * PI / 180.0, // Convert each rotational value to radians jRot = rotation->components[1] * PI / 180.0, kRot = rotation->components[2] * PI / 180.0; double sinI = sin(iRot), // Compute the sine and cosine of each rotation to avoid computing them multiple times cosI = cos(iRot), sinJ = sin(jRot), cosJ = cos(jRot), sinK = sin(kRot), cosK = cos(kRot); // rotate around i axis (i component remains constant) // not hardcore enough to write the rotations as one-liners *j = (*j)*cosI - (*k)*sinI; *k = (*j)*sinI + (*k)*cosI; // rotate around j axis (j component remains constant) *i = (*k)*sinJ + (*i)*cosJ; *k = (*k)*cosJ - (*i)*sinJ; // rotate around k axis (k component remains constant) *i = (*i)*cosK - (*j)*sinK; *j = (*i)*sinK + (*j)*cosK; return rotated; }
void Simulation::setTape(std::string stringTape) { if (tape != NULL) delete tape; std::vector<char> vectorCopy(stringTape.size() + 1); std::copy(stringTape.begin(), stringTape.end(), vectorCopy.begin()); vectorCopy.pop_back(); // Erase the ending \O character tape = new Tape(vectorCopy); }
void Camera::right(void) { float tempForward[3], tempUp[3], tempRight[3]; transformWithMouse(mouseLeftRight, mouseUpDown, tempForward, tempUp, tempRight); float vec[3]; vectorCopy(vec, tempRight); vectorMul(vec, cameraSpeed); vectorAdd(position, vec); }
int _start(void) { enableMMU(); vectorCopy();//拷贝中断向量表 enableInterruptCPSR(); enableExt(64, 26,4, 0); return 0; }
camera_frame_t* CAM_RecordCurrentFrame(void) { camera_frame_t* newFrame; newFrame = calloc(1,sizeof(camera_frame_t)); newFrame->next = 0; newFrame->time = simulationTime; vectorCopy(camera.position,newFrame->position); Quat_CreateFromMat3x3(orientationMatrix, newFrame->orientation); return newFrame; }
DLLEX Vector *vectorAdd(Vector *a, Vector *b) { Vector *v; v = vectorCopy(a); // create a vector copy of a, which will be returned // add up matching components v->components[0] += b->components[0]; v->components[1] += b->components[1]; v->components[2] += b->components[2]; return v; }
DLLEX Vector *vectorSub(Vector *a, Vector *b) { Vector *v; v = vectorCopy(a); // subtract matching components v->components[0] -= b->components[0]; v->components[1] -= b->components[1]; v->components[2] -= b->components[2]; return v; }
DLLEX Vector *vectorMul(Vector *a, double b) { Vector *v; v = vectorCopy(a); // we simply multiply each component of the vector by the scalar // this way the magnitude will be scaled by b, but the direction will remain the same v->components[0] *= b; v->components[1] *= b; v->components[2] *= b; return v; }
DLLEX Vector *vectorDiv(Vector *a, double b) { Vector *v; v = vectorCopy(a); // we simply divide each component of the vector by the scalar // this way the magnitude will be divided by b, but the direction will remain the same v->components[0] /= b; v->components[1] /= b; v->components[2] /= b; return v; }
void object::getVector(long opCode, float *vector) { switch (opCode) { case 0: vectorCopy(vector, mOrigin); break; case 1: vectorCopy(vector, mAngle); break; case 2: vectorCopy(vector, mScale); break; case 3: vectorCopy(vector, mPosVel); break; case 4: vectorCopy(vector, mAngVel); break; case 5: vectorCopy(vector, mPosAcc); break; case 6: vectorCopy(vector, mAngAcc); } }
void object::move(long opCode, float *vector) { switch (opCode) { case 0: vectorCopy(mOrigin, vector); break; case 1: vectorCopy(mAngle, vector); break; case 2: vectorCopy(mScale, vector); break; case 3: vectorCopy(mPosVel, vector); break; case 4: vectorCopy(mAngVel, vector); break; case 5: vectorCopy(mPosAcc, vector); break; case 6: vectorCopy(mAngAcc, vector); } buildTrans(); }
void CAM_SetCamera(vec3_t position,float head,float pitch,float aspect , float fov , float zNearl , float zFarl) { vectorCopy(position,camera.position); camera.position[3] = 1; camera.head = head; camera.pitch=pitch; CAM_UpdateOrientationMatrix(); camera.aspect = aspect; camera.fov = fov; camera.zNear = zNearl; camera.zFar = zFarl; }
bool checkSphereMeshCollision(float *sphereposition, float r, Mesh *mesh, float *normal, float *contactpoint) { float linenormal[3]; float pointnormal[3]; float maxdist = 0; bool planecollision = false; bool linecollision = false; bool pointcollision = false; int i, j; for (i = 0; i < mesh->polygoncount; i++) { class Polygon *polygon = &mesh->polygons[i]; float dist = distanceFromPlane(sphereposition, polygon->planenormal, polygon->planedistance); if (dist < r && dist > -r) { bool directcollision = true; for (j = 0; j < polygon->vertexcount; j++) { float *p1 = polygon->vertices[j]->position; float *p2 = polygon->vertices[(j + 1) % polygon->vertexcount]->position; float *p3 = polygon->vertices[(j + 2) % polygon->vertexcount]->position; float v1[3], v2[3]; vectorSub(v1, p2, p1); // Collision for polygon surface vectorSub(v2, p3, p2); float t1[3]; vectorProject(t1, v2, v1); float norm[3]; vectorSub(norm, v2, t1); vectorNormalize(norm); // Collision for polygon edges float newpoint[3]; vectorSub(newpoint, sphereposition, p1); float dist2 = vectorDot(newpoint, norm); if (dist2 < 0) { directcollision = false; float projloc = vectorDot(newpoint, v1) / vectorDot(v1, v1); if (projloc >= 0 && projloc <= 1) { float proj[3]; vectorScale(proj, v1, projloc); float projorth[3]; vectorSub(projorth, newpoint, proj); float l2 = vectorDot(projorth, projorth); if (l2 < r * r) { vectorNormalize(linenormal, projorth); if (dist < 0) vectorScale(linenormal, -1); linecollision = true; } } } // Collision for polygon vertices float pointdiff[3]; vectorSub(pointdiff, sphereposition, p1); float l3 = vectorDot(pointdiff, pointdiff); if (l3 < r * r) { vectorScale(pointnormal, pointdiff, 1.0 / sqrt(l3)); if (dist < 0) vectorScale(pointnormal, -1); pointcollision = true; } } if (directcollision) { if (dist > maxdist || !planecollision) { vectorCopy(normal, polygon->planenormal); maxdist = dist; planecollision = true; } } } } if (planecollision) { vectorScale(contactpoint, normal, -r); vectorAdd(contactpoint, sphereposition); } else if (linecollision) { vectorScale(contactpoint, linenormal, -r); vectorAdd(contactpoint, sphereposition); vectorCopy(normal, linenormal); } else if (pointcollision) { vectorScale(contactpoint, pointnormal, -r); vectorAdd(contactpoint, sphereposition); vectorCopy(normal, pointnormal); } else { return false; } return true; }
bool checkEdgeMeshCollision(float *p1, float *p2, Mesh *mesh, float *normal, float *contactpoint) { float ray[3]; vectorSub(ray, p2, p1); // UNUSED//float maxdist = 0; // UNUSED//bool collision = false; int i, j; tracehit hits[MAXPOLYGONS]; int hitcount = 0; for (i = 0; i < mesh->polygoncount; i++) { class Polygon *polygon = &mesh->polygons[i]; if (tracePlane(&hits[hitcount], p1, ray, polygon)) { hitcount++; } } if (hitcount < 2) return false; for (i = 1; i < hitcount; i++) { for (j = i; j > 0; j--) { if (hits[j].t < hits[j - 1].t) { float tempt = hits[j].t; hits[j].t = hits[j - 1].t; hits[j - 1].t = tempt; class Polygon *tempp = hits[j].polygon; hits[j].polygon = hits[j - 1].polygon; hits[j - 1].polygon = tempp; } else break; } } int negative = -1, positive = -1; for (i = 0; i < hitcount; i++) { // UNUSED//float t = hits[i].t; class Polygon *polygon = hits[i].polygon; float dot = vectorDot(ray, polygon->planenormal); if (dot > 0 && positive == -1) positive = i; if (dot < 0) negative = i; if (dot < 0 && positive != -1) return false; } if (negative == -1 || positive == -1) return false; /*for (i = 0; i < hitcount; i++){ float t = hits[i].t; class Polygon *polygon = hits[i].polygon; float dot = vectorDot(ray, polygon->planenormal); printf("%f ", dot); } printf("\n");*/ if (hits[negative].t < 0 || hits[positive].t > 1) return false; Edge *edge2 = findSharingEdge(hits[negative].polygon, hits[positive].polygon); // fflush(stdout); float cp1[3], cp2[3]; vectorScale(cp1, ray, hits[negative].t); vectorAdd(cp1, p1); vectorScale(cp2, ray, hits[positive].t); vectorAdd(cp2, p1); if (edge2 != NULL) { /*float ev1[3]; vectorSub(ev1, edge2->v2->position, edge2->v1->position); vectorCross(normal, ev1, ray); vectorScale(normal, vectorDot(normal, hits[positive].polygon->planenormal)); vectorNormalize(normal); float at = (hits[negative].t + hits[positive].t) / 2; vectorScale(contactpoint, ray, at); vectorAdd(contactpoint, p1);*/ float dot1 = fabs(vectorDot(ray, hits[negative].polygon->planenormal)); float dot2 = fabs(vectorDot(ray, hits[positive].polygon->planenormal)); if (dot1 > dot2) { // vectorScale(contactpoint, ray, hits[negative].t); // vectorAdd(contactpoint, p1); vectorCopy(contactpoint, cp1); vectorCopy(normal, hits[positive].polygon->planenormal); } else { // vectorScale(contactpoint, ray, hits[positive].t); // vectorAdd(contactpoint, p1); vectorCopy(contactpoint, cp2); vectorCopy(normal, hits[negative].polygon->planenormal); } } else { Polygon *polygon = findNearestPolygon(hits[negative].polygon, cp1); if (polygon != NULL) { /*vectorCopy(contactpoint, cp1); vectorAdd(contactpoint, cp2); vectorScale(contactpoint, 0.5);*/ float at = (hits[negative].t + hits[positive].t) / 2; vectorScale(contactpoint, ray, at); vectorAdd(contactpoint, p1); vectorCopy(normal, polygon->planenormal); } else { return false; } } // shotsound->play(); return true; }
void ENT_GenerateWorldSpaceBBox(entity_t* entity) { //Transform the bbox from modelSpace to WorldSpace. vec4_t modelSpaceMinPoint ; vec4_t modelSpaceMaxPoint; vec4_t worldSpaceMinPoint ; vec4_t worldSpaceMaxPoint; vectorCopy(entity->model->modelSpacebbox.min,modelSpaceMinPoint); modelSpaceMinPoint[3] = 1; vectorCopy(entity->model->modelSpacebbox.max,modelSpaceMaxPoint); modelSpaceMaxPoint[3] = 1; matrix_transform_vec4t(entity->matrix, modelSpaceMinPoint , worldSpaceMinPoint); matrix_transform_vec4t(entity->matrix, modelSpaceMaxPoint , worldSpaceMaxPoint); // printf("bbox min: [%.2f,%.2f,%.2f]\n", worldSpaceMinPoint[0], worldSpaceMinPoint[1], worldSpaceMinPoint[2]); // printf("bbox max: [%.2f,%.2f,%.2f]\n", worldSpaceMaxPoint[0], worldSpaceMaxPoint[1], worldSpaceMaxPoint[2]); //Generate 6 points defining the box limits. //Unit cube volume //x y z //0 0 0 //1 0 0 //0 0 1 //1 0 1 //0 1 0 //1 1 0 //0 1 1 //1 1 1 // 0 0 0 entity->worldSpacebbox[0][0] = worldSpaceMinPoint[0] ; entity->worldSpacebbox[0][1] = worldSpaceMinPoint[1] ; entity->worldSpacebbox[0][2] = worldSpaceMinPoint[2] ; // 1 0 0 entity->worldSpacebbox[1][0] = worldSpaceMaxPoint[0] ; entity->worldSpacebbox[1][1] = worldSpaceMinPoint[1] ; entity->worldSpacebbox[1][2] = worldSpaceMinPoint[2] ; // 0 0 1 entity->worldSpacebbox[2][0] = worldSpaceMinPoint[0] ; entity->worldSpacebbox[2][1] = worldSpaceMinPoint[1] ; entity->worldSpacebbox[2][2] = worldSpaceMaxPoint[2] ; //1 0 1 entity->worldSpacebbox[3][0] = worldSpaceMaxPoint[0] ; entity->worldSpacebbox[3][1] = worldSpaceMinPoint[1] ; entity->worldSpacebbox[3][2] = worldSpaceMaxPoint[2] ; //0 1 0 entity->worldSpacebbox[4][0] = worldSpaceMinPoint[0] ; entity->worldSpacebbox[4][1] = worldSpaceMaxPoint[1] ; entity->worldSpacebbox[4][2] = worldSpaceMinPoint[2] ; //1 1 0 entity->worldSpacebbox[5][0] = worldSpaceMaxPoint[0] ; entity->worldSpacebbox[5][1] = worldSpaceMaxPoint[1] ; entity->worldSpacebbox[5][2] = worldSpaceMinPoint[2] ; //0 1 1 entity->worldSpacebbox[6][0] = worldSpaceMinPoint[0] ; entity->worldSpacebbox[6][1] = worldSpaceMaxPoint[1] ; entity->worldSpacebbox[6][2] = worldSpaceMaxPoint[2] ; //1 1 1 entity->worldSpacebbox[7][0] = worldSpaceMaxPoint[0] ; entity->worldSpacebbox[7][1] = worldSpaceMaxPoint[1] ; entity->worldSpacebbox[7][2] = worldSpaceMaxPoint[2] ; }
char OBJ_Load(char* filename,entity_t* entity) { static vec3_t tmpVertices[DE_USHRT_MAX]; ushort num_vertices=0; static vec2_t tmpTextureCoor[DE_USHRT_MAX]; ushort num_textuCoor=0; int idata[9]; int i; ushort indiceOfCurrentVertex; filehandle_t* objFile ; obj_t* obj; objFile = FS_OpenFile(filename, "rt"); if (!objFile) { printf("Could not Load OBJ: '%s'.\n",filename); return 0; } OBJ_InitVerticeCache(); obj = (obj_t*)entity->model; obj->vertices = (obj_vertex_t*)calloc(DE_USHRT_MAX, sizeof(obj_vertex_t)); obj->num_vertices = 0; obj->indices = (ushort*)calloc(DE_USHRT_MAX, sizeof(ushort)); obj->num_indices = 0; LE_pushLexer(); LE_init(objFile); while (LE_hasMoreData()) { LE_readToken(); if (!strcmp("v",LE_getCurrentToken())) { tmpVertices[num_vertices][0] = LE_readReal(); tmpVertices[num_vertices][1] = LE_readReal(); tmpVertices[num_vertices][2] = LE_readReal(); num_vertices++; } else if (!strcmp("vt",LE_getCurrentToken())) { tmpTextureCoor[num_textuCoor][0] = LE_readReal(); tmpTextureCoor[num_textuCoor][1] = 1- LE_readReal(); num_textuCoor++; } else if (!strcmp("vn",LE_getCurrentToken())) { for(i=0;i<3;i++) LE_readReal(); //Drop it (like it's hot) } else if (!strcmp("f",LE_getCurrentToken())) { LE_SetWhiteCharValue('/', 1); for(i=0;i<9;i++) { idata[i] = LE_readReal(); idata[i]--; } #ifdef OBJ_FACE_CCW for(i=0;i<3;i++)//OBJ Standard: Couter-clockwise #else for(i=2;i>=0;i--) //Bugged expoter. #endif { indiceOfCurrentVertex = GetCacheVertex(idata[3*i],tmpTextureCoor[idata[3*i+1]]); //indiceOfCurrentVertex = obj->num_indices; if (indiceOfCurrentVertex+1 > obj->num_vertices) obj->num_vertices = indiceOfCurrentVertex+1; vectorCopy (tmpVertices[idata[3*i]] , obj->vertices[indiceOfCurrentVertex].position); //for(i=0;i<3;i++) //{ // printf("Vertices: %d, %d, %d.\n",obj->vertices[indiceOfCurrentVertex].position[0],obj->vertices[indiceOfCurrentVertex].position[1],obj->vertices[indiceOfCurrentVertex].position[2]); //} vector2Copy(tmpTextureCoor[idata[3*i+1]], obj->vertices[indiceOfCurrentVertex].textCoo); //printf("num_indices = %d, indice pointer=%d\n",obj->num_indices,indiceOfCurrentVertex); obj->indices[obj->num_indices++] = indiceOfCurrentVertex ; } LE_SetWhiteCharValue('/', 0); } else if (!strcmp("usemtl",LE_getCurrentToken())) { LE_readToken(); entity->material = MATLIB_Get(LE_getCurrentToken()); if (entity->material == 0) printf("************[OBJ Loader] Could not find material: '%s'\n",LE_getCurrentToken()); } } LE_popLexer(); FS_CloseFile(objFile); //Adjust size of vertices and indices obj->vertices = realloc(obj->vertices,obj->num_vertices * sizeof(obj_vertex_t)); obj->indices = realloc(obj->indices ,obj->num_indices * sizeof(ushort)); //printf("Obj %s has %d vertices.\n",filename,obj->num_vertices); //printf("Obj %s has %d indices.\n",filename,obj->num_indices); OBJ_DestroyVerticeCache(); //Generate lighting infos. OBJ_GenerateLightingInfo(obj); return 1; }