Skip to content
Snippets Groups Projects
Commit 148897a7 authored by Woohyung Choi's avatar Woohyung Choi
Browse files

Add uv vec2 for uvcoord.

parent 77c6f2bf
Branches
No related tags found
No related merge requests found
...@@ -326,15 +326,37 @@ GLuint allocate_VBO(const GLuint attribIndex, std::vector<glm::vec3> *VBO) ...@@ -326,15 +326,37 @@ GLuint allocate_VBO(const GLuint attribIndex, std::vector<glm::vec3> *VBO)
return VBOIndex; return VBOIndex;
} }
GLuint *allocate_VBOs(GLuint VAO, std::vector<std::vector<glm::vec3> *> &vertexInfo) GLuint allocate_VBO(const GLuint attribIndex, std::vector<glm::vec2> *VBO)
{ {
GLuint *VBOindicies = new GLuint[vertexInfo.size()]; GLuint VBOIndex = 0;
glGenBuffers(1, &VBOIndex);
glBindBuffer(GL_ARRAY_BUFFER, VBOIndex);
glBufferData(GL_ARRAY_BUFFER, VBO->size() * sizeof(glm::vec2), &(VBO->front()), GL_STATIC_DRAW);
glEnableVertexAttribArray(attribIndex);
glVertexAttribPointer(attribIndex, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
return VBOIndex;
}
GLuint *allocate_VBOs(GLuint VAO, std::vector<std::vector<glm::vec3> *> &vertexInfoVec3, std::vector<std::vector<glm::vec2> *> &vertexInfoVec2)
{
auto size = vertexInfoVec3.size() + vertexInfoVec2.size();
GLuint *VBOindicies = new GLuint[size];
glBindVertexArray(VAO); glBindVertexArray(VAO);
for (GLuint i = 0; i < vertexInfo.size(); i++) GLuint i = 0;
for (i = 0; i < vertexInfoVec3.size(); i++)
{
VBOindicies[i] = allocate_VBO(i, vertexInfoVec3.at(i));
}
for (i = vertexInfoVec3.size(); i < size; i++)
{ {
VBOindicies[i] = allocate_VBO(i, vertexInfo.at(i)); VBOindicies[i] = allocate_VBO(i, vertexInfoVec2.at(i - vertexInfoVec3.size()));
} }
glBindVertexArray(0); glBindVertexArray(0);
...@@ -354,7 +376,7 @@ GLuint allocate_VAO() ...@@ -354,7 +376,7 @@ GLuint allocate_VAO()
Mesh *make_mesh(const std::string fileName) Mesh *make_mesh(const std::string fileName)
{ {
std::vector<glm::vec3> vertices; std::vector<glm::vec3> vertices;
std::vector<glm::vec3> vertexTexCoord; std::vector<glm::vec2> vertexTexCoord;
std::vector<glm::vec3> vertexNormals; std::vector<glm::vec3> vertexNormals;
const std::string ext = get_extension(fileName); const std::string ext = get_extension(fileName);
...@@ -367,19 +389,21 @@ Mesh *make_mesh(const std::string fileName) ...@@ -367,19 +389,21 @@ Mesh *make_mesh(const std::string fileName)
std::cout << "Can't Open " + fileName + " Extension." << std::endl; std::cout << "Can't Open " + fileName + " Extension." << std::endl;
} }
std::vector<std::vector<glm::vec3> *> vertexInfo; std::vector<std::vector<glm::vec3> *> vertexInfoVec3;
vertexInfo.push_back(&vertices); std::vector<std::vector<glm::vec2> *> vertexInfoVec2;
vertexInfoVec3.push_back(&vertices);
if (vertexTexCoord.size() > 0) if (vertexTexCoord.size() > 0)
{ {
vertexInfo.push_back(&vertexTexCoord); vertexInfoVec2.push_back(&vertexTexCoord);
} }
if (vertexNormals.size() > 0) if (vertexNormals.size() > 0)
{ {
vertexInfo.push_back(&vertexNormals); vertexInfoVec3.push_back(&vertexNormals);
} }
auto VAO = allocate_VAO(); auto VAO = allocate_VAO();
auto VBOs = allocate_VBOs(VAO, vertexInfo); auto VBOs = allocate_VBOs(VAO, vertexInfoVec3, vertexInfoVec2);
Mesh *m = new Mesh(vertices.size(), VAO, VBOs); Mesh *m = new Mesh(vertices.size(), VAO, VBOs);
......
...@@ -31,7 +31,7 @@ std::string get_extension(const std::string &filePath) ...@@ -31,7 +31,7 @@ std::string get_extension(const std::string &filePath)
return filePath.substr(filePath.find_last_of(".") + 1); return filePath.substr(filePath.find_last_of(".") + 1);
} }
bool openObj(const std::string fileName, std::vector<glm::vec3> &vertices, std::vector<glm::vec3> &vertexTexCoord, std::vector<glm::vec3> &vertexNormals) bool openObj(const std::string fileName, std::vector<glm::vec3> &vertices, std::vector<glm::vec2> &vertexTexCoord, std::vector<glm::vec3> &vertexNormals)
{ {
vertices.clear(); vertices.clear();
vertexTexCoord.clear(); vertexTexCoord.clear();
...@@ -42,7 +42,7 @@ bool openObj(const std::string fileName, std::vector<glm::vec3> &vertices, std:: ...@@ -42,7 +42,7 @@ bool openObj(const std::string fileName, std::vector<glm::vec3> &vertices, std::
char op[3]; char op[3];
std::vector<glm::vec3> vertexIndices; std::vector<glm::vec3> vertexIndices;
std::vector<glm::vec3> vertexTexCoordIndices; std::vector<glm::vec2> vertexTexCoordIndices;
std::vector<glm::vec3> vertexNormalIndices; std::vector<glm::vec3> vertexNormalIndices;
ifs.open("../Models/" + fileName); ifs.open("../Models/" + fileName);
...@@ -74,7 +74,7 @@ bool openObj(const std::string fileName, std::vector<glm::vec3> &vertices, std:: ...@@ -74,7 +74,7 @@ bool openObj(const std::string fileName, std::vector<glm::vec3> &vertices, std::
} }
else if (strcmp(op, "vt") == false) else if (strcmp(op, "vt") == false)
{ {
glm::vec3 pos = { 0,0,0 }; glm::vec2 pos = { 0,0 };
sscanf_s(line.c_str(), "%f %f", &pos.x, &pos.y); sscanf_s(line.c_str(), "%f %f", &pos.x, &pos.y);
vertexTexCoordIndices.push_back(pos); vertexTexCoordIndices.push_back(pos);
} }
......
...@@ -31,6 +31,6 @@ public: ...@@ -31,6 +31,6 @@ public:
}; };
std::string get_extension(const std::string &filePath); std::string get_extension(const std::string &filePath);
bool openObj(const std::string fileName, std::vector<glm::vec3> &vertices, std::vector<glm::vec3> &vertexTexCoord, std::vector<glm::vec3> &vertexNormals); bool openObj(const std::string fileName, std::vector<glm::vec3> &vertices, std::vector<glm::vec2> &vertexTexCoord, std::vector<glm::vec3> &vertexNormals);
Image *load_Image(std::string fileName, int *width, int *height, int *nrChannels); Image *load_Image(std::string fileName, int *width, int *height, int *nrChannels);
void free_image(Image *img); void free_image(Image *img);
\ No newline at end of file
...@@ -70,7 +70,7 @@ int main() ...@@ -70,7 +70,7 @@ int main()
auto container_specular = load_image("container2_specular.png"); auto container_specular = load_image("container2_specular.png");
auto defaultMaterial = new Material(lightmap, orange, transparent); auto defaultMaterial = new Material(lightmap, orange, transparent);
auto planeMaterial = new Material(texture_shader, container_diffuse, transparent); auto planeMaterial = new Material(lightmap, container_diffuse, transparent);
auto teapot = make_render_object(make_mesh("teapot.obj")); auto teapot = make_render_object(make_mesh("teapot.obj"));
{ {
...@@ -106,7 +106,7 @@ int main() ...@@ -106,7 +106,7 @@ int main()
plane->set_scale(glm::vec3(10, 10, 1)); plane->set_scale(glm::vec3(10, 10, 1));
} }
{ {
plane->set_material(planeMaterial); plane->set_material(defaultMaterial);
} }
auto cube3 = make_render_object(cube); auto cube3 = make_render_object(cube);
...@@ -135,7 +135,7 @@ int main() ...@@ -135,7 +135,7 @@ int main()
cube1->render(camera); cube1->render(camera);
cube2->render(camera); cube2->render(camera);
cube3->render(camera); cube3->render(camera);
//plane->render(camera); plane->render(camera);
} }
glfwSwapBuffers(window); glfwSwapBuffers(window);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment