JNR
laRenderer.h
1 /*
2 
3 Jump'n'Run Engine
4 http://www.atanaslaskov.com/jnr/
5 
6 BSD LICENSE
7 Copyright (c) 2007-2013, Atanas Laskov
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12 1. Redistributions of source code must retain the above copyright notice,
13 this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 */
29 #ifndef M_RENDERER_H
30 #define M_RENDERER_H
31 
33 
34 
42 
44 //
46 public:
47  unsigned nType;
48 
50 
51  M_BOOL bWrapU;
52  M_BOOL bWrapV;
54 
56  laTextureParams(unsigned type=M_TEX_SIMPLE, M_BOOL wrapu=M_TRUE, M_BOOL wrapv=M_TRUE) {
57  nType = type;
58  bWrapU = wrapu;
59  bWrapV = wrapv;
60  }
61 
62 };
63 
65 //
66 class laOutline
67 {
68 public:
69 
70  M_BOOL bEnabled;
71  laColor color;
72  double dWidth;
73  M_BOOL bTopmost;
74 
75  unsigned id;
76 
77  // Renderer flags
78  M_BOOL bPass1_Enabled;
79  M_BOOL bPass2_Enabled;
80 
82  //
83  laOutline( M_BOOL bOn = M_TRUE,
84  const laColor &col = laColor(), const double &width = 1, M_BOOL topmosy = M_FALSE)
85  {
86  bEnabled = bOn;
87  color = col;
88  dWidth = width;
89  bTopmost = topmosy;
90 
91  id = 1;
92  bPass1_Enabled = bPass2_Enabled = M_TRUE;
93  }
94 };
95 
97 //
99 {
100 protected:
101  M_BOOL _bShowProfiler;
102 
103  laFontManager _fontman;
104  laColor _rgbBg;
106 
108 
109  class laTexture* _pVarTex;
110  double _dDetailTex_Scale;
111  laPoint3 _ptVarTexOffset;
113 
115  // NOTE: Only the laSystemIntegrator ccan access create and discard
117  virtual void create() = 0;
118  virtual void discard() = 0;
119  friend class laSystemIntegrator;
121 
123  // NOTE: Texture loading calls are to be used directly only by the laTexutre class;
124  // They don't implement checks if the texture is already loaded but only provide the necessary grpahics code.
125  //
127  friend class laTexture;
128  virtual laTexture texLoad(std::string strFile, laTextureParams params)=0;
129  virtual void texUse(laTexture *pTex)=0;
131 
133 
134  static laPoint3* _arQuadData;
135  static laPoint2* _arQuadUV;
136  static laColor* _arQuadRGB;
137  static unsigned _nMaxQuads;
139 
141  //
143  unsigned _nW, _nH;
145  laPoint2 _ptCamAngX_sc, _ptCamAngY_sc;
146 
147 
148 public:
149  laRenderer();
150  ~laRenderer(void);
151 
152  virtual laPoint3 getCameraAngles() = 0;
153 
155 
156  virtual void modeInterface() =0;
157  virtual void modeWorld(const laPoint3 &pos, const laPoint3 &ang = laPoint3()) =0;
158 
159  virtual void commit() =0;
160 
161 
163 
164  virtual void modeDepthBuffer(M_BOOL bOn) =0;
165  virtual void modeTexture(M_BOOL bOn) =0;
166  virtual void modeLight(M_BOOL bOn) =0;
167  virtual void modeFog(M_BOOL bOn) =0;
168  virtual void modeBlend(M_BOOL bOn) =0;
169 
171  virtual double modeBlendFilter(const double &dMinVisisbleAlpha) =0;
172 
174  virtual void modeOutline(const laOutline &outline) { _outline = outline; }
176 
178 
179  virtual void modeProfiler(M_BOOL bOn) { _bShowProfiler = bOn; }
180  M_BOOL isProfilerOn() { return _bShowProfiler; }
181 
182 
184  // This is a tiled texture displayed on top of the usual object texture. It is suitable
185  // for adding cracks, vegetation and other detail to rendered terrains.
187  virtual void vtex(laTexture* pTex = NULL, double scale = 0.2) = 0; //<! Set detail texture (or invalidate if null)
188  virtual void vtexOffset(laPoint3 ofs) { _ptVarTexOffset = ofs; }
189 
190 
192  inline laFont* font(char* strName) { return _fontman.get(strName); }
193 
195 
196  virtual void lightMakeProjector(unsigned nLight, const laPoint3 &pos, const laPoint3 &dir, const double &angle=360) = 0;
197  virtual void lightMakeSunlight(unsigned nLight, const laPoint3 &pos) = 0;
198 
199  virtual void lightAmbient(unsigned nLight, const laColor &rgb) = 0;
200  virtual void lightDiffuse(unsigned nLight, const laColor &rgb) = 0;
201  virtual void lightSpecular(unsigned nLight, const laColor &rgb) = 0;
202  virtual void lightAttenuation(unsigned nLight, double a) = 0;
203 
204  virtual void lightSetCount(unsigned n)=0;
206 
208 
209  virtual void fogColor(const laColor &rgb) =0;
210  virtual void fogRange(const double &start, const double &end) =0;
211  virtual M_BOOL getFogMode()=0;
213 
215 
216  virtual void styleSet(const laColor &diffuse, const laColor &specular=laColor(255,255,255,255)) =0;
217  virtual void styleShininess(unsigned s)=0;
218  virtual void styleLine(double dWidth)=0;
219 
220  virtual unsigned styleShaderLoad(char* strShaderName) =0;
221  virtual void styleShader(unsigned ShaderID) =0;
222 
223  virtual void styleBackground(const laColor &bg) {_rgbBg=bg;};
225 
227 
228  virtual void transTranslate(const laPoint3 &pos)=0;
229  virtual void transRotate(const double &ang, const laPoint3 &axis)=0;
230  virtual void transScale(const laPoint3 &sz)=0;
231 
232  virtual void transPush()=0;
233  virtual void transPop()=0;
235 
237 
238  virtual unsigned compiledActivate(M_BOOL bOn) = 0;
239  virtual void compiledDraw(unsigned nID) =0;
241 
243  //
244  // Volatile" quads are an array of vertex data (4 points per quad) accessible by all objects;
245  // Objects that need to change vertex data on every frame (eg. particle Fx, Fonts) can use VQ as a way to save memory
246  // and speed up rendering; VQ don't require the client of the renderer to store any fertex data and also all VQ are
247  // commited to the rendering device together.
248  //
250 
252  inline unsigned vquadsCount() const{ return _nMaxQuads; }
253 
255  inline void vquadsAllocate( unsigned nMaxQuads )
256  {
257  ASSERT(_nMaxQuads<nMaxQuads, "Wrong argument");
258  laPoint3 *pv = new laPoint3[ nMaxQuads*4 ];
259  laPoint2 *pt = new laPoint2[ nMaxQuads*4 ];
260  laColor *pc = new laColor[ nMaxQuads*4 ];
261 
262  if(_arQuadData) {
263  memcpy(pv, _arQuadData, sizeof(laPoint3)*_nMaxQuads*4);
264  memcpy(pt, _arQuadUV, sizeof(laPoint2)*_nMaxQuads*4);
265  memcpy(pc, _arQuadRGB, sizeof(laColor)*_nMaxQuads*4);
266 
267  delete [] _arQuadData;
268  delete [] _arQuadUV;
269  delete [] _arQuadRGB;
270  }
271  _arQuadData = pv; _arQuadUV = pt; _arQuadRGB = pc;
272 
273  _nMaxQuads = nMaxQuads;
274  MLOG("VQ array resized %d", _nMaxQuads);
275  }
276 
278  inline laPoint3* vquadsData(unsigned nQuad, unsigned nCount = 1) {
279  if( (nQuad+nCount)>=_nMaxQuads ) vquadsAllocate( nQuad + nCount + 20 );
280  return _arQuadData + nQuad*4;
281  }
282 
284  inline laPoint2* vquadsTexCoord(unsigned nQuad, unsigned nCount = 1) {
285  if( (nQuad+nCount)>=_nMaxQuads ) vquadsAllocate( nQuad + nCount + 20 );
286  return _arQuadUV + nQuad*4;
287  }
288 
290  inline laColor* vquadsCol(unsigned nQuad, unsigned nCount = 1) {
291  if( (nQuad+nCount)>=_nMaxQuads ) vquadsAllocate( nQuad + nCount + 20 );
292  return _arQuadRGB + nQuad*4;
293  }
294 
296  virtual void vquadsDraw(unsigned nQuads, laPoint2 *ar_uv = NULL, laColor *ar_color = NULL,
297  M_BOOL bBillboards = M_FALSE, M_BOOL bUseColorArrays = M_FALSE) = 0;
298 
300  virtual void vquadsDrawSingle(laPoint2 *ar_uv = NULL) = 0;
301 
303  inline void vquadsMakeBillboards(unsigned nQuads, laPoint3 *ar_pos, double *ar_size)
304  {
305  // Use camera info to buld camera-facing billboards
306  // NOTE: The derived class is responsible for keeping track fo camera angles
307  //
308  PROFILE_REN(laRenderer_vquadsMakeBillboards);
309  static double sz;
310  laPoint3 *ppt = vquadsData(0, nQuads);
311 
312  laPoint3 qv[4] = { laPoint3(-0.5*_ptCamAngY_sc(1), -0.5, -0.5*_ptCamAngY_sc(0)),
313  laPoint3(+0.5*_ptCamAngY_sc(1), -0.5, +0.5*_ptCamAngY_sc(0)),
314  laPoint3(+0.5*_ptCamAngY_sc(1), +0.5, +0.5*_ptCamAngY_sc(0)),
315  laPoint3(-0.5*_ptCamAngY_sc(1), +0.5, -0.5*_ptCamAngY_sc(0)) };
316 
317  for(unsigned i=0; i<nQuads; i++) {
318  sz = ar_size[i];
319 
320  *(ppt++) = qv[0]*sz + ar_pos[i];
321  *(ppt++) = qv[1]*sz + ar_pos[i];
322  *(ppt++) = qv[2]*sz + ar_pos[i];
323  *(ppt++) = qv[3]*sz + ar_pos[i];
324  }
325  }
326 
328  inline void vquadsMakeXYRect( unsigned nIndex,
329  const laPoint3 &pos, const laPoint3 &sz, const laPoint2 &uv, const laPoint2 &uv_sz )
330  {
331  PROFILE_REN(laRenderer_vquadsMakeXYRect);
332  laPoint3 *pquad = vquadsData(nIndex);
333  laPoint2 *puv = vquadsTexCoord(nIndex);
334 
335  *(pquad++) = pos;
336  *(pquad++) = pos + laPoint3(sz.x(), 0);
337  *(pquad++) = pos + laPoint3(sz);
338  *(pquad++) = pos + laPoint3(0, sz.y());
339 
340  *(puv++) = uv + laPoint2(0, uv_sz.y());
341  *(puv++) = uv + uv_sz;
342  *(puv++) = uv + laPoint2(uv_sz.x(), 0);
343  *(puv++) = uv;
344 
345  }
347 
348  // Misc unused
349  //
350  virtual void trianglesDraw( laPoint3* ar_v, laPoint2* ar_tv, laPoint3* ar_n, unsigned nvert )=0;
351 
352  virtual void drawRect(const laPoint3 &pos, const laPoint3 &sz, const laPoint2 &uv, const laPoint2 &uv_sz,
353  double an1=0, double an2=0, M_BOOL bAlignX=M_TRUE)=0;
354 
355  virtual void drawLine(const laPoint3 &a, const laPoint3 &b)=0;
356 
357  virtual void drawEllipse2(laPoint3 a, laPoint3 b, double z=0, bool flat=true, bool lighten=true) =0;
358 
359  //Misc
360  //
361  //unsigned displayGetW(){return _nW;};
362  //unsigned displayGetH(){return _nH;};
363  //void displaySetScale(double s){_dScale=s;};
364  //double displayGetScale(){return _dScale;};
365  //virtual void displayClip(laPoint3 a, laPoint3 b)=0;
366  //virtual void displayGetClip(double *x1, double *y1, double *x2, double *y2);
367 
368  //double displayGetIntensity() {return _dIntensity;}
369  //void displaySetIntensity(double i);
370  //double displayGetBrightness() {return _dBrightness;}
371  //void displaySetBrightness(double b);
372 
373  static M_BOOL _bDbgEnable;
374  static double _dDbgOfsX;
375  static double _dDbgOfsY;
376 };
378 #endif //#ifndef M_RENDERER_H
laFont * font(char *strName)
Get a font renderer.
Definition: laRenderer.h:192
laPoint2 * vquadsTexCoord(unsigned nQuad, unsigned nCount=1)
Get a pointer to VQ texture coordinates.
Definition: laRenderer.h:284
virtual void modeTexture(M_BOOL bOn)=0
Enable/disable texturing.
laOutline(M_BOOL bOn=M_TRUE, const laColor &col=laColor(), const double &width=1, M_BOOL topmosy=M_FALSE)
Outline constructor.
Definition: laRenderer.h:83
M_BOOL bEnabled
Is the outline enabled.
Definition: laRenderer.h:70
unsigned _nW
\ Camera-related varaibles
Definition: laRenderer.h:143
Font and Text Drawing.
Definition: laFont.h:41
static laPoint2 * _arQuadUV
Quad texture coordinates.
Definition: laRenderer.h:135
virtual void modeFog(M_BOOL bOn)=0
Enable/disable fox.
unsigned nType
Texture opacity type (defined in Core-Parameters.h)
Definition: laRenderer.h:47
virtual void vquadsDrawSingle(laPoint2 *ar_uv=NULL)=0
Draw a single VQ (Note this is slower than drawing an array of VQ and should be avoided) ...
laTextureParams(unsigned type=M_TEX_SIMPLE, M_BOOL wrapu=M_TRUE, M_BOOL wrapv=M_TRUE)
Class constructor.
Definition: laRenderer.h:56
2D Point
Definition: laPoint_novec.h:41
void vquadsMakeXYRect(unsigned nIndex, const laPoint3 &pos, const laPoint3 &sz, const laPoint2 &uv, const laPoint2 &uv_sz)
Makes a quad aligned to the XY plane; (handy for GUI rendering )
Definition: laRenderer.h:328
laColor * vquadsCol(unsigned nQuad, unsigned nCount=1)
Get a pointer to VQ color data.
Definition: laRenderer.h:290
static laPoint3 * _arQuadData
Internal variablse for storing "Volatile Quads" (used by the particle system and the GUI) ...
Definition: laRenderer.h:134
M_BOOL bTopmost
If set the outline is visible even when the object is behind something.
Definition: laRenderer.h:73
laPoint2 _ptCamAngY_sc
Precomputed sin/cos of camera angle.
Definition: laRenderer.h:145
Texture creation parameters used by laTexture.
Definition: laRenderer.h:45
unsigned vquadsCount() const
Current nuber of quads.
Definition: laRenderer.h:252
Object outline properties.
Definition: laRenderer.h:66
virtual void modeProfiler(M_BOOL bOn)
Show/hide profiler window.
Definition: laRenderer.h:179
unsigned _nH
Viewport width and height (in pixels)
Definition: laRenderer.h:143
laRenderer()
Default constructor for the renderer.
Definition: laRenderer.cpp:48
laColor color
Outline color.
Definition: laRenderer.h:71
laPoint2 _ptCamAng
Camera angle along vertical axis.
Definition: laRenderer.h:144
laFontManager _fontman
Font manager pointer.
Definition: laRenderer.h:103
unsigned id
Outline identifiery (should be unique for outlines that don't merge)
Definition: laRenderer.h:75
static laColor * _arQuadRGB
Quad colors.
Definition: laRenderer.h:136
virtual void modeBlend(M_BOOL bOn)=0
Enable/disable alpha blending (opacity)
M_BOOL isProfilerOn()
Query profiler window status.
Definition: laRenderer.h:180
2D Texture
Definition: laTexture.h:45
virtual void vquadsDraw(unsigned nQuads, laPoint2 *ar_uv=NULL, laColor *ar_color=NULL, M_BOOL bBillboards=M_FALSE, M_BOOL bUseColorArrays=M_FALSE)=0
Draw an array of VQ, starting with the psecified pointers (or the first VQ if null) ...
virtual void modeOutline(const laOutline &outline)
Set outline parameters.
Definition: laRenderer.h:174
virtual void modeDepthBuffer(M_BOOL bOn)=0
Enable/disable depth buffer.
laOutline _outline
Active object outline.
Definition: laRenderer.h:105
laColor _rgbBg
Background clear color.
Definition: laRenderer.h:104
#define M_TEX_SIMPLE
Simple texture (no transparency)
double dWidth
Outline width.
Definition: laRenderer.h:72
Virtual interface for the Engine graphics renderer.
Definition: laRenderer.h:98
virtual void modeInterface()=0
Switch to interface rendering (2D projection mode)
virtual void modeLight(M_BOOL bOn)=0
Enable/disable lighting.
laPoint3 * vquadsData(unsigned nQuad, unsigned nCount=1)
Get a pointer to VQ vertex data.
Definition: laRenderer.h:278
virtual void commit()=0
Display renderer output.
virtual double modeBlendFilter(const double &dMinVisisbleAlpha)=0
Set a "blend filter" (cutting out elements with opacity lower than dMinVisisbleAlpha) ...
virtual void modeWorld(const laPoint3 &pos, const laPoint3 &ang=laPoint3())=0
Switch to "world rendering" (3D projection mode)
Engine Sub-systems Integrator.
virtual void vtexOffset(laPoint3 ofs)
Set detail texture offset.
Definition: laRenderer.h:188
void vquadsMakeBillboards(unsigned nQuads, laPoint3 *ar_pos, double *ar_size)
Use camera data to create an array of camera-facing VQ (handy for particle effects) ...
Definition: laRenderer.h:303
void vquadsAllocate(unsigned nMaxQuads)
Allocate memory for VQ (This is slow, so it should be called a minimal number ot times e...
Definition: laRenderer.h:255