K3dDrawTriangle.cpp

Go to the documentation of this file.
00001 
00014 /***************************************************************************
00015  *   Copyright (C) 2007 by Jan Koci   *
00016  *   honza.koci@email.cz   *
00017  *   http://kengine.sourceforge.net/tutorial/
00018  *                                                                         *
00019  *   This program is free software; you can redistribute it and/or modify  *
00020  *   it under the terms of the GNU General Public License as published by  *
00021  *   the Free Software Foundation; either version 2 of the License, or     *
00022  *   (at your option) any later version.                                   *
00023  *                                                                         *
00024  *   This program is distributed in the hope that it will be useful,       *
00025  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00026  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00027  *   GNU General Public License for more details.                          *
00028  *                                                                         *
00029  *   You should have received a copy of the GNU General Public License     *
00030  *   along with this program; if not, write to the                         *
00031  *   Free Software Foundation, Inc.,                                       *
00032  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00033  ***************************************************************************/
00034 
00035 #include "K3dDrawTriangle.h"
00036 #include "../Geometry/K3dTriangle.h"
00037 
00039 void K3dDrawTriangle::DrawTriangleArray()
00040 {
00041         // Color for rendering
00042         
00043 
00044 
00045 
00046 /*
00047         //if(glIsEnabled(GL_TEXTURE_2D))
00048         //{
00049         //      glDisable(GL_TEXTURE_2D);
00050         //}
00051         
00052         if(pTriangle->IsBlend())
00053         {
00054                 glColor3fv( aC );               // Set color
00055                 // Disable depth test
00056                 //CGL::m_glDisable( GL_DEPTH_TEST );    
00057                 //if(CGL::m_glIsEnabled(GL_DEPTH_TEST)) // If enabled depth buffer test
00058                 //      CGL::m_glDisable( GL_DEPTH_TEST );      // Disable 
00059                 // Enable blending
00060                 glEnable(GL_BLEND);
00061                 // Set blend function
00062                 glBlendFunc(GL_ONE, GL_ONE);
00063                 // Set vertex array 
00064                 glVertexPointer(3, GL_FLOAT, 0, akV);//akTriangle[0]);
00065                 // Enable vertex array 
00066                 glEnableClientState(GL_VERTEX_ARRAY);
00067                 
00068                 
00069                 //CGL::m_glPushMatrix();
00070                 // Draw square
00071                 //CGL::m_glDrawArrays(GL_TRIANGLES, 0, 3);
00072                 glDrawArrays(GL_TRIANGLES, 0, 3);
00073                 //CGL::m_glPopMatrix();
00074                 // Disable vertex array 
00075                 glDisableClientState(GL_VERTEX_ARRAY);
00076                 //glColor3ub( 255, 255, 255 );          // Set white color
00077                 // Disable blending
00078                 glDisable(GL_BLEND);
00079                 //CGL::m_glEnable( GL_DEPTH_TEST );     // Enable depth buffer test 
00080                 glColor3ub( 255, 255, 255 );            // Set white color
00081                 // Enable depth test
00082                 //CGL::m_glEnable( GL_DEPTH_TEST );     
00083         }
00084         else*/
00085 
00086 
00087 
00088         float aC[3];
00089         aC[0] = 0;
00090         aC[1] = 0;
00091         aC[2] = 1;
00092         glColor3fv( aC );               // Set color
00093         glDrawArrays(GL_TRIANGLES, 0, m_iNumVertices );
00094 
00095         // Dreaw triangle array lines
00096         aC[0] = 0;
00097         aC[1] = 1;
00098         aC[2] = 0;
00099         glColor3fv( aC );               // Set color
00100         glDisable(GL_LIGHTING);
00101                 glBegin(GL_LINES);
00102                 for(int i=0; i<m_iNumVertices-2; i++)
00103                 {
00104                         // First line
00105                         glVertex3fv(m_ptVertexArray[i].afVector);
00106                         glVertex3fv(m_ptVertexArray[i+1].afVector);
00107                         // Second line
00108                         glVertex3fv(m_ptVertexArray[i+1].afVector);
00109                         glVertex3fv(m_ptVertexArray[i+2].afVector);
00110                         // Third line
00111                         glVertex3fv(m_ptVertexArray[i+2].afVector);
00112                         glVertex3fv(m_ptVertexArray[i].afVector);
00113                         // Jump to next triangle
00114                         i=i+2;
00115                 }
00116                 glEnd();
00117         glEnable(GL_LIGHTING);
00118         //glDrawArrays(GL_LINES, 0, m_iNumVertices );
00119         glColor3ub( 255, 255, 255 );            // Set white color
00120 
00121 
00122 }
00123 
00124 void K3dDrawTriangle::InitTriangleArray()
00125 {
00126         cout << "void K3dDrawTriangle::InitTriangleArray()" << endl;
00127         // Enable vertex array 
00128         glEnableClientState(GL_VERTEX_ARRAY);
00129         
00130         // Set vertex array 
00131         glVertexPointer(3, GL_FLOAT, sizeof(TRenderVertex), m_ptVertexArray);
00132         
00133         // Enable normal vector array
00134         glEnableClientState(GL_NORMAL_ARRAY);
00135         // Set normal vector array
00136         glNormalPointer(GL_FLOAT, sizeof(TRenderVertex), m_ptNormalArray);
00137         
00138 }
00139 
00141 void K3dDrawTriangle::ReallocVertexArray(const int _iNewSize)
00142 {
00143         cout << "void K3dDrawTriangle::ReallocVertexArray()" << endl;
00144         // Return if new size is not greater than number of vertices
00145         if(m_iNumVertices >= _iNewSize)
00146                 return;
00147         
00148         TRenderVertex *ptNewVertexArray = new TRenderVertex[_iNewSize];
00149         TRenderVertex *ptNewNormalArray = new TRenderVertex[_iNewSize];
00150         // Copy current render array to the new render array
00151         for(int i=0; i<m_iNumVertices; i++)
00152         {
00153                 ptNewVertexArray[i] = m_ptVertexArray[i];
00154                 ptNewNormalArray[i] = m_ptNormalArray[i];
00155         }
00156         // Detele previous vertex array
00157         DeleteVertexArray();
00158         // Copy current pointer to new pointer
00159         m_ptVertexArray = ptNewVertexArray;
00160         m_ptNormalArray = ptNewNormalArray;
00161         // Set new number of vertices
00162         m_iNumVertices = _iNewSize;
00163 }
00164 
00165 void K3dDrawTriangle::AddTriangle(TRenderTriangle &_rtTriangle)
00166 {
00167         cout << "void K3dDrawTriangle::AddTriangle()" << endl;
00168         // Add 3 vertices to the array
00169         ReallocVertexArray(m_iNumVertices+3);
00170         
00171         // Set new 3 vertices in vertex array
00172         int iNumVerts = m_iNumVertices - 3;
00173         // Go through triangle vertices
00174         for(int i=0; i<3; i++)
00175         {
00176                 // Set new vertex to the vertex array
00177                 m_ptVertexArray[iNumVerts] = _rtTriangle.atVertex[i];
00178                 // Set new normal to the vertex array
00179                 m_ptNormalArray[iNumVerts] = _rtTriangle.atNormal[i];
00180                 // Add number of vertices
00181                 iNumVerts++;
00182         }
00183 }
00184 
00185 void K3dDrawTriangle::DeleteVertexArray()
00186 {
00187         cout << "void K3dDrawTriangle::DeleteVertexArray()" << endl;
00188         if(m_ptVertexArray != NULL)
00189         {
00190                 glDisableClientState(GL_VERTEX_ARRAY);
00191                 //m_ptVertexArray = g_RenderVertexSP.Delete(m_ptVertexArray);
00192                 delete [] m_ptVertexArray;
00193                 m_ptVertexArray = NULL;
00194         }
00195         if(m_ptNormalArray != NULL)
00196         {
00197                 glDisableClientState(GL_NORMAL_ARRAY);
00198                 //m_ptNormalArray = g_RenderVertexSP.Delete(m_ptNormalArray);
00199                 delete [] m_ptNormalArray;
00200                 m_ptNormalArray = NULL;
00201         }
00202         m_iNumVertices=0;
00203 }
00204 

Generated on Thu Aug 16 23:53:26 2007 for K3dEngine by  doxygen 1.5.0