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  glPixelStorei( GL_PACK_ALIGNMENT, 1 );
54  glReadBuffer( GL_BACK_LEFT );
55 
56  glReadPixels( viewport.originX, viewport.originY,
57  viewport.x, viewport.y,
58  GL_RGB, GL_UNSIGNED_BYTE, pixelbuffer );
59 
60  // glReadPixels( viewport.originX, viewport.originY,
61  // viewport.x, viewport.y,
62  // GL_ALPHA, GL_UNSIGNED_BYTE, alphabuffer );
63 
64  // "Sets the image data without performing checks.
65  // The data given must have the size (width*height*3)
66  // The data must have been allocated with malloc()
67  // If static_data is false, after this call the pointer to the data is owned
68  // by the wxImage object, that will be responsible for deleting it."
69  aDstImage.SetData( pixelbuffer, viewport.x, viewport.y, false );
70 
71  //aDstImage.SetAlpha( alphabuffer, false );
72 
73  aDstImage = aDstImage.Mirror( false );
74 }
75 
76 
77 GLuint OGL_LoadTexture( const CIMAGE &aImage )
78 {
79  unsigned char* rgbaBuffer = (unsigned char*) malloc( aImage.GetWidth() *
80  aImage.GetHeight() * 4 );
81 
82  unsigned char* dst = rgbaBuffer;
83  const unsigned char* ori = aImage.GetBuffer();
84 
85  for( unsigned int i = 0; i < (aImage.GetWidth() * aImage.GetHeight()); ++i )
86  {
87  unsigned char v = *ori;
88 
89  ori++;
90 
91  dst[0] = v;
92  dst[1] = v;
93  dst[2] = v;
94  dst[3] = v;
95  dst+= 4;
96  }
97 
98  GLuint texture;
99  glPixelStorei( GL_UNPACK_ALIGNMENT, 4 );
100  glPixelStorei( GL_PACK_ALIGNMENT, 4 );
101 
102  glGenTextures( 1, &texture );
103  glBindTexture( GL_TEXTURE_2D, texture );
104 
105  /*gluBuild2DMipmaps( GL_TEXTURE_2D,
106  GL_RGBA,
107  aImage.GetWidth(),
108  aImage.GetHeight(),
109  GL_RGBA,
110  GL_UNSIGNED_BYTE,
111  rgbaBuffer );*/
112 
113  glTexImage2D( GL_TEXTURE_2D,
114  0,
115  GL_RGBA,
116  aImage.GetWidth(),
117  aImage.GetHeight(),
118  0,
119  GL_RGBA,
120  GL_UNSIGNED_BYTE,
121  rgbaBuffer );
122 
123  //glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
124  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
125  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
126  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
127  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
128 
129  glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
130 
131  glBindTexture( GL_TEXTURE_2D, 0 );
132 
133  glFinish();
134 
135  free( rgbaBuffer );
136 
137  return texture;
138 }
139 
140 
141 void OGL_SetMaterial( const SMATERIAL & aMaterial )
142 {
143  const SFVEC4F ambient = SFVEC4F( aMaterial.m_Ambient, 1.0f );
144  const SFVEC4F diffuse = SFVEC4F( aMaterial.m_Diffuse, 1.0f -
145  aMaterial.m_Transparency );
146  const SFVEC4F specular = SFVEC4F( aMaterial.m_Specular, 1.0f );
147  const SFVEC4F emissive = SFVEC4F( aMaterial.m_Emissive, 1.0f );
148 
149  const float shininess = 128.0f * ( (aMaterial.m_Shininess > 1.0f)?
150  1.0f:
151  aMaterial.m_Shininess );
152 
153  glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, &ambient.r );
154  glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, &diffuse.r );
155  glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, &specular.r );
156  glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, &emissive.r );
157  glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, shininess );
158 }
159 
160 
161 void OGL_SetDiffuseOnlyMaterial( const SFVEC3F &aMaterialDiffuse )
162 {
163  const SFVEC4F ambient = SFVEC4F( 0.2f, 0.2f, 0.2f, 1.0f );
164  const SFVEC4F diffuse = SFVEC4F( aMaterialDiffuse, 1.0f );
165  const SFVEC4F specular = SFVEC4F( 0.0f, 0.0f, 0.0f, 1.0f );
166  const SFVEC4F emissive = SFVEC4F( 0.0f, 0.0f, 0.0f, 1.0f );
167 
168  glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, &ambient.r );
169  glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, &diffuse.r );
170  glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, &specular.r );
171  glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, &emissive.r );
172  glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, 0.0f );
173 }
174 
175 
176 void OGL_DrawBackground( const SFVEC3F &aTopColor, const SFVEC3F &aBotColor )
177 {
178  glMatrixMode( GL_PROJECTION );
179  glLoadIdentity();
180 
181  glMatrixMode( GL_MODELVIEW );
182  glLoadIdentity();
183 
184  glDisable( GL_LIGHTING );
185  glDisable( GL_COLOR_MATERIAL );
186  glDisable( GL_DEPTH_TEST );
187  glDisable( GL_TEXTURE_2D );
188  glDisable( GL_BLEND );
189  glDisable( GL_ALPHA_TEST );
190 
191  glBegin( GL_QUADS );
192  glColor4f( aTopColor.x, aTopColor.y, aTopColor.z, 1.0f );
193  glVertex2f( -1.0, 1.0 ); // Top left corner
194 
195  glColor4f( aBotColor.x, aBotColor.y, aBotColor.z, 1.0f );
196  glVertex2f( -1.0,-1.0 ); // bottom left corner
197  glVertex2f( 1.0,-1.0 ); // bottom right corner
198 
199  glColor4f( aTopColor.x, aTopColor.y, aTopColor.z, 1.0f );
200  glVertex2f( 1.0, 1.0 ); // top right corner
201  glEnd();
202 }
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:67
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:77
unsigned int GetHeight() const
Definition: cimage.h:207
void OGL_DrawBackground(const SFVEC3F &aTopColor, const SFVEC3F &aBotColor)
OGL_DrawBackground.
Definition: ogl_utils.cpp:176
void OGL_SetMaterial(const SMATERIAL &aMaterial)
OGL_SetMaterial - Set OpenGL materials.
Definition: ogl_utils.cpp:141
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
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:161
SFVEC3F m_Specular
Definition: c3dmodel.h:42