KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 <[email protected]>
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 <stdexcept>
31#include <gal/opengl/kiglew.h> // Must be included first
32
33#include "openGL_includes.h"
34#include "ogl_utils.h"
35
36
37void OglGetScreenshot( wxImage& aDstImage )
38{
39 struct viewport_params
40 {
41 GLint originX;
42 GLint originY;
43 GLint x;
44 GLint y;
45 } viewport;
46
47 glGetIntegerv( GL_VIEWPORT, (GLint*) &viewport );
48
49 unsigned char* pixelbuffer = (unsigned char*) malloc( viewport.x * viewport.y * 4 );
50
51 // Call glFinish before screenshot to ensure everything is fully drawn.
52 glFinish();
53
54 glPixelStorei( GL_PACK_ALIGNMENT, 1 );
55 glReadBuffer( GL_BACK_LEFT );
56
57 glReadPixels( viewport.originX, viewport.originY, viewport.x, viewport.y, GL_RGBA,
58 GL_UNSIGNED_BYTE, pixelbuffer );
59
60 unsigned char* rgbBuffer = (unsigned char*) malloc( viewport.x * viewport.y * 3 );
61 unsigned char* alphaBuffer = (unsigned char*) malloc( viewport.x * viewport.y );
62
63 unsigned char* rgbaPtr = pixelbuffer;
64 unsigned char* rgbPtr = rgbBuffer;
65 unsigned char* alphaPtr = alphaBuffer;
66
67 for( int y = 0; y < viewport.y; y++ )
68 {
69 for( int x = 0; x < viewport.x; x++ )
70 {
71 rgbPtr[0] = rgbaPtr[0];
72 rgbPtr[1] = rgbaPtr[1];
73 rgbPtr[2] = rgbaPtr[2];
74 alphaPtr[0] = rgbaPtr[3];
75
76 rgbaPtr += 4;
77 rgbPtr += 3;
78 alphaPtr += 1;
79 }
80 }
81
82 // "Sets the image data without performing checks.
83 // The data given must have the size (width*height*3)
84 // The data must have been allocated with malloc()
85 // If static_data is false, after this call the pointer to the data is owned
86 // by the wxImage object, that will be responsible for deleting it."
87 aDstImage.SetData( rgbBuffer, viewport.x, viewport.y, false );
88 aDstImage.SetAlpha( alphaBuffer, false );
89
90 free( pixelbuffer );
91
92 aDstImage = aDstImage.Mirror( false );
93}
94
95
96GLuint OglLoadTexture( const IMAGE& aImage )
97{
98 unsigned char* rgbaBuffer = (unsigned char*) malloc( aImage.GetWidth() *
99 aImage.GetHeight() * 4 );
100
101 unsigned char* dst = rgbaBuffer;
102 const unsigned char* ori = aImage.GetBuffer();
103
104 for( unsigned int i = 0; i < ( aImage.GetWidth() * aImage.GetHeight() ); ++i )
105 {
106 unsigned char v = *ori;
107
108 ori++;
109
110 dst[0] = 255;
111 dst[1] = 255;
112 dst[2] = 255;
113 dst[3] = v;
114 dst+= 4;
115 }
116
117 GLuint texture;
118 glPixelStorei( GL_UNPACK_ALIGNMENT, 4 );
119 glPixelStorei( GL_PACK_ALIGNMENT, 4 );
120
121 glGenTextures( 1, &texture );
122 glBindTexture( GL_TEXTURE_2D, texture );
123
124 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, aImage.GetWidth(), aImage.GetHeight(), 0, GL_RGBA,
125 GL_UNSIGNED_BYTE, rgbaBuffer );
126
127 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
128 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
129 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
130 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
131
132 glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
133
134 glBindTexture( GL_TEXTURE_2D, 0 );
135
136 glFlush();
137
138 free( rgbaBuffer );
139
140 return texture;
141}
142
143
144void OglSetMaterial( const SMATERIAL& aMaterial, float aOpacity, bool aUseSelectedMaterial,
145 SFVEC3F aSelectionColor )
146{
147 const SFVEC4F ambient = SFVEC4F( aMaterial.m_Ambient, 1.0f );
148
149 // !TODO: at this moment, diffuse color is added via
150 // glEnableClientState( GL_COLOR_ARRAY ) so this line may has no effect
151 // but can be used for optimization
152 const SFVEC4F diffuse = SFVEC4F( aUseSelectedMaterial ? aSelectionColor : aMaterial.m_Diffuse,
153 ( 1.0f - aMaterial.m_Transparency ) * aOpacity );
154 const SFVEC4F specular = SFVEC4F( aMaterial.m_Specular, 1.0f );
155 const SFVEC4F emissive = SFVEC4F( aMaterial.m_Emissive, 1.0f );
156
157 const float shininess =
158 128.0f * ( (aMaterial.m_Shininess > 1.0f) ? 1.0f : aMaterial.m_Shininess );
159
160 glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, &ambient.r );
161 glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, &diffuse.r );
162 glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, &specular.r );
163 glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, &emissive.r );
164 glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, shininess );
165}
166
167
168void OglSetDiffuseMaterial( const SFVEC3F &aMaterialDiffuse, float aOpacity,
169 bool aUseSelectedMaterial, SFVEC3F aSelectionColor )
170{
171 const SFVEC4F ambient = SFVEC4F( 0.2f, 0.2f, 0.2f, 1.0f );
172 const SFVEC4F diffuse = SFVEC4F( aUseSelectedMaterial ? aSelectionColor : aMaterialDiffuse,
173 aOpacity );
174 const SFVEC4F specular = SFVEC4F( 0.0f, 0.0f, 0.0f, 1.0f );
175 const SFVEC4F emissive = SFVEC4F( 0.0f, 0.0f, 0.0f, 1.0f );
176
177 glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, &ambient.r );
178 glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, &diffuse.r );
179 glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, &specular.r );
180 glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, &emissive.r );
181 glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, 0.0f );
182}
183
184
185void OglDrawBackground( const SFVEC4F& aTopColor, const SFVEC4F& aBotColor )
186{
187 glMatrixMode( GL_PROJECTION );
188 glLoadIdentity();
189
190 glMatrixMode( GL_MODELVIEW );
191 glLoadIdentity();
192
193 glDisable( GL_LIGHTING );
194 glDisable( GL_COLOR_MATERIAL );
195 glDisable( GL_DEPTH_TEST );
196 glDisable( GL_TEXTURE_2D );
197 glDisable( GL_BLEND );
198 glDisable( GL_ALPHA_TEST );
199
200 glBegin( GL_QUADS );
201 glColor4f( aTopColor.r, aTopColor.g, aTopColor.b, aTopColor.a );
202 glVertex2f( -1.0, 1.0 ); // Top left corner
203
204 glColor4f( aBotColor.r, aBotColor.g, aBotColor.b, aBotColor.a );
205 glVertex2f( -1.0,-1.0 ); // bottom left corner
206 glVertex2f( 1.0,-1.0 ); // bottom right corner
207
208 glColor4f( aTopColor.r, aTopColor.g, aTopColor.b, aTopColor.a);
209 glVertex2f( 1.0, 1.0 ); // top right corner
210 glEnd();
211}
212
213
215{
216 if( !glActiveTexture || !glClientActiveTexture )
217 throw std::runtime_error( "The OpenGL context no longer exists: unable to Reset Textures" );
218
219 glActiveTexture( GL_TEXTURE0 );
220 glBindTexture( GL_TEXTURE_2D, 0 );
221 glClientActiveTexture( GL_TEXTURE0 );
222 glDisable( GL_TEXTURE_2D );
223 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
224
225 const SFVEC4F zero = SFVEC4F( 0.0f );
226
227 glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, static_cast<const float*>( &zero.x ) );
228}
Manage an 8-bit channel image.
Definition: image.h:90
unsigned char * GetBuffer() const
Get the image buffer pointer.
Definition: image.cpp:73
unsigned int GetHeight() const
Definition: image.h:214
unsigned int GetWidth() const
Definition: image.h:213
void OglGetScreenshot(wxImage &aDstImage)
Get the pixel data of current OpenGL image.
Definition: ogl_utils.cpp:37
void OglResetTextureState()
Reset to default state the texture settings.
Definition: ogl_utils.cpp:214
void OglSetDiffuseMaterial(const SFVEC3F &aMaterialDiffuse, float aOpacity, bool aUseSelectedMaterial, SFVEC3F aSelectionColor)
Sets only the diffuse color and keep other parameters with default values.
Definition: ogl_utils.cpp:168
void OglSetMaterial(const SMATERIAL &aMaterial, float aOpacity, bool aUseSelectedMaterial, SFVEC3F aSelectionColor)
Set OpenGL materials.
Definition: ogl_utils.cpp:144
GLuint OglLoadTexture(const IMAGE &aImage)
Generate a new OpenGL texture.
Definition: ogl_utils.cpp:96
void OglDrawBackground(const SFVEC4F &aTopColor, const SFVEC4F &aBotColor)
Definition: ogl_utils.cpp:185
Define generic OpenGL functions that are common to any OpenGL target.
includes in a proper way the openGL related includes
float m_Shininess
Definition: c3dmodel.h:43
SFVEC3F m_Specular
Definition: c3dmodel.h:42
SFVEC3F m_Ambient
Definition: c3dmodel.h:39
float m_Transparency
1.0 is completely transparent, 0.0 completely opaque
Definition: c3dmodel.h:44
SFVEC3F m_Emissive
Definition: c3dmodel.h:41
SFVEC3F m_Diffuse
Default diffuse color if m_Color is NULL.
Definition: c3dmodel.h:40
glm::vec3 SFVEC3F
Definition: xv3d_types.h:44
glm::vec4 SFVEC4F
Definition: xv3d_types.h:46