Functional multiple texture mapping !

This commit is contained in:
Hugo Willaume 2019-03-20 16:15:44 +09:00
parent 2fbd67f9c6
commit 910ae8e5f0
14 changed files with 133 additions and 56 deletions

View File

@ -158,6 +158,7 @@
<ClCompile Include="MyGLWindow.cpp" /> <ClCompile Include="MyGLWindow.cpp" />
<ClCompile Include="Shader.cpp" /> <ClCompile Include="Shader.cpp" />
<ClCompile Include="Source.cpp" /> <ClCompile Include="Source.cpp" />
<ClCompile Include="Texture.cpp" />
<ClCompile Include="Viewer.cpp" /> <ClCompile Include="Viewer.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@ -179,6 +180,7 @@
<ClInclude Include="MyGLWindow.h" /> <ClInclude Include="MyGLWindow.h" />
<ClInclude Include="SceneContext.h" /> <ClInclude Include="SceneContext.h" />
<ClInclude Include="Shader.h" /> <ClInclude Include="Shader.h" />
<ClInclude Include="Texture.h" />
<ClInclude Include="Viewer.h" /> <ClInclude Include="Viewer.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@ -210,6 +212,7 @@
<ItemGroup> <ItemGroup>
<Image Include="brick1.jpg" /> <Image Include="brick1.jpg" />
<Image Include="earth.jpg" /> <Image Include="earth.jpg" />
<Image Include="moss.png" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">

View File

@ -63,6 +63,9 @@
<ClCompile Include="Dataset.cpp"> <ClCompile Include="Dataset.cpp">
<Filter>Models</Filter> <Filter>Models</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Texture.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="ModelView.h"> <ClInclude Include="ModelView.h">
@ -122,6 +125,9 @@
<ClInclude Include="imgui\stb_image.h"> <ClInclude Include="imgui\stb_image.h">
<Filter>imgui</Filter> <Filter>imgui</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Texture.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="simple.frag"> <None Include="simple.frag">
@ -168,5 +174,8 @@
<Image Include="earth.jpg"> <Image Include="earth.jpg">
<Filter>Resource Files</Filter> <Filter>Resource Files</Filter>
</Image> </Image>
<Image Include="moss.png">
<Filter>Resource Files</Filter>
</Image>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -4,7 +4,7 @@ void Dataset::sphere(float radius, GLuint slices, GLuint stacks)
{ {
clear(); clear();
// Generate vertexes and normals // Generate vertexes, normals and texture mapping
GLfloat theta, phi; GLfloat theta, phi;
GLfloat thetaFac = glm::two_pi<float>() / slices; GLfloat thetaFac = glm::two_pi<float>() / slices;
GLfloat phiFac = glm::pi<float>() / stacks; GLfloat phiFac = glm::pi<float>() / stacks;
@ -21,6 +21,8 @@ void Dataset::sphere(float radius, GLuint slices, GLuint stacks)
nz = cosf(phi); nz = cosf(phi);
vertices.emplace_back(radius * nx, radius * ny, radius * nz); vertices.emplace_back(radius * nx, radius * ny, radius * nz);
normals.emplace_back(nx, ny, nz); normals.emplace_back(nx, ny, nz);
tex_mapping.emplace_back(s, t);
idx++; idx++;
} }
} }
@ -65,18 +67,18 @@ void Dataset::simpleCube()
{ 1.0, 1.0, -1.0 },{ -1.0, 1.0, -1.0 },{ -1.0, 1.0, 1.0 } }; { 1.0, 1.0, -1.0 },{ -1.0, 1.0, -1.0 },{ -1.0, 1.0, 1.0 } };
tex_mapping = { tex_mapping = {
{ 0.0f, 0.0f },{ 1.0f, 0.0f },{ 1.0f, 1.0f }, // one triangle
{ 0.0f, 0.0f },{ 1.0f, 1.0f },{ 0.0f, 1.0f }, //the other triangle
{ 0.0f, 0.0f },{ 1.0f, 0.0f },{ 1.0f, 1.0f }, // one triangle { 0.0f, 0.0f },{ 1.0f, 0.0f },{ 1.0f, 1.0f }, // one triangle
{ 0.0f, 0.0f },{ 1.0f, 1.0f },{ 0.0f, 1.0f }, //the other triangle { 1.0f, 1.0f },{ 0.0f, 1.0f },{ 0.0f, 0.0f }, //the other triangle
{ 0.0f, 0.0f },{ 1.0f, 0.0f },{ 1.0f, 1.0f }, // one triangle { 0.0f, 0.0f },{ 1.0f, 0.0f },{ 1.0f, 1.0f }, // one triangle
{ 0.0f, 0.0f },{ 1.0f, 1.0f },{ 0.0f, 1.0f }, //the other triangle { 1.0f, 1.0f },{ 0.0f, 1.0f },{ 0.0f, 0.0f }, //the other triangle
{ 0.0f, 0.0f },{ 1.0f, 0.0f },{ 1.0f, 1.0f }, // one triangle { 0.0f, 0.0f },{ 1.0f, 0.0f },{ 1.0f, 1.0f }, // one triangle
{ 0.0f, 0.0f },{ 1.0f, 1.0f },{ 0.0f, 1.0f }, //the other triangle { 1.0f, 1.0f },{ 0.0f, 1.0f },{ 0.0f, 0.0f }, //the other triangle
{ 0.0f, 0.0f },{ 1.0f, 0.0f },{ 1.0f, 1.0f }, // one triangle { 0.0f, 0.0f },{ 1.0f, 0.0f },{ 1.0f, 1.0f }, // one triangle
{ 0.0f, 0.0f },{ 1.0f, 1.0f },{ 0.0f, 1.0f }, //the other triangle { 1.0f, 1.0f },{ 0.0f, 1.0f },{ 0.0f, 0.0f }, //the other triangle
{ 0.0f, 0.0f },{ 1.0f, 0.0f },{ 1.0f, 1.0f }, // one triangle { 0.0f, 0.0f },{ 1.0f, 0.0f },{ 1.0f, 1.0f }, // one triangle
{ 0.0f, 0.0f },{ 1.0f, 1.0f },{ 0.0f, 1.0f }, //the other triangle { 1.0f, 1.0f },{ 0.0f, 1.0f },{ 0.0f, 0.0f }, //the other triangle
{ 0.0f, 0.0f },{ 1.0f, 0.0f },{ 1.0f, 1.0f }, // one triangle
{ 1.0f, 1.0f },{ 0.0f, 1.0f },{ 0.0f, 0.0f }, //the other triangle
}; };
colors = { colors = {

View File

@ -226,9 +226,19 @@ Mesh::MeshEntry::~MeshEntry() {
/** /**
* Renders this MeshEntry * Renders this MeshEntry
**/ **/
void Mesh::MeshEntry::render(SceneContext ctx) { void Mesh::MeshEntry::render(SceneContext &ctx, Shader *shd) {
glEnable(GL_TEXTURE_2D);
for (GLuint i = 0; i < shd->textures.size(); i++)
{
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, shd->textures[i].tex_ref);
shd->addUniform("tex[" + std::to_string(i) + "]", (int)i);
}
if (shd->textures.size() > 0)
shd->addUniform("TexCount", (int)shd->textures.size());
glBindTexture(GL_TEXTURE_2D, ctx.textures);
glBindVertexArray(vao); glBindVertexArray(vao);
if (renderType == NO_INDEX) if (renderType == NO_INDEX)
{ {
@ -353,7 +363,7 @@ void Mesh::draw(SceneContext ctx) {
shader->setUniforms(ctx); shader->setUniforms(ctx);
meshEntries.at(i)->render(ctx); meshEntries.at(i)->render(ctx, shader);
model.glPopMatrix(); model.glPopMatrix();
} }
disableCulling(); disableCulling();

View File

@ -58,7 +58,7 @@ public:
MeshEntry(Dataset &set, Mesh *m); MeshEntry(Dataset &set, Mesh *m);
~MeshEntry(); ~MeshEntry();
Mesh * parent; Mesh * parent;
void render(SceneContext ctx); void render(SceneContext &ctx, Shader * shd);
}; };
public: public:

View File

@ -57,24 +57,14 @@ void MyGlWindow::setBgColor(float bgColor[3])
void MyGlWindow::textureSetup() void MyGlWindow::textureSetup()
{ {
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &_scnctx.textures); //tex_2d is a member variable
glBindTexture(GL_TEXTURE_2D, _scnctx.textures);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
int width, height, channel; _scnctx.textures.emplace("BrickTex", Texture("brick1.jpg", GL_RGB));
unsigned char * image = stbi_load("brick1.jpg", &width, &height, &channel, 0); _scnctx.textures.emplace("MossTex", Texture(std::string("moss.png"), GL_RGBA));
_scnctx.textures.emplace("EarthTex", Texture("earth.jpg", GL_RGB));
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(image);
glBindTexture(GL_TEXTURE_2D, 0);
} }
void MyGlWindow::shaderSetup() void MyGlWindow::shaderSetup()
@ -85,8 +75,12 @@ void MyGlWindow::shaderSetup()
shaders["BaseLight"] = new Shader("base_light.vert", "base_light.frag"); shaders["BaseLight"] = new Shader("base_light.vert", "base_light.frag");
shaders["Fog"] = new Shader("fog.vert", "fog.frag"); shaders["Fog"] = new Shader("fog.vert", "fog.frag");
shaders["TexBaseLight"] = new Shader("tex_base_light.vert", "tex_base_light.frag"); shaders["BrickBaseLight"] = new Shader("tex_base_light.vert", "tex_base_light.frag");
shaders["TexBaseLight"]->addUniform("tex1", 0); shaders["BrickBaseLight"]->assignTexture(_scnctx.textures["BrickTex"]);
shaders["BrickBaseLight"]->assignTexture(_scnctx.textures["MossTex"]);
shaders["EarthBaseLight"] = new Shader("tex_base_light.vert", "tex_base_light.frag");
shaders["EarthBaseLight"]->assignTexture(_scnctx.textures["EarthTex"]);
shaders["Silhouette"] = new Shader("silhouette.vert", "silhouette.frag"); shaders["Silhouette"] = new Shader("silhouette.vert", "silhouette.frag");
shaders["Silhouette"]->uniformFlags = ShaderFlags::MVP_FLAG; shaders["Silhouette"]->uniformFlags = ShaderFlags::MVP_FLAG;
@ -120,24 +114,24 @@ void MyGlWindow::setup()
Dataset moddata; Dataset moddata;
moddata.checkeredFloor(20, 20, glm::vec3(0.1, 0.1, 0.1), glm::vec3(0.7, 0.7, 0.7)); moddata.checkeredFloor(20, 20, glm::vec3(0.1, 0.1, 0.1), glm::vec3(0.7, 0.7, 0.7));
meshes.push_back(new Mesh(moddata, shaders["Simple"])); meshes.emplace("Floor", new Mesh(moddata, shaders["Simple"]));
meshes.back()->addTranslation(glm::vec4(0, -0.05, 0, 1)); meshes["Floor"]->addTranslation(glm::vec4(0, -0.05, 0, 1));
meshes.back()->cullMode = BACK; meshes["Floor"]->cullMode = BACK;
moddata.simpleCube(); moddata.simpleCube();
meshes.push_back(new Mesh(moddata, shaders["TexBaseLight"])); meshes.emplace("Cube", new Mesh(moddata, shaders["BrickBaseLight"]));
meshes.back()->addTranslation(glm::vec4(0, 1, 0, 1)); meshes["Cube"]->addTranslation(glm::vec4(0, 1, 0, 1));
moddata.sphere(1, 100, 100); moddata.sphere(1, 100, 100);
meshes.push_back(new Mesh(moddata, shaders["BaseLight"])); meshes.emplace("Sphere", new Mesh(moddata, shaders["EarthBaseLight"]));
meshes.back()->addTranslation(glm::vec4(-5, 1, -3, 1)); meshes["Sphere"]->addTranslation(glm::vec4(-5, 1, -3, 1));
meshes.push_back(new Mesh("teapot.obj", shaders["Silhouette"])); meshes.emplace("TeapotSilhouette", new Mesh("teapot.obj", shaders["Silhouette"]));
meshes.back()->addTranslation(glm::vec4(5, 0 ,3, 1)); meshes["TeapotSilhouette"]->addTranslation(glm::vec4(5, 0 ,3, 1));
meshes.back()->cullMode = FRONT; meshes["TeapotSilhouette"]->cullMode = FRONT;
meshes.push_back(new Mesh("teapot.obj", shaders["Toon"])); meshes.emplace("Teapot", new Mesh("teapot.obj", shaders["Toon"]));
meshes.back()->addTranslation(glm::vec4(5, 0, 3, 1)); meshes["Teapot"]->addTranslation(glm::vec4(5, 0, 3, 1));
meshes.back()->cullMode = BACK; meshes["Teapot"]->cullMode = BACK;
} }
@ -158,7 +152,7 @@ void MyGlWindow::draw()
_scnctx.viewMatrix = view; _scnctx.viewMatrix = view;
_scnctx.projectionMatrix = projection; _scnctx.projectionMatrix = projection;
for (auto it = meshes.begin(); it != meshes.end(); it++) for (auto it = meshes.begin(); it != meshes.end(); it++)
(*it)->draw(_scnctx); (*it).second->draw(_scnctx);
} }
void MyGlWindow::resize(int w, int h) void MyGlWindow::resize(int w, int h)

View File

@ -26,7 +26,7 @@ public:
void setBgColor(float bgColor[3]); void setBgColor(float bgColor[3]);
std::map<std::string, Shader *> shaders; std::map<std::string, Shader *> shaders;
std::vector<Mesh *> meshes; std::map<std::string, Mesh *> meshes;
void resize(int w, int h); void resize(int w, int h);
Viewer viewer; Viewer viewer;

View File

@ -2,6 +2,7 @@
#include <map> #include <map>
#include <glm/mat3x3.hpp> #include <glm/mat3x3.hpp>
#include <glm/mat4x4.hpp> #include <glm/mat4x4.hpp>
#include "Texture.h"
struct SceneContext struct SceneContext
@ -16,5 +17,5 @@ struct SceneContext
glm::mat4x4 modelViewMatrix; glm::mat4x4 modelViewMatrix;
glm::mat3x3 normalMatrix; glm::mat3x3 normalMatrix;
GLuint textures; std::map<std::string, Texture> textures;
}; };

View File

@ -6,6 +6,7 @@
#include <functional> #include <functional>
#include "glm/gtc/type_ptr.hpp" #include "glm/gtc/type_ptr.hpp"
#include "Texture.h"
#include "Material.h" #include "Material.h"
#include "Light.h" #include "Light.h"
#include "SceneContext.h" #include "SceneContext.h"
@ -31,6 +32,7 @@ public:
Shader(const std::string vtx_name, const std::string frag_name); Shader(const std::string vtx_name, const std::string frag_name);
~Shader(); ~Shader();
std::vector<Texture> textures;
int uniformFlags = KD_FLAG | KA_FLAG | KS_FLAG | SHINE_FLAG | MVP_FLAG | NORMAL_MAT_FLAG | MODELVIEW_FLAG | LIGHTS_FLAG; int uniformFlags = KD_FLAG | KA_FLAG | KS_FLAG | SHINE_FLAG | MVP_FLAG | NORMAL_MAT_FLAG | MODELVIEW_FLAG | LIGHTS_FLAG;
Material mat; Material mat;
@ -42,15 +44,20 @@ public:
void setUniforms(SceneContext ctx); void setUniforms(SceneContext ctx);
void addUniform(const std::string name, glm::vec3 data) void addUniform(const std::string name, glm::vec3 data)
{ {
_program.use(); glUniform3fv(_program.addUniform(name), 1, glm::value_ptr(data)); ; _program.disable(); _program.use(); glUniform3fv(_program.addUniform(name), 1, glm::value_ptr(data));
} }
void addUniform(const std::string name, int data) void addUniform(const std::string name, int data)
{ {
_program.use(); glUniform1i(_program.addUniform(name), data); _program.disable(); _program.use(); glUniform1i(_program.addUniform(name), data);
} }
void addUniform(const std::string name, float data) void addUniform(const std::string name, float data)
{ {
_program.use(); glUniform1fv(_program.addUniform(name), 1, &data); _program.disable(); _program.use(); glUniform1fv(_program.addUniform(name), 1, &data);
}
void assignTexture(Texture &texture)
{
textures.emplace_back(texture);
} }
private: private:

28
BaseGLProject/Texture.cpp Normal file
View File

@ -0,0 +1,28 @@
#include "Texture.h"
Texture::Texture(std::string file_name, GLuint enum_rgb)
{
int width, height, channel;
unsigned char * image;
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &tex_ref);
glBindTexture(GL_TEXTURE_2D, tex_ref);
image = stbi_load(file_name.c_str(), &width, &height, &channel, 0);
glTexImage2D(GL_TEXTURE_2D, 0, enum_rgb, width, height, 0, enum_rgb,
GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(image);
glBindTexture(GL_TEXTURE_2D, 0);
}
Texture::Texture(const Texture &other)
{
tex_ref = other.tex_ref;
}
Texture::~Texture()
{
}

19
BaseGLProject/Texture.h Normal file
View File

@ -0,0 +1,19 @@
#pragma once
#include <GL/glew.h>
#include <string>
#include <map>
#include "imgui/stb_image.h"
class Texture
{
public:
GLuint tex_ref;
Texture(std::string file_name, GLuint enum_rgb);
Texture(const Texture &other);
Texture() {};
~Texture();
void sendUniform(GLuint uni);
};

View File

@ -20,9 +20,6 @@ uniform int LightCount;
in vec3 fNormal; in vec3 fNormal;
in vec3 pos; in vec3 pos;
in vec2 texCoord;
uniform sampler2D tex1;
void main() void main()
{ {
@ -46,7 +43,5 @@ void main()
specular_sum += specular; specular_sum += specular;
} }
FragColors = (vec4(diffuse_sum + ambient, 1) + vec4(specular_sum, 1.0));
vec4 texColor = texture(tex1, texCoord);
FragColors = (vec4(diffuse_sum + ambient, 1) * texColor + vec4(specular_sum, 1.0));
} }

BIN
BaseGLProject/moss.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 MiB

View File

@ -21,7 +21,8 @@ in vec3 fNormal;
in vec3 pos; in vec3 pos;
in vec2 texCoord; in vec2 texCoord;
uniform sampler2D tex1; uniform int TexCount;
uniform sampler2D tex[32];
void main() void main()
{ {
@ -46,6 +47,14 @@ void main()
specular_sum += specular; specular_sum += specular;
} }
vec4 texColor = texture(tex1, texCoord); vec4 texColor = texture(tex[0], texCoord);
FragColors = (vec4(diffuse_sum + ambient, 1) * texColor + vec4(specular_sum, 1.0));
for (int i=1; i < TexCount; i++)
{
vec4 new_tex = texture(tex[i], texCoord);
texColor = mix(new_tex, texColor, new_tex.a);
}
FragColors = (vec4(diffuse_sum + ambient, 1) * texColor + vec4(specular_sum, 1.0));
} }