diff --git a/.vs/shader/v14/.suo b/.vs/shader/v14/.suo index 64e3f10..90a2641 100644 Binary files a/.vs/shader/v14/.suo and b/.vs/shader/v14/.suo differ diff --git a/Res/test.fs b/Res/test.fs index 6e4f27d..b4ee29d 100644 --- a/Res/test.fs +++ b/Res/test.fs @@ -2,9 +2,10 @@ precision mediump float; #endif uniform sampler2D U_Texture; +uniform sampler2D U_Texture2; varying vec4 V_Color; varying vec2 V_Texcoord; void main() { - gl_FragColor=V_Color*texture2D(U_Texture,V_Texcoord); + gl_FragColor=V_Color*texture2D(U_Texture,V_Texcoord)*texture2D(U_Texture2,V_Texcoord); } \ No newline at end of file diff --git a/Res/test2.bmp b/Res/test2.bmp new file mode 100644 index 0000000..90b5dee Binary files /dev/null and b/Res/test2.bmp differ diff --git a/scene.cpp b/scene.cpp index cd7dfa1..0eb3444 100644 --- a/scene.cpp +++ b/scene.cpp @@ -3,7 +3,6 @@ #include "utils.h" #include "ground.h" -GLuint texture; glm::mat4 modelMatrix, viewMatrix, projectionMatrix; Ground ground; GLint textureLocation; @@ -30,13 +29,11 @@ void Init() { /* ³υΚΌ»― shader */ shader = new Shader; shader->Init("Res/test.vs", "Res/test.fs"); - - textureLocation = glGetUniformLocation(shader->mProgram, "U_Texture"); + shader->SetTexture("U_Texture", "Res/test.bmp"); + shader->SetTexture("U_Texture2", "Res/test2.bmp"); modelMatrix = glm::translate(0.0f, 0.0f, -0.6f); - texture = CreateTexture2DFromBMP("Res/test.bmp"); - ground.Init(); } void SetViewPortSize(float width, float height) { @@ -51,8 +48,6 @@ void Draw() { vertexbuffer->Bind(); shader->Bind(glm::value_ptr(modelMatrix), glm::value_ptr(viewMatrix), glm::value_ptr(projectionMatrix)); - glBindTexture(GL_TEXTURE_2D, texture); - glUniform1i(textureLocation, 0); glDrawArrays(GL_TRIANGLES, 0, 3); vertexbuffer->Unbind(); } \ No newline at end of file diff --git a/shader.cpp b/shader.cpp index 3b22e76..7db3232 100644 --- a/shader.cpp +++ b/shader.cpp @@ -36,6 +36,13 @@ void Shader::Bind(float *M, float *V, float*P) { glUniformMatrix4fv(mViewMatrixLocation, 1, GL_FALSE, V); glUniformMatrix4fv(mProjectionMatrixLocation, 1, GL_FALSE, P); + int iIndex = 0; + for (auto iter = mUniformTextures.begin(); iter != mUniformTextures.end(); ++iter) { + glActiveTexture(GL_TEXTURE0 + iIndex); + glBindTexture(GL_TEXTURE_2D, iter->second->mTexture); + glUniform1i(iter->second->mLocation, iIndex++); + } + glEnableVertexAttribArray(mPositionLocation); glVertexAttribPointer(mPositionLocation, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0); @@ -47,4 +54,20 @@ void Shader::Bind(float *M, float *V, float*P) { glEnableVertexAttribArray(mNormalLocation); glVertexAttribPointer(mNormalLocation, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(sizeof(float) * 12)); +} +void Shader::SetTexture(const char * name, const char*imagePath) { + auto iter = mUniformTextures.find(name); + if (iter == mUniformTextures.end()) { + GLint location = glGetUniformLocation(mProgram, name); + if (location != -1) { + UniformTexture* t = new UniformTexture; + t->mLocation = location; + t->mTexture = CreateTexture2DFromBMP(imagePath); + mUniformTextures.insert(std::pair(name, t)); + } + } + else { + glDeleteTextures(1, &iter->second->mTexture); + iter->second->mTexture = CreateTexture2DFromBMP(imagePath); + } } \ No newline at end of file diff --git a/shader.h b/shader.h index c4e145c..1775678 100644 --- a/shader.h +++ b/shader.h @@ -1,10 +1,20 @@ #pragma once #include "ggl.h" +struct UniformTexture { + GLint mLocation; + GLuint mTexture; + UniformTexture() { + mLocation = -1; + mTexture = 0; + } +}; class Shader { public: GLuint mProgram; + std::map mUniformTextures; 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); }; \ No newline at end of file