* Add "less" target to view make output through less.
[matthijs/ABM2.git] / ABM2 / Amaltheia / Graphics.h
1 /***************************************************************************
2  *   Copyright (C) 2005 by Dimitris Saougos & Filippos Papadopoulos   *
3  *   <psybases@gmail.com>                                                             *
4  *                                                                                                       *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU Library General Public License as       *
7  *   published by the Free Software Foundation; either version 2 of the    *
8  *   License, or (at your option) any later version.                                    *
9  *                                                                                                           *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU Library General Public     *
16  *   License along with this program; if not, write to the                 *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #ifndef _Graphics
22 #define _Graphics
23
24
25 #define GL_GLEXT_PROTOTYPES 1
26
27 #ifdef _WIN32                                                   // an eimaste se Windows
28         #include <windows.h>
29         #define _USE_MATH_DEFINES
30 #endif
31
32 /* #define GLX_GLXEXT_PROTOTYPES
33  #include <GL/glx.h>
34  #include <GL/glxext.h> */
35
36
37 extern char **argv;
38 extern int argc;
39
40 /********
41 group: Types
42 */
43
44
45
46
47 struct colour
48 {
49         float r;
50         float g;
51         float b;
52         float a;
53 };
54
55 /*struct: particle */
56 struct particle
57 {
58         float x;
59         float y;
60         float z;
61         float size;
62         colour col;
63 };
64
65
66
67
68 /*struct: vertex
69 This struct represents a 3D vertex
70
71 see also:
72 <COLOUR_RGBA>, <Graphics::renderVertexArray>, <Graphics::renderTriangle>*/
73 struct vertex
74 {
75 /*      field:  u,v
76         texture coordinates*/
77         float u,v;
78 /*      field: col
79         the vertex colour*/
80         colour col;
81 /*      field: nx,ny,nz
82         direction of the normal vector for the vertex */
83         float nx,ny,nz;
84 /*fields: x,y,z
85         3D coordinates for the vertex*/
86         float x,y,z;
87
88 };
89
90
91 struct camera
92 {
93         float camx, camy, camz;
94         float lookx, looky, lookz;
95         float upx, upy, upz;
96 };
97
98
99 /*struct: light
100 3D light type
101
102 see also:
103 <Graphics::setLight>, <Graphics::showLight>, <COLOUR_RGBA>*/
104 struct light
105 {
106         /*field: type
107          the type of light.     _type_ can be one of
108
109                         + *AM_POINT_LIGHT*  specifies a point light. This type of light has a position within the scene but no single direction.
110                         They emit their light in all directions. Think of a light-bulb in your house ;)
111                         + *AM_DIRECTIONAL_LIGHT* specifies a directional light. This type of light has a direction but not position.
112                         Think the sun here.
113                         + *AM_SPOT_LIGHT* specifies a spot light. *NOTE* currently, spot light is partially implemented.        */
114         int type;
115 /*field: diffuse
116         the diffuse RGBA intensity of the light */
117         colour diffuse;
118 /*field: specular
119         the specular RGBA intensity of the light*/
120         colour specular;
121 /*field:ambient
122         the ambient RGBA intensity of the light*/
123         colour ambient;
124 /*fields: posx,posy,posz
125         the position of the point light. *Only* for point lights*/
126         float posx,posy,posz;
127
128 /*fields: dirx,diry,dirz
129         the direction of the directional light. *Only* for directional lights */
130         float dirx,diry,dirz;
131 /*field: range
132         only for spot lights*/
133         float range;
134 /*fields: falloff,theta,phi
135         only for spot lights*/
136         float falloff,theta,phi;
137 };
138
139
140 #define AM_POINT_LIGHT                                  1
141 #define AM_DIRECTIONAL_LIGHT    2
142 #define AM_SPOT_LIGHT                                   3
143
144 #define AM_SOFTWARE_PARTICLE    4
145 #define AM_HARDWARE_PARTICLE    5
146
147 #define AM_CULL_FRONT           6
148 #define AM_CULL_BACK            7
149 #define AM_CULL_NONE            8
150
151 #define AM_PLAIN                                        9
152 #define AM_LINEAR                                       10
153 #define AM_ANISOTROPIC  11
154
155 #define AM_TRIANGLE_STRIP       12
156 #define AM_TRIANGLE_FAN         13
157 #define AM_TRIANGLES                            14
158 #define AM_POINT_LIST                           15
159 #define AM_LINE_LIST                            16
160 #define AM_LINE_STRIP                           17
161
162 #define AM_ALPHA                        18
163 #define AM_RGBA                         19
164
165 #define AM_UBYTE                        20
166
167 #define AM_XYZ  21
168 #define AM_XZY  22
169 #define AM_ZXY  23
170 #define AM_ZYX  24
171 #define AM_YZX  25
172 #define AM_YXZ  26
173
174
175
176 /*
177 group: Macros
178 */
179
180 /*function: COLOUR_RGBA
181
182 parameters:
183  r,g,b,a - the Red, Green, Blue and Alpha colour components */
184 extern colour COLOUR_RGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a );
185
186
187
188
189 /*
190 group: Classes
191 */
192
193 class Graphics;
194 class FontImp;
195
196 class Font
197 {
198 public:
199         Font();
200         ~Font();
201         
202         Font(const char *name, int size, Graphics *g);
203         
204         void print( const wchar_t *str, int x1, int y1, int x2, int y2, colour c );
205         void print( char *str, int x1, int y1, int x2, int y2, colour c );
206
207 private:
208         FontImp *imp;
209         /*      FTGLTextureFont *font;
210                 int fontSize;
211                 Graphics *g;*/
212 };
213
214
215 class Texture;
216 class GraphicsImp;
217
218
219 /* class: Graphics
220  This class is the core class of the Amaltheia library. It handles the display and
221  via its methods you can render 2D and 3D primitives.
222 */
223 class Graphics
224 {
225
226 public:
227
228 /* group: Constructors*/
229         /* constructor: Graphics
230         
231 parameters:
232                 width - the desired width of the display's resolution
233                 height - the desired height of the display's resolution
234                 fullScreen - if *true* run in full screen, otherwise in a window
235                 bpp - the desired Bits Per Pixel for the display
236                 alpha - enables/disables alpha blending
237                 lighting - enables/disables lighting
238
239 see also:
240                         <createDisplay>         */
241         Graphics( int width = 1024, int height = 768, int bpp = 16, bool fullScreen = false,
242                                                                                 bool alpha = false, bool lighting = false );
243         ~Graphics();
244
245         
246 /* group: Methods */
247         
248         /*      Method: setWidth 
249                 Sets the width of the display
250         
251         parameters:
252                 w - the desired width
253         
254         see also:
255                 <getWidth>*/
256         void setWidth( int w = 1024 );
257
258 /*      Method: getWidth
259         Returns the width of the display
260
261         returns:
262                 the current display's width in pixels
263         
264         see also:
265                 <setWidth>*/
266         int getWidth();
267
268 /*      Method: setHeight
269         Sets the height of the display
270
271 parameters:
272         h - the desired height
273
274 see also:
275         <getHeight>*/
276         void setHeight( int h = 768 );
277         
278 /*      Method: getHeight
279          Returns the height of the display
280
281 returns:
282         the height of the display in pixels
283
284 see also:
285         <setHeight>*/
286         int getHeight();
287
288         /*method: setBpp
289         Sets the Bits per Pixel for the display
290
291         parameters:
292                 bpp - bits per pixel
293
294         see also:
295                 <getBpp> */
296         void setBpp( int bpp = 16);
297
298         /*method: getBpp
299         Returns the current BPP of the display
300
301         returns:
302                 bits per pixel
303
304         see also:
305                 <setBpp>*/
306         int getBpp();
307
308         void enableVsync( bool b);
309         bool isVsync();
310
311         
312         void enableFullScreen( bool b);
313         bool isFullScreen();
314
315         /*method: enableLighting
316                 Enables/disables lighting in the scene
317
318         parameters:
319                 *true* to enable lighting or *false* to disable it
320
321         see also:
322                 <isLighting>    */
323         void enableLighting( bool );
324
325         /*method: isLighting
326         Is lighting enabled?
327
328         returns:
329                 *true* if lighting is enabled, *false* otherwise
330
331 see also:
332                 <enableLighting>*/
333         bool isLighting();
334
335         /*method: enableDepthBuffer
336         Enables/disables the depth (Z) buffer
337
338         parameters:
339                 *true* to enable the Z-buffer, *false* to disable it
340
341         see also:
342         <isDepthBuffer> */
343         void enableDepthBuffer( bool );
344
345         /*method: isDepthBuffer
346         Is the depth buffer enabled?
347
348         returns:
349                 *true* if the depth buffer is enabled, *false* otherwise
350
351         see also:
352                 <enableDepthBuffer>     */
353         bool isDepthBuffer();
354
355         /*method: enableAlpha
356         Enables/disables alpha blending
357
358         parameters:
359                 *true* to enable alpha blending, *false* to disable it
360
361         see also:
362                 <isAlpha>       */
363         void enableAlpha( bool );
364
365         /*method: isAlpha
366         Is alpha blending enabled?
367
368         returns:
369                 *true* is alpha blending is enabled, *false* otherwise
370
371         see also:
372                 <enableAlpha>   */
373         bool isAlpha();
374
375
376         void enableCursor( bool );
377         bool isCursor();
378
379         
380         /* method: createDisplay
381                 Creates the display. You should call this method *once*.
382
383         see also:
384         <Graphics>*/
385         int createDisplay();
386
387         /*method: beginScene
388         You must call this method before starting drawing/rendering primitives to the display.
389         This method does some initializations e.g clears the color and depth buffers, etc...
390
391         see also:
392                 <endScene>*/
393         void beginScene();
394
395         /*method: endScene
396         You must call this method after you have finished rendering the current frame and you want
397         to be visible on the screen. <beginScene> and <endScene> methods must be pair, so you must
398         call <beginScene> once, before calling <endScene>.
399
400         see also:
401                 <beginScene>*/
402         void endScene();
403
404
405         
406         /*method: setWorld
407         Sets the world tranformations matrices (translation, rotation, scale)
408
409         parameters:
410         transx - number of units to translate in the X axis
411         transy - number of units to translate in the Y axis
412         transz - number of units to translate in the Z axis
413         rotx - degrees to rotate in the X axis
414         roty - degrees to rotate in the Y axis
415         rotz - degrees to rotate in the Z axis
416         scalex - scale factor in the X axis (a factor of 1.0 performs no scale)
417         scaley - scale factor in the Y axis
418         scalez - scale factor in the Z axis     */
419         int setWorld( float transx, float transy, float transz,
420                                           float rotx, float roty, float rotz,
421                                           float scalex, float scaley, float scalez, int rotationOrder = AM_XYZ);
422
423
424         /*
425         int setWorld2( float transx, float transy, float transz,
426                                           float rotx, float roty, float rotz,
427                                           float scalex, float scaley, float scalez );
428
429         */
430         
431         
432         /*method: setCamera
433         Specifies the position and the direction of the camera.
434
435         parameters:
436                 camx - the x coordinate of the camera position
437                 camy - the y coordinate of the camera position
438                 camz - the z coordinate of the camera position
439                 lookx - the x coordinate of the reference point
440                 looky - the y coordinate of the reference point
441                 lookz - the z coordinate of the reference point
442                 upx -  the x coordinate of the up vector
443                 upy -  the y coordinate of the up vector
444                 upz - the z coordinate of the up vector
445
446         see also:
447                 <getCamera> */
448         int setCamera( float camx, float camy, float camz,
449                                                  float lookx, float looky, float lookz,
450                        float upx, float upy, float upz );
451
452 /*method: getCamera
453         Returns a <camera> struct containing the position and the direction of the camera
454
455         returns:
456                 a <camera> struct
457
458         see also:
459                 <setCamera>*/
460         struct camera getCamera();
461         
462
463         /*method: setBackground
464          Sets the background colour of the display.
465
466         parameters:
467                 c - the colour to set. Use the COLOUR_RGBA() macro to specify it
468
469         see also:
470                 <getBackground>*/
471         void setBackground( colour c );
472
473         /* method: getBackground
474
475         returns:
476                 the colour of the display background
477
478         see also:
479                 <setBackground> */
480         colour getBackground();
481
482         /* method: setTexture
483         Sets which will be the current texture to use.
484
485         parameters:
486                 tex - a pointer to a <Texture> object
487
488         see also:
489         <getTexture>, <Texture> class*/
490         void setTexture( Texture *tex );
491
492         /*method: getTexture
493
494         returns:
495                 a pointer to the current <Texture> object
496
497         see also:
498         <setTexture>, <Texture> class   */
499         Texture* getTexture();
500         
501         /*method: setCullingMode
502         Specifies the culling mode to use, whether front or back facets will be culled.
503         The orientation of front-facing polygons is clock-wise.
504
505         parameters:
506                 mode - the desired culling mode
507
508         valid values for _mode_ are
509
510         + *AM_CULL_BACK* for culling back facets
511         + *AM_CULL_FRONT* for culling front facets
512         + *AM_CULL_NONE* for culling none of the facets
513
514         see also:
515                 <getCullingMode>        */
516         void setCullingMode(int mode);
517
518         /*method: getCullingMode
519
520         returns:
521                 The current culling mode
522
523         see also:
524         <setCullingMode>        */
525         int  getCullingMode();
526
527
528         /*method:  renderTriangle
529         Renders a triangle in the display
530
531         parameters:
532         v1, v2, v3 - the 3 vertices that specify the triangle
533
534         see also:
535                 <vertex> struct, <renderVertexArray>    */
536         int renderTriangle(vertex v1, vertex v2, vertex v3 );
537
538         /*method: renderVertexArray
539         Renders a mesh of vertices
540
541         parameters:
542                 array - an array containing the vertices
543                 numOfVertices - the number of vertices in the array
544                 type - how to connect the vertices
545
546         
547         valid values for _type_ are
548         
549         + *AM_TRIANGLE_STRIP*  renders a connected group of triangles. One triangle is defined for each vertex
550         presented after the first two vertices. For odd i, vertices i, i+1, and i+2 define triangle i.
551         For even i, vertices i+1, i, and i+2 define triangle i. 'numOfVertices-2' triangles are rendered
552         + *AM_TRIANGLE_FAN*     renders a connected group of triangles. One triangle is defined for each vertex
553         presented after the first two vertices. Vertices 1, i+1, and i+2 define triangle i. 'numOfVertices/-2' triangles are rendered
554         + *AM_TRIANGLES*  renders each triplet of vertices as an independent triangle.
555         Vertices 3i-2, 3i-1, and 3i define triangle i. 'numOfVertices/3' triangles are rendered
556         + *AM_POINT_LIST*       renders each vertex as a single point. Vertex i defines point i. 'numOfVertices' points are rendered
557         + *AM_LINE_LIST*        renders each pair of vertices as an independent line segment.
558         Vertices 2i-1 and 2i define line i. 'numOfVertices/2' lines are rendered
559         + *AM_LINE_STRIP*       renders a connected group of line segments from the first vertex to the last. Vertices i and i+1 define line i. 'numOfVertices - 1' lines are rendered
560
561 see also:
562         <renderTriangle>, <vertex> struct */
563         void renderVertexArray(vertex *array, unsigned int numOfVertices, int type);
564
565
566
567
568         /*method: setLight
569         Creates a new light numbered 'numLight' with the properties defined in  'lt'
570
571         parameters:
572         numLight - Specifies a light number. The number of lights depends on the underlying implementation,
573         but at least eight lights are supported. They are identified by integers 0 .. maximum lights
574
575         lt - a struct <light> variable which defines the properties of the 'numLight' light
576         
577 see also:
578         struct <light>, <showLight>*/
579         void setLight( int numLight, const light& lt );
580
581         /*method: showLight
582         Enables/disables light 'numLight'. Light 'numLight' must first have been created with the <setLight> method.
583
584         parameters:
585         numLight - the number of the light to enable/disable
586
587         state - *true* to enable the light, *false* otherwise
588         
589         see also:
590                 struct <light>, <setLight>*/
591         void showLight( int numLight, bool state );
592
593         /*method: renderParticle
594         Renders a particle in the display
595
596         parameters:
597         x - the x coordinate of the particle position
598         
599         y - the y coordinate of the particle position
600         
601         z - the z coordinate of the particle position
602         
603         size - size of the particle
604         
605         col - colour of the particle
606         
607         type - can be *AM_SOFTWARE_PARTICLE* or *AM_HARDWARE_PARTICLE*
608
609         see also:
610         <renderParticleArray>*/
611         void renderParticle(float x, float y, float z, float size, colour col, int type);
612
613         /*method: renderParticleArray
614         Useful for rendering lots of particles
615
616         parameters:
617         array - the array of particles
618
619         sizeOfArray - guess what...
620
621         type - can be *AM_SOFTWARE_PARTICLE* or *AM_HARDWARE_PARTICLE*
622
623         see also:
624         <renderParticle>*/
625         void renderParticleArray(particle *array, unsigned int sizeOfArray, int type);
626         void setPerspective(float fov, float ratio, float zNear, float zFar);
627
628         /*method: project*/
629         void project( float objx, float objy, float objz,
630                                           float *winx, float *winy, float *winz );
631
632         /*method: unproject*/
633         void unproject( float winx, float winy, float winz,
634                                                  float *objx, float *objy, float *objz );
635
636         /*method: planeIntersect*/
637         void planeIntersect( float a, float b, float c, float d,
638                                                                 float p1x, float p1y, float p1z, float p2x, float p2y, float p2z,
639                                                                 float *poutx, float *pouty, float *poutz );
640
641         /*method: enter2dMode*/
642         void enter2dMode();
643         /*method: leave2dMode*/
644         void leave2dMode();
645
646 private:
647         static bool oneGraphics;        //gia na dhmiourgeitai mono ena antikeimeno Graphics
648         int width;
649         int height;
650         int bpp;
651         bool fullScreen;
652         bool vSync;
653         bool lighting;
654         bool alpha;
655         bool culling;
656         int cmode;
657         bool zBuffer;
658         bool cursorState;
659         Texture *currentTexture;
660         struct camera cam;
661         bool sceneBegin; //flag gia to an egine sceneBegin
662         float bgcolour[ 4 ];
663         unsigned int gpuBuf;
664         int parameter;
665         float maxSize; //point size gia particles me point pixel
666         void drawBillboardParticle(float x, float y, float z, float size, colour col);
667         GraphicsImp *imp;
668         //SDL_Surface *screen;
669
670 };
671
672
673
674 class Sprite;
675
676 /*class: Texture
677 A class for representing 2d textures
678
679 see also:
680 <Graphics>*/
681
682 class Texture
683 {
684         friend class Sprite;
685 private:
686         colour colourKey;
687         int filtering, filteringDefinition;
688         bool colourKeyEnabled;
689         unsigned long width;
690         unsigned long height;
691  //     GLuint gl_Tex;
692         Graphics *g;
693         //void swap( unsigned char &a, unsigned char &b );
694 //      void CreateTexture( unsigned int textureArray[], char *strFileName, int textureID );
695 //      int ImageLoad( const char *filename, Image *image );
696         void LoadGLTextures( unsigned int *gl_Tex, const char * filename );
697
698         friend void Graphics::setTexture( Texture *tex );
699
700 public:
701         
702         unsigned int gl_Tex;
703         
704 /* group: Constructors*/
705         
706
707         /* constructor: Texture
708         Creates a texture from an image file
709
710         parameters:
711                 file - the filename of the image to use as a texture. Accepted formats are PNG, TGA, BMP, JPG, DDS.
712                 g - a pointer to a Graphics object*/
713         Texture( char *file, Graphics *g);
714         /* constructor: Texture
715         Creates a texture from an image file
716
717         parameters:
718         file - the filename of the image to use as a texture. Accepted formats are PNG, TGA, BMP, JPG.
719         key - the colour key to use
720         g - a pointer to a Graphics object*/
721         Texture( char *file, colour key, Graphics *g);
722         /* constructor: Texture
723         Creates a texture from memory
724
725         parameters:
726         data - a pointer to the textures data
727         w - the width of the texture
728         h - the height of the texture
729         format - the format of the pixels in _data_. Accepted values are AM_RGBA, AM_ALPHA
730         type - currently the one supported type is AM_BYTE
731         g - a pointer to a Graphics object*/
732         Texture(void *data, unsigned int w, unsigned int h, int format, int type, Graphics *g);
733         ~Texture();
734
735 /*group: Methods */
736         void texSubImage2D(int xoff, int yoff, unsigned int w, unsigned int h, int format, int type, const void *pixels);
737         void setFiltering(int f, float anisotropyLevel = 1.0);
738         int getFiltering();
739         /*method: getWidth
740         returns:
741          the texture width */
742         unsigned long getWidth();
743         /*method: getHeight
744         returns:
745          the texture height*/
746         unsigned long getHeight();
747         //unsigned int gl_getGLTex();
748 };
749
750
751 #endif