KiCad PCB EDA Suite
ogl_utils.cpp File Reference

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

#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)
 OGL_SetMaterial - Set OpenGL materials. More...
 
void OGL_SetDiffuseOnlyMaterial (const SFVEC3F &aMaterialDiffuse)
 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...
 

Detailed Description

implements generic openGL functions that are common to any openGL target

Definition in file ogl_utils.cpp.

Function Documentation

void OGL_DrawBackground ( const SFVEC3F aTopColor,
const SFVEC3F aBotColor 
)

OGL_DrawBackground.

Parameters
aTopColor
aBotColor

Definition at line 176 of file ogl_utils.cpp.

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

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.

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

Definition at line 35 of file ogl_utils.cpp.

Referenced by EDA_3D_CANVAS::GetScreenshot().

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 }
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 77 of file ogl_utils.cpp.

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

Referenced by C3D_RENDER_OGL_LEGACY::initializeOpenGL().

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 }
unsigned char * GetBuffer() const
Function GetBuffer get the image buffer pointer.
Definition: cimage.cpp:67
unsigned int GetHeight() const
Definition: cimage.h:207
unsigned int GetWidth() const
Definition: cimage.h:206
void OGL_SetDiffuseOnlyMaterial ( const SFVEC3F aMaterialDiffuse)

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 161 of file ogl_utils.cpp.

Referenced by C_OGL_3DMODEL::C_OGL_3DMODEL().

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 }
glm::vec4 SFVEC4F
Definition: xv3d_types.h:49
void OGL_SetMaterial ( const SMATERIAL aMaterial)

OGL_SetMaterial - Set OpenGL materials.

Parameters
aMateriala material structure with parameters to set

Definition at line 141 of file ogl_utils.cpp.

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::C_OGL_3DMODEL(), C3D_RENDER_OGL_LEGACY::Redraw(), and C3D_RENDER_OGL_LEGACY::set_layer_material().

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 }
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