00001 /**************************************************************************** 00002 00003 This file is part of the GLC-lib library. 00004 Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net) 00005 http://glc-lib.sourceforge.net 00006 00007 GLC-lib is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU Lesser General Public License as published by 00009 the Free Software Foundation; either version 3 of the License, or 00010 (at your option) any later version. 00011 00012 GLC-lib is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU Lesser General Public License for more details. 00016 00017 You should have received a copy of the GNU Lesser General Public License 00018 along with GLC-lib; if not, write to the Free Software 00019 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 00021 *****************************************************************************/ 00022 00024 00025 #include "glc_rectangle.h" 00026 #include "../glc_state.h" 00027 00028 GLC_Rectangle::GLC_Rectangle() 00029 :GLC_Mesh() 00030 , m_L1(1.0) 00031 , m_L2(1.0) 00032 { 00033 00034 } 00035 00036 GLC_Rectangle::GLC_Rectangle(double l1, double l2) 00037 :GLC_Mesh() 00038 , m_L1(l1) 00039 , m_L2(l2) 00040 { 00041 00042 } 00043 00044 GLC_Rectangle::GLC_Rectangle(const GLC_Rectangle& rect) 00045 :GLC_Mesh(rect) 00046 , m_L1(rect.m_L1) 00047 , m_L2(rect.m_L2) 00048 { 00049 00050 } 00051 00052 00053 GLC_Rectangle::~GLC_Rectangle() 00054 { 00055 00056 } 00058 // Get Functions 00060 00061 GLC_Geometry* GLC_Rectangle::clone() const 00062 { 00063 return new GLC_Rectangle(*this); 00064 } 00065 00066 // return the rectangle bounding box 00067 const GLC_BoundingBox& GLC_Rectangle::boundingBox() 00068 { 00069 if (GLC_Mesh::isEmpty()) 00070 { 00071 createMeshAndWire(); 00072 } 00073 return GLC_Mesh::boundingBox(); 00074 } 00075 00077 // Set Functions 00079 00080 GLC_Rectangle& GLC_Rectangle::setRectangle(double l1, double l2) 00081 { 00082 m_L1= l1; 00083 m_L2= l2; 00084 00085 GLC_Mesh::clearMeshWireAndBoundingBox(); 00086 00087 return *this; 00088 } 00089 00090 void GLC_Rectangle::setLength1(double value) 00091 { 00092 m_L1= value; 00093 00094 GLC_Mesh::clearMeshWireAndBoundingBox(); 00095 } 00096 00097 void GLC_Rectangle::setLength2(double value) 00098 { 00099 m_L2= value; 00100 00101 GLC_Mesh::clearMeshWireAndBoundingBox(); 00102 } 00103 00105 // Private OpenGL Functions 00107 00108 void GLC_Rectangle::glDraw(const GLC_RenderProperties& renderProperties) 00109 { 00110 if (GLC_Mesh::isEmpty()) 00111 { 00112 createMeshAndWire(); 00113 } 00114 00115 GLC_Mesh::glDraw(renderProperties); 00116 } 00117 00119 // Private services Functions 00121 00122 void GLC_Rectangle::createMeshAndWire() 00123 { 00124 Q_ASSERT(GLC_Mesh::isEmpty()); 00125 00126 const GLfloat lgX= static_cast<const GLfloat>(m_L1 / 2.0); 00127 const GLfloat lgY= static_cast<const GLfloat>(m_L2 / 2.0); 00128 00129 GLfloatVector verticeVector; 00130 GLfloatVector normalsVector; 00131 GLfloatVector texelVector; 00132 00133 // Wire data 00134 GLfloatVector wireData; 00135 00136 // the unique face of this rectangle 00137 verticeVector << -lgX; verticeVector << -lgY; verticeVector << 0.0f; 00138 normalsVector << 0.0f; normalsVector << 0.0f; normalsVector << 1.0f; 00139 texelVector << 0.0f; texelVector << 0.0f; 00140 00141 verticeVector << lgX; verticeVector << -lgY; verticeVector << 0.0f; 00142 normalsVector << 0.0f; normalsVector << 0.0f; normalsVector << 1.0f; 00143 texelVector << 1.0f; texelVector << 0.0f; 00144 00145 verticeVector << lgX; verticeVector << lgY; verticeVector << 0.0f; 00146 normalsVector << 0.0f; normalsVector << 0.0f; normalsVector << 1.0f; 00147 texelVector << 1.0f; texelVector << 1.0f; 00148 00149 verticeVector << -lgX; verticeVector << lgY; verticeVector << 0.0f; 00150 normalsVector << 0.0f; normalsVector << 0.0f; normalsVector << 1.0f; 00151 texelVector << 0.0f; texelVector << 1.0f; 00152 00153 // Add bulk data in to the mesh 00154 GLC_Mesh::addVertice(verticeVector); 00155 GLC_Mesh::addNormals(normalsVector); 00156 GLC_Mesh::addTexels(texelVector); 00157 00158 // Add the first point of the rectangle for wire 00159 verticeVector << -lgX; verticeVector << -lgY; verticeVector << 0.0f; 00160 GLC_Geometry::addVerticeGroup(verticeVector); 00161 00162 // Set the material to use 00163 GLC_Material* pMaterial; 00164 if (hasMaterial()) 00165 { 00166 pMaterial= this->firstMaterial(); 00167 } 00168 else 00169 { 00170 pMaterial= new GLC_Material(); 00171 } 00172 00173 IndexList index; 00174 // Face 1 00175 index << 0 << 1 << 3 << 2; 00176 GLC_Mesh::addTrianglesStrip(pMaterial, index); 00177 00178 GLC_Mesh::finish(); 00179 } 00180 00181