Visualization Library 2.1.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
Clear.cpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #include <vlGraphics/Clear.hpp>
33 #include <vlGraphics/Camera.hpp>
34 #include <vlCore/Vector4.hpp>
35 #include <vlCore/Log.hpp>
36 
37 using namespace vl;
38 
39 //-----------------------------------------------------------------------------
40 Clear::Clear(): mClearColorMode(CCM_Float), mClearDepthValue(1.0f), mClearStencilValue(0),
41  mClearColorBuffer(false), mClearDepthBuffer(false), mClearStencilBuffer(false)
42 {
43  VL_DEBUG_SET_OBJECT_NAME()
44  // no scissor box by default
45  mScissorBox[0] = 0;
46  mScissorBox[1] = 0;
47  mScissorBox[2] = -1;
48  mScissorBox[3] = -1;
49 }
50 //-----------------------------------------------------------------------------
51 void Clear::render_Implementation(const Actor*, const Shader*, const Camera* camera, OpenGLContext*) const
52 {
53  // build buffer bit mask
54  GLbitfield mask = 0;
55  mask = mask | (mClearColorBuffer ? GL_COLOR_BUFFER_BIT : 0);
56  mask = mask | (mClearDepthBuffer ? GL_DEPTH_BUFFER_BIT : 0);
57  mask = mask | (mClearStencilBuffer ? GL_STENCIL_BUFFER_BIT : 0);
58 
59  // check for integer texture support
60  if ( (clearColorMode() == CCM_Int || clearColorMode() == CCM_UInt) && !Has_GL_EXT_texture_integer)
61  {
62  Log::error("Clear::render(): glClearColorIiEXT and glClearColorIuiEXT not supported.\n");
63  return;
64  }
65 
66  if (mask)
67  {
68  int viewport[] = { camera->viewport()->x(), camera->viewport()->y(), camera->viewport()->width(), camera->viewport()->height() };
69 
70  // save scissor settings
71  GLboolean scissor_on = glIsEnabled(GL_SCISSOR_TEST);
72  int scissor_box_save[4] = {0,0,-1,-1};
73  glGetIntegerv(GL_SCISSOR_BOX, scissor_box_save);
74 
75  int scissor_box[4] = {0,0,-1,-1};
76 
77  // fit to the viewport
78  if (mScissorBox[2] == -1 || mScissorBox[3] == -1)
79  {
80  scissor_box[0] = viewport[0];
81  scissor_box[1] = viewport[1];
82  scissor_box[2] = viewport[2];
83  scissor_box[3] = viewport[3];
84  }
85  else
86  // scissor box in viewport coords
87  {
88  scissor_box[0] = mScissorBox[0] + viewport[0];
89  scissor_box[1] = mScissorBox[1] + viewport[1];
90  scissor_box[2] = mScissorBox[2];
91  scissor_box[3] = mScissorBox[3];
92  }
93 
94  // viewport from x,y,w,h -> x,y,x2,y2
95  viewport[2] = viewport[0] + viewport[2] -1;
96  viewport[3] = viewport[1] + viewport[3] -1;
97  // scissor_box from x,y,w,h -> x,y,x2,y2
98  scissor_box[2] = scissor_box[0] + scissor_box[2] -1;
99  scissor_box[3] = scissor_box[1] + scissor_box[3] -1;
100  // clip scissor box
101  if (scissor_box[0] < viewport[0]) scissor_box[0] = viewport[0];
102  if (scissor_box[1] < viewport[1]) scissor_box[1] = viewport[1];
103  if (scissor_box[2] > viewport[2]) scissor_box[2] = viewport[2];
104  if (scissor_box[3] > viewport[3]) scissor_box[3] = viewport[3];
105  // culling
106  if (scissor_box[0] > scissor_box[2])
107  return;
108  if (scissor_box[1] > scissor_box[3])
109  return;
110  // scissor_box from x,y,x2,y2 -> x,y,w,h
111  scissor_box[2] = scissor_box[2] -scissor_box[0] +1;
112  scissor_box[3] = scissor_box[3] -scissor_box[1] +1;
113 
114  // enable scissor test
115  glEnable(GL_SCISSOR_TEST);
116  glScissor(scissor_box[0], scissor_box[1], scissor_box[2], scissor_box[3]); VL_CHECK_OGL()
117 
118  // defines the clear values
119  if (mClearColorBuffer)
120  {
121  switch(clearColorMode())
122  {
123  case CCM_Float: glClearColor( mClearColorValue.r(), mClearColorValue.g(), mClearColorValue.b(), mClearColorValue.a()); break;
124  case CCM_Int: glClearColorIiEXT( mClearColorValueInt.r(), mClearColorValueInt.g(), mClearColorValueInt.b(), mClearColorValueInt.a()); break;
126  }
127  }
128  if (mClearDepthBuffer)
129  glClearDepth(mClearDepthValue);
131  glClearStencil(mClearStencilValue);
132 
133  // clear!
134  glClear(mask);
135 
136  // restore scissor settings
137  if (!scissor_on)
138  glDisable(GL_SCISSOR_TEST);
139  glScissor(scissor_box_save[0], scissor_box_save[1], scissor_box_save[2], scissor_box_save[3]); VL_CHECK_OGL()
140  }
141 }
142 //-----------------------------------------------------------------------------
Associates a Renderable object to an Effect and Transform.
Definition: Actor.hpp:130
int y() const
Definition: Viewport.hpp:67
uvec4 mClearColorValueUInt
Definition: Clear.hpp:118
float mClearDepthValue
Definition: Clear.hpp:121
const T_Scalar & r() const
Definition: Vector4.hpp:112
ivec4 mClearColorValueInt
Definition: Clear.hpp:117
bool mClearDepthBuffer
Definition: Clear.hpp:124
Represents an OpenGL context, possibly a widget or a pbuffer, which can also respond to keyboard...
static void error(const String &message)
Use this function to provide information about run-time errors: file not found, out of memory...
Definition: Log.cpp:165
virtual void render_Implementation(const Actor *, const Shader *, const Camera *, OpenGLContext *) const
Definition: Clear.cpp:51
int mScissorBox[4]
Definition: Clear.hpp:119
Viewport * viewport()
The viewport bound to a camera.
Definition: Camera.hpp:140
Visualization Library main namespace.
fvec4 mClearColorValue
Definition: Clear.hpp:116
const T_Scalar & g() const
Definition: Vector4.hpp:113
int height() const
Definition: Viewport.hpp:71
int width() const
Definition: Viewport.hpp:69
Clear()
Definition: Clear.cpp:40
bool mClearColorBuffer
Definition: Clear.hpp:123
int x() const
Definition: Viewport.hpp:65
const T_Scalar & b() const
Definition: Vector4.hpp:114
Manages most of the OpenGL rendering states responsible of the final aspect of the rendered objects...
Definition: Shader.hpp:1830
bool mClearStencilBuffer
Definition: Clear.hpp:125
#define VL_CHECK_OGL()
Definition: OpenGL.hpp:156
Represents a virtual camera defining, among other things, the point of view from which scenes can be ...
Definition: Camera.hpp:49
EClearColorMode clearColorMode() const
Definition: Clear.hpp:96
int mClearStencilValue
Definition: Clear.hpp:122
const T_Scalar & a() const
Definition: Vector4.hpp:115