/*****************************************************************************1*2* Elmer, A Finite Element Software for Multiphysical Problems3*4* Copyright 1st April 1995 - , CSC - IT Center for Science Ltd., Finland5*6* This program is free software; you can redistribute it and/or7* modify it under the terms of the GNU General Public License8* as published by the Free Software Foundation; either version 29* of the License, or (at your option) any later version.10*11* This program is distributed in the hope that it will be useful,12* but WITHOUT ANY WARRANTY; without even the implied warranty of13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14* GNU General Public License for more details.15*16* You should have received a copy of the GNU General Public License17* along with this program (in file fem/GPL-2); if not, write to the18* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,19* Boston, MA 02110-1301, USA.20*21*****************************************************************************/2223/*******************************************************************************24*25* Camera main module & utilities26*27*******************************************************************************28*29* Author: Juha Ruokolainen30*31* Address: CSC - IT Center for Science Ltd.32* Keilaranta 14, P.O. BOX 40533* 02101 Espoo, Finland34* Tel. +358 0 457 272335* Telefax: +358 0 457 230236* EMail: [email protected]37*38* Date: 3 Oct 199539*40* Modification history:41*42******************************************************************************/4344extern void *TCLInterp;454647/*48* $Id: camera.c,v 1.4 2004/11/29 08:27:09 jpr Exp $49*50* $Log: camera.c,v $51* Revision 1.4 2004/11/29 08:27:09 jpr52* *** empty log message ***53*54* Revision 1.3 2003/02/06 09:37:46 jpr55* *** empty log message ***56*57* Revision 1.2 1998/08/01 12:34:09 jpr58*59* Added Id, started Log.60*61*62*/6364#define MODULE_CAMERAS6566#include "../elmerpost.h"6768#ifdef NOTDEF69void cam_set_frame(int ncam,int status)70{71camera[--ncam].frame = status;72}7374void cam_set_camera_obj_mask(int ncam,int obj,int status)75{76--ncam;77camera[ncam].obj_mask[obj] = status;78}79#endif8081/*******************************************************************************82*83* Name: cam_set_viewport( camera_t *,double,double,double,double )84*85* Purpose: set viewport values given camera structure pointer and86* two points.87*88* Parameters:89*90* Input: (double,double) lower left hand corner of the viewport91* (double,double) upper right hand corner of the viewport92*93* Output: (camera_t *) camera structure is modified94*95* Return value: void96*97*******************************************************************************/98void cam_set_viewport(camera_t *camera,double lx,double ly,double hx,double hy)99{100camera->ViewportLowX = lx;101camera->ViewportLowY = ly;102camera->ViewportHighX = hx;103camera->ViewportHighY = hy;104}105106/*******************************************************************************107*108* Name: cam_set_projection( camera_t *,camera_proj_t )109*110* Purpose: Set camera projection type. Internal only.111*112* Parameters:113*114* Input: (camera_proj_t) projection type to set.115*116* Output: (camera_t *) camera structure is modified117*118* Return value: void119*120*******************************************************************************/121void cam_set_projection( camera_t *camera,camera_proj_t projection )122{123camera->ProjectionType = projection;124}125126/*******************************************************************************127*128* Name: cam_set_file_angle( camera_t *,float )129*130* Purpose: Set camera filed angle for perspective projection.131* Internal only.132*133* Parameters:134*135* Input: (float) input field angle.136*137* Output: (camera_t *) camera structure is modified138*139* Return value: void140*141*******************************************************************************/142void cam_set_field_angle( camera_t *camera, double angle )143{144camera->FieldAngle = angle;145}146147/*******************************************************************************148*149* Name: cam_set_look_from( camera_t *,double,double,double,int )150*151* Purpose: Set camera position to given value.152*153* Parameters:154*155* Input: (double,double,double) x,y,z coordinates of the camera156* (int) flag saying if inputs should be added to current157* position158*159* Output: (camera_t *) camera structure is modified160*161* Return value: void162*163*******************************************************************************/164void cam_set_look_from(camera_t *camera,double x,double y,double z,int relative)165{166if ( relative )167{168camera->LookFromX += x;169camera->LookFromY += y;170camera->LookFromZ += z;171} else172{173camera->LookFromX = x;174camera->LookFromY = y;175camera->LookFromZ = z;176}177}178179/*******************************************************************************180*181* Name: cam_set_up_vector( camera_t *,double,double,double )182*183* Purpose: Set camera up vector184*185* Parameters:186*187* Input: (double,double,double) x,y,z direction upwadrds188*189* Output: (camera_t *) camera structure is modified190*191* Return value: void192*193*******************************************************************************/194void cam_set_up_vector(camera_t *camera,double x,double y,double z )195{196camera->UpX = x;197camera->UpY = y;198camera->UpZ = z;199}200201/*******************************************************************************202*203* Name: cam_set_clip( camera_t *,double,double )204*205* Purpose: Set camera clip viewing planes206*207* Parameters:208*209* Input: (double,double) near and far plane coordinates210*211* Output: (camera_t *) camera structure is modified212*213* Return value: void214*215*******************************************************************************/216void cam_set_clip(camera_t *camera,double n,double f)217{218camera->ClipNear = n;219camera->ClipFar = f;220}221222/*******************************************************************************223*224* Name: cam_set_look_to( camera_t *,double,double,double,int )225*226* Purpose: Set point at which the camera is aimed227*228* Parameters:229*230* Input: (double,double,double) x,y,z coordinates231* (int) flag saying if inputs should be added to current232* position233*234* Output: (camera_t *) camera structure is modified235*236* Return value: void237*238*******************************************************************************/239void cam_set_look_to(camera_t *camera,double x,double y,double z,int relative)240{241if ( relative )242{243camera->LookAtX += x;244camera->LookAtY += y;245camera->LookAtZ += z;246} else247{248camera->LookAtX = x;249camera->LookAtY = y;250camera->LookAtZ = z;251}252}253254void cam_set_onoff( camera_t *camera,int onoff)255{256camera->OnOff = onoff;257}258259/*******************************************************************************260*261* Name: cam_add_camera( camera_t *, char * )262*263* Purpose: Add camera to a given list of camera with given name.264* If one with given name already exists, it is returned.265*266* Parameters:267*268* Input: (camera_t *) input list of cameras269* (char *) name to be given to the camera270*271* Output: (camera_t *) camera structure is modified272*273* Return value: (camera_t *) pointer to the created camera structure274*275*******************************************************************************/276camera_t *cam_add_camera( camera_t *camera,char *name )277{278camera_t *new_cam = camera;279280281while( new_cam )282{283if ( strcmp( new_cam->Name,name ) == 0 ) return new_cam;284new_cam = new_cam->Next;285}286287new_cam = (camera_t *)calloc(1,sizeof(camera_t) );288289if ( !new_cam )290{291fprintf( stderr, "cam_add_camera: FATAL: can't alloc a few bytes of memory\n" );292return NULL;293}294295if ( !(new_cam->Name = (char *)malloc( strlen(name)+1 ) ) )296{297fprintf( stderr, "cam_add_camera: FATAL: can't alloc a few bytes of memory\n" );298free( new_cam );299return NULL;300}301302strcpy( new_cam->Name, name );303304if ( camera )305{306while( camera->Next ) camera = camera->Next;307camera->Next = new_cam;308}309310return new_cam;311}312313/*******************************************************************************314*315* Name: cam_delete_list( camera_t * )316*317* Purpose: Delete (free mem ) list of camera definitions318*319* Parameters:320*321* Input: (camera_t *) input list of cameras322*323* Output: none324*325* Return value: void326*327*******************************************************************************/328void cam_delete_list( camera_t *camera )329{330camera_t *ptr;331332while( camera )333{334if ( camera->Name ) free( camera->Name );335336ptr = camera;337camera = camera->Next;338339free( ptr );340}341}342343static double upx,upy,upz,viewx,viewy,viewz,tox,toy,toz;344345#include <tcl.h>346/*******************************************************************************347*348* Name: cam_display_list( camera_t *, object_t * )349*350* Purpose: Display list of given objecst is given list of cameras351*352* Parameters:353*354* Input: (camera_t *) input list of cameras355* (object_t *) input list of objecst356*357* Output: graphics358*359* Return value: if mouse interaction is going on and too slow FALSE,360* otherwise TRUE361*362*******************************************************************************/363int cam_display_list( camera_t *camera, object_t *object )364{365double t = RealTime(), ct = CPUTime();366367int FitToPage = 0, nofcameras;368camera_t *cam;369void initglp(), stopglp(), gra_set_projection();370371if ( GlobalOptions.OutputPS ) {372initglp( Tcl_GetVar( TCLInterp, "PSFileName", TCL_GLOBAL_ONLY ),373GlobalOptions.FitToPagePS );374}375if ( user_hook_before_all ) (*user_hook_before_all)( camera,object );376377nofcameras = 0;378for( cam=camera; cam != NULL; cam = cam->Next, nofcameras++ );379380for( GlobalPass=0; GlobalPass < 2; GlobalPass++ )381{382for( cam=camera; cam != NULL; cam = cam->Next )383{384if ( !cam->OnOff ) continue;385386gra_set_projection( cam->ProjectionType, cam->FieldAngle,387cam->ViewportLowX, cam->ViewportHighX,388cam->ViewportLowY, cam->ViewportHighY,389cam->ClipNear, cam->ClipFar, nofcameras>1 );390391gra_push_matrix();392393gra_look_at(394cam->LookFromX, cam->LookFromY, cam->LookFromZ,395cam->LookAtX, cam->LookAtY, cam->LookAtZ,396cam->UpX, cam->UpY, cam->UpZ397);398399if ( user_hook_camera_before ) (*user_hook_camera_before)( GlobalPass,cam,object,t );400401if ( !obj_display_list( object, t ) ) return FALSE;402403if ( user_hook_camera_after ) (*user_hook_camera_after)( GlobalPass,cam,object,t );404405gra_pop_matrix();406407if ( BreakLoop ) break;408409}410if ( BreakLoop ) break;411}412413if ( user_hook_after_all ) (*user_hook_after_all)( camera,object );414if ( GlobalOptions.OutputPS ) stopglp();415416return TRUE;417}418419420