diff --git a/.vs/shader/v14/.suo b/.vs/shader/v14/.suo index 90a2641..d2da945 100644 Binary files a/.vs/shader/v14/.suo and b/.vs/shader/v14/.suo differ diff --git a/Res/model.fs b/Res/model.fs index 2f90d25..ff9f42b 100644 --- a/Res/model.fs +++ b/Res/model.fs @@ -1,8 +1,23 @@ #ifdef GL_ES precision mediump float; #endif +uniform vec4 U_LightPos; +uniform vec4 U_LightAmbient; +uniform vec4 U_LightDiffuse; +uniform vec4 U_AmbientMaterial; +uniform vec4 U_DiffuseMaterial; varying vec4 V_Color; +varying vec4 V_Normal; void main() { - gl_FragColor=V_Color; + vec4 color=vec4(0.0,0.0,0.0,0.0); + vec4 ambientColor=U_LightAmbient*U_AmbientMaterial; + vec3 lightPos=U_LightPos.xyz; + vec3 L=lightPos; + L=normalize(L); + vec3 n=normalize(V_Normal.xyz); + float diffuseIntensity=max(0.0,dot(L,n)); + vec4 diffuseColor=U_LightDiffuse*U_DiffuseMaterial*diffuseIntensity; + color=ambientColor+diffuseColor; + gl_FragColor=color; } \ No newline at end of file diff --git a/Res/model.vs b/Res/model.vs index 1b33dcf..e0c9c76 100644 --- a/Res/model.vs +++ b/Res/model.vs @@ -1,12 +1,16 @@ attribute vec4 position; attribute vec4 color; -attribute vec2 texcoord; +attribute vec4 texcoord; +attribute vec4 normal; uniform mat4 ModelMatrix; uniform mat4 ViewMatrix; uniform mat4 ProjectionMatrix; +uniform mat4 IT_ModelMatrix; varying vec4 V_Color; +varying vec4 V_Normal; void main() { V_Color=color; + V_Normal=IT_ModelMatrix*normal; gl_Position=ProjectionMatrix*ViewMatrix*ModelMatrix*position; } \ No newline at end of file diff --git a/model.cpp b/model.cpp index ef28a4a..3f8d0cd 100644 --- a/model.cpp +++ b/model.cpp @@ -102,6 +102,11 @@ void Model::Init(const char*modelPath) { mShader = new Shader; mShader->Init("Res/model.vs", "Res/model.fs"); + mShader->SetVec4("U_LightPos", 0.0f, 1.0f, 1.0f, 0.0f);//设置光源位置 + mShader->SetVec4("U_LightAmbient", 1.0f, 1.0f, 1.0f, 1.0f);//设置环境光分量 + mShader->SetVec4("U_LightDiffuse", 1.0f, 1.0f, 1.0f, 1.0f);//设置慢反射光分量 + mShader->SetVec4("U_AmbientMaterial", 0.1f, 0.1f, 0.1f, 1.0f);//设置环境光材质分量 + mShader->SetVec4("U_DiffuseMaterial", 0.6f, 0.6f, 0.6f, 1.0f);//设置漫反射材质分量 } void Model::Draw(glm::mat4 & viewMatrix, glm::mat4 projectionMatrix) { diff --git a/shader.VC.db b/shader.VC.db index 9f9e9a6..9c2b5ca 100644 Binary files a/shader.VC.db and b/shader.VC.db differ diff --git a/shader.cpp b/shader.cpp index 7db3232..3615b43 100644 --- a/shader.cpp +++ b/shader.cpp @@ -70,4 +70,25 @@ void Shader::SetTexture(const char * name, const char*imagePath) { glDeleteTextures(1, &iter->second->mTexture); iter->second->mTexture = CreateTexture2DFromBMP(imagePath); } +} +void Shader::SetVec4(const char * name, float x, float y, float z, float w) { + auto iter = mUniformVec4s.find(name); + if (iter == mUniformVec4s.end()) { + GLint location = glGetUniformLocation(mProgram, name); + if (location != -1) { + UniformVector4f*v = new UniformVector4f; + v->v[0] = x; + v->v[1] = y; + v->v[2] = z; + v->v[3] = w; + v->mLocation = location; + mUniformVec4s.insert(std::pair(name, v)); + } + } + else { + iter->second->v[0] = x; + iter->second->v[1] = y; + iter->second->v[2] = z; + iter->second->v[3] = w; + } } \ No newline at end of file diff --git a/shader.h b/shader.h index 1775678..8358e5d 100644 --- a/shader.h +++ b/shader.h @@ -8,13 +8,23 @@ struct UniformTexture { mTexture = 0; } }; +struct UniformVector4f { + GLint mLocation; + float v[4]; + UniformVector4f() { + mLocation = -1; + memset(v, 0, sizeof(float) * 4); + } +}; class Shader { public: GLuint mProgram; std::map mUniformTextures; + std::map mUniformVec4s; GLint mModelMatrixLocation, mViewMatrixLocation, mProjectionMatrixLocation; GLint mPositionLocation, mColorLocation, mTexcoordLocation, mNormalLocation; void Init(const char*vs, const char*fs); void Bind(float *M, float *V, float*P); void SetTexture(const char * name, const char*imagePath); + void SetVec4(const char * name, float x, float y, float z, float w); }; \ No newline at end of file diff --git a/shader.vcxproj b/shader.vcxproj index efd4f70..edf2ade 100644 --- a/shader.vcxproj +++ b/shader.vcxproj @@ -163,6 +163,8 @@ + + diff --git a/shader.vcxproj.filters b/shader.vcxproj.filters index 55722d1..f8a7d60 100644 --- a/shader.vcxproj.filters +++ b/shader.vcxproj.filters @@ -73,5 +73,11 @@ 璧勬簮鏂囦欢 + + 璧勬簮鏂囦欢 + + + 璧勬簮鏂囦欢 + \ No newline at end of file