]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/model/cpicosurface.cpp
Fixed transparency (qer_trans) not working on models
[xonotic/netradiant.git] / plugins / model / cpicosurface.cpp
1 /*
2    Copyright (C) 1999-2007 id Software, Inc. and contributors.
3    For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5    This file is part of GtkRadiant.
6
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.
11
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.
16
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
20  */
21
22 #include "cpicosurface.h"
23
24 // public
25
26 CPicoSurface::CPicoSurface( picoSurface_t *pSurface ){
27         refCount = 1;
28
29         m_pSurface = pSurface;
30
31         // PicoFixSurfaceNormals( pSurface );
32
33         AccumulateBBox();
34
35         m_shader = QERApp_Shader_ForName( GetShaderName() );
36 }
37
38 CPicoSurface::~CPicoSurface(){
39         m_shader->DecRef();
40 }
41
42 void CPicoSurface::Draw( int state, int rflags ){
43         Draw( state, m_shader, rflags );
44 }
45
46 void CPicoSurface::Draw( int state, IShader *pShader, int rflags ){
47         int j;
48
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) {
55                                 return;
56                         }
57                         g_QglTable.m_pfn_qglBindTexture( GL_TEXTURE_2D, pShader->getTexture()->texture_number );
58                         if ( ( rflags & DRAW_RF_CAM ) && ( pShader->getFlags() & QER_ALPHAFUNC ) ) {
59                                 int nFunc = 0;
60                                 float fRef = 0.f;
61
62                                 g_QglTable.m_pfn_qglColor4f( 1.f, 1.f, 1.f, pShader->getTrans() ); // transparency
63
64                                 g_QglTable.m_pfn_qglEnable( GL_ALPHA_TEST );
65
66                                 pShader->getAlphaFunc( &nFunc, &fRef );
67                                 g_QglTable.m_pfn_qglAlphaFunc( nFunc, fRef );
68                         }
69                 }
70                 else
71                 {
72                         //g_QglTable.m_pfn_qglColor3fv( pShader->getTexture()->color );
73 /*      g_QglTable.m_pfn_qglEnableClientState(GL_COLOR_ARRAY);*/
74                 }
75
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 );
80                         }
81                         else // is 1
82                         {
83                                 g_QglTable.m_pfn_qglCullFace( GL_BACK );
84                         }
85                 }
86         }
87
88         switch ( PicoGetSurfaceType( m_pSurface ) )
89         {
90         case PICO_TRIANGLES:  g_QglTable.m_pfn_qglBegin( GL_TRIANGLES );
91                 for ( j = 0; j < PicoGetSurfaceNumIndexes( m_pSurface ); j++ )
92                 {
93                         g_QglTable.m_pfn_qglNormal3fv( PicoGetSurfaceNormal( m_pSurface,PicoGetSurfaceIndex( m_pSurface,j ) ) );
94
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 ) ) );
98                                 }
99                                 else {
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 );
106                                 }
107                         }
108                         g_QglTable.m_pfn_qglVertex3fv( PicoGetSurfaceXYZ( m_pSurface, PicoGetSurfaceIndex( m_pSurface, j ) ) );
109                 }
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 ) );
116                    } else {
117                     g_QglTable.m_pfn_qglColorPointer( 4, GL_UNSIGNED_BYTE, 0, PicoGetSurfaceColor( m_pSurface, 0, 0 ) );
118                    }
119                    }
120                    g_QglTable.m_pfn_qglDrawElements( GL_TRIANGLES, PicoGetSurfaceNumIndexes( m_pSurface ), GL_UNSIGNED_INT, PicoGetSurfaceIndexes( m_pSurface, 0 ) );*/
121
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++ ) {
125                    vec3_t outerpoint;
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 );
129                    }
130                    g_QglTable.m_pfn_qglEnd();*/
131
132                 break;
133         default:              Sys_Printf( "ERROR: Unsupported Pico Surface Type: %i", PicoGetSurfaceType( m_pSurface ) );
134                 break;
135         }
136
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 );
140                 }
141
142 /*      if(!(state & DRAW_GL_TEXTURE_2D)) {
143       g_QglTable.m_pfn_qglDisableClientState(GL_COLOR_ARRAY);
144     }*/
145
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 );
150                         }
151                         else // is 1
152                         {
153                                 g_QglTable.m_pfn_qglCullFace( GL_FRONT );
154                         }
155                 }
156         }
157 }
158
159 // private
160
161 void CPicoSurface::AccumulateBBox(){
162         int i;
163         picoVec_t *p;
164         aabb_clear( &m_BBox );
165         for ( i = 0; i < PicoGetSurfaceNumVertexes( m_pSurface ); i++ )
166         {
167                 p = PicoGetSurfaceXYZ( m_pSurface,i );
168                 aabb_extend_by_point( &m_BBox, p );
169         }
170         aabb_update_radius( &m_BBox );
171 }
172
173 bool CPicoSurface::TestRay( const ray_t *ray, vec_t *dist ) const {
174         int i;
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 ) )
179                 {
180                 case PICO_TRIANGLES:
181                         for ( i = 0; i < PicoGetSurfaceNumIndexes( m_pSurface ); i += 3 )
182                         {
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 ) {
187                                         *dist = local_dist;
188                                 }
189                         }
190                         break;
191                 default:
192                         Sys_Printf( "ERROR: Unsupported Pico Surface Type: %i", PicoGetSurfaceType( m_pSurface ) );
193                         break;
194                 }
195         }
196         return ( *dist < start_dist );
197 }