You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
4.7 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2014-2015 Mario Luzeiro <mrluzeiro@gmail.com>
  5. * Copyright (C) 1992-2012 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. /**
  25. * @file 3d_class.cpp
  26. */
  27. #include <fctsys.h>
  28. #include <3d_struct.h>
  29. #include <3d_material.h>
  30. #include <info3d_visu.h>
  31. #ifdef __WXMAC__
  32. # ifdef __DARWIN__
  33. # include <OpenGL/glu.h>
  34. # else
  35. # include <glu.h>
  36. # endif
  37. #else
  38. # include <GL/glu.h>
  39. #endif
  40. S3D_MATERIAL::S3D_MATERIAL( S3D_MASTER* father, const wxString& name ) :
  41. EDA_ITEM( father, NOT_USED )
  42. {
  43. m_Name = name;
  44. m_AmbientColor.clear();
  45. m_DiffuseColor.clear();
  46. m_EmissiveColor.clear();
  47. m_SpecularColor.clear();
  48. m_Shininess.clear();
  49. m_Transparency.clear();
  50. m_ColorPerVertex = false;
  51. }
  52. void SetOpenGlDefaultMaterial()
  53. {
  54. glm::vec4 ambient( 0.2f, 0.2f, 0.2f, 1.0f );
  55. glm::vec4 specular( 0.0f, 0.0f, 0.0f, 1.0f );
  56. glm::vec4 emissive( 0.0f, 0.0f, 0.0f, 1.0f );
  57. glm::vec4 diffuse( 0.0f, 0.0f, 0.0f, 1.0f );
  58. GLint shininess_value = 0;
  59. glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
  60. glMateriali ( GL_FRONT_AND_BACK, GL_SHININESS, shininess_value );
  61. glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, &emissive.x );
  62. glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, &specular.x );
  63. glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, &ambient.x );
  64. glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, &diffuse.x );
  65. }
  66. bool S3D_MATERIAL::SetOpenGLMaterial( unsigned int aMaterialIndex, bool aUseMaterial )
  67. {
  68. if( aUseMaterial )
  69. {
  70. float transparency_value = 0.0f;
  71. if( m_Transparency.size() > aMaterialIndex )
  72. {
  73. transparency_value = m_Transparency[aMaterialIndex];
  74. }
  75. else
  76. {
  77. if( m_Transparency.size() > 0 )
  78. transparency_value = m_Transparency[0];
  79. }
  80. if( m_DiffuseColor.size() > aMaterialIndex )
  81. {
  82. glm::vec3 color = m_DiffuseColor[aMaterialIndex];
  83. glColor4f( color.x, color.y, color.z, 1.0f - transparency_value );
  84. }
  85. else
  86. {
  87. if( m_DiffuseColor.size() == 0 )
  88. {
  89. glColor4f( 0.8f, 0.8f, 0.8f, 1.0f );
  90. }
  91. }
  92. if( m_Shininess.size() > 0 )
  93. {
  94. glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, m_Shininess[0] );
  95. }
  96. // emissive
  97. if( m_EmissiveColor.size() > aMaterialIndex )
  98. {
  99. glm::vec4 emissive;
  100. emissive[0] = m_EmissiveColor[aMaterialIndex].x;
  101. emissive[1] = m_EmissiveColor[aMaterialIndex].y;
  102. emissive[2] = m_EmissiveColor[aMaterialIndex].z;
  103. emissive[3] = 1.0f;
  104. glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, &emissive.x );
  105. }
  106. // specular
  107. if( m_SpecularColor.size() > aMaterialIndex )
  108. {
  109. glm::vec4 specular;
  110. specular[0] = m_SpecularColor[aMaterialIndex].x;
  111. specular[1] = m_SpecularColor[aMaterialIndex].y;
  112. specular[2] = m_SpecularColor[aMaterialIndex].z;
  113. specular[3] = 1.0f;
  114. glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, &specular.x );
  115. }
  116. // ambient
  117. if( m_AmbientColor.size() > aMaterialIndex )
  118. {
  119. glm::vec4 ambient;
  120. ambient[0] = m_AmbientColor[aMaterialIndex].x;
  121. ambient[1] = m_AmbientColor[aMaterialIndex].y;
  122. ambient[2] = m_AmbientColor[aMaterialIndex].z;
  123. ambient[3] = 1.0f;
  124. glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, &ambient.x );
  125. }
  126. return (transparency_value != 0.0f);
  127. }
  128. else
  129. {
  130. if( m_DiffuseColor.size() > aMaterialIndex )
  131. {
  132. glm::vec3 color = m_DiffuseColor[aMaterialIndex];
  133. glColor4f( color.x, color.y, color.z, 1.0 );
  134. }
  135. }
  136. return false;
  137. }