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

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

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.

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

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

Referenced by C3D_RENDER_OGL_LEGACY::initializeOpenGL().

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 }
unsigned char * GetBuffer() const
Function GetBuffer get the image buffer pointer.
Definition: cimage.cpp:71
unsigned int GetHeight() const
Definition: cimage.h:207
unsigned int GetWidth() const
Definition: cimage.h:206
size_t i
Definition: json11.cpp:597
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 164 of file ogl_utils.cpp.

Referenced by C_OGL_3DMODEL::C_OGL_3DMODEL().

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 }
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 144 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().

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