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