source: golgotha/src/test/spin_cube/spin_cube.cc

Last change on this file was 80, checked in by Sam Hocevar, 13 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: 7.2 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 "window/window.hh"
10#include "app/app.hh"
11#include "main/main.hh"
12#include "r1_api.hh"
13#include "r1_win.hh"
14#include "window/style.hh"
15#include "file/file.hh"
16#include "loaders/load.hh"
17#include "window/wmanager.hh"
18#include "math/transform.hh"
19#include "gui/text.hh"
20#include "gui/button.hh"
21#include "loaders/load.hh"
22#include "tmanage.hh"
23
24
25// merge test
26// merge test 2.5
27
28
29r1_render_api_class *api=0;
30
31class test_win_class : public i4_window_class
32
33public:
34  float xr,yr,zr;   // rotation numbers for the cube
35  r1_texture_handle texture;
36
37
38  test_win_class(w16 w, w16 h)
39    : i4_window_class(w,h) 
40  {
41    xr=yr=zr=0;
42
43    r1_texture_manager_class *tman=api->get_tmanager();
44    texture=tman->register_image(i4_load_image("texture.tga"));
45  }
46
47  void draw(i4_draw_context_class &context)
48  {
49    int i;
50
51    // initialize the rendering api to a "normal" state
52    api->default_state();
53
54    // the z range tells the api what range your vertexes will be in so it can scale
55    // them to fit it's own internal z buffer range
56    float far_z=1000;
57    api->set_z_range(0.01, far_z);
58
59    // clear the screen black, since frame buffer is initialized to far_z, the clear z
60    // bust be less than this to appear
61    w32 color=0;   
62    api->clear_area(0,0,width()-1,height()-1, color, far_z-1); 
63
64
65    // points on the cube
66    float cube_points[8*3]={-1, 1, -1,  1,1,-1,  1,-1,-1,  -1, -1, -1,
67                            -1, 1,  1,  1,1,1,   1,-1,1,   -1, -1, 1};
68
69    // some colors           red    green blue   white
70    float colors[4 * 3] = { 1,0,0, 0,1,0, 0,0,1, 1,1,1 };
71    int point_colors[8] = { 0,1,2,3,3,2,1,1 };
72
73    // faces on the cube
74    int verts[4 * 6] = { 0,1,2,3,  4,5,6,7,  3,2,6,7, 0,1,5,4, 1,5,6,2, 0,3,7,4 };
75    float tcoord_s[4] = { 0,1,1,0 };
76    float tcoord_t[4] = { 1,1,0,0 };
77
78    // setup of the transformation matrix
79    i4_transform_class matrix, tmp_matrix;
80    matrix.identity();
81    matrix.t.z=4;        // move camera 5 units away from cube
82
83    tmp_matrix.rotate_x(xr);
84    matrix.multiply(tmp_matrix);
85
86    tmp_matrix.rotate_y(yr);
87    matrix.multiply(tmp_matrix);
88
89    tmp_matrix.rotate_y(zr);
90    matrix.multiply(tmp_matrix);
91
92    xr+=0.01;    yr+=0.02;    zr+=0.011;  // change rotation for next draw
93   
94    // transform the points into view spave
95    i4_3d_point_class transformed_points[8];
96    for (i=0; i<8; i++)
97    {
98      i4_3d_point_class p(cube_points[i*3], cube_points[i*3+1], cube_points[i*3+2]);
99      matrix.transform(p, transformed_points[i]);
100    }
101
102    api->use_texture(texture, 256, 0);
103
104    // now copy the points into an r1_vert and project them onto the scren
105    float center_x=width()/2, center_y=height()/2;
106    r1_vert v[4];
107    for (i=0; i<6; i++)  // loop through the faces
108    {
109      for (int j=0; j<4; j++)  // loop through the points on the face
110      {
111        int pn=verts[i*4+j];  //point number
112
113        // this stores the x,y,z of the point in world space (only x & y are actually needed,
114        // unless you clip the polygon - which we are not)
115        v[j].v=*((r1_3d_point_class *)&transformed_points[pn]);
116
117        // w is the homogenous coordinate (1/z)
118        v[j].w=1.0/v[j].v.z;
119
120        // project onto screen and scale to fit the camera view port
121        v[j].px=v[j].w * v[j].v.x * center_x + center_x;
122        v[j].py=v[j].w * v[j].v.y * center_y + center_y;
123
124        // set the color of the points
125        v[j].r=colors[point_colors[pn]*3];       // color values are [0..1] 1=brightest
126        v[j].g=colors[point_colors[pn]*3+1];
127        v[j].b=colors[point_colors[pn]*3+2];
128
129        // we don't need to set s & t (texture coordinates)
130        // because we aren't texture mapping, but just so you know they are there..
131        v[j].s = tcoord_s[j];     // texture coordinates are between 0..1, 0 is left or top
132        v[j].t = tcoord_t[j];
133      }
134
135      api->render_poly(4, v);
136    }
137    request_redraw();
138  }
139 
140  char *name() { return "test_win"; }
141};
142
143class test_app : public i4_application_class
144{
145public:
146  enum { QUIT };
147
148  void init()
149  {
150    i4_application_class::init();
151   
152    // create a rendering api (display comes from the application which was
153    // created by init)
154    // the second parameter is the suggested texture memory size
155    api=r1_create_api(display);
156    if (!api)
157      i4_error("could not find an rending api for this display");
158
159    // wm is the window manager, created by app::init()
160    // wm->width is the resolution of the screen
161    int w=wm->width()/2, h=wm->height()/2;
162
163    // wm has a "style" associated with it that controlls the way the gui looks
164    i4_graphical_style_class *style=wm->get_style();
165
166    // you can get the window manager's style by calling get_style()
167    // a mp_window (--meta physical--) is a framed draggable window
168    i4_parent_window_class *p1=style->create_mp_window(-1,-1, w,h,
169                                                       i4_const_str("Test window 1"), 0);
170
171    // all rendering must reside in render_window so the render api can initialize
172    // the context before and after the drawing operations (like x/y window offset,
173    // clipping rectangles, etc). 
174    r1_render_window_class *rwin = api->create_render_window(p1->width(), p1->height());
175   
176    // render_area_width() & height may be different from width() if you are
177    // using double pixel or interlaced mode
178    i4_window_class *test_win;
179    test_win=new test_win_class(rwin->render_area_width(), rwin->render_area_height());
180   
181    // this adds the test window into the render_api's render_window
182    rwin->add_child(0, 0, test_win);
183
184    // this adds the render_api's window into the mp_window (which is automatically added
185    // to your desktop by create_mp_window)
186    p1->add_child(0,0,rwin);
187
188    i4_button_class *quit=new i4_button_class(0,
189                                              new i4_text_window_class(i4_const_str("Quit"),
190                                                                       style),
191                                              style,
192                                              new i4_event_reaction_class(this, QUIT));
193    wm->add_child(0,0, quit);
194  } 
195
196
197  void receive_event(i4_event *ev)
198  {
199    if (ev->type()==i4_event::USER_MESSAGE && ((i4_user_message_event_class *)ev)->sub_type==QUIT)
200      quit();
201    else
202      i4_application_class::receive_event(ev);
203  }
204
205  void uninit()
206  {   
207    // cleanup the the render_api
208    r1_destroy_api(api);
209
210    // the application will close the display and cleanup memory here
211    i4_application_class::uninit();
212  }
213
214
215  char *name() { return "test_app"; }
216};
217
218void i4_main(w32 argc, i4_const_str *argv)
219{
220  test_app test;
221  test.run();
222}
223
Note: See TracBrowser for help on using the repository browser.