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