KiCad PCB EDA Suite
ogl_utils.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2015-2016 Mario Luzeiro <mrluzeiro@ua.pt>
5  * Copyright (C) 1992-2020 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
30 #include <gal/opengl/kiglew.h> // Must be included first
31 
32 #include "openGL_includes.h"
33 #include "ogl_utils.h"
34 
35 
36 void OglGetScreenshot( wxImage& aDstImage )
37 {
38  struct viewport_params
39  {
40  GLint originX;
41  GLint originY;
42  GLint x;
43  GLint y;
44  } viewport;
45 
46  glGetIntegerv( GL_VIEWPORT, (GLint*) &viewport );
47 
48  unsigned char* pixelbuffer = (unsigned char*) malloc( viewport.x * viewport.y * 3 );
49 
50  // Call glFinish before screenshot to ensure everything is fully drawn.
51  glFinish();
52 
53  glPixelStorei( GL_PACK_ALIGNMENT, 1 );
54  glReadBuffer( GL_BACK_LEFT );
55 
56  glReadPixels( viewport.originX, viewport.originY, viewport.x, viewport.y, GL_RGB,
57  GL_UNSIGNED_BYTE, pixelbuffer );
58 
59  // "Sets the image data without performing checks.
60  // The data given must have the size (width*height*3)
61  // The data must have been allocated with malloc()
62  // If static_data is false, after this call the pointer to the data is owned
63  // by the wxImage object, that will be responsible for deleting it."
64  aDstImage.SetData( pixelbuffer, viewport.x, viewport.y, false );
65 
66  aDstImage = aDstImage.Mirror( false );
67 }
68 
69 
70 GLuint OglLoadTexture( const IMAGE& aImage )
71 {
72  unsigned char* rgbaBuffer = (unsigned char*) malloc( aImage.GetWidth() *
73  aImage.GetHeight() * 4 );
74 
75  unsigned char* dst = rgbaBuffer;
76  const unsigned char* ori = aImage.GetBuffer();
77 
78  for( unsigned int i = 0; i < ( aImage.GetWidth() * aImage.GetHeight() ); ++i )
79  {
80  unsigned char v = *ori;
81 
82  ori++;
83 
84  dst[0] = v;
85  dst[1] = v;
86  dst[2] = v;
87  dst[3] = v;
88  dst+= 4;
89  }
90 
91  GLuint texture;
92  glPixelStorei( GL_UNPACK_ALIGNMENT, 4 );
93  glPixelStorei( GL_PACK_ALIGNMENT, 4 );
94 
95  glGenTextures( 1, &texture );
96  glBindTexture( GL_TEXTURE_2D, texture );
97 
98  glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, aImage.GetWidth(), aImage.GetHeight(), 0, GL_RGBA,
99  GL_UNSIGNED_BYTE, rgbaBuffer );
100 
101  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
102  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
103  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
104  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
105 
106  glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
107 
108  glBindTexture( GL_TEXTURE_2D, 0 );
109 
110  glFlush();
111 
112  free( rgbaBuffer );
113 
114  return texture;
115 }
116 
117 
118 void OglSetMaterial( const SMATERIAL& aMaterial, float aOpacity, bool aUseSelectedMaterial,
119  SFVEC3F aSelectionColor )
120 {
121  const SFVEC4F ambient = SFVEC4F( aMaterial.m_Ambient, 1.0f );
122 
123  // !TODO: at this moment, diffuse color is added via
124  // glEnableClientState( GL_COLOR_ARRAY ) so this line may has no effect
125  // but can be used for optimization
126  const SFVEC4F diffuse = SFVEC4F( aUseSelectedMaterial?aSelectionColor:aMaterial.m_Diffuse,
127  ( 1.0f - aMaterial.m_Transparency ) * aOpacity );
128  const SFVEC4F specular = SFVEC4F( aMaterial.m_Specular, 1.0f );
129  const SFVEC4F emissive = SFVEC4F( aMaterial.m_Emissive, 1.0f );
130 
131  const float shininess =
132  128.0f * ( (aMaterial.m_Shininess > 1.0f) ? 1.0f : aMaterial.m_Shininess );
133 
134  glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, &ambient.r );
135  glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, &diffuse.r );
136  glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, &specular.r );
137  glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, &emissive.r );
138  glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, shininess );
139 }
140 
141 
142 void OglSetDiffuseMaterial( const SFVEC3F &aMaterialDiffuse, float aOpacity )
143 {
144  const SFVEC4F ambient = SFVEC4F( 0.2f, 0.2f, 0.2f, 1.0f );
145  const SFVEC4F diffuse = SFVEC4F( aMaterialDiffuse, aOpacity );
146  const SFVEC4F specular = SFVEC4F( 0.0f, 0.0f, 0.0f, 1.0f );
147  const SFVEC4F emissive = SFVEC4F( 0.0f, 0.0f, 0.0f, 1.0f );
148 
149  glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, &ambient.r );
150  glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, &diffuse.r );
151  glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, &specular.r );
152  glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, &emissive.r );
153  glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, 0.0f );
154 }
155 
156 
157 void OglDrawBackground( const SFVEC3F& aTopColor, const SFVEC3F& aBotColor )
158 {
159  glMatrixMode( GL_PROJECTION );
160  glLoadIdentity();
161 
162  glMatrixMode( GL_MODELVIEW );
163  glLoadIdentity();
164 
165  glDisable( GL_LIGHTING );
166  glDisable( GL_COLOR_MATERIAL );
167  glDisable( GL_DEPTH_TEST );
168  glDisable( GL_TEXTURE_2D );
169  glDisable( GL_BLEND );
170  glDisable( GL_ALPHA_TEST );
171 
172  glBegin( GL_QUADS );
173  glColor4f( aTopColor.x, aTopColor.y, aTopColor.z, 1.0f );
174  glVertex2f( -1.0, 1.0 ); // Top left corner
175 
176  glColor4f( aBotColor.x, aBotColor.y, aBotColor.z, 1.0f );
177  glVertex2f( -1.0,-1.0 ); // bottom left corner
178  glVertex2f( 1.0,-1.0 ); // bottom right corner
179 
180  glColor4f( aTopColor.x, aTopColor.y, aTopColor.z, 1.0f );
181  glVertex2f( 1.0, 1.0 ); // top right corner
182  glEnd();
183 }
184 
185 
187 {
188  if( !glActiveTexture || !glClientActiveTexture )
189  throw std::runtime_error( "The OpenGL context no longer exists: unable to Reset Textures" );
190 
191  glActiveTexture( GL_TEXTURE0 );
192  glBindTexture( GL_TEXTURE_2D, 0 );
193  glClientActiveTexture( GL_TEXTURE0 );
194  glDisable( GL_TEXTURE_2D );
195  glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
196 
197  const SFVEC4F zero = SFVEC4F( 0.0f );
198 
199  glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, static_cast<const float*>( &zero.x ) );
200 }
unsigned char * GetBuffer() const
Get the image buffer pointer.
SFVEC3F m_Ambient
Definition: c3dmodel.h:39
void OglSetMaterial(const SMATERIAL &aMaterial, float aOpacity, bool aUseSelectedMaterial, SFVEC3F aSelectionColor)
Set OpenGL materials.
Definition: ogl_utils.cpp:118
glm::vec4 SFVEC4F
Definition: xv3d_types.h:49
float m_Transparency
1.0 is completely transparent, 0.0 completely opaque
Definition: c3dmodel.h:44
void OglResetTextureState()
Reset to default state the texture settings.
Definition: ogl_utils.cpp:186
void OglSetDiffuseMaterial(const SFVEC3F &aMaterialDiffuse, float aOpacity)
Sets only the diffuse color and keep other parameters with default values.
Definition: ogl_utils.cpp:142
float m_Shininess
Definition: c3dmodel.h:43
void OglDrawBackground(const SFVEC3F &aTopColor, const SFVEC3F &aBotColor)
Definition: ogl_utils.cpp:157
SFVEC3F m_Emissive
Definition: c3dmodel.h:41
glm::vec3 SFVEC3F
Definition: xv3d_types.h:47
unsigned int GetHeight() const
Definition: image.h:204
includes in a proper way the openGL related includes
Define generic OpenGL functions that are common to any OpenGL target.
Manage an 8-bit channel image.
Definition: image.h:89
SFVEC3F m_Diffuse
Default diffuse color if m_Color is NULL.
Definition: c3dmodel.h:40
unsigned int GetWidth() const
Definition: image.h:203
GLuint OglLoadTexture(const IMAGE &aImage)
Generate a new OpenGL texture.
Definition: ogl_utils.cpp:70
void OglGetScreenshot(wxImage &aDstImage)
Get the pixel data of current OpenGL image.
Definition: ogl_utils.cpp:36
SFVEC3F m_Specular
Definition: c3dmodel.h:42