source: golgotha/src/golg/objs/field_camera.cc

Last change on this file was 80, checked in by Sam Hocevar, 12 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 3.5 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#include "g1_object.hh"
10#include "object_definer.hh"
11#include "lisp/li_class.hh"
12#include "controller.hh"
13#include "lisp/li_init.hh"
14#include "map.hh"
15
16class g1_field_camera_object_class;
17
18
19
20class g1_field_camera_object_class : public g1_object_class
21{
22public:
23  g1_field_camera_object_class *next;
24 
25  g1_field_camera_object_class(g1_object_type id, g1_loader_class *fp)
26    : g1_object_class(id, fp)
27  {
28    draw_params.setup("camera");   
29  }
30 
31  void draw(g1_draw_context_class *context)
32  {
33    g1_editor_model_draw(this, draw_params, context);
34  }
35 
36  void think() {;} 
37  i4_bool occupy_location(); 
38  void unoccupy_location();
39
40};
41
42static i4_isl_list<g1_field_camera_object_class> g1_camera_list;
43
44g1_object_definer<g1_field_camera_object_class>
45  g1_field_camera_object_def("field_camera", g1_object_definition_class::EDITOR_SELECTABLE);
46
47i4_bool g1_field_camera_object_class::occupy_location()
48{
49  g1_camera_list.insert(*this);
50  return g1_object_class::occupy_location();
51}
52
53void g1_field_camera_object_class::unoccupy_location()
54{
55  g1_camera_list.find_and_unlink(this);
56  g1_object_class::unoccupy_location();
57}
58
59g1_object_class *g1_find_closest_field_camera(const i4_3d_vector &pos)
60{
61  i4_isl_list<g1_field_camera_object_class>::iterator i=g1_camera_list.begin();
62 
63  g1_field_camera_object_class *closest=0;
64  float closest_dist=100000000;
65
66  i4_transform_class t;
67
68 
69  for (; i!=g1_camera_list.end(); ++i)
70  {
71    // direction vector from object to camera
72    i4_3d_vector v=i4_3d_vector(i->x-pos.x, i->y-pos.y, i->h-pos.z), cam_dir;
73    float l=v.length();
74    v/=l;                   // normalize the vector
75
76    // get direction camera is facing
77    i->calc_world_transform(1, &t);
78    t.transform_3x3(i4_3d_vector(1,0,0), cam_dir);
79   
80    float dproduct=cam_dir.dot(v);
81
82    // is the camera facing the object?
83    if (dproduct<0)
84    {
85      if (l<closest_dist)
86      {
87        closest=&(*i);
88        closest_dist=l;
89      }
90    }
91  }
92
93  return closest;
94}
95                           
96li_string_class_member li_name("name");
97
98g1_object_class *g1_find_named_camera(char *name)
99{
100  for (g1_field_camera_object_class *f=g1_camera_list.first(); f; f=f->next)
101  {
102    li_class_context context(f->vars);
103    if (strcmp(li_name(), name)==0)
104      return f;
105  }
106
107  return 0;
108}
109
110
111static li_object *place_camera(li_object *o, li_environment *env)
112{
113  if (g1_current_controller.get())
114  {
115    li_call("add_undo", li_make_list(new li_int(G1_MAP_OBJECTS)));
116
117    g1_camera_info_struct *c=g1_current_controller->view.get_camera();
118    g1_field_camera_object_class *fc=(g1_field_camera_object_class *)
119      g1_create_object(g1_field_camera_object_def.type);
120    if (fc)
121    {
122      fc->theta=c->ground_rotate;
123      fc->pitch=c->horizon_rotate;
124      fc->roll=0;
125      fc->x=c->gx;
126      fc->y=c->gy;
127      fc->h=c->gz;
128      fc->grab_old();
129
130
131      fc->occupy_location();
132    }
133
134    li_call("redraw");
135  }
136
137  return 0;
138}
139
140
141li_automatic_add_function(place_camera, "place_camera");
Note: See TracBrowser for help on using the repository browser.