这几天忙着做项目 引擎进度缓慢
先上封转的GLSL吧(这个是从网上找的一段代码修改的)
如下:
1 ////////////////////////////////////////////////////////////
2 /// GL着色器基类
3 ////////////////////////////////////////////////////////////
4 class GLSLShader : public Object
5 {
6 public:
7 ////////////////////////////////////////////////////////
8 /// 构造,析构GL着色器
9 ////////////////////////////////////////////////////////
10 GLSLShader();
11 virtual ~GLSLShader();
12
13 ////////////////////////////////////////////////////////
14 /// 着色器编译,检测是否编译
15 ////////////////////////////////////////////////////////
16 virtual bool Compile() = 0;
17 virtual bool IsCompiled() const = 0;
18
19 ////////////////////////////////////////////////////////
20 /// 获取着色器日志
21 ////////////////////////////////////////////////////////
22 virtual void GetShaderLog(engine_string &log) const = 0;
23
24 ////////////////////////////////////////////////////////
25 /// 获取,设置着色器数据
26 ////////////////////////////////////////////////////////
27 virtual void GetShaderSource(engine_string &shader) const = 0;
28 virtual void SetShaderSource(engine_string &code) = 0;
29
30 ////////////////////////////////////////////////////////
31 /// 获取着色器句柄(标号)
32 ////////////////////////////////////////////////////////
33 virtual GLuint GetHandle() const = 0;
34
35 ////////////////////////////////////////////////////////
36 /// 获取着色器参数
37 ////////////////////////////////////////////////////////
38 virtual void GetParameter(GLenum param, GLint *data) const = 0;
39 private:
40 DECLARE_OBJECT(GLSLShader)
41 };
42
1 ////////////////////////////////////////////////////////////
2 /// 定义引擎GLSL着色程序
3 ////////////////////////////////////////////////////////////
4 class GLSLProgram : public Object
5 {
6 public:
7 ////////////////////////////////////////////////////////
8 /// 构造,析构GL着色程序
9 ////////////////////////////////////////////////////////
10 GLSLProgram();
11 virtual ~GLSLProgram();
12
13 ////////////////////////////////////////////////////////
14 /// 链接,卸载着色程序
15 ////////////////////////////////////////////////////////
16 virtual void Attach(const RefPtr<GLSLShader> &shader) = 0;
17 virtual void Detach(const RefPtr<GLSLShader> &shader) = 0;
18
19 ////////////////////////////////////////////////////////
20 /// 链接着色程序
21 ////////////////////////////////////////////////////////
22 virtual bool Link() = 0;
23
24 ////////////////////////////////////////////////////////
25 /// 启用,禁止着色程序
26 ////////////////////////////////////////////////////////
27 virtual void Begin() const = 0;
28 virtual void End() const = 0;
29
30 ////////////////////////////////////////////////////////
31 /// 设置着色程序参数
32 ////////////////////////////////////////////////////////
33 virtual void SendUniform(const engine_string &name, float x) = 0;
34 virtual void SendUniform(const engine_string &name, float x, float y) = 0;
35 virtual void SendUniform(const engine_string &name, float x, float y, float z) = 0;
36 virtual void SendUniform(const engine_string &name, float x, float y, float z, float w) = 0;
37 virtual void SendUniform(const engine_string &name, int x) = 0;
38 virtual void SendUniform(const engine_string &name, int x, int y) = 0;
39 virtual void SendUniform(const engine_string &name, int x, int y, int z) = 0;
40 virtual void SendUniform(const engine_string &name, int x, int y, int z, int w) = 0;
41 virtual void SendUniform(const engine_string &name, float *m, bool transp = false, int size = 4) = 0;
42
43 ////////////////////////////////////////////////////////
44 /// 设置指定位置的着色程序参数
45 ////////////////////////////////////////////////////////
46 virtual void SendUniform(GLuint location, float x) = 0;
47 virtual void SendUniform(GLuint location, float x, float y) = 0;
48 virtual void SendUniform(GLuint location, float x, float y, float z) = 0;
49 virtual void SendUniform(GLuint location, float x, float y, float z, float w) = 0;
50 virtual void SendUniform(GLuint location, int x) = 0;
51 virtual void SendUniform(GLuint location, int x, int y) = 0;
52 virtual void SendUniform(GLuint location, int x, int y, int z) = 0;
53 virtual void SendUniform(GLuint location, int x, int y, int z, int w) = 0;
54 virtual void SendUniform(GLuint location, float *m, bool transp = false, int size = 4) = 0;
55
56 ////////////////////////////////////////////////////////
57 /// 设置给定索引的顶点属性参数
58 ////////////////////////////////////////////////////////
59 virtual void SendVertexAttrib(GLuint index, int x) = 0;
60 virtual void SendVertexAttrib(GLuint index, int x, int y) = 0;
61 virtual void SendVertexAttrib(GLuint index, int x, int y, int z) = 0;
62 virtual void SendVertexAttrib(GLuint index, int x, int y, int z, int w) = 0;
63
64 virtual void SendVertexAttrib(GLuint index, float x) = 0;
65 virtual void SendVertexAttrib(GLuint index, float x, float y) = 0;
66 virtual void SendVertexAttrib(GLuint index, float x, float y, float z) = 0;
67 virtual void SendVertexAttrib(GLuint index, float x, float y, float z, float w) = 0;
68
69 ////////////////////////////////////////////////////////
70 /// 获取指定参数的位置
71 ////////////////////////////////////////////////////////
72 virtual GLuint GetUniformLocation(const engine_string &name) const = 0;
73 virtual void SetAttributeLocation(const engine_string &name, GLuint location) = 0;
74 virtual GLuint GetAttributeLocation(const engine_string &name) const = 0;
75
76 ////////////////////////////////////////////////////////
77 /// 获取程序日志
78 ////////////////////////////////////////////////////////
79 virtual void GetProgramLog(engine_string &log) const = 0;
80
81 ////////////////////////////////////////////////////////
82 /// 获取着色程序句柄
83 ////////////////////////////////////////////////////////
84 virtual GLuint GetHandle() const = 0;
85
86 ////////////////////////////////////////////////////////
87 /// 获取着色器参数
88 ////////////////////////////////////////////////////////
89 virtual void GetParameter(GLenum param, GLint *data) const = 0;
90
91 ////////////////////////////////////////////////////////
92 /// 着色器合法性检查
93 ////////////////////////////////////////////////////////
94 virtual bool IsValid() const = 0;
95 virtual bool IsValidProgram() const = 0;
96 virtual void GetAttributeInfo(GLuint location, engine_string &name, GLenum &type, GLint &size) const = 0;
97 virtual void GetUniformInfo(GLuint location, engine_string &name, GLenum &datatype, GLint &size) const = 0;
98 virtual void GetAttachedShaders(std::vector<GLuint> &shaderhandles) = 0;
99 virtual void GetAttachedShaders(std::vector<GLSLShader> &shaders) = 0;
100 private:
101 DECLARE_OBJECT(GLSLProgram)
102 };
这个没什么说的
如何获取其指针?
从这里!
1 ///////////////////////////////////////////////////////
2 /// 定义引擎渲染者对象O(∩_∩)O~
3 ///////////////////////////////////////////////////////
4 class G_DLL_API Renderer : public Object
5 {
6 public:
7 ///////////////////////////////////////////////////////
8 /// 构造,析构可渲染对象
9 ///////////////////////////////////////////////////////
10 Renderer();
11 virtual ~Renderer();
12 public:
13 ///////////////////////////////////////////////////////
14 /// 获取着色器程序指针
15 ///////////////////////////////////////////////////////
16 virtual RefPtr<GLSLProgram> CreateGLSLProgram(const engine_string& vertex,const engine_string& frag) = 0;
17
然后是使用的一段代码(感觉越写越复杂了)
1 //! 获取材质管理器
2 core::RefPtr<core::MaterialManager> materialmanager = resourcemanager->GetMaterialManager();
3 //! 获取一个材质指针(我们从脚本中载入材质数据O(∩_∩)O~)
4 core::RefPtr<core::Material> material = materialmanager->CreateObject("material","..\\script//move_light_material.lua","material");
5 //! 材质渲染
6 material->Render();
7
8 //! 获取光管理器指针
9 core::RefPtr<core::LightManager> lightmanager = resourcemanager->GetLightManager();
10 //! 启用光源
11 lightmanager->EnableLight();
12
13 //! 获取光模型指针
14 lightmodel = lightmanager->GetLightModel();
15 lightmodel->SetLocalViewer(false);
16 lightmodel->SetAmbientColor(core::Color(0.4f,0.4f,1.0f,1.0f));
17 lightmodel->Render();
18
19 light[0] = lightmanager->CreateObject("light1","..\\script//move_light_light.lua","light0");
20 light[1] = lightmanager->CreateObject("light2","..\\script//move_light_light.lua","light1");
21 light[2] = lightmanager->CreateObject("light3","..\\script//move_light_light.lua","light2");
22
23 //! 获取渲染器
24 core::RefPtr<core::Renderer> renderer = device->GetRenderer();
25
26 //获取GLSL着色指针
27 core::RefPtr<core::GLSLProgram> glsl1 = renderer->CreateGLSLProgram("..\\shader//multilight//pervertex1.vert","..\\shader//multilight//pervertex1.frag");
28 core::RefPtr<core::GLSLProgram> glsl2 = renderer->CreateGLSLProgram("..\\shader//multilight//perpixel1.vert","..\\shader//multilight//perpixel1.frag");
29 core::RefPtr<core::GLSLProgram> glsl3 = renderer->CreateGLSLProgram("..\\shader//multilight//pervertex3.vert","..\\shader//multilight//pervertex3.frag");
30 core::RefPtr<core::GLSLProgram> glsl4 = renderer->CreateGLSLProgram("..\\shader//multilight//perpixel3.vert","..\\shader//multilight//perpixel3.frag");
31 core::RefPtr<core::GLSLProgram> glsl5 = renderer->CreateGLSLProgram("..\\shader//multilight//deferredfirst.vert","..\\shader//multilight//deferredfirst.frag");
32 core::RefPtr<core::GLSLProgram> glsl6 = renderer->CreateGLSLProgram("..\\shader//multilight//deferredsecond.vert","..\\shader//multilight//deferredsecond.frag");
33
34 glsl6->Begin();
35 glsl6->SendUniform("Tex0",0);
36 glsl6->SendUniform("Tex1",1);
37 glsl6->SendUniform("Tex2",2);
38 glsl6->End();
39
40 core::RefPtr<core::Image> wallimage = imagemanager->CreateObject("wall","..\\image//wall.bmp");
41 core::RefPtr<core::Image> cubeimage = imagemanager->CreateObject("cube","..\\image//logo.bmp");
42 core::RefPtr<core::Texture> wall = texturemanager->CreateObject("wall",wallimage);
43 core::RefPtr<core::Texture> cube = texturemanager->CreateObject("cubel",cubeimage);
44 wall->Generate();
45 cube->Generate();
46
47 texid[0] = wall->GetTextureId();
48 texid[1] = cube->GetTextureId();
49
50 //! 获取摄像机指针
51 core::RefPtr<core::Camera> camera = scenemanager->GetCameraManager()->CreateCamera("camera",
52 Vector3f(500,35,400),
53 Vector3f(504,35,400));
54 camera->SetViewPort(Recti(0,0,640,480));
55 camera->SetPerspective(45.0f,640.0f/480.0f,0.1f,100.0f);
56
57 BEGIN_LOOP(device)
58 videodriver->SetClearBuffer(ENGINE_CLEAR_COLOR | ENGINE_CLEAR_DEPTH);
59
60 if(input->IsPressedKey(KEY_M) == true)
61 movelights = !movelights;
62 if(input->IsPressedKey(KEY_SPACE) == true)
63 move = !move;
64 if(input->IsPressedKey(KEY_R) == true)
65 rotation = !rotation;
66 if(input->IsPressedKey(KEY_B) == true)
67 bg = !bg;
68 if(input->IsPressedKey(KEY_KP_0) == true)
69 {
70 movz = 0.0f;
71 roty = 0.0f;
72 movl = 0.0f;
73 }
74 if(input->IsPressedKey(KEY_U) == true)
75 {
76 renderer->DisableGLSL();
77 lightmanager->DisableLight();
78 rendermode = 0;
79 }
80 if(input->IsPressedKey(KEY_H) == true)
81 {
82 renderer->DisableGLSL();
83 lightmanager->EnableLight();
84 rendermode = 0;
85 }
86 if(input->IsPressedKey(KEY_K) == true)
87 {
88 renderer->DisableGLSL();
89 lightmanager->EnableLight();
90 multish = 0;
91 rendermode = 1;
92 }
93 if(input->IsPressedKey(KEY_S) == true)
94 {
95 multish = glsl1->GetHandle();
96 rendermode = 1;
97 }
98 if(input->IsPressedKey(KEY_T) == true)
99 {
100 multish = glsl2->GetHandle();
101 rendermode = 1;
102 }
103 if(input->IsPressedKey(KEY_L) == true)
104 {
105 glsl3->Begin();
106 rendermode = 0;
107 }
108 if(input->IsPressedKey(KEY_O) == true)
109 {
110 glsl4->Begin();
111 rendermode = 0;
112 }
113
114 interval = 16;
115
116 if (move)
117 movz += 0.0025 * interval;
118 if (rotation)
119 roty += 0.1 * interval;
120 if (movelights)
121 movl += 0.0025 * interval;
122
123 DrawLight(light,movl);
124
125 videodriver->SetClearBuffer(ENGINE_CLEAR_COLOR | ENGINE_CLEAR_DEPTH);
126 glLoadIdentity();
127 videodriver->SetPilotView(0,2,10,0,0,-15);
128 switch(rendermode)
129 {
130 case 0:
131 DrawScene(bg,texid[0],texid[1],roty,movz);
132 break;
133 case 1:
134 if (!multish)
135 {
136 for (i = 1; i < NUM_LIGHTS; i++)
137 light[i]->SetVisible(false);
138 }
139
140 glUseProgram(multish);
141 for (i = 0; i < NUM_LIGHTS; i++)
142 {
143 if (i > 0)
144 {
145 lightmodel->Render();
146 videodriver->SetBlending(true);
147 }
148 light[i]->Render();
149 DrawScene(bg,texid[0],texid[1],roty,movz);
150 }
151
152 videodriver->SetBlending(false);
153
154 if (!multish)
155 {
156 for (i = 1; i < NUM_LIGHTS; i++)
157 {light[i]->SetVisible(true);light[i]->Render();}
158 }
159 break;
160 }
161
162 DrawLights(light);
163
164 END_LOOP(device)
165
下面是几张对应的图片
其三:
其四:
第五个:
盖莫游戏引擎2.1.2估计会在6月份中旬发布
至于2.1.3版本将不再发布
因为打算直接发布2.1.4版本(届时将根据需要对引擎框架做大量修改和完成)