master
sigonasr2 1 year ago
parent 05e821adc3
commit 1aa67c5f15
  1. 2
      LoadingModels/LoadingModels.vcxproj
  2. 6
      LoadingModels/LoadingModels.vcxproj.filters
  3. 1
      LoadingModels/Sphere.cpp
  4. 89
      LoadingModels/Torus.cpp
  5. 32
      LoadingModels/Torus.h
  6. 92
      LoadingModels/main.cpp

@ -141,6 +141,7 @@
<ItemGroup>
<ClCompile Include="main.cpp" />
<ClCompile Include="Sphere.cpp" />
<ClCompile Include="Torus.cpp" />
</ItemGroup>
<ItemGroup>
<None Include="fragShader.glsl" />
@ -149,6 +150,7 @@
<ItemGroup>
<ClInclude Include="ShaderError.h" />
<ClInclude Include="Sphere.h" />
<ClInclude Include="Torus.h" />
<ClInclude Include="utils.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

@ -21,6 +21,9 @@
<ClCompile Include="Sphere.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Torus.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="fragShader.glsl">
@ -40,5 +43,8 @@
<ClInclude Include="Sphere.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Torus.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

@ -1,7 +1,6 @@
#include <cmath>
#include <vector>
#include <iostream>
#include <glm/glm.hpp>
#include "Sphere.h"
using namespace std;

@ -0,0 +1,89 @@
#include <cmath>
#include <vector>
#include <iostream>
#include <glm/gtc/matrix_transform.hpp>
#include "Torus.h"
using namespace std;
Torus::Torus() {
prec = 48;
inner = 0.5f;
outer = 0.2f;
init();
}
Torus::Torus(float in, float out, int precIn) {
prec = precIn;
inner = in;
outer = out;
init();
}
float Torus::toRadians(float degrees) { return (degrees * 2.0f * 3.14159f) / 360.0f; }
void Torus::init() {
numVertices = (prec + 1) * (prec + 1);
numIndices = prec * prec * 6;
for (int i = 0; i < numVertices; i++) { vertices.push_back(glm::vec3()); }
for (int i = 0; i < numVertices; i++) { texCoords.push_back(glm::vec2()); }
for (int i = 0; i < numVertices; i++) { normals.push_back(glm::vec3()); }
for (int i = 0; i < numVertices; i++) { sTangents.push_back(glm::vec3()); }
for (int i = 0; i < numVertices; i++) { tTangents.push_back(glm::vec3()); }
for (int i = 0; i < numIndices; i++) { indices.push_back(0); }
// calculate first ring
for (int i = 0; i < prec + 1; i++) {
float amt = toRadians(i*360.0f / prec);
glm::mat4 rMat = glm::rotate(glm::mat4(1.0f), amt, glm::vec3(0.0f, 0.0f, 1.0f));
glm::vec3 initPos(rMat * glm::vec4(0.0f, outer, 0.0f, 1.0f));
vertices[i] = glm::vec3(initPos + glm::vec3(inner, 0.0f, 0.0f));
texCoords[i] = glm::vec2(0.0f, ((float)i / (float)prec));
rMat = glm::rotate(glm::mat4(1.0f), amt + (3.14159f / 2.0f), glm::vec3(0.0f, 0.0f, 1.0f));
tTangents[i] = glm::vec3(rMat * glm::vec4(0.0f, -1.0f, 0.0f, 1.0f));
sTangents[i] = glm::vec3(glm::vec3(0.0f, 0.0f, -1.0f));
normals[i] = glm::cross(tTangents[i], sTangents[i]);
}
// rotate the first ring about Y to get the other rings
for (int ring = 1; ring < prec + 1; ring++) {
for (int i = 0; i < prec + 1; i++) {
float amt = (float)toRadians((float)ring * 360.0f / (prec));
glm::mat4 rMat = glm::rotate(glm::mat4(1.0f), amt, glm::vec3(0.0f, 1.0f, 0.0f));
vertices[ring*(prec + 1) + i] = glm::vec3(rMat * glm::vec4(vertices[i], 1.0f));
texCoords[ring*(prec + 1) + i] = glm::vec2((float)ring*2.0f / (float)prec, texCoords[i].t);
rMat = glm::rotate(glm::mat4(1.0f), amt, glm::vec3(0.0f, 1.0f, 0.0f));
sTangents[ring*(prec + 1) + i] = glm::vec3(rMat * glm::vec4(sTangents[i], 1.0f));
rMat = glm::rotate(glm::mat4(1.0f), amt, glm::vec3(0.0f, 1.0f, 0.0f));
tTangents[ring*(prec + 1) + i] = glm::vec3(rMat * glm::vec4(tTangents[i], 1.0f));
rMat = glm::rotate(glm::mat4(1.0f), amt, glm::vec3(0.0f, 1.0f, 0.0f));
normals[ring*(prec + 1) + i] = glm::vec3(rMat * glm::vec4(normals[i], 1.0f));
}
}
// calculate triangle indices
for (int ring = 0; ring < prec; ring++) {
for (int i = 0; i < prec; i++) {
indices[((ring*prec + i) * 2) * 3 + 0] = ring*(prec + 1) + i;
indices[((ring*prec + i) * 2) * 3 + 1] = (ring + 1)*(prec + 1) + i;
indices[((ring*prec + i) * 2) * 3 + 2] = ring*(prec + 1) + i + 1;
indices[((ring*prec + i) * 2 + 1) * 3 + 0] = ring*(prec + 1) + i + 1;
indices[((ring*prec + i) * 2 + 1) * 3 + 1] = (ring + 1)*(prec + 1) + i;
indices[((ring*prec + i) * 2 + 1) * 3 + 2] = (ring + 1)*(prec + 1) + i + 1;
}
}
}
int Torus::getNumVertices() { return numVertices; }
int Torus::getNumIndices() { return numIndices; }
std::vector<int> Torus::getIndices() { return indices; }
std::vector<glm::vec3> Torus::getVertices() { return vertices; }
std::vector<glm::vec2> Torus::getTexCoords() { return texCoords; }
std::vector<glm::vec3> Torus::getNormals() { return normals; }
std::vector<glm::vec3> Torus::getStangents() { return sTangents; }
std::vector<glm::vec3> Torus::getTtangents() { return tTangents; }

@ -0,0 +1,32 @@
#include <cmath>
#include <vector>
#include <glm/glm.hpp>
class Torus
{
private:
int numVertices;
int numIndices;
int prec;
float inner;
float outer;
std::vector<int> indices;
std::vector<glm::vec3> vertices;
std::vector<glm::vec2> texCoords;
std::vector<glm::vec3> normals;
std::vector<glm::vec3> sTangents;
std::vector<glm::vec3> tTangents;
void init();
float toRadians(float degrees);
public:
Torus();
Torus(float innerRadius,float outerRadius,int prec);
int getNumVertices();
int getNumIndices();
std::vector<int> getIndices();
std::vector<glm::vec3> getVertices();
std::vector<glm::vec2> getTexCoords();
std::vector<glm::vec3> getNormals();
std::vector<glm::vec3> getStangents();
std::vector<glm::vec3> getTtangents();
};

@ -9,9 +9,10 @@
#include <array>
#include <stack>
#include "Sphere.h"
#include "Torus.h"
#define numVAOs 1
#define numVBOs 3
#define numVBOs 8
GLuint renderingProgram;
GLuint vao[numVAOs];
@ -27,8 +28,12 @@ std::stack<glm::mat4>transforms;
GLuint pyrTex;
Sphere sphere(48);
Torus torus(0.5,0.2,48);
void setupVertices(void) { // 36 vertices, 12 triangles, makes 2x2x2 cube placed at origin
void setupVertices(void) {
glGenVertexArrays(1, vao);
glBindVertexArray(vao[0]);
glGenBuffers(numVBOs, vbo);
std::vector<int>indices=sphere.getIndices();
std::vector<glm::vec3>vertices=sphere.getVertices();
@ -38,29 +43,59 @@ void setupVertices(void) { // 36 vertices, 12 triangles, makes 2x2x2 cube placed
std::vector<float>pvalues;
std::vector<float>tvalues;
std::vector<float>nvalues;
for(int i=0;i<sphere.getNumIndices();i++){
pvalues.push_back(vertices[indices[i]].x);
pvalues.push_back(vertices[indices[i]].y);
pvalues.push_back(vertices[indices[i]].z);
tvalues.push_back(uvs[indices[i]].s);
tvalues.push_back(uvs[indices[i]].t);
nvalues.push_back(normals[indices[i]].x);
nvalues.push_back(normals[indices[i]].y);
nvalues.push_back(normals[indices[i]].z);
for(int i=0;i<sphere.getNumVertices();i++){
pvalues.push_back(vertices[i].x);
pvalues.push_back(vertices[i].y);
pvalues.push_back(vertices[i].z);
tvalues.push_back(uvs[i].s);
tvalues.push_back(uvs[i].t);
nvalues.push_back(normals[i].x);
nvalues.push_back(normals[i].y);
nvalues.push_back(normals[i].z);
}
glGenVertexArrays(1, vao);
glBindVertexArray(vao[0]);
glGenBuffers(numVBOs, vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glBufferData(GL_ARRAY_BUFFER, pvalues.size()*4, &pvalues[0], GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, pvalues.size()*sizeof(float), pvalues.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
glBufferData(GL_ARRAY_BUFFER, tvalues.size()*4, &tvalues[0],GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, tvalues.size()*sizeof(float), tvalues.data(),GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vbo[2]);
glBufferData(GL_ARRAY_BUFFER, nvalues.size()*4, &nvalues[0], GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, nvalues.size()*sizeof(float), nvalues.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[3]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size()*sizeof(float), indices.data(), GL_STATIC_DRAW);
indices=torus.getIndices();
vertices=torus.getVertices();
uvs=torus.getTexCoords();
normals=torus.getNormals();
pvalues.clear();
tvalues.clear();
nvalues.clear();
for(int i=0;i<torus.getNumVertices();i++){
pvalues.push_back(vertices[i].x);
pvalues.push_back(vertices[i].y);
pvalues.push_back(vertices[i].z);
tvalues.push_back(uvs[i].s);
tvalues.push_back(uvs[i].t);
nvalues.push_back(normals[i].x);
nvalues.push_back(normals[i].y);
nvalues.push_back(normals[i].z);
}
glBindBuffer(GL_ARRAY_BUFFER, vbo[4]);
glBufferData(GL_ARRAY_BUFFER, pvalues.size()*sizeof(float), pvalues.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vbo[5]);
glBufferData(GL_ARRAY_BUFFER, tvalues.size()*sizeof(float), tvalues.data(),GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vbo[6]);
glBufferData(GL_ARRAY_BUFFER, nvalues.size()*sizeof(float), nvalues.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[7]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size()*sizeof(float), indices.data(), GL_STATIC_DRAW);
}
void setupTextures(){
@ -89,8 +124,23 @@ void DrawSphere(){
glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,0,0);
glBindBuffer(GL_ARRAY_BUFFER,vbo[2]);
glVertexAttribPointer(2,3,GL_FLOAT,GL_FALSE,0,0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,vbo[3]);
glDrawElements(GL_TRIANGLES,sphere.getNumIndices(),GL_UNSIGNED_INT,0);
}
void DrawTorus(){
glUniformMatrix4fv(mvLoc,1,GL_FALSE,glm::value_ptr(mvMat));
glUniformMatrix4fv(projLoc,1,GL_FALSE,glm::value_ptr(pMat));
glBindBuffer(GL_ARRAY_BUFFER,vbo[4]);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0);
glBindBuffer(GL_ARRAY_BUFFER,vbo[5]);
glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,0,0);
glBindBuffer(GL_ARRAY_BUFFER,vbo[6]);
glVertexAttribPointer(2,3,GL_FLOAT,GL_FALSE,0,0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,vbo[7]);
glDrawArrays(GL_TRIANGLES,0,sphere.getNumIndices());
glDrawElements(GL_TRIANGLES,torus.getNumIndices(),GL_UNSIGNED_INT,0);
}
double lastTime=0;
@ -120,11 +170,11 @@ void display(GLFWwindow* window, double currentTime) {
vMat=glm::translate(glm::mat4(1.0f),glm::vec3{camera.x,camera.y,camera.z});
mMat=glm::translate(glm::mat4(1.0f),{0.f,0.f,0.f});
mMat=glm::rotate(mMat,float(currentTime/4),{0.f,1.f,0.2f});
mMat=glm::rotate(mMat,float(currentTime/4),{0.f,1.f,0.4f});
mMat=glm::scale(mMat,{5,5,5});
mvMat=vMat*mMat;
DrawSphere();
DrawTorus();
}
int main(void) {

Loading…
Cancel
Save