void spherify(Solid* s) { unsigned n = s->nVerts; Vec3 centroid = { 0,0,0 }; unsigned i; for(i=0;i<n;++i) { vec3Add(centroid,centroid,s->verts[i].loc); } vec3Mult(centroid,centroid,1.0/n); Vec3* diffs = malloc(n*sizeof(Vec3)); float* dists = malloc(n*sizeof(float)); float maxR = 0; for(i=0;i<n;++i) { vec3Sub(diffs[i],s->verts[i].loc,centroid); float r = vec3Mag(diffs[i]); dists[i] = r; if(r > maxR) { maxR = r; } } for(i=0;i<n;++i) { vec3Mult(diffs[i],diffs[i],1.0/dists[i]); vec3Copy(s->verts[i].normal,diffs[i]); vec3Mult(diffs[i],diffs[i],maxR); vec3Add(s->verts[i].loc,centroid,diffs[i]); } free(dists); free(diffs); }
static void applyForces(struct particle_system *ps, struct particle *p) { int i = 0; vec3 force; vec3 dir; float m, d, s; vec3Copy(p->dir, p->acc); do { switch(ps->forces[i].type) { case PARTICLE_FORCE_DIRECTIONAL: vec3Copy(ps->forces[i].dir, force); break; case PARTICLE_FORCE_FLUID: s = vec3Mag(p->acc); vec3Copy(p->acc, force); vec3Normalize(force, force); vec3Mul(force, -ps->forces[i].value*s*s, force); break; case PARTICLE_FORCE_ATTRACTION: vec3Sub(ps->forces[i].loc, p->loc, dir); d = vec3Mag(dir); vec3Normalize(dir, dir); m = 0.00001*ps->forces[i].value*p->mass / (d*d); vec3Mul(dir, m, force); break; } vec3Add(p->acc, force, p->acc); } while(++i < ps->nbforces); vec3Div(p->acc, p->mass, p->acc); vec3Add(p->vel, p->acc, p->vel); vec3Add(p->loc, p->vel, p->loc); }
void Prop::tick(float dt) { if (propModel) { vec3Add(&propModel->position, &propModel->position, &velocity); } }
// Inverse edge length weighted method. As described in Real-time rendering (3rd ed.) p.546. // Ref: Max, N. L., (1999) 'Weights for computing vertex normals from facet normals' // in Journal of grahics tools, vol.4, no.2, pp.1-6. static GLfloat* meshGenerateNormals(int num_verts, int num_elems, const GLfloat *verts, const GLushort *elems) { GLfloat *norms = (GLfloat*) calloc(3*num_verts, sizeof(GLfloat)); GLfloat e1[3], e2[3], no[3]; for (int i = 0; i < num_elems; i += 3) { for (int j = 0; j < 3; ++j) { int a = 3*elems[i+j]; int b = 3*elems[i+((j+1)%3)]; int c = 3*elems[i+((j+2)%3)]; vec3Sub(e1, &verts[c], &verts[b]); vec3Sub(e2, &verts[a], &verts[b]); vec3Cross(no, e1, e2); double d = vec3Length2(e1) * vec3Length2(e2); vec3DivScalar(no, no, d); vec3Add(&norms[b], &norms[b], no); } } for (int i = 0; i < 3*num_verts; i += 3) { vec3Normalize(&norms[i], &norms[i]); } return norms; }
double vec3ClosestOnSegment(GLfloat *result, const GLfloat *p, const GLfloat *a, const GLfloat *b) { GLfloat pa[3], ba[3]; vec3Sub(pa, p, a); vec3Sub(ba, b, a); double dotbaba = vec3Length2(ba); double t = 0; if (dotbaba != 0) { t = vec3Dot(pa, ba) / dotbaba; if (t < 0) t = 0; else if (t > 1) t = 1; } vec3MulScalar(ba, ba, t); vec3Add(result, a, ba); return t; }
int main(int argc, char* argv[]){ if(argc != 5){ printf("%s",HELP_MESSAGE); return -1; } const int width = atoi(argv[2]); const int height = atoi(argv[3]); const int maxIterations = atoi(argv[4]); if(width <= 0 || height <= 0){ printf("%s",BAD_WIDTH_HEIGHT_OR_MAX_ITERATIONS); return -1; } FILE* file = fopen("test.ppm","wb"); if(file == NULL){ printf(FAILED_TO_CREATE_FILE); return -1; } createFileHeader(file,width,height); Vector3 direction; direction = (Vector3){.x=0.5,.y=0.5,.z=1}; Vector3 from; from = (Vector3){.x=-100,.y=-100,.z=-100}; int x = 0; int y = 0; int sampleRate = height/10; int loadCount = 0; for(y = 0; y<height; y++){ fprintf(file," "); for(x = 0; x<width; x++){ Vector3 offset; offset = (Vector3){.x = x, .y = y, .z = 0}; int value = (int)rayMarch(vec3Add(from,offset),direction,maxIterations); fprintf(file,"%d %d %d ",value,value,value); } fprintf(file,"\n"); if(y % sampleRate == 0){ printf("\r%i\%% done",loadCount*10); loadCount++; } } fclose(file); return 0; }
void update(float _deltaTime) { if (m_mouseDown) { int32_t deltaX = m_mouseNow.m_mx - m_mouseLast.m_mx; int32_t deltaY = m_mouseNow.m_my - m_mouseLast.m_my; m_horizontalAngle += m_mouseSpeed * float(deltaX); m_verticalAngle -= m_mouseSpeed * float(deltaY); m_mouseLast.m_mx = m_mouseNow.m_mx; m_mouseLast.m_my = m_mouseNow.m_my; } float direction[3] = { cosf(m_verticalAngle) * sinf(m_horizontalAngle), sinf(m_verticalAngle), cosf(m_verticalAngle) * cosf(m_horizontalAngle), }; float right[3] = { sinf(m_horizontalAngle - float(M_PI)/2.0f), 0, cosf(m_horizontalAngle - float(M_PI)/2.0f), }; if (m_keys & CAMERA_KEY_UP) { // m_eye += direction * _deltaTime * m_moveSpeed float tmpRhs[3]; float tmpPos[3]; memcpy(tmpPos, m_eye, sizeof(float)*3); vec3Mul(tmpRhs, direction, _deltaTime * m_moveSpeed); vec3Add(m_eye, tmpPos, tmpRhs); setKeyState(CAMERA_KEY_UP, false); } if (m_keys & CAMERA_KEY_DOWN) { // m_eye -= direction * _deltaTime * m_moveSpeed float tmpRhs[3]; float tmpPos[3]; memcpy(tmpPos, m_eye, sizeof(float)*3); vec3Mul(tmpRhs, direction, _deltaTime * m_moveSpeed); vec3Sub(m_eye, tmpPos, tmpRhs); setKeyState(CAMERA_KEY_DOWN, false); } if (m_keys & CAMERA_KEY_LEFT) { // m_eye += right * _deltaTime * m_moveSpeed float tmpRhs[3]; float tmpPos[3]; memcpy(tmpPos, m_eye, sizeof(float)*3); vec3Mul(tmpRhs, right, _deltaTime * m_moveSpeed); vec3Add(m_eye, tmpPos, tmpRhs); setKeyState(CAMERA_KEY_LEFT, false); } if (m_keys & CAMERA_KEY_RIGHT) { // m_eye -= right * _deltaTime * m_moveSpeed float tmpRhs[3]; float tmpPos[3]; memcpy(tmpPos, m_eye, sizeof(float)*3); vec3Mul(tmpRhs, right, _deltaTime * m_moveSpeed); vec3Sub(m_eye, tmpPos, tmpRhs); setKeyState(CAMERA_KEY_RIGHT, false); } vec3Add(m_at, m_eye, direction); vec3Cross(m_up, right, direction); }