]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/shaders/shaders.h
uncrustify! now the code is only ugly on the *inside*
[xonotic/netradiant.git] / plugins / shaders / shaders.h
1 /*
2    Copyright (c) 2001, Loki software, inc.
3    All rights reserved.
4
5    Redistribution and use in source and binary forms, with or without modification,
6    are permitted provided that the following conditions are met:
7
8    Redistributions of source code must retain the above copyright notice, this list
9    of conditions and the following disclaimer.
10
11    Redistributions in binary form must reproduce the above copyright notice, this
12    list of conditions and the following disclaimer in the documentation and/or
13    other materials provided with the distribution.
14
15    Neither the name of Loki software nor the names of its contributors may be used
16    to endorse or promote products derived from this software without specific prior
17    written permission.
18
19    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
20    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22    DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
23    DIRECT,INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26    ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 //-----------------------------------------------------------------------------
32 //
33 // DESCRIPTION:
34 // internal implementation of IShaders related stuff
35 //
36
37 #ifndef __SHADERS_H_
38 #define __SHADERS_H_
39
40 //++timo TODO: track all the calls to IncRef and look for not-called DecRef bugs
41 //++timo TODO: move all needed stuff into the IShader interface
42
43 // Radiant's internal implementation of the IShader object
44 class CShader : public IShader
45 {
46 int refCount;
47 qtexture_t *m_pTexture;
48 // name is shader / texture name (if not a real shader) reletive to "textures/" directory
49 char m_Name[QER_MAX_NAMELEN];
50 char m_ShaderFileName[QER_MAX_NAMELEN];
51 int m_nFlags;
52 float m_fTrans;
53 // the name of the texture file to be used to represent the shader
54 //++timo FIXME?
55 // must fit the qtexture_t convention or not? I think so ..
56 // ((old .. NOTE: may be a straight copy of the .shader file, doesn't fit the qtexture_t naming requirements))
57 CString m_strTextureName;
58 bool m_bDisplayed;
59 bool m_bInUse;
60 // color stuff
61 bool m_bColor;
62 vec3_t m_vColor;
63 // alphafunc stuff
64 int m_nAlphaFunc;
65 float m_fAlphaRef;
66 // cull stuff
67 int m_nCull;
68
69 // will hook itself in g_ActiveShaders and increment ref count
70 // will also update the underlying qtexture_t with some information about the shader name etc.
71 void RegisterActivate();
72
73 public:
74 CShader() { refCount = 0; m_pTexture = NULL; m_Name[0] = '\0'; m_ShaderFileName[0] = '\0'; m_nFlags = 0; m_bInUse = false; m_bDisplayed = false; m_bColor = false; m_fTrans = 1.0f; m_nAlphaFunc = 0; m_fAlphaRef = 0.f; m_nCull = 0; }
75 virtual ~CShader() { }
76
77 // IShaders implementation -----------------
78 // Increment the number of references to this object
79 void IncRef() { refCount++; }
80 // Decrement the reference count
81 void DecRef(){
82         if ( --refCount <= 0 ) {
83                 delete this;
84         }
85 }
86 // get/set the qtexture_t* Radiant uses to represent this shader object
87 qtexture_t* getTexture() const { return m_pTexture; }
88 void setTexture( qtexture_t *pTex ) { m_pTexture = pTex; }
89 // get shader name
90 const char* getName() const { return m_Name; }
91 bool IsDisplayed() const { return m_bDisplayed; }
92 void SetDisplayed( bool b ) { m_bDisplayed = b; }
93 // setting in use also sets the display flag on
94 bool IsInUse() const { return m_bInUse; }
95 void SetInUse( bool b ) {
96         m_bInUse = b; if ( m_pTexture ) {
97                 m_pTexture->inuse = true;
98         }
99         if ( b ) {
100                 m_bDisplayed = true;
101         }
102 }
103 // get the shader flags
104 int getFlags() { return m_nFlags; }
105 // get the transparency value
106 float getTrans() { return m_fTrans; }
107 // test if it's a true shader, or a default shader created to wrap around a texture
108 bool IsDefault() { return m_ShaderFileName[0] == '\0'; }
109 // test if it's a plain color shader, i.e. a shader we use on plain color stuff (like info_playerstart)
110 bool IsColor() { return m_bColor; }
111 // get the related color then!
112 void getColor( vec3_t v ) { VectorCopy( m_vColor, v ); }
113 // get the alphaFunc
114 void getAlphaFunc( int *func, float *ref ) { *func = m_nAlphaFunc; *ref = m_fAlphaRef; };
115 // get the cull type
116 int getCull() { return m_nCull; };
117 // get/set shader file name (ie the file where this one is defined)
118 const char* getShaderFileName() const { return m_ShaderFileName; }
119 // -----------------------------------------
120
121 // parse yourself!
122 bool Parse();
123
124 // search / load the texture to be used when displaying the shader
125 // after a successfull call to one of these the shader will get displayed in the tex wnd
126 // if m_strTextureName could not be loaded will set m_pTexture to NULL
127 void Try_Activate();
128 // if m_strTextureName could not be loaded will use a default qtexture
129 // FIXME TTimo: Activate forces activation, always true
130 bool Activate();
131
132 // set shader name
133 void setName( const char* name ) { strcpy( m_Name, name ); }
134 void setShaderFileName( const char* name ) { strcpy( m_ShaderFileName, name ); }
135 // create a default shader for a given texture name
136 // will not activate!
137 // NOTE: CreateDefault expects a texture name reletive to the base path. Adding a "textures/" may be needed
138 void CreateDefault( const char* name );
139 const char* getTextureName() { return m_strTextureName; }
140
141 //++timo clean
142 // color stuff
143 //      void setColor( vec3_t c ) { VectorCopy( c, m_vColor ); m_bColor = true; }
144 // create a color shader
145 void CreateColor( const char* name );
146 };
147
148 // the classical CPtrArray with some enhancements
149 class CShaderArray : public CPtrArray
150 {
151 public:
152 CShaderArray() { }
153 virtual ~CShaderArray() { }
154 // look for a shader with a given name (may return NULL)
155 CShader* Shader_ForName( const char * ) const;
156 // look for a shader with a given texture name (may return NULL)
157 // NOTE: the texture name is supposed to fit qtexture_t naming conventions .. _DEBUG builds will check
158 CShader* Shader_ForTextureName( const char * ) const;
159 // will Add the given object if not already in
160 void AddSingle( void* );
161 // will copy / add another CShaderArray, and IncRef
162 void operator =( const class CShaderArray & );
163 // will empty the array, decreasing the refcount by 1
164 void ReleaseAll();
165 // will empty all shaders that match a given filename, decreasing the refcount by 1
166 void ReleaseForShaderFile( const char * );
167 // sort the array by shader name
168 void SortShaders();
169 // set the IsDisplayed flag for all shaders stored
170 void SetDisplayed( bool b );
171 // set the InUse flag for all shaders stored
172 void SetInUse( bool b );
173 };
174
175 #endif