]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/spritemodel/spritemodel.cpp
Merge branch 'fix-fast' into 'master'
[xonotic/netradiant.git] / plugins / spritemodel / spritemodel.cpp
1 /*
2    Copyright (C) 2002 Dominic Clifton.
3
4    This file is part of GtkRadiant.
5
6    GtkRadiant is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    GtkRadiant is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with GtkRadiant; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 //
22 // Sprite Model Plugin
23 //
24 // Code by Hydra aka Dominic Clifton
25 //
26 // Based on MD3Model source code by SPoG
27 //
28
29 #include "spritemodel.h"
30
31 void LoadSpriteModel(entity_interfaces_t *interfaces, const char *name)
32 {
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; // NULL;
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 {
56     m_pShader = pShader;
57     aabb_clear(&m_BBox);
58     /*
59        md3Surface_t *pSurface = (md3Surface_t *)(((unsigned char *)pHeader) + pHeader->ofsSurfaces);
60        m_nSurfaces = pHeader->numSurfaces;
61        CMD3Surface* surfaces = new CMD3Surface[m_nSurfaces];
62        for (int i = 0; i < m_nSurfaces; i++ )
63        {
64        surfaces[i].Construct(pSurface);
65           pSurface = (md3Surface_t *) ((( char * ) pSurface) + pSurface->ofsEnd);
66        }
67        m_children = surfaces;
68        AccumulateBBox();
69      */
70 }
71
72 CSpriteModel::CSpriteModel()
73 {
74     refCount = 1;
75     //m_nSurfaces = 0;
76     //m_children = NULL;
77     m_pShader = NULL;
78 }
79
80 CSpriteModel::~CSpriteModel()
81 {
82     // if(m_children) delete[] m_children;
83     if (m_pShader) {
84         m_pShader->DecRef();
85     }
86 }
87
88 void CSpriteModel::Draw(int state, int rflags) const
89 {
90
91 /*
92    // Draw a point in the middle of the bbox
93    vec3_t middle = {0,0,0};
94    g_QglTable.m_pfn_qglPointSize (4);
95    g_QglTable.m_pfn_qglColor3f (0,1,0);
96    g_QglTable.m_pfn_qglBegin (GL_POINTS);
97    g_QglTable.m_pfn_qglVertex3fv (middle);
98    g_QglTable.m_pfn_qglEnd ();
99  */
100
101     qtexture_t *q = m_pShader->getTexture();
102
103     // convert pixels to units and divide in half again so we draw in the middle
104     // of the bbox.
105     int h = q->height / 8;
106     int w = q->width / 8;
107
108     // setup opengl stuff
109
110     g_QglTable.m_pfn_qglPushAttrib(GL_ALL_ATTRIB_BITS); // GL_ENABLE_BIT
111     //g_QglTable.m_pfn_qglColor3f (1,1,1);   //testing
112     //g_QglTable.m_pfn_qglColor4f (1,1,1,1); //testing
113     g_QglTable.m_pfn_qglBindTexture(GL_TEXTURE_2D, q->texture_number);
114
115     //g_QglTable.m_pfn_qglEnable (GL_TEXTURE_2D); // FIXME: ? this forces textures, even in wireframe mode, bad... ?
116
117     g_QglTable.m_pfn_qglAlphaFunc(GL_LESS, 1);
118     g_QglTable.m_pfn_qglEnable(GL_ALPHA_TEST);
119
120     // get rid of this when sprite always faces camera
121     g_QglTable.m_pfn_qglDisable(GL_CULL_FACE);
122     g_QglTable.m_pfn_qglPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
123
124     // draw the sprite
125
126 #if 0
127     // using x/y axis, it appears FLAT without the proper transform and rotation.
128
129     g_QglTable.m_pfn_qglBegin( GL_QUADS );
130     g_QglTable.m_pfn_qglTexCoord2f( 0,0 );
131     g_QglTable.m_pfn_qglVertex3f( 0 - w,0 - h, 0 );
132     g_QglTable.m_pfn_qglTexCoord2f( 1,0 );
133     g_QglTable.m_pfn_qglVertex3f( w,0 - h, 0 );
134     g_QglTable.m_pfn_qglTexCoord2f( 1,1 );
135     g_QglTable.m_pfn_qglVertex3f( w, h, 0 );
136     g_QglTable.m_pfn_qglTexCoord2f( 0,1 );
137     g_QglTable.m_pfn_qglVertex3f( 0 - w, h, 0 );
138     g_QglTable.m_pfn_qglEnd();
139 #else
140
141     // so draw it using y/z instead.
142     g_QglTable.m_pfn_qglBegin(GL_QUADS);
143     g_QglTable.m_pfn_qglTexCoord2f(0.0f, 0.0f);
144     g_QglTable.m_pfn_qglVertex3f(0.0f, static_cast<float>( w ), static_cast<float>( h ));
145     g_QglTable.m_pfn_qglTexCoord2f(1.0f, 0.0f);
146     g_QglTable.m_pfn_qglVertex3f(0.0f, 0.0f - static_cast<float>( w ), static_cast<float>( h ));
147     g_QglTable.m_pfn_qglTexCoord2f(1.0f, 1.0f);
148     g_QglTable.m_pfn_qglVertex3f(0.0f, 0.0f - static_cast<float>( w ), 0.0f - static_cast<float>( h ));
149     g_QglTable.m_pfn_qglTexCoord2f(0.0f, 0.0f);
150     g_QglTable.m_pfn_qglVertex3f(0.0f, static_cast<float>( w ), 0.0f - static_cast<float>( h ));
151     g_QglTable.m_pfn_qglEnd();
152 #endif
153
154     g_QglTable.m_pfn_qglBindTexture(GL_TEXTURE_2D, 0);
155     g_QglTable.m_pfn_qglPopAttrib();
156 }
157
158 /*
159    bool CSpriteModel::TestRay(const ray_t *ray, vec_t *dist) const
160    {
161    vec_t depth_start = *dist;
162    vec_t depth_local = *dist;
163
164    if (aabb_test_ray(&m_BBox, ray) == 0)
165     return false;
166
167    for(int i=0; i<m_nSurfaces; i++)
168    {
169     if(m_children[i].TestRay(ray, &depth_local))
170     {
171       if (depth_local < *dist) *dist = depth_local;
172     }
173    }
174
175    return *dist < depth_start;
176    }
177  */