KiCad PCB EDA Suite
ogl_utils.cpp File Reference

implements generic openGL functions that are common to any openGL target More...

#include <stdexcept>
#include <gal/opengl/kiglew.h>
#include "openGL_includes.h"
#include "ogl_utils.h"

Go to the source code of this file.

Functions

void OglGetScreenshot (wxImage &aDstImage)
 Get the pixel data of current OpenGL image. More...
 
GLuint OglLoadTexture (const IMAGE &aImage)
 Generate a new OpenGL texture. More...
 
void OglSetMaterial (const SMATERIAL &aMaterial, float aOpacity, bool aUseSelectedMaterial, SFVEC3F aSelectionColor)
 Set OpenGL materials. More...
 
void OglSetDiffuseMaterial (const SFVEC3F &aMaterialDiffuse, float aOpacity, bool aUseSelectedMaterial, SFVEC3F aSelectionColor)
 Sets only the diffuse color and keep other parameters with default values. More...
 
void OglDrawBackground (const SFVEC3F &aTopColor, const SFVEC3F &aBotColor)
 
void OglResetTextureState ()
 Reset to default state the texture settings. More...
 

Detailed Description

implements generic openGL functions that are common to any openGL target

Definition in file ogl_utils.cpp.

Function Documentation

◆ OglDrawBackground()

void OglDrawBackground ( const SFVEC3F aTopColor,
const SFVEC3F aBotColor 
)

Definition at line 160 of file ogl_utils.cpp.

161{
162 glMatrixMode( GL_PROJECTION );
163 glLoadIdentity();
164
165 glMatrixMode( GL_MODELVIEW );
166 glLoadIdentity();
167
168 glDisable( GL_LIGHTING );
169 glDisable( GL_COLOR_MATERIAL );
170 glDisable( GL_DEPTH_TEST );
171 glDisable( GL_TEXTURE_2D );
172 glDisable( GL_BLEND );
173 glDisable( GL_ALPHA_TEST );
174
175 glBegin( GL_QUADS );
176 glColor4f( aTopColor.x, aTopColor.y, aTopColor.z, 1.0f );
177 glVertex2f( -1.0, 1.0 ); // Top left corner
178
179 glColor4f( aBotColor.x, aBotColor.y, aBotColor.z, 1.0f );
180 glVertex2f( -1.0,-1.0 ); // bottom left corner
181 glVertex2f( 1.0,-1.0 ); // bottom right corner
182
183 glColor4f( aTopColor.x, aTopColor.y, aTopColor.z, 1.0f );
184 glVertex2f( 1.0, 1.0 ); // top right corner
185 glEnd();
186}

Referenced by RENDER_3D_OPENGL::Redraw(), and RENDER_3D_RAYTRACE::Redraw().

◆ OglGetScreenshot()

void OglGetScreenshot ( wxImage &  aDstImage)

Get the pixel data of current OpenGL image.

Parameters
aDstImagethe output image. the image must be destroyed to free the data.

Definition at line 37 of file ogl_utils.cpp.

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 * 3 );
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_RGB,
58 GL_UNSIGNED_BYTE, pixelbuffer );
59
60 // "Sets the image data without performing checks.
61 // The data given must have the size (width*height*3)
62 // The data must have been allocated with malloc()
63 // If static_data is false, after this call the pointer to the data is owned
64 // by the wxImage object, that will be responsible for deleting it."
65 aDstImage.SetData( pixelbuffer, viewport.x, viewport.y, false );
66
67 aDstImage = aDstImage.Mirror( false );
68}

Referenced by EDA_3D_CANVAS::GetScreenshot().

◆ OglLoadTexture()

GLuint OglLoadTexture ( const IMAGE aImage)

Generate a new OpenGL texture.

Parameters
aImagea image to generate the texture from.
Returns
the OpenGL texture index created.

Definition at line 71 of file ogl_utils.cpp.

72{
73 unsigned char* rgbaBuffer = (unsigned char*) malloc( aImage.GetWidth() *
74 aImage.GetHeight() * 4 );
75
76 unsigned char* dst = rgbaBuffer;
77 const unsigned char* ori = aImage.GetBuffer();
78
79 for( unsigned int i = 0; i < ( aImage.GetWidth() * aImage.GetHeight() ); ++i )
80 {
81 unsigned char v = *ori;
82
83 ori++;
84
85 dst[0] = 255;
86 dst[1] = 255;
87 dst[2] = 255;
88 dst[3] = v;
89 dst+= 4;
90 }
91
92 GLuint texture;
93 glPixelStorei( GL_UNPACK_ALIGNMENT, 4 );
94 glPixelStorei( GL_PACK_ALIGNMENT, 4 );
95
96 glGenTextures( 1, &texture );
97 glBindTexture( GL_TEXTURE_2D, texture );
98
99 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, aImage.GetWidth(), aImage.GetHeight(), 0, GL_RGBA,
100 GL_UNSIGNED_BYTE, rgbaBuffer );
101
102 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
103 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
104 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
105 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
106
107 glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
108
109 glBindTexture( GL_TEXTURE_2D, 0 );
110
111 glFlush();
112
113 free( rgbaBuffer );
114
115 return texture;
116}
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

References IMAGE::GetBuffer(), IMAGE::GetHeight(), and IMAGE::GetWidth().

Referenced by RENDER_3D_OPENGL::initializeOpenGL().

◆ OglResetTextureState()

void OglResetTextureState ( )

Reset to default state the texture settings.

Definition at line 189 of file ogl_utils.cpp.

190{
191 if( !glActiveTexture || !glClientActiveTexture )
192 throw std::runtime_error( "The OpenGL context no longer exists: unable to Reset Textures" );
193
194 glActiveTexture( GL_TEXTURE0 );
195 glBindTexture( GL_TEXTURE_2D, 0 );
196 glClientActiveTexture( GL_TEXTURE0 );
197 glDisable( GL_TEXTURE_2D );
198 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
199
200 const SFVEC4F zero = SFVEC4F( 0.0f );
201
202 glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, static_cast<const float*>( &zero.x ) );
203}
glm::vec4 SFVEC4F
Definition: xv3d_types.h:46

Referenced by RENDER_3D_OPENGL::Redraw().

◆ OglSetDiffuseMaterial()

void OglSetDiffuseMaterial ( const SFVEC3F aMaterialDiffuse,
float  aOpacity,
bool  aUseSelectedMaterial = false,
SFVEC3F  aSelectionColor = SFVEC3F(0.0f) 
)

Sets only the diffuse color and keep other parameters with default values.

Parameters
aMaterialDiffusethe diffuse color to assign to material properties.

Definition at line 143 of file ogl_utils.cpp.

145{
146 const SFVEC4F ambient = SFVEC4F( 0.2f, 0.2f, 0.2f, 1.0f );
147 const SFVEC4F diffuse = SFVEC4F( aUseSelectedMaterial ? aSelectionColor : aMaterialDiffuse,
148 aOpacity );
149 const SFVEC4F specular = SFVEC4F( 0.0f, 0.0f, 0.0f, 1.0f );
150 const SFVEC4F emissive = SFVEC4F( 0.0f, 0.0f, 0.0f, 1.0f );
151
152 glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, &ambient.r );
153 glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, &diffuse.r );
154 glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, &specular.r );
155 glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, &emissive.r );
156 glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, 0.0f );
157}

Referenced by MODEL_3D::Draw().

◆ OglSetMaterial()

void OglSetMaterial ( const SMATERIAL aMaterial,
float  aOpacity,
bool  aUseSelectedMaterial = false,
SFVEC3F  aSelectionColor = SFVEC3F(0.0f) 
)

Set OpenGL materials.

Parameters
aMateriala material structure with parameters to set.
aOpacitymain model opacity 0.0 full transparent, 1.0 full opaque.
aUseSelectedMaterialif material should be set for selection mode.
aSelectionColormaterial to use for selection in case it is enabled.

Definition at line 119 of file ogl_utils.cpp.

121{
122 const SFVEC4F ambient = SFVEC4F( aMaterial.m_Ambient, 1.0f );
123
124 // !TODO: at this moment, diffuse color is added via
125 // glEnableClientState( GL_COLOR_ARRAY ) so this line may has no effect
126 // but can be used for optimization
127 const SFVEC4F diffuse = SFVEC4F( aUseSelectedMaterial ? aSelectionColor : aMaterial.m_Diffuse,
128 ( 1.0f - aMaterial.m_Transparency ) * aOpacity );
129 const SFVEC4F specular = SFVEC4F( aMaterial.m_Specular, 1.0f );
130 const SFVEC4F emissive = SFVEC4F( aMaterial.m_Emissive, 1.0f );
131
132 const float shininess =
133 128.0f * ( (aMaterial.m_Shininess > 1.0f) ? 1.0f : aMaterial.m_Shininess );
134
135 glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, &ambient.r );
136 glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, &diffuse.r );
137 glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, &specular.r );
138 glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, &emissive.r );
139 glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, shininess );
140}
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

References SMATERIAL::m_Ambient, SMATERIAL::m_Diffuse, SMATERIAL::m_Emissive, SMATERIAL::m_Shininess, SMATERIAL::m_Specular, and SMATERIAL::m_Transparency.

Referenced by MODEL_3D::Draw(), RENDER_3D_OPENGL::Redraw(), RENDER_3D_OPENGL::renderBoardBody(), RENDER_3D_OPENGL::setCopperMaterial(), and RENDER_3D_OPENGL::setLayerMaterial().