]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/shaders/shaders.h
Backing out r347 and r345. Keeping r346.
[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         // get/set the qtexture_t* Radiant uses to represent this shader object
86         qtexture_t* getTexture() const { return m_pTexture; }
87         void setTexture(qtexture_t *pTex) { m_pTexture = pTex; }
88         // get shader name
89         const char* getName() const { return m_Name; }
90   bool IsDisplayed() const { return m_bDisplayed; }
91   void SetDisplayed(bool b) { m_bDisplayed = b; }
92   // setting in use also sets the display flag on
93         bool IsInUse() const { return m_bInUse; }
94         void SetInUse(bool b) { m_bInUse = b; if (m_pTexture) m_pTexture->inuse = true; if (b) m_bDisplayed = true; }
95   // get the shader flags
96   int getFlags() { return m_nFlags; }
97   // get the transparency value
98   float getTrans() { return m_fTrans; }
99         // test if it's a true shader, or a default shader created to wrap around a texture
100         bool IsDefault() { return m_ShaderFileName[0] == '\0'; }
101         // test if it's a plain color shader, i.e. a shader we use on plain color stuff (like info_playerstart)
102         bool IsColor() { return m_bColor; }
103         // get the related color then!
104         void getColor( vec3_t v) { VectorCopy( m_vColor, v); }
105   // get the alphaFunc
106   void getAlphaFunc(int *func, float *ref) { *func = m_nAlphaFunc; *ref = m_fAlphaRef; };
107   // get the cull type
108   int getCull() { return m_nCull; };
109         // get/set shader file name (ie the file where this one is defined)
110         const char* getShaderFileName() const { return m_ShaderFileName; }
111         // -----------------------------------------
112
113         // parse yourself!
114         bool Parse();
115
116         // search / load the texture to be used when displaying the shader
117         // after a successfull call to one of these the shader will get displayed in the tex wnd
118         // if m_strTextureName could not be loaded will set m_pTexture to NULL
119         void Try_Activate();
120         // if m_strTextureName could not be loaded will use a default qtexture
121   // FIXME TTimo: Activate forces activation, always true
122         bool Activate();
123         
124         // set shader name
125         void setName(const char* name) { strcpy(m_Name, name); }
126         void setShaderFileName(const char* name) { strcpy(m_ShaderFileName, name); }
127   // create a default shader for a given texture name
128   // will not activate!
129         // NOTE: CreateDefault expects a texture name reletive to the base path. Adding a "textures/" may be needed
130   void CreateDefault(const char* name);
131   const char* getTextureName() { return m_strTextureName; }
132
133         //++timo clean
134         // color stuff
135 //      void setColor( vec3_t c ) { VectorCopy( c, m_vColor ); m_bColor = true; }
136         // create a color shader
137         void CreateColor(const char* name);
138 };
139
140 // the classical CPtrArray with some enhancements
141 class CShaderArray : public CPtrArray
142 {
143 public:
144         CShaderArray() { }
145         virtual ~CShaderArray() { }
146   // look for a shader with a given name (may return NULL)
147         CShader* Shader_ForName( const char * ) const;
148   // look for a shader with a given texture name (may return NULL)
149   // NOTE: the texture name is supposed to fit qtexture_t naming conventions .. _DEBUG builds will check
150   CShader* Shader_ForTextureName( const char * ) const;
151         // will Add the given object if not already in
152         void AddSingle(void*);
153         // will copy / add another CShaderArray, and IncRef
154         void operator = (const class CShaderArray &);
155         // will empty the array, decreasing the refcount by 1
156         void ReleaseAll();
157   // will empty all shaders that match a given filename, decreasing the refcount by 1
158   void ReleaseForShaderFile( const char * );
159         // sort the array by shader name
160         void SortShaders();
161         // set the IsDisplayed flag for all shaders stored
162         void SetDisplayed(bool b);
163         // set the InUse flag for all shaders stored
164         void SetInUse(bool b);
165 };
166
167 #endif