Single-Texture mapping is working !

This commit is contained in:
Hurlu 2019-03-18 18:45:29 +09:00
parent 909fec23f1
commit 4fbdc1a180
17 changed files with 87 additions and 69 deletions

View File

@ -208,6 +208,10 @@
<ItemGroup> <ItemGroup>
<None Include="simple.frag" /> <None Include="simple.frag" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Image Include="brick1.jpg" />
<Image Include="earth.jpg" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">
</ImportGroup> </ImportGroup>

View File

@ -164,4 +164,12 @@
<Filter>Shaders</Filter> <Filter>Shaders</Filter>
</None> </None>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Image Include="brick1.jpg">
<Filter>Resource Files</Filter>
</Image>
<Image Include="earth.jpg">
<Filter>Resource Files</Filter>
</Image>
</ItemGroup>
</Project> </Project>

View File

@ -15,10 +15,13 @@ public:
{ {
vertices.clear(); vertices.clear();
normals.clear(); normals.clear();
colors.clear();
tex_mapping.clear();
} }
void simpleCube() void simpleCube()
{ {
clear();
vertices = { { -1.0, -1.0, 1.0}, { 1.0, -1.0, 1.0}, { 1.0, 1.0, 1.0}, vertices = { { -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}, { 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}, { 1.0, -1.0, 1.0}, { 1.0, -1.0, -1.0}, { 1.0, 1.0, -1.0},
@ -67,18 +70,19 @@ public:
void simpleFloor() void simpleFloor()
{ {
clear();
vertices = {{-10.0, 0.0, -10.0}, {-10.0, 0.0, 10.0}, {10.0, 0.0, -10.0}, 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}}; {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}, 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} }; {0.7, 0.7, 0.7}, {0.7, 0.7, 0.7}, {0.7, 0.7, 0.7} };
genEmptyTexture();
genNormals(); genNormals();
} }
void checkeredFloor(float size, int squares, void checkeredFloor(float size, int squares,
glm::vec3 light_color, glm::vec3 dark_color) glm::vec3 light_color, glm::vec3 dark_color)
{ {
clear();
float maxX = size / 2.f, maxY = size / 2.f; float maxX = size / 2.f, maxY = size / 2.f;
float minX = -size / 2.f, minY = -size / 2.f; float minX = -size / 2.f, minY = -size / 2.f;
float side_size = size / (float)squares; float side_size = size / (float)squares;
@ -94,7 +98,6 @@ public:
vertices.emplace_back(x, 0, y); //upleft vertices.emplace_back(x, 0, y); //upleft
vertices.emplace_back(x, 0, y + side_size); //downleft vertices.emplace_back(x, 0, y + side_size); //downleft
vertices.emplace_back(x + side_size, 0, y + side_size); //downright vertices.emplace_back(x + side_size, 0, y + side_size); //downright
vertices.emplace_back(x + side_size, 0, y + side_size); //downright vertices.emplace_back(x + side_size, 0, y + side_size); //downright
vertices.emplace_back(x + side_size, 0, y); //upright vertices.emplace_back(x + side_size, 0, y); //upright
vertices.emplace_back(x, 0, y); //upleft vertices.emplace_back(x, 0, y); //upleft
@ -104,18 +107,10 @@ public:
} }
color *= -1; color *= -1;
} }
genEmptyTexture();
genNormals(); genNormals();
} }
private: 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() void genNormals()
{ {

View File

@ -22,6 +22,7 @@ Mesh::MeshEntry::MeshEntry(Dataset &set, Mesh *m)
{ {
renderType = NO_INDEX; renderType = NO_INDEX;
parent = m; parent = m;
shininessStrength = 0;
vbo[VERTEX_BUFFER] = NULL; vbo[VERTEX_BUFFER] = NULL;
vbo[NORMAL_BUFFER] = NULL; vbo[NORMAL_BUFFER] = NULL;
@ -51,27 +52,30 @@ Mesh::MeshEntry::MeshEntry(Dataset &set, Mesh *m)
glEnableVertexAttribArray(NORMAL_BUFFER); glEnableVertexAttribArray(NORMAL_BUFFER);
//Copy texture mapping to VBO //Copy texture mapping to VBO
if (set.tex_mapping.size() > 0)
{
glGenBuffers(1, &vbo[TEXCOORD_BUFFER]); glGenBuffers(1, &vbo[TEXCOORD_BUFFER]);
glBindBuffer(GL_ARRAY_BUFFER, vbo[TEXCOORD_BUFFER]); glBindBuffer(GL_ARRAY_BUFFER, vbo[TEXCOORD_BUFFER]);
glBufferData(GL_ARRAY_BUFFER, 2 * set.tex_mapping.size() * sizeof(GLfloat), set.tex_mapping.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, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 2, NULL); glVertexAttribPointer(TEXCOORD_BUFFER, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 2, NULL);
glEnableVertexAttribArray(TEXCOORD_BUFFER); glEnableVertexAttribArray(TEXCOORD_BUFFER);
}
//Copy vertice color to VBO //Copy vertice color to VBO
if (set.colors.size() > 0)
{
glGenBuffers(1, &vbo[COLOR_BUFFER]); glGenBuffers(1, &vbo[COLOR_BUFFER]);
glBindBuffer(GL_ARRAY_BUFFER, 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); 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); glVertexAttribPointer(COLOR_BUFFER, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 3, NULL);
glEnableVertexAttribArray(COLOR_BUFFER); glEnableVertexAttribArray(COLOR_BUFFER);
}
dset_size = set.vertices.size(); dset_size = set.vertices.size();
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0); glBindVertexArray(0);
shininessStrength = 0;
} }
@ -208,8 +212,9 @@ Mesh::MeshEntry::~MeshEntry() {
/** /**
* Renders this MeshEntry * Renders this MeshEntry
**/ **/
void Mesh::MeshEntry::render() { void Mesh::MeshEntry::render(SceneContext ctx) {
glBindTexture(GL_TEXTURE_2D, ctx.textures);
glBindVertexArray(vao); glBindVertexArray(vao);
if (renderType == NO_INDEX) if (renderType == NO_INDEX)
{ {
@ -222,6 +227,7 @@ void Mesh::MeshEntry::render() {
glDrawElements(GL_TRIANGLES, size / sizeof(unsigned int), GL_UNSIGNED_INT, NULL); glDrawElements(GL_TRIANGLES, size / sizeof(unsigned int), GL_UNSIGNED_INT, NULL);
} }
glBindVertexArray(0); glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
} }
/** /**
@ -333,7 +339,7 @@ void Mesh::draw(SceneContext ctx) {
shader->setUniforms(ctx); shader->setUniforms(ctx);
meshEntries.at(i)->render(); meshEntries.at(i)->render(ctx);
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(); void render(SceneContext ctx);
}; };
public: public:

View File

@ -1,4 +1,4 @@
#include <algorithm> #include <direct.h>
#include "MyGlWindow.h" #include "MyGlWindow.h"
//Getting the projection matrix //Getting the projection matrix
glm::mat4x4 perspective(float fovy, float aspect, float near, float far) glm::mat4x4 perspective(float fovy, float aspect, float near, float far)
@ -58,8 +58,8 @@ void MyGlWindow::setBgColor(float bgColor[3])
void MyGlWindow::setup() void MyGlWindow::setup()
{ {
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &_textures); //tex_2d is a member variable glGenTextures(1, &_scnctx.textures); //tex_2d is a member variable
glBindTexture(GL_TEXTURE_2D, _textures); 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);
@ -67,7 +67,7 @@ void MyGlWindow::setup()
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; int width, height, channel;
unsigned char * image = stbi_load("brick.jpg", &width, &height, &channel, 0); unsigned char * image = stbi_load("brick1.jpg", &width, &height, &channel, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
GL_UNSIGNED_BYTE, image); GL_UNSIGNED_BYTE, image);
@ -76,11 +76,15 @@ void MyGlWindow::setup()
stbi_image_free(image); stbi_image_free(image);
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
_scnctx.lights.emplace("Light1", Light(glm::vec3(0.0f, 0.5f, 0.5f), glm::vec4(10, 10, 0, 1))); //Showcase lights
_scnctx.lights.emplace("Light1", Light(glm::vec3(0.8f), glm::vec4(3, 10, 5, 1)));
//Party lights
/*_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("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))); _scnctx.lights.emplace("Light3", Light(glm::vec3(0.5f, 0.0f, 0.0f), glm::vec4(-8.09, 10, 5.87, 1)));
_scnctx.lights.emplace("Light4", Light(glm::vec3(0.0f, 0.5f, 0.0f), glm::vec4(-8.09, 10, -5.87, 1))); _scnctx.lights.emplace("Light4", Light(glm::vec3(0.0f, 0.5f, 0.0f), glm::vec4(-8.09, 10, -5.87, 1)));
_scnctx.lights.emplace("Light5", Light(glm::vec3(0.5f, 0.5f, 0.5f), glm::vec4(3.09, 10, -9.51, 1))); _scnctx.lights.emplace("Light5", Light(glm::vec3(0.5f, 0.5f, 0.5f), glm::vec4(3.09, 10, -9.51, 1)));*/
shaders["Simple"] = new Shader("simple.vert", "simple.frag"); shaders["Simple"] = new Shader("simple.vert", "simple.frag");
shaders["Simple"]->uniformFlags = ShaderFlags::MVP_FLAG; shaders["Simple"]->uniformFlags = ShaderFlags::MVP_FLAG;

View File

@ -38,7 +38,6 @@ private:
float _bgColor[3]; float _bgColor[3];
GLuint _textures;
GLuint _vaoHandle; GLuint _vaoHandle;
GLuint _iboHandle; GLuint _iboHandle;

View File

@ -15,4 +15,6 @@ struct SceneContext
glm::mat4x4 mvpMatrix; glm::mat4x4 mvpMatrix;
glm::mat4x4 modelViewMatrix; glm::mat4x4 modelViewMatrix;
glm::mat3x3 normalMatrix; glm::mat3x3 normalMatrix;
GLuint textures;
}; };

View File

@ -44,9 +44,9 @@ public:
{ {
_program.use(); glUniform3fv(_program.addUniform(name), 1, glm::value_ptr(data)); ; _program.disable(); _program.use(); glUniform3fv(_program.addUniform(name), 1, glm::value_ptr(data)); ; _program.disable();
} }
void addUnform(const std::string name, int data) void addUniform(const std::string name, int data)
{ {
_program.use(); glUniform1iv(_program.addUniform(name), 1, &data); _program.disable(); _program.use(); glUniform1i(_program.addUniform(name), data); _program.disable();
} }
void addUniform(const std::string name, float data) void addUniform(const std::string name, float data)
{ {

View File

@ -175,7 +175,7 @@ int loop(GLFWwindow *window)
{ {
ImGui::SetWindowPos(ImVec2(20, 20)); ImGui::SetWindowPos(ImVec2(20, 20));
static float bgColor[3] = { .3f, .3f, .3f }; static float bgColor[3] = { .0f, .0f, .1f };
ImGui::ColorEdit3("Background", bgColor, 0); ImGui::ColorEdit3("Background", bgColor, 0);
glWin.setBgColor(bgColor); glWin.setBgColor(bgColor);

View File

Before

Width:  |  Height:  |  Size: 206 KiB

After

Width:  |  Height:  |  Size: 206 KiB

View File

Before

Width:  |  Height:  |  Size: 242 KiB

After

Width:  |  Height:  |  Size: 242 KiB

View File

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

View File

@ -46,7 +46,6 @@ void main()
specular_sum += specular; specular_sum += specular;
} }
vec4 texColor = texture(tex1, texCoord); vec4 texColor = texture(tex1, texCoord);
FragColors = (vec4(diffuse_sum + ambient, 1) * texColor + vec4(specular_sum, 1.0)); FragColors = (vec4(diffuse_sum + ambient, 1) * texColor + vec4(specular_sum, 1.0));
} }

View File

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

View File

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

View File

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