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