KiCad PCB EDA Suite
ogl_utils.cpp File Reference

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

#include <GL/glew.h>
#include "openGL_includes.h"
#include "ogl_utils.h"

Go to the source code of this file.

Functions

void OGL_GetScreenshot (wxImage &aDstImage)
 OGL_GetScreenshot - got the pixel data of current OpenGL image. More...
 
GLuint OGL_LoadTexture (const CIMAGE &aImage)
 OGL_LoadTexture - generate a new OpenGL texture. More...
 
void OGL_SetMaterial (const SMATERIAL &aMaterial, float aOpacity)
 OGL_SetMaterial - Set OpenGL materials. More...
 
void OGL_SetDiffuseOnlyMaterial (const SFVEC3F &aMaterialDiffuse, float aOpacity)
 OGL_SetDiffuseOnlyMaterial - sets only the diffuse color and keep other parameters with default values. More...
 
void OGL_DrawBackground (const SFVEC3F &aTopColor, const SFVEC3F &aBotColor)
 OGL_DrawBackground. More...
 
void OGL_ResetTextureStateDefaults ()
 OGL_ResetTextureStateDefaults - resets 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

◆ OGL_DrawBackground()

void OGL_DrawBackground ( const SFVEC3F aTopColor,
const SFVEC3F aBotColor 
)

OGL_DrawBackground.

Parameters
aTopColor
aBotColor

Definition at line 184 of file ogl_utils.cpp.

185 {
186  glMatrixMode( GL_PROJECTION );
187  glLoadIdentity();
188 
189  glMatrixMode( GL_MODELVIEW );
190  glLoadIdentity();
191 
192  glDisable( GL_LIGHTING );
193  glDisable( GL_COLOR_MATERIAL );
194  glDisable( GL_DEPTH_TEST );
195  glDisable( GL_TEXTURE_2D );
196  glDisable( GL_BLEND );
197  glDisable( GL_ALPHA_TEST );
198 
199  glBegin( GL_QUADS );
200  glColor4f( aTopColor.x, aTopColor.y, aTopColor.z, 1.0f );
201  glVertex2f( -1.0, 1.0 ); // Top left corner
202 
203  glColor4f( aBotColor.x, aBotColor.y, aBotColor.z, 1.0f );
204  glVertex2f( -1.0,-1.0 ); // bottom left corner
205  glVertex2f( 1.0,-1.0 ); // bottom right corner
206 
207  glColor4f( aTopColor.x, aTopColor.y, aTopColor.z, 1.0f );
208  glVertex2f( 1.0, 1.0 ); // top right corner
209  glEnd();
210 }

Referenced by C3D_RENDER_OGL_LEGACY::Redraw(), and C3D_RENDER_RAYTRACING::Redraw().

◆ OGL_GetScreenshot()

void OGL_GetScreenshot ( wxImage &  aDstImage)

OGL_GetScreenshot - got the pixel data of current OpenGL image.

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

Definition at line 36 of file ogl_utils.cpp.

37 {
38  struct viewport_params
39  {
40  GLint originX;
41  GLint originY;
42  GLint x;
43  GLint y;
44  } viewport;
45 
46  glGetIntegerv( GL_VIEWPORT, (GLint*) &viewport );
47 
48  unsigned char* pixelbuffer = (unsigned char*) malloc( viewport.x *
49  viewport.y * 3 );
50 
51  // Alphabuffer was causing some transparency problems on some systems (Windows)
52  // unsigned char* alphabuffer = (unsigned char*) malloc( viewport.x * viewport.y );
53 
54  // Call glFinish before screenshot to ensure everything is fully drawn.
55  glFinish();
56 
57  glPixelStorei( GL_PACK_ALIGNMENT, 1 );
58  glReadBuffer( GL_BACK_LEFT );
59 
60  glReadPixels( viewport.originX, viewport.originY,
61  viewport.x, viewport.y,
62  GL_RGB, GL_UNSIGNED_BYTE, pixelbuffer );
63 
64  // glReadPixels( viewport.originX, viewport.originY,
65  // viewport.x, viewport.y,
66  // GL_ALPHA, GL_UNSIGNED_BYTE, alphabuffer );
67 
68  // "Sets the image data without performing checks.
69  // The data given must have the size (width*height*3)
70  // The data must have been allocated with malloc()
71  // If static_data is false, after this call the pointer to the data is owned
72  // by the wxImage object, that will be responsible for deleting it."
73  aDstImage.SetData( pixelbuffer, viewport.x, viewport.y, false );
74 
75  //aDstImage.SetAlpha( alphabuffer, false );
76 
77  aDstImage = aDstImage.Mirror( false );
78 }

Referenced by EDA_3D_CANVAS::GetScreenshot().

◆ OGL_LoadTexture()

GLuint OGL_LoadTexture ( const CIMAGE aImage)

OGL_LoadTexture - generate a new OpenGL texture.

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

Definition at line 81 of file ogl_utils.cpp.

82 {
83  unsigned char* rgbaBuffer = (unsigned char*) malloc( aImage.GetWidth() *
84  aImage.GetHeight() * 4 );
85 
86  unsigned char* dst = rgbaBuffer;
87  const unsigned char* ori = aImage.GetBuffer();
88 
89  for( unsigned int i = 0; i < (aImage.GetWidth() * aImage.GetHeight()); ++i )
90  {
91  unsigned char v = *ori;
92 
93  ori++;
94 
95  dst[0] = v;
96  dst[1] = v;
97  dst[2] = v;
98  dst[3] = v;
99  dst+= 4;
100  }
101 
102  GLuint texture;
103  glPixelStorei( GL_UNPACK_ALIGNMENT, 4 );
104  glPixelStorei( GL_PACK_ALIGNMENT, 4 );
105 
106  glGenTextures( 1, &texture );
107  glBindTexture( GL_TEXTURE_2D, texture );
108 
109  /*gluBuild2DMipmaps( GL_TEXTURE_2D,
110  GL_RGBA,
111  aImage.GetWidth(),
112  aImage.GetHeight(),
113  GL_RGBA,
114  GL_UNSIGNED_BYTE,
115  rgbaBuffer );*/
116 
117  glTexImage2D( GL_TEXTURE_2D,
118  0,
119  GL_RGBA,
120  aImage.GetWidth(),
121  aImage.GetHeight(),
122  0,
123  GL_RGBA,
124  GL_UNSIGNED_BYTE,
125  rgbaBuffer );
126 
127  //glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
128  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
129  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
130  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
131  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
132 
133  glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
134 
135  glBindTexture( GL_TEXTURE_2D, 0 );
136 
137  glFlush();
138 
139  free( rgbaBuffer );
140 
141  return texture;
142 }
unsigned int GetWidth() const
Definition: cimage.h:209
unsigned int GetHeight() const
Definition: cimage.h:210
unsigned char * GetBuffer() const
Function GetBuffer get the image buffer pointer.
Definition: cimage.cpp:72

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

Referenced by C3D_RENDER_OGL_LEGACY::initializeOpenGL().

◆ OGL_ResetTextureStateDefaults()

void OGL_ResetTextureStateDefaults ( )

OGL_ResetTextureStateDefaults - resets to default state the texture settings.

Definition at line 213 of file ogl_utils.cpp.

214 {
215  glActiveTexture( GL_TEXTURE0 );
216  glBindTexture( GL_TEXTURE_2D, 0 );
217  glClientActiveTexture( GL_TEXTURE0 );
218  glDisable( GL_TEXTURE_2D );
219  glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
220 
221  const SFVEC4F zero = SFVEC4F( 0.0f );
222 
223  glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, static_cast<const float*>( &zero.x ) );
224 }
glm::vec4 SFVEC4F
Definition: xv3d_types.h:49

Referenced by C3D_RENDER_OGL_LEGACY::Redraw().

◆ OGL_SetDiffuseOnlyMaterial()

void OGL_SetDiffuseOnlyMaterial ( const SFVEC3F aMaterialDiffuse,
float  aOpacity 
)

OGL_SetDiffuseOnlyMaterial - sets only the diffuse color and keep other parameters with default values.

Parameters
aMaterialDiffusethe diffese color to assign to material properties

Definition at line 169 of file ogl_utils.cpp.

170 {
171  const SFVEC4F ambient = SFVEC4F( 0.2f, 0.2f, 0.2f, 1.0f );
172  const SFVEC4F diffuse = SFVEC4F( aMaterialDiffuse, aOpacity );
173  const SFVEC4F specular = SFVEC4F( 0.0f, 0.0f, 0.0f, 1.0f );
174  const SFVEC4F emissive = SFVEC4F( 0.0f, 0.0f, 0.0f, 1.0f );
175 
176  glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, &ambient.r );
177  glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, &diffuse.r );
178  glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, &specular.r );
179  glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, &emissive.r );
180  glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, 0.0f );
181 }
glm::vec4 SFVEC4F
Definition: xv3d_types.h:49

Referenced by C_OGL_3DMODEL::Draw().

◆ OGL_SetMaterial()

void OGL_SetMaterial ( const SMATERIAL aMaterial,
float  aOpacity 
)

OGL_SetMaterial - Set OpenGL materials.

Parameters
aMateriala material structure with parameters to set
aOpacitymain model opacity 0.0 full transparente, 1.0 full opaque

Definition at line 145 of file ogl_utils.cpp.

146 {
147  const SFVEC4F ambient = SFVEC4F( aMaterial.m_Ambient, 1.0f );
148 
149  // !TODO: at this moment, diffuse color is added via
150  // glEnableClientState( GL_COLOR_ARRAY ) so this line may has no effect
151  // but can be used for optimization
152  const SFVEC4F diffuse = SFVEC4F( aMaterial.m_Diffuse,
153  ( 1.0f - aMaterial.m_Transparency ) * aOpacity );
154  const SFVEC4F specular = SFVEC4F( aMaterial.m_Specular, 1.0f );
155  const SFVEC4F emissive = SFVEC4F( aMaterial.m_Emissive, 1.0f );
156 
157  const float shininess = 128.0f * ( (aMaterial.m_Shininess > 1.0f)?
158  1.0f:
159  aMaterial.m_Shininess );
160 
161  glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, &ambient.r );
162  glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, &diffuse.r );
163  glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, &specular.r );
164  glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, &emissive.r );
165  glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, shininess );
166 }
SFVEC3F m_Ambient
Definition: c3dmodel.h:39
glm::vec4 SFVEC4F
Definition: xv3d_types.h:49
float m_Transparency
1.0 is completely transparent, 0.0 completely opaque
Definition: c3dmodel.h:44
float m_Shininess
Definition: c3dmodel.h:43
SFVEC3F m_Emissive
Definition: c3dmodel.h:41
SFVEC3F m_Diffuse
Default diffuse color if m_Color is NULL.
Definition: c3dmodel.h:40
SFVEC3F m_Specular
Definition: c3dmodel.h:42

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

Referenced by C_OGL_3DMODEL::Draw(), C3D_RENDER_OGL_LEGACY::Redraw(), C3D_RENDER_OGL_LEGACY::render_board_body(), C3D_RENDER_OGL_LEGACY::set_layer_material(), and C3D_RENDER_OGL_LEGACY::setCopperMaterial().