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-2016 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 
31 #include "openGL_includes.h"
32 #include "ogl_utils.h"
33 
34 
35 void OGL_GetScreenshot( wxImage &aDstImage )
36 {
37  struct viewport_params
38  {
39  GLint originX;
40  GLint originY;
41  GLint x;
42  GLint y;
43  } viewport;
44 
45  glGetIntegerv( GL_VIEWPORT, (GLint*) &viewport );
46 
47  unsigned char* pixelbuffer = (unsigned char*) malloc( viewport.x *
48  viewport.y * 3 );
49 
50  // Alphabuffer was causing some transparency problems on some systems (Windows)
51  // unsigned char* alphabuffer = (unsigned char*) malloc( viewport.x * viewport.y );
52 
53  // Call glFinish before screenshot to ensure everything is fully drawn.
54  glFinish();
55 
56  glPixelStorei( GL_PACK_ALIGNMENT, 1 );
57  glReadBuffer( GL_BACK_LEFT );
58 
59  glReadPixels( viewport.originX, viewport.originY,
60  viewport.x, viewport.y,
61  GL_RGB, GL_UNSIGNED_BYTE, pixelbuffer );
62 
63  // glReadPixels( viewport.originX, viewport.originY,
64  // viewport.x, viewport.y,
65  // GL_ALPHA, GL_UNSIGNED_BYTE, alphabuffer );
66 
67  // "Sets the image data without performing checks.
68  // The data given must have the size (width*height*3)
69  // The data must have been allocated with malloc()
70  // If static_data is false, after this call the pointer to the data is owned
71  // by the wxImage object, that will be responsible for deleting it."
72  aDstImage.SetData( pixelbuffer, viewport.x, viewport.y, false );
73 
74  //aDstImage.SetAlpha( alphabuffer, false );
75 
76  aDstImage = aDstImage.Mirror( false );
77 }
78 
79 
80 GLuint OGL_LoadTexture( const CIMAGE &aImage )
81 {
82  unsigned char* rgbaBuffer = (unsigned char*) malloc( aImage.GetWidth() *
83  aImage.GetHeight() * 4 );
84 
85  unsigned char* dst = rgbaBuffer;
86  const unsigned char* ori = aImage.GetBuffer();
87 
88  for( unsigned int i = 0; i < (aImage.GetWidth() * aImage.GetHeight()); ++i )
89  {
90  unsigned char v = *ori;
91 
92  ori++;
93 
94  dst[0] = v;
95  dst[1] = v;
96  dst[2] = v;
97  dst[3] = v;
98  dst+= 4;
99  }
100 
101  GLuint texture;
102  glPixelStorei( GL_UNPACK_ALIGNMENT, 4 );
103  glPixelStorei( GL_PACK_ALIGNMENT, 4 );
104 
105  glGenTextures( 1, &texture );
106  glBindTexture( GL_TEXTURE_2D, texture );
107 
108  /*gluBuild2DMipmaps( GL_TEXTURE_2D,
109  GL_RGBA,
110  aImage.GetWidth(),
111  aImage.GetHeight(),
112  GL_RGBA,
113  GL_UNSIGNED_BYTE,
114  rgbaBuffer );*/
115 
116  glTexImage2D( GL_TEXTURE_2D,
117  0,
118  GL_RGBA,
119  aImage.GetWidth(),
120  aImage.GetHeight(),
121  0,
122  GL_RGBA,
123  GL_UNSIGNED_BYTE,
124  rgbaBuffer );
125 
126  //glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
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 
144 void OGL_SetMaterial( const SMATERIAL & aMaterial )
145 {
146  const SFVEC4F ambient = SFVEC4F( aMaterial.m_Ambient, 1.0f );
147  const SFVEC4F diffuse = SFVEC4F( aMaterial.m_Diffuse, 1.0f -
148  aMaterial.m_Transparency );
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 = 128.0f * ( (aMaterial.m_Shininess > 1.0f)?
153  1.0f:
154  aMaterial.m_Shininess );
155 
156  glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, &ambient.r );
157  glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, &diffuse.r );
158  glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, &specular.r );
159  glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, &emissive.r );
160  glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, shininess );
161 }
162 
163 
164 void OGL_SetDiffuseOnlyMaterial( const SFVEC3F &aMaterialDiffuse )
165 {
166  const SFVEC4F ambient = SFVEC4F( 0.2f, 0.2f, 0.2f, 1.0f );
167  const SFVEC4F diffuse = SFVEC4F( aMaterialDiffuse, 1.0f );
168  const SFVEC4F specular = SFVEC4F( 0.0f, 0.0f, 0.0f, 1.0f );
169  const SFVEC4F emissive = SFVEC4F( 0.0f, 0.0f, 0.0f, 1.0f );
170 
171  glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, &ambient.r );
172  glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, &diffuse.r );
173  glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, &specular.r );
174  glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, &emissive.r );
175  glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, 0.0f );
176 }
177 
178 
179 void OGL_DrawBackground( const SFVEC3F &aTopColor, const SFVEC3F &aBotColor )
180 {
181  glMatrixMode( GL_PROJECTION );
182  glLoadIdentity();
183 
184  glMatrixMode( GL_MODELVIEW );
185  glLoadIdentity();
186 
187  glDisable( GL_LIGHTING );
188  glDisable( GL_COLOR_MATERIAL );
189  glDisable( GL_DEPTH_TEST );
190  glDisable( GL_TEXTURE_2D );
191  glDisable( GL_BLEND );
192  glDisable( GL_ALPHA_TEST );
193 
194  glBegin( GL_QUADS );
195  glColor4f( aTopColor.x, aTopColor.y, aTopColor.z, 1.0f );
196  glVertex2f( -1.0, 1.0 ); // Top left corner
197 
198  glColor4f( aBotColor.x, aBotColor.y, aBotColor.z, 1.0f );
199  glVertex2f( -1.0,-1.0 ); // bottom left corner
200  glVertex2f( 1.0,-1.0 ); // bottom right corner
201 
202  glColor4f( aTopColor.x, aTopColor.y, aTopColor.z, 1.0f );
203  glVertex2f( 1.0, 1.0 ); // top right corner
204  glEnd();
205 }
void OGL_GetScreenshot(wxImage &aDstImage)
OGL_GetScreenshot - got the pixel data of current OpenGL image.
Definition: ogl_utils.cpp:35
SFVEC3F m_Ambient
Definition: c3dmodel.h:39
unsigned char * GetBuffer() const
Function GetBuffer get the image buffer pointer.
Definition: cimage.cpp:71
glm::vec4 SFVEC4F
Definition: xv3d_types.h:49
float m_Transparency
1.0 is completely transparent, 0.0 completely opaque
Definition: c3dmodel.h:44
GLuint OGL_LoadTexture(const CIMAGE &aImage)
OGL_LoadTexture - generate a new OpenGL texture.
Definition: ogl_utils.cpp:80
unsigned int GetHeight() const
Definition: cimage.h:207
void OGL_DrawBackground(const SFVEC3F &aTopColor, const SFVEC3F &aBotColor)
OGL_DrawBackground.
Definition: ogl_utils.cpp:179
void OGL_SetMaterial(const SMATERIAL &aMaterial)
OGL_SetMaterial - Set OpenGL materials.
Definition: ogl_utils.cpp:144
unsigned int GetWidth() const
Definition: cimage.h:206
float m_Shininess
Definition: c3dmodel.h:43
SFVEC3F m_Emissive
Definition: c3dmodel.h:41
glm::vec3 SFVEC3F
Definition: xv3d_types.h:47
size_t i
Definition: json11.cpp:597
includes in a proper way the openGL related includes
implements generic openGL functions that are common to any openGL target
SFVEC3F m_Diffuse
Default diffuse color if m_Color is NULL.
Definition: c3dmodel.h:40
Class CIMAGE manages a 8-bit channel image.
Definition: cimage.h:86
void OGL_SetDiffuseOnlyMaterial(const SFVEC3F &aMaterialDiffuse)
OGL_SetDiffuseOnlyMaterial - sets only the diffuse color and keep other parameters with default value...
Definition: ogl_utils.cpp:164
SFVEC3F m_Specular
Definition: c3dmodel.h:42