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