Starting texmapping

This commit is contained in:
Hugo Willaume 2019-03-18 15:20:28 +09:00
parent babd5c06fd
commit 909fec23f1
16 changed files with 7659 additions and 27 deletions

View File

@ -173,6 +173,7 @@
<ClInclude Include="imgui\imstb_rectpack.h" />
<ClInclude Include="imgui\imstb_textedit.h" />
<ClInclude Include="imgui\imstb_truetype.h" />
<ClInclude Include="imgui\stb_image.h" />
<ClInclude Include="Light.h" />
<ClInclude Include="Loader.h" />
<ClInclude Include="Material.h" />

View File

@ -128,6 +128,9 @@
<ClInclude Include="Material.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="imgui\stb_image.h">
<Filter>imgui</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="simple.frag">

View File

@ -9,6 +9,7 @@ public:
std::vector<glm::vec3> vertices;
std::vector<glm::vec3> normals;
std::vector<glm::vec3> colors;
std::vector<glm::vec2> tex_mapping;
void clear()
{
@ -31,6 +32,21 @@ public:
{ -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 = {
{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, 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, 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, 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, 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, 1.0f}, {0.0f, 1.0f}, //the other triangle
};
colors = {
{1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0},
{0.0, 0.0, 1.0}, {1.0, 1.0, 1.0}, {1.0, 0.0, 0.0},
@ -53,8 +69,10 @@ public:
{
vertices = {{-10.0, 0.0, -10.0}, {-10.0, 0.0, 10.0}, {10.0, 0.0, -10.0},
{10.0, 0.0, 10.0}, {10.0, 0.0, -10.0}, {-10.0, 0.0, 10.0}};
colors = { {0.7, 0.7, 0.7}, {0.7, 0.7, 0.7}, {0.7, 0.7, 0.7},
{0.7, 0.7, 0.7}, {0.7, 0.7, 0.7}, {0.7, 0.7, 0.7} };
genEmptyTexture();
genNormals();
}
@ -86,10 +104,18 @@ public:
}
color *= -1;
}
genEmptyTexture();
genNormals();
}
private:
void genEmptyTexture()
{
tex_mapping.clear();
for (auto vert : vertices)
for (int i = 0; i < 6; i++)
tex_mapping.push_back(glm::vec2(0, 0));
}
void genNormals()
{

View File

@ -24,8 +24,9 @@ Mesh::MeshEntry::MeshEntry(Dataset &set, Mesh *m)
parent = m;
vbo[VERTEX_BUFFER] = NULL;
vbo[TEXCOORD_BUFFER] = NULL;
vbo[NORMAL_BUFFER] = NULL;
vbo[TEXCOORD_BUFFER] = NULL;
vbo[COLOR_BUFFER] = NULL;
vbo[INDEX_BUFFER] = NULL;
glGenVertexArrays(1, &vao);
@ -49,16 +50,23 @@ Mesh::MeshEntry::MeshEntry(Dataset &set, Mesh *m)
glVertexAttribPointer(NORMAL_BUFFER, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 3, NULL);
glEnableVertexAttribArray(NORMAL_BUFFER);
//Copy vertice color to VBO
//Copy texture mapping to VBO
glGenBuffers(1, &vbo[TEXCOORD_BUFFER]);
glBindBuffer(GL_ARRAY_BUFFER, vbo[TEXCOORD_BUFFER]);
glBufferData(GL_ARRAY_BUFFER, set.colors.size() * 3 * sizeof(GLfloat), set.colors.data(), GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, 2 * set.tex_mapping.size() * sizeof(GLfloat), set.tex_mapping.data(), GL_STATIC_DRAW);
glVertexAttribPointer(TEXCOORD_BUFFER, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 3, NULL);
glVertexAttribPointer(TEXCOORD_BUFFER, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 2, NULL);
glEnableVertexAttribArray(TEXCOORD_BUFFER);
//Copy vertice color to VBO
glGenBuffers(1, &vbo[COLOR_BUFFER]);
glBindBuffer(GL_ARRAY_BUFFER, vbo[COLOR_BUFFER]);
glBufferData(GL_ARRAY_BUFFER, set.colors.size() * 3 * sizeof(GLfloat), set.colors.data(), GL_STATIC_DRAW);
glVertexAttribPointer(COLOR_BUFFER, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 3, NULL);
glEnableVertexAttribArray(COLOR_BUFFER);
dset_size = set.vertices.size();
glBindBuffer(GL_ARRAY_BUFFER, 0);
@ -75,6 +83,7 @@ Mesh::MeshEntry::MeshEntry(aiMesh *mesh, const aiScene* scene, Mesh * m)
vbo[VERTEX_BUFFER] = NULL;
vbo[TEXCOORD_BUFFER] = NULL;
vbo[NORMAL_BUFFER] = NULL;
vbo[COLOR_BUFFER] = NULL;
vbo[INDEX_BUFFER] = NULL;
glGenVertexArrays(1, &vao);
@ -189,6 +198,10 @@ Mesh::MeshEntry::~MeshEntry() {
glDeleteBuffers(1, &vbo[INDEX_BUFFER]);
}
if (vbo[COLOR_BUFFER]) {
glDeleteBuffers(1, &vbo[COLOR_BUFFER]);
}
glDeleteVertexArrays(1, &vao);
}

View File

@ -39,10 +39,10 @@ class Mesh
public:
struct MeshEntry {
enum BUFFERS {
VERTEX_BUFFER, NORMAL_BUFFER, TEXCOORD_BUFFER, INDEX_BUFFER, SHADERSTORAGE_BUFFER
VERTEX_BUFFER, NORMAL_BUFFER, COLOR_BUFFER, TEXCOORD_BUFFER, INDEX_BUFFER
};
GLuint vao;
GLuint vbo[4];
GLuint vbo[5];
unsigned int elementCount;
RenderType renderType;

View File

@ -57,6 +57,25 @@ void MyGlWindow::setBgColor(float bgColor[3])
void MyGlWindow::setup()
{
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &_textures); //tex_2d is a member variable
glBindTexture(GL_TEXTURE_2D, _textures);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_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_T, GL_MIRRORED_REPEAT);
int width, height, channel;
unsigned char * image = stbi_load("brick.jpg", &width, &height, &channel, 0);
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);
_scnctx.lights.emplace("Light1", Light(glm::vec3(0.0f, 0.5f, 0.5f), glm::vec4(10, 10, 0, 1)));
_scnctx.lights.emplace("Light2", Light(glm::vec3(0.0f, 0.0f, 0.5f), glm::vec4(3.09, 10, 9.51, 1)));
_scnctx.lights.emplace("Light3", Light(glm::vec3(0.5f, 0.0f, 0.0f), glm::vec4(-8.09, 10, 5.87, 1)));
@ -65,7 +84,9 @@ void MyGlWindow::setup()
shaders["Simple"] = new Shader("simple.vert", "simple.frag");
shaders["Simple"]->uniformFlags = ShaderFlags::MVP_FLAG;
shaders["Light"] = new Shader("light.vert", "light.frag");
shaders["Light"]->addUniform("tex1", 0);
shaders["Fog"] = new Shader("fog.vert", "fog.frag");
shaders["Silhouette"] = new Shader("silhouette.vert", "silhouette.frag");
@ -78,22 +99,21 @@ void MyGlWindow::setup()
shaders["Toon"]->uniformFlags &= ~ShaderFlags::KS_FLAG;
shaders["Toon"]->uniformFlags &= ~ShaderFlags::SHINE_FLAG;
Dataset cube;
cube.simpleCube();
Dataset floor;
floor.checkeredFloor(20, 40, glm::vec3(0.1, 0.1, 0.1), glm::vec3(0.7, 0.7, 0.7));
floor.checkeredFloor(20, 20, glm::vec3(0.1, 0.1, 0.1), glm::vec3(0.7, 0.7, 0.7));
meshes.push_back(new Mesh(floor, shaders["Simple"]));
meshes.back()->addTranslation(glm::vec4(0, -0.05, 0, 1));
meshes.back()->cullMode = BACK;
floor.simpleCube();
meshes.push_back(new Mesh(floor, shaders["Light"]));
meshes.back()->addTranslation(glm::vec4(0, 1, 0, 1));
/*meshes.push_back(new Mesh("teapot.obj", shaders["Silhouette"]));
meshes.back()->cullMode = FRONT;
meshes.back()->addTranslation(glm::vec4(-2, 0, -5, 0));
meshes.push_back(new Mesh("teapot.obj", shaders["Toon"]));
meshes.back()->cullMode = BACK;
meshes.back()->addTranslation(glm::vec4(-2, 0, -5, 0));
meshes.push_back(new Mesh(cube, shaders["Simple"]));
meshes.back()->addTranslation(glm::vec4(0, 1.01, 0, 1));*/
meshes.push_back(new Mesh(floor, shaders["Simple"]));
meshes.back()->cullMode = BACK;
meshes.back()->cullMode = BACK; */
}
void MyGlWindow::draw()

View File

@ -10,6 +10,7 @@
#include "Light.h"
#include "SceneContext.h"
#include "Models/Mesh.h"
#include "imgui/stb_image.h"
struct vertexAttr {
GLfloat posX, posY, posZ;
@ -37,6 +38,7 @@ private:
float _bgColor[3];
GLuint _textures;
GLuint _vaoHandle;
GLuint _iboHandle;

Binary file not shown.

After

Width:  |  Height:  |  Size: 206 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 242 KiB

View File

@ -44,10 +44,15 @@ public:
{
_program.use(); glUniform3fv(_program.addUniform(name), 1, glm::value_ptr(data)); ; _program.disable();
}
void addUnform(const std::string name, int data)
{
_program.use(); glUniform1iv(_program.addUniform(name), 1, &data); _program.disable();
}
void addUniform(const std::string name, float data)
{
_program.use(); glUniform1fv(_program.addUniform(name), 1, &data); _program.disable();
}
private:
void setMaterial(SceneContext ctx);
void setCamera(SceneContext ctx);

View File

@ -2,7 +2,8 @@
layout(location=0) in vec3 coord3d;
layout(location=1) in vec3 v_normal;
layout(location=2) in vec3 v_color;
layout(location=2) in vec2 v_texmap;
layout(location=3) in vec3 v_color;
uniform mat4 mvp;
uniform mat3 NormalMatrix;

File diff suppressed because it is too large Load Diff

View File

@ -20,10 +20,15 @@ uniform int LightCount;
in vec3 fNormal;
in vec3 pos;
in vec2 texCoord;
uniform sampler2D tex1;
void main()
{
vec3 finalColor;
vec3 diffuse_sum;
vec3 specular_sum;
vec3 ambient;
ambient = Ka * Light[0].Intensity;
@ -37,8 +42,11 @@ void main()
vec3 diffuse = Kd * Light[i].Intensity * max(dot(L, N), 0.0);
vec3 specular = Ks * Light[i].Intensity * pow(max(dot(H, N), 0.0), Shininess);
finalColor = finalColor + diffuse + specular;
diffuse_sum += diffuse;
specular_sum += specular;
}
FragColors = vec4(finalColor + ambient, 1);
vec4 texColor = texture(tex1, texCoord);
FragColors = (vec4(diffuse_sum + ambient, 1) * texColor + vec4(specular_sum, 1.0));
}

View File

@ -2,7 +2,8 @@
layout(location=0) in vec3 coord3d;
layout(location=1) in vec3 v_normal;
layout(location=2) in vec3 v_color;
layout(location=2) in vec2 v_texmap;
layout(location=3) in vec3 v_color;
uniform mat4 mvp;
uniform mat3 NormalMatrix;
@ -10,11 +11,14 @@ uniform mat4 ModelViewMatrix;
out vec3 fNormal;
out vec3 pos;
out vec2 texCoord;
void main(void)
{
fNormal = normalize(NormalMatrix * v_normal);
pos = (ModelViewMatrix * vec4(coord3d, 1.0)).xyz;
texCoord = v_texmap;
gl_Position = mvp * vec4(coord3d, 1.0f);
}

View File

@ -2,7 +2,8 @@
layout(location=0) in vec3 coord3d;
layout(location=1) in vec3 v_normal;
layout(location=2) in vec3 v_color;
layout(location=2) in vec2 v_texmap;
layout(location=3) in vec3 v_color;
uniform mat4 mvp;
uniform float sil_offset;

View File

@ -2,7 +2,8 @@
layout(location=0) in vec3 coord3d;
layout(location=1) in vec3 v_normal;
layout(location=2) in vec3 v_color;
layout(location=2) in vec2 v_texmap;
layout(location=3) in vec3 v_color;
out vec3 f_color;