Visualization Library 2.0.0

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

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
Viewport.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/Viewport.hpp>
33 #include <vlGraphics/OpenGL.hpp>
34 #include <vlCore/Log.hpp>
35 #include <vlCore/Say.hpp>
36 
37 using namespace vl;
38 
39 //-----------------------------------------------------------------------------
40 // Viewport
41 //-----------------------------------------------------------------------------
43 {
44  VL_DEBUG_SET_OBJECT_NAME()
45  mX = 0;
46  mY = 0;
47  mWidth = 0;
48  mHeight = 0;
49  mClearColor = fvec4(0,0,0,1);
50  mClearDepth = 1.0f;
51  mClearStencil = 0;
54  mScissorEnabled = true;
55 }
56 //-----------------------------------------------------------------------------
57 Viewport::Viewport(int x, int y, int w, int h)
58 {
59  VL_DEBUG_SET_OBJECT_NAME()
60  mX = x;
61  mY = y;
62  mWidth = w;
63  mHeight = h;
64  mClearColor = fvec4(0,0,0,1);
65  mClearDepth = 1.0f;
66  mClearStencil = 0;
69  mScissorEnabled = true;
70 }
71 //-----------------------------------------------------------------------------
72 void Viewport::activate() const
73 {
74  VL_CHECK_OGL()
75 
76  // viewport
77  int x = mX;
78  int y = mY;
79  int w = mWidth;
80  int h = mHeight;
81 
82  if (w < 1) w = 1;
83  if (h < 1) h = 1;
84 
85  glViewport( x, y, w, h );
86 
87  // clear viewport
88  if ( mClearFlags )
89  {
90  #ifndef NDEBUG
91  if (!Has_GL_EXT_texture_integer)
92  {
93  switch( clearColorMode() )
94  {
95  case CCM_Int:
96  case CCM_UInt:
97  Log::bug("Viewport::activate(): GL_EXT_texture_integer not supported.\n");
98  break;
99  default:
100  break;
101  }
102  }
103  #endif
104 
105  // save writemask status to be restored later
106  GLboolean color_write_mask[4] = {0,0,0,0};
107  glGetBooleanv( GL_COLOR_WRITEMASK, color_write_mask);
108 
109  GLboolean depth_write_mask = 0;
110  glGetBooleanv( GL_DEPTH_WRITEMASK, &depth_write_mask );
111 
112  GLboolean stencil_write_mask = 0;
113  glGetBooleanv( GL_STENCIL_WRITEMASK, &stencil_write_mask );
114 
115  // make sure we clear everything
116  glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
117  glDepthMask( GL_TRUE );
118  glStencilMask( GL_TRUE );
119 
120  // enable scissor
121  if ( mScissorEnabled )
122  {
123  glEnable( GL_SCISSOR_TEST );
124  glScissor( x, y, w, h );
125  }
126  else {
127  glDisable( GL_SCISSOR_TEST );
128  }
129 
130  switch( clearColorMode() )
131  {
132  case CCM_Float: glClearColor( mClearColor.r(), mClearColor.g(), mClearColor.b(), mClearColor.a() ); break;
133  case CCM_Int: glClearColorIiEXT( mClearColorInt.r(), mClearColorInt.g(), mClearColorInt.b(), mClearColorInt.a() ); break;
134  case CCM_UInt: glClearColorIuiEXT( mClearColorUInt.r(), mClearColorUInt.g(), mClearColorUInt.b(), mClearColorUInt.a() ); break;
135  }
136 
137  glClearDepth( mClearDepth );
138 
139  glClearStencil( mClearStencil );
140 
141  glClear( mClearFlags );
142 
143  // restore writemasks
144  glColorMask( color_write_mask[0], color_write_mask[1], color_write_mask[2], color_write_mask[3] );
145  glDepthMask( depth_write_mask );
146  glStencilMask( stencil_write_mask );
147 
148  VL_CHECK_OGL()
149  }
150 }
151 //-----------------------------------------------------------------------------
152 bool Viewport::isPointInside(int x, int y, int framebuffer_height) const
153 {
154  // set x/y relative to the viewport
155  x -= this->x();
156  y -= framebuffer_height - 1 - (this->y() + height() -1);
157 
158  // check that the click is in the viewport
159 
160  if (x<0 || y<0 || x>=this->width() || y>=this->height())
161  return false;
162  else
163  return true;
164 }
165 //-----------------------------------------------------------------------------
166 
int y() const
Definition: Viewport.hpp:67
real mClearDepth
Definition: Viewport.hpp:121
EClearColorMode mClearColorMode
Definition: Viewport.hpp:124
uvec4 mClearColorUInt
Definition: Viewport.hpp:119
Vector4< float > fvec4
A 4 components vector with float precision.
Definition: Vector4.hpp:279
const T_Scalar & r() const
Definition: Vector4.hpp:111
bool isPointInside(int x, int y, int framebuffer_height) const
Returns true if the given point is inside the Viewport.
Definition: Viewport.cpp:152
fvec4 mClearColor
Definition: Viewport.hpp:117
Visualization Library main namespace.
EClearColorMode clearColorMode() const
Definition: Viewport.hpp:102
void activate() const
Definition: Viewport.cpp:72
static void bug(const String &message)
Use this function to provide information about programming errors: wrong parameter initialization...
Definition: Log.cpp:175
const T_Scalar & g() const
Definition: Vector4.hpp:112
int height() const
Definition: Viewport.hpp:71
int width() const
Definition: Viewport.hpp:69
int mClearStencil
Definition: Viewport.hpp:122
EClearFlags mClearFlags
Definition: Viewport.hpp:123
ivec4 mClearColorInt
Definition: Viewport.hpp:118
int x() const
Definition: Viewport.hpp:65
const T_Scalar & b() const
Definition: Vector4.hpp:113
#define VL_CHECK_OGL()
Definition: OpenGL.hpp:156
bool mScissorEnabled
Definition: Viewport.hpp:130
const T_Scalar & a() const
Definition: Vector4.hpp:114