2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant 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.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "cpicosurface.h"
26 CPicoSurface::CPicoSurface( picoSurface_t *pSurface ){
29 m_pSurface = pSurface;
31 // PicoFixSurfaceNormals( pSurface );
35 m_shader = QERApp_Shader_ForName( GetShaderName() );
38 CPicoSurface::~CPicoSurface(){
42 void CPicoSurface::Draw( int state, int rflags ){
43 Draw( state, m_shader, rflags );
46 void CPicoSurface::Draw( int state, IShader *pShader, int rflags ){
49 if ( !( rflags & ( DRAW_RF_SEL_OUTLINE | DRAW_RF_SEL_FILL | DRAW_RF_XY ) ) ) {
50 if ( state & DRAW_GL_TEXTURE_2D ) {
51 bool bTrans = ( pShader->getFlags() & QER_TRANS ) == QER_TRANS;
52 bool bDrawBlend = ( state & DRAW_GL_BLEND ) == DRAW_GL_BLEND;
53 //only draw transparent stuff when in transparent stuff pass and vice versa
54 if(bTrans != bDrawBlend) {
57 g_QglTable.m_pfn_qglBindTexture( GL_TEXTURE_2D, pShader->getTexture()->texture_number );
58 if ( ( rflags & DRAW_RF_CAM ) && ( pShader->getFlags() & QER_ALPHAFUNC ) ) {
62 g_QglTable.m_pfn_qglColor4f( 1.f, 1.f, 1.f, pShader->getTrans() ); // transparency
64 g_QglTable.m_pfn_qglEnable( GL_ALPHA_TEST );
66 pShader->getAlphaFunc( &nFunc, &fRef );
67 g_QglTable.m_pfn_qglAlphaFunc( nFunc, fRef );
72 //g_QglTable.m_pfn_qglColor3fv( pShader->getTexture()->color );
73 /* g_QglTable.m_pfn_qglEnableClientState(GL_COLOR_ARRAY);*/
76 if ( !( state & DRAW_GL_WIRE ) && ( pShader->getFlags() & QER_CULL ) ) {
77 if ( pShader->getCull() == 2 ) {
78 g_QglTable.m_pfn_qglDisable( GL_CULL_FACE );
79 g_QglTable.m_pfn_qglPolygonMode( GL_FRONT, GL_FILL );
83 g_QglTable.m_pfn_qglCullFace( GL_BACK );
88 switch ( PicoGetSurfaceType( m_pSurface ) )
90 case PICO_TRIANGLES: g_QglTable.m_pfn_qglBegin( GL_TRIANGLES );
91 for ( j = 0; j < PicoGetSurfaceNumIndexes( m_pSurface ); j++ )
93 g_QglTable.m_pfn_qglNormal3fv( PicoGetSurfaceNormal( m_pSurface,PicoGetSurfaceIndex( m_pSurface,j ) ) );
95 if ( !( rflags & ( DRAW_RF_SEL_OUTLINE | DRAW_RF_SEL_FILL | DRAW_RF_XY ) ) ) {
96 if ( state & DRAW_GL_TEXTURE_2D ) {
97 g_QglTable.m_pfn_qglTexCoord2fv( PicoGetSurfaceST( m_pSurface,0,PicoGetSurfaceIndex( m_pSurface,j ) ) );
100 picoByte_t *vertexColor = PicoGetSurfaceColor( m_pSurface,0,PicoGetSurfaceIndex( m_pSurface,j ) );
101 //% g_QglTable.m_pfn_qglColor4f( vertexColor[ 0 ] / 255.f,
102 //% vertexColor[ 1 ] / 255.f,
103 //% vertexColor[ 2 ] / 255.f,
104 //% vertexColor[ 3 ] / 255.f );
105 g_QglTable.m_pfn_qglColor4ubv( vertexColor );
108 g_QglTable.m_pfn_qglVertex3fv( PicoGetSurfaceXYZ( m_pSurface, PicoGetSurfaceIndex( m_pSurface, j ) ) );
110 g_QglTable.m_pfn_qglEnd();
111 /*g_QglTable.m_pfn_qglVertexPointer( 3, GL_FLOAT, 0, PicoGetSurfaceXYZ( m_pSurface, 0 ) );
112 g_QglTable.m_pfn_qglNormalPointer( GL_FLOAT, 0, PicoGetSurfaceNormal( m_pSurface, 0 ) );
113 if( !(rflags & (DRAW_RF_SEL_OUTLINE|DRAW_RF_SEL_FILL|DRAW_RF_XY)) ) {
114 if( state & DRAW_GL_TEXTURE_2D ) {
115 g_QglTable.m_pfn_qglTexCoordPointer( 2, GL_FLOAT, 0, PicoGetSurfaceST( m_pSurface, 0, 0 ) );
117 g_QglTable.m_pfn_qglColorPointer( 4, GL_UNSIGNED_BYTE, 0, PicoGetSurfaceColor( m_pSurface, 0, 0 ) );
120 g_QglTable.m_pfn_qglDrawElements( GL_TRIANGLES, PicoGetSurfaceNumIndexes( m_pSurface ), GL_UNSIGNED_INT, PicoGetSurfaceIndexes( m_pSurface, 0 ) );*/
122 /*g_QglTable.m_pfn_qglColor3f( 0.f, .5f, 1.f );
123 g_QglTable.m_pfn_qglBegin( GL_LINES );
124 for( int i = 0; i < PicoGetSurfaceNumIndexes( m_pSurface ); i++ ) {
126 VectorMA( PicoGetSurfaceXYZ( m_pSurface, PicoGetSurfaceIndex( m_pSurface, i ) ), .3f, PicoGetSurfaceNormal( m_pSurface, PicoGetSurfaceIndex( m_pSurface, i ) ), outerpoint );
127 g_QglTable.m_pfn_qglVertex3fv( PicoGetSurfaceXYZ( m_pSurface, PicoGetSurfaceIndex( m_pSurface, i ) ) );
128 g_QglTable.m_pfn_qglVertex3fv( outerpoint );
130 g_QglTable.m_pfn_qglEnd();*/
133 default: Sys_Printf( "ERROR: Unsupported Pico Surface Type: %i", PicoGetSurfaceType( m_pSurface ) );
137 if ( !( rflags & ( DRAW_RF_SEL_OUTLINE | DRAW_RF_SEL_FILL | DRAW_RF_XY ) ) ) {
138 if ( ( state & DRAW_GL_TEXTURE_2D ) && ( rflags & DRAW_RF_CAM ) && ( pShader->getFlags() & QER_ALPHAFUNC ) ) {
139 g_QglTable.m_pfn_qglDisable( GL_ALPHA_TEST );
142 /* if(!(state & DRAW_GL_TEXTURE_2D)) {
143 g_QglTable.m_pfn_qglDisableClientState(GL_COLOR_ARRAY);
146 if ( !( state & DRAW_GL_WIRE ) && ( pShader->getFlags() & QER_CULL ) ) {
147 if ( pShader->getCull() == 2 ) {
148 g_QglTable.m_pfn_qglPolygonMode( GL_FRONT, GL_LINE );
149 g_QglTable.m_pfn_qglEnable( GL_CULL_FACE );
153 g_QglTable.m_pfn_qglCullFace( GL_FRONT );
161 void CPicoSurface::AccumulateBBox(){
164 aabb_clear( &m_BBox );
165 for ( i = 0; i < PicoGetSurfaceNumVertexes( m_pSurface ); i++ )
167 p = PicoGetSurfaceXYZ( m_pSurface,i );
168 aabb_extend_by_point( &m_BBox, p );
170 aabb_update_radius( &m_BBox );
173 bool CPicoSurface::TestRay( const ray_t *ray, vec_t *dist ) const {
175 vec_t start_dist = *dist;
176 vec_t local_dist = *dist;
177 if ( aabb_intersect_ray( &m_BBox, ray, &local_dist ) ) {
178 switch ( PicoGetSurfaceType( m_pSurface ) )
181 for ( i = 0; i < PicoGetSurfaceNumIndexes( m_pSurface ); i += 3 )
183 local_dist = ray_intersect_triangle( ray, true, PicoGetSurfaceXYZ( m_pSurface,PicoGetSurfaceIndex( m_pSurface,i + 2 ) ),
184 PicoGetSurfaceXYZ( m_pSurface,PicoGetSurfaceIndex( m_pSurface,i + 1 ) ),
185 PicoGetSurfaceXYZ( m_pSurface,PicoGetSurfaceIndex( m_pSurface,i ) ) );
186 if ( local_dist < *dist ) {
192 Sys_Printf( "ERROR: Unsupported Pico Surface Type: %i", PicoGetSurfaceType( m_pSurface ) );
196 return ( *dist < start_dist );