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