]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/spritemodel/spritemodel.cpp
uncrustify! now the code is only ugly on the *inside*
[xonotic/netradiant.git] / plugins / spritemodel / spritemodel.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 //
23 // Sprite Model Plugin
24 //
25 // Code by Hydra aka Dominic Clifton
26 //
27 // Based on MD3Model source code by SPoG
28 //
29
30 #include "spritemodel.h"
31
32 void LoadSpriteModel( entity_interfaces_t *interfaces, const char *name ){
33         IShader *pShader;
34
35         pShader = QERApp_Shader_ForName( name );
36
37         if ( !pShader ) {
38                 Sys_Printf( "ERROR: can't find shader (or image) for: %s\n", name );
39                 return;
40         }
41
42         CSpriteModel *model = new CSpriteModel();
43         model->Construct( pShader );
44         interfaces->pRender = (IRender*)model;
45         interfaces->pRender->IncRef();
46         //interfaces->pSelect = (ISelect*)model;
47         //interfaces->pSelect->IncRef();
48         interfaces->pSelect = NULL;
49         interfaces->pEdit = NULL;
50         model->DecRef();
51
52 }
53
54 void CSpriteModel::Construct( IShader *pShader ){
55         m_pShader = pShader;
56         aabb_clear( &m_BBox );
57         /*
58            md3Surface_t *pSurface = (md3Surface_t *)(((unsigned char *)pHeader) + pHeader->ofsSurfaces);
59            m_nSurfaces = pHeader->numSurfaces;
60            CMD3Surface* surfaces = new CMD3Surface[m_nSurfaces];
61            for (int i = 0; i < m_nSurfaces; i++ )
62            {
63            surfaces[i].Construct(pSurface);
64               pSurface = (md3Surface_t *) ((( char * ) pSurface) + pSurface->ofsEnd);
65            }
66            m_children = surfaces;
67            AccumulateBBox();
68          */
69 }
70
71 CSpriteModel::CSpriteModel(){
72         refCount = 1;
73         //m_nSurfaces = 0;
74         //m_children = NULL;
75         m_pShader = NULL;
76 }
77
78 CSpriteModel::~CSpriteModel(){
79         // if(m_children) delete[] m_children;
80         if ( m_pShader ) {
81                 m_pShader->DecRef();
82         }
83 }
84
85 void CSpriteModel::Draw( int state, int rflags ) const {
86
87 /*
88    // Draw a point in the middle of the bbox
89    vec3_t middle = {0,0,0};
90    g_QglTable.m_pfn_qglPointSize (4);
91    g_QglTable.m_pfn_qglColor3f (0,1,0);
92    g_QglTable.m_pfn_qglBegin (GL_POINTS);
93    g_QglTable.m_pfn_qglVertex3fv (middle);
94    g_QglTable.m_pfn_qglEnd ();
95  */
96
97         qtexture_t    *q = m_pShader->getTexture();
98
99         // convert pixels to units and divide in half again so we draw in the middle
100         // of the bbox.
101         int h = q->height / 8;
102         int w = q->width / 8;
103
104         // setup opengl stuff
105
106         g_QglTable.m_pfn_qglPushAttrib( GL_ALL_ATTRIB_BITS ); // GL_ENABLE_BIT
107         //g_QglTable.m_pfn_qglColor3f (1,1,1);   //testing
108         //g_QglTable.m_pfn_qglColor4f (1,1,1,1); //testing
109         g_QglTable.m_pfn_qglBindTexture( GL_TEXTURE_2D, q->texture_number );
110
111         //g_QglTable.m_pfn_qglEnable (GL_TEXTURE_2D); // FIXME: ? this forces textures, even in wireframe mode, bad... ?
112
113         g_QglTable.m_pfn_qglAlphaFunc( GL_LESS, 1 );
114         g_QglTable.m_pfn_qglEnable( GL_ALPHA_TEST );
115
116         // get rid of this when sprite always faces camera
117         g_QglTable.m_pfn_qglDisable( GL_CULL_FACE );
118         g_QglTable.m_pfn_qglPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
119
120         // draw the sprite
121
122 #if 0
123         // using x/y axis, it appears FLAT without the proper transform and rotation.
124
125         g_QglTable.m_pfn_qglBegin( GL_QUADS );
126         g_QglTable.m_pfn_qglTexCoord2f( 0,0 );
127         g_QglTable.m_pfn_qglVertex3f( 0 - w,0 - h, 0 );
128         g_QglTable.m_pfn_qglTexCoord2f( 1,0 );
129         g_QglTable.m_pfn_qglVertex3f( w,0 - h, 0 );
130         g_QglTable.m_pfn_qglTexCoord2f( 1,1 );
131         g_QglTable.m_pfn_qglVertex3f( w, h, 0 );
132         g_QglTable.m_pfn_qglTexCoord2f( 0,1 );
133         g_QglTable.m_pfn_qglVertex3f( 0 - w, h, 0 );
134         g_QglTable.m_pfn_qglEnd();
135 #else
136
137         // so draw it using y/z instead.
138         g_QglTable.m_pfn_qglBegin( GL_QUADS );
139         g_QglTable.m_pfn_qglTexCoord2f( 0,0 );
140         g_QglTable.m_pfn_qglVertex3f( 0,w,h );
141         g_QglTable.m_pfn_qglTexCoord2f( 1,0 );
142         g_QglTable.m_pfn_qglVertex3f( 0,0 - w,h );
143         g_QglTable.m_pfn_qglTexCoord2f( 1,1 );
144         g_QglTable.m_pfn_qglVertex3f( 0,0 - w,0 - h );
145         g_QglTable.m_pfn_qglTexCoord2f( 0,1 );
146         g_QglTable.m_pfn_qglVertex3f( 0,w,0 - h );
147         g_QglTable.m_pfn_qglEnd();
148 #endif
149
150         g_QglTable.m_pfn_qglBindTexture( GL_TEXTURE_2D, 0 );
151         g_QglTable.m_pfn_qglPopAttrib();
152 }
153
154 /*
155    bool CSpriteModel::TestRay(const ray_t *ray, vec_t *dist) const
156    {
157    vec_t depth_start = *dist;
158    vec_t depth_local = *dist;
159
160    if (aabb_test_ray(&m_BBox, ray) == 0)
161     return false;
162
163    for(int i=0; i<m_nSurfaces; i++)
164    {
165     if(m_children[i].TestRay(ray, &depth_local))
166     {
167       if (depth_local < *dist) *dist = depth_local;
168     }
169    }
170
171    return *dist < depth_start;
172    }
173  */