void camera_view(gdouble *v, gpointer data) { struct camera_pak *camera = data; g_assert(data != NULL); ARR3SET(v, camera->v); if (camera->mode == LOCKED) quat_rotate(v, camera->q); }
void Camera_offset_orientation(Camera* const camera, float yaw, float pitch) { quat pitch_rotation, yaw_rotation; quat_rotate(yaw_rotation, yaw, G_UP); vec3 right; quat_mul_vec3(right, camera->transform.orientation, G_RIGHT); vec3_norm(right, right); quat_rotate(pitch_rotation, pitch, right); quat orientation; quat_mul(orientation, yaw_rotation, pitch_rotation); quat_mul( camera->transform.orientation, orientation, camera->transform.orientation ); quat_norm(camera->transform.orientation, camera->transform.orientation); }
/* rotate Internally used helper for CCameraObject::RotateByMouse */ static void rotate(float* camPos, float* lookAtPos, float* upVec, float xDir) { quat qRot, qView, qNewView; quat_rotate(qRot, deg_2_rad(xDir), upVec); qView[0] = lookAtPos[0] - camPos[0]; qView[1] = lookAtPos[1] - camPos[1]; qView[2] = lookAtPos[2] - camPos[2]; qView[3] = 0.0f; quat_mul(qRot, qView, qNewView); quat_conjugate(qRot); quat_mul(qNewView, qRot, qNewView); lookAtPos[0] = camPos[0] + qNewView[0]; lookAtPos[1] = camPos[1] + qNewView[1]; lookAtPos[2] = camPos[2] + qNewView[2]; }
int sleeper(void*) { int i; trotxy = rrotxy; trotyz = rrotyz; trotxz = rrotxz; trotwy = rrotwy; trotwx = rrotwx; trotwz = rrotwz; // while(!drawnonce); /* posbuf_run(); posbuf_run(); */ // posbuf_run_once(); while(!done) { if(arot) { // while(lock); // lock = 1; arotxz = aspeed*(0.05*((rand1%((int)(234525))) /234525.0)); arotyz = aspeed*(0.05*((rand2%((int)(457646))) /457646.0)); arotxy = aspeed*(0.05*((rand3%((int)(957431))) /957431.0)); arotwx = aspeed*(0.05*((rand4%((int)(255689))) /255689.0)); arotwy = aspeed*(0.05*((rand5%((int)(737345))) /737345.0)); arotwz = aspeed*(0.05*((rand6%((int)(231455))) /231455.0)); // lock = 0; } else { arotxz = arotyz = arotxy = arotwx = arotwy = arotwz = 0; } if(((trotxy != rrotxy || trotyz != rrotyz || trotxz != rrotxz || trotwy != rrotwy || trotwx != rrotwx || trotwz != rrotwz) || arot) && !lock && !resetpso) { while(lock); lock = 1; trotxz = arotxz+rrotxz; trotyz = arotyz+rrotyz; trotxy = arotxy+rrotxy; trotwx = arotwx+rrotwx; trotwy = arotwy+rrotwy; trotwz = arotwz+rrotwz; lock = 0; if(!enablemov && 0) // ?????? CMPQ { if(bulletf == 1) { if(auto2 == 0 && auto1 == 0) auto2 = 10.0f; if(auto2 >= 10 && auto1 < 10) auto1 += 0.2f; if(auto1 >= 10 && auto2 > -10) auto2 -= 0.2f; if(auto2 <= -10 && auto1 > -10) auto1 -= 0.2f; if(auto1 <= -10 && auto2 < 10) auto2 += 0.2f; if(auto2 >= 10 && auto1 > 2.0f && auto1 < 2.2f) auto4 = 10; if(auto4 >= 10 && auto3 < 10) auto3 += 0.2f; if(auto3 >= 10 && auto4 > -10) auto4 -= 0.2f; if(auto4 <= -10 && auto3 > -10) auto3 -= 0.2f; if(auto3 <= -10 && auto4 < 10) auto4 += 0.2f; } i = 0; int yy; // for(yy = 0; yy < maxquat; yy++) // q[yy] = qo[yy]; } for(i = 0; i < quatnum; i++) { // q[i] = qo[i]; if(enablemov) if(i > movquat) break; quat_rotate(q, i); } if(ddebug) printf("rotate finish %d\n", quatnum); trotxy = rrotxy; trotyz = rrotyz; trotxz = rrotxz; trotwy = rrotwy; trotwx = rrotwx; trotwz = rrotwz; SDL_Delay(33); } else SDL_Delay(1); } }
int drawGLscene() { while(resetpso) SDL_Delay(10); int i,j,k; float alpha; unsigned char clear; int sloop = 0; float ratio; for(sloop = 0; sloop < 2; sloop++) { if(blurenable) glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo[0]); else if(stereo) { if(sloop == 0) glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo[1]); if(sloop == 1) glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo[2]); } if(!blurenable && !stereo) glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); if(stereo) { // ratio = (GLfloat)w_width/(2*(GLfloat)w_height); // gluPerspective(45.0f, ratio, 0.1f, 800.0f); ratio = w_width/3; glViewport(-ratio,0,overtexs+ratio*2,overtexs); } glMatrixMode(GL_MODELVIEW); glDisable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA); // glBlendFunc(GL_SRC_ALPHA,GL_ONE); // GL_ACCUM_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); glLoadIdentity(); // if(stereo && sloop == 0) { glTranslatef(0, 0, 0); } // if(stereo && sloop == 1) { glTranslatef(0, 0, 0); } glBindTexture(GL_TEXTURE_2D, 0); // while(lock); // lock = 1; // rrotxz = rrotyz = rrotxy = rrotwx = rrotwy = rrotwz = 0; // lock = 0; if(azoom) { if(zoom > 3 || zoom < -3) azoom = -azoom; zoom += azoom; } playerpos.set(posx, posy, posz, posw); glDisable(GL_FOG); glFogfv(GL_FOG_COLOR, fogcolor); glFogi(GL_FOG_MODE, GL_LINEAR); glFogf(GL_FOG_DENSITY, 500); glFogf(GL_FOG_START, (1+((zoom*10)))*sqrt(sqrt(depth/20))); glFogf(GL_FOG_END, (5+((zoom*10)))*sqrt(sqrt(depth/20))); glTranslatef(0, 0, -10*zoom); if(points) { if(!intensswitch) { intensval += 0.025f*intensui; if(intensval > 1.1f) intensval = -0.1f; } // list1 = glGenLists (1); // glNewList(list1, GL_COMPILE); if(stereo && sloop == 0) drawdl('l', intensval); if(stereo && sloop == 1) drawdl('r', intensval); else drawdl('c', intensval); // glEndList(); // glCallList(list1); if(ddebug) printf("draw finish %d\n", quatnum); } else { if(stereo) posbuf_run(sloop); else posbuf_run(10); for(j = (quatnum/4)-1; j >= 0; j--) { i = 4*zposbufi[j]; if(enablemov) if(i >= movquat) continue; color(2, 1); if(zposbufi[j] >= movquat/4) glBindTexture(GL_TEXTURE_2D, texture[2]); if(zposbufi[j] >= 0) // else if(zposbufi[j] >= freequat/4) glBindTexture(GL_TEXTURE_2D, texture[1]); else if(zposbufi[j] >= bquat/4) glBindTexture(GL_TEXTURE_2D, texture[2]); else if(zposbufi[j] >= 0) glBindTexture(GL_TEXTURE_2D, texture[2]); else glBindTexture(GL_TEXTURE_2D, texture[2]); color(((48+i)/48), 0.2f); if(zposbufi[j] >= 0) color(((48+i)/48), 0.2f); else if(stereo && sloop == 0) quad_draw_l(q, i); else if(stereo && sloop == 1) quad_draw_r(q, i); //else quad_draw_new(q, i); else quad_draw(q, i); } } qbuffer[0].set(padd.a+playerpos.a-1, padd.b+playerpos.b, padd.c+playerpos.c, padd.d+playerpos.d); qbuffer[1].set(padd.a+playerpos.a+1, padd.b+playerpos.b, padd.c+playerpos.c, padd.d+playerpos.d); qbuffer[2].set(playerpos.a+1, playerpos.b+0.05, playerpos.c, playerpos.d); qbuffer[3].set(playerpos.a-1, playerpos.b+0.05, playerpos.c, playerpos.d); qbuffer[4].set(padd.a+playerpos.a, padd.b+playerpos.b-1, padd.c+playerpos.c, padd.d+playerpos.d); qbuffer[5].set(padd.a+playerpos.a, padd.b+playerpos.b+1, padd.c+playerpos.c, padd.d+playerpos.d); qbuffer[6].set(playerpos.a+0.05, playerpos.b+1, playerpos.c, playerpos.d); qbuffer[7].set(playerpos.a+0.05, playerpos.b-1, playerpos.c, playerpos.d); color(3, 1); glBindTexture(GL_TEXTURE_2D, texture[3]); for(i = 0; i < 8; i++) quat_rotate(qbuffer, i); // quad_draw(qbuffer, 0); // quad_draw(qbuffer, 4); if(blurenable) DrawBlur(10*blurr,0.001f); } if(stereo) DrawStereo(); SDL_GL_SwapBuffers(); drawnonce = 1; return(0); }
int quaternion_test() { #define FAIL_TEST { fprintf(stderr, "quaternion_test failed at line %d\n", \ __LINE__ ); return 0; } fprintf(stderr, "running quaternion test\n"); double theta = 0; double rvec[] = { 0, 0, 1 }; double q[4]; double roll, pitch, yaw; quat_from_angle_axis( theta, rvec, q ); if( ! qeq( q, 1, 0, 0, 0 ) ) FAIL_TEST; quat_to_roll_pitch_yaw( q, &roll, &pitch, &yaw ); if( ! rpyeq( roll,pitch,yaw, 0,0,0 ) ) FAIL_TEST; // quat_from_angle_axis theta = M_PI; quat_from_angle_axis( theta, rvec, q ); fprintf(stderr,"<%.3f, %.3f, %.3f, %.3f>\n", q[0], q[1], q[2], q[3]); if( ! qeq( q, 0, 0, 0, 1 ) ) FAIL_TEST; // quat_to_angle_axis quat_to_angle_axis( q, &theta, rvec ); if( !feq( theta, M_PI ) ) FAIL_TEST; if( !feq(rvec[0], 0) || !feq(rvec[1], 0) || !feq(rvec[2], 1) ) FAIL_TEST; quat_to_roll_pitch_yaw( q, &roll, &pitch, &yaw ); if( ! rpyeq( roll,pitch,yaw, 0,0,M_PI ) ) FAIL_TEST; double q2[4]; double q3[4]; double axis1[] = { 0, 1, 0 }; double axis2[] = { 0, 0, 1 }; quat_from_angle_axis( M_PI/2, axis1, q ); quat_from_angle_axis( M_PI/2, axis2, q2 ); quat_mult( q3, q, q2 ); rvec[0] = 0; rvec[1] = 0; rvec[2] = 1; quat_rotate( q, rvec ); fprintf(stderr, "by q: [ %.2f, %.2f, %.2f ]\n", rvec[0], rvec[1], rvec[2]); rvec[0] = 0; rvec[1] = 0; rvec[2] = 1; quat_rotate( q2, rvec ); fprintf(stderr, "by q2: [ %.2f, %.2f, %.2f ]\n", rvec[0], rvec[1], rvec[2]); rvec[0] = 0; rvec[1] = 0; rvec[2] = 1; quat_rotate( q3, rvec ); fprintf(stderr, "by q*q2: [ %.2f, %.2f, %.2f ]\n", rvec[0], rvec[1], rvec[2]); rvec[0] = 0; rvec[1] = 0; rvec[2] = 1; quat_mult( q3, q2, q ); quat_rotate( q3, rvec ); fprintf(stderr, "by q2*q: [ %.2f, %.2f, %.2f ]\n", rvec[0], rvec[1], rvec[2]); // TODO #undef FAIL_TEST fprintf(stderr, "quaternion_test complete\n"); return 1; }
void vec3_rotate(vec3_t *out, const vec3_t *v, float angle, const vec3_t *axis) { quat_t q; quat_axis_angle(&q, axis, angle); quat_rotate(out, &q, v); }
void sbrot(int x, int y, int z) { float axis_len = (float)sqrt(x * x + y * y + z * z); rot = quat_rotate(rot, axis_len * 0.001f, -x / axis_len, -y / axis_len, z / axis_len); glutPostRedisplay(); }
void povray_hdr(FILE *fp, struct model_pak *data) { gdouble xvec, yvec, amb, pos[3], colour[3]; gdouble x[3], o[3], v[3], e[3]; GSList *list; struct light_pak *light; struct camera_pak *camera; g_assert(data != NULL); g_assert(data->camera != NULL); fprintf(fp,"#include \"colors.inc\" \n"); fprintf(fp,"#include \"finish.inc\" \n"); fprintf(fp,"#include \"glass.inc\" \n"); fprintf(fp,"#include \"metals.inc\" \n"); fprintf(fp,"#include \"textures.inc\" \n"); /* background colour (except for glass morphologies) */ fprintf(fp,"background { color rgb<%f,%f,%f0> }\n", sysenv.render.bg_colour[0], sysenv.render.bg_colour[1], sysenv.render.bg_colour[2]); /* pixel to angstrom conversion, with yet another magic number... */ p2a = 0.565 * (gdouble) sysenv.render.width / data->rmax; /* preserve model aspect ratio for the given image size */ xvec = yvec = 2.0*sysenv.rsize; if (sysenv.render.width > sysenv.render.height) xvec *= sysenv.render.width/sysenv.render.height; if (sysenv.render.height > sysenv.render.width) yvec *= sysenv.render.height/sysenv.render.width; /* compute camera position and orientation */ camera = data->camera; ARR3SET(x, camera->x); ARR3SET(o, camera->o); ARR3SET(v, camera->v); switch (camera->mode) { case FREE: break; default: case LOCKED: quat_rotate(x, camera->q); quat_rotate(o, camera->q); quat_rotate(v, camera->q); break; } /* convert viewing vector to a location */ ARR3ADD(v, x); /* camera zoom */ xvec *= camera->zoom; yvec *= camera->zoom; /* NEW - enable movies of left/right eye to be produced */ if (sysenv.stereo) { /* get axis for eye translation (view x up vector) */ crossprod(e, v, o); normalize(e, 3); /* the old 2% rule ... */ VEC3MUL(e, 0.02 * sysenv.rsize); /* default is left eye only */ if (sysenv.render.stereo_right) { ARR3ADD(x, e); ARR3ADD(v, e); } else { ARR3SUB(x, e); ARR3SUB(v, e); } } /* sky is the orientation vector */ /* right and up give the perspective */ if (camera->perspective) { fprintf(fp,"camera { location <%f,%f,%f>\n", x[0], x[1], x[2]); fprintf(fp," sky <%f,%f,%f>\n", o[0], o[1], o[2]); fprintf(fp," right <%f,0,0> up <%f,0,0>\n", xvec, yvec); fprintf(fp," look_at <%f,%f,%f>\n", v[0], v[1], v[2]); fprintf(fp," angle %f }\n", camera->fov); } else { fprintf(fp,"camera { orthographic location <%f,%f,%f>\n", x[0], x[1], x[2]); fprintf(fp," sky <%f,%f,%f>\n", o[0], o[1], o[2]); fprintf(fp," right <%f,0,0> up <%f,0,0>\n", xvec, yvec); fprintf(fp," look_at <%f,%f,%f> }\n", v[0], v[1], v[2]); } /* create light sources */ for (list=sysenv.render.light_list ; list ; list=g_slist_next(list)) { light = list->data; ARR3SET(pos, light->x); /* OpenGL -> POVRay axes */ pos[0] *= -1.0; pos[1] *= -1.0; pos[2] *= -1.0; quat_rotate(pos, camera->q); switch (light->type) { case POSITIONAL: fprintf(fp,"light_source\n {\n <%f,%f,%f>\n", pos[0], pos[1], pos[2]); break; case DIRECTIONAL: /* move away far enough so the rays are ~ // */ VEC3MUL(pos, 100.0*data->rmax); fprintf(fp,"light_source\n {\n <%f,%f,%f>\n", pos[0], pos[1], pos[2]); break; default: continue; } /* simulate OpenGL style lights */ ARR3SET(colour, light->colour); VEC3MUL(colour, light->specular); if (sysenv.render.shadowless) fprintf(fp," color rgb<%f,%f,%f> shadowless }\n", colour[0], colour[1], colour[2]); else { /* old style lighting */ /* fprintf(fp," color rgb<%f,%f,%f> }\n", colour[0], colour[1], colour[2]); */ fprintf (fp,"color White\n"); fprintf (fp," area_light <5, 0, 0,>, <0, 0, 5>, 5, 5\n"); fprintf (fp," adaptive 1\n jitter\n}\n"); } } /* fill-light to bring out the shadows */ fprintf(fp,"light_source{<%f,%f,%f> color Gray80 shadowless}\n", pos[0], pos[1], pos[2]); /* morph is too dark with just the above, sky_sphere is *nice* */ /* TODO - choice of colour eg white/grey/light blue */ /* NB: white is a bit too bright (even with 0 ambience) */ if (data->id == MORPH) { if (!sysenv.render.wire_surface && !sysenv.render.shadowless) { fprintf(fp,"sky_sphere { pigment {gradient y color_map " "{[0, 1 color Gray20 color White]} rotate x*45}}\n"); } } /* POVRay is a bit darker than OpenGL */ amb = 20.0*sysenv.render.ambience; fprintf(fp,"global_settings { ambient_light rgb<%f, %f, %f> assumed_gamma 2.2}\n",amb,amb,amb); }