diff --git a/Lighting/ImportedModel.cpp b/Lighting/ImportedModel.cpp new file mode 100644 index 0000000..ee6df67 --- /dev/null +++ b/Lighting/ImportedModel.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +#include "ImportedModel.h" +using namespace std; + +ImportedModel::ImportedModel() {} + +ImportedModel::ImportedModel(const char *filePath) { + ModelImporter modelImporter = ModelImporter(); + modelImporter.parseOBJ(filePath); + numVertices = modelImporter.getNumVertices(); + std::vector verts = modelImporter.getVertices(); + std::vector tcs = modelImporter.getTextureCoordinates(); + std::vector normals = modelImporter.getNormals(); + + for (int i = 0; i < numVertices; i++) { + vertices.push_back(glm::vec3(verts[i*3], verts[i*3+1], verts[i*3+2])); + texCoords.push_back(glm::vec2(tcs[i*2], tcs[i*2+1])); + normalVecs.push_back(glm::vec3(normals[i*3], normals[i*3+1], normals[i*3+2])); + } +} + +int ImportedModel::getNumVertices() { return numVertices; } +std::vector ImportedModel::getVertices() { return vertices; } +std::vector ImportedModel::getTextureCoords() { return texCoords; } +std::vector ImportedModel::getNormals() { return normalVecs; } + +// --------------------------------------------------------------- + +ModelImporter::ModelImporter() {} + +void ModelImporter::parseOBJ(const char *filePath) { + float x, y, z; + string content; + ifstream fileStream(filePath, ios::in); + string line = ""; + while (!fileStream.eof()) { + getline(fileStream, line); + if (line.compare(0, 2, "v ") == 0) { + stringstream ss(line.erase(0, 1)); + ss >> x; ss >> y; ss >> z; + vertVals.push_back(x); + vertVals.push_back(y); + vertVals.push_back(z); + } + if (line.compare(0, 2, "vt") == 0) { + stringstream ss(line.erase(0, 2)); + ss >> x; ss >> y; + stVals.push_back(x); + stVals.push_back(y); + } + if (line.compare(0, 2, "vn") == 0) { + stringstream ss(line.erase(0, 2)); + ss >> x; ss >> y; ss >> z; + normVals.push_back(x); + normVals.push_back(y); + normVals.push_back(z); + } + if (line.compare(0, 2, "f ") == 0) { + string oneCorner, v, t, n; + stringstream ss(line.erase(0, 2)); + for (int i = 0; i < 3; i++) { + getline(ss, oneCorner, ' '); + stringstream oneCornerSS(oneCorner); + getline(oneCornerSS, v, '/'); + getline(oneCornerSS, t, '/'); + getline(oneCornerSS, n, '/'); + + int vertRef = (stoi(v) - 1) * 3; + int tcRef = (stoi(t) - 1) * 2; + int normRef = (stoi(n) - 1) * 3; + + triangleVerts.push_back(vertVals[vertRef]); + triangleVerts.push_back(vertVals[vertRef + 1]); + triangleVerts.push_back(vertVals[vertRef + 2]); + + textureCoords.push_back(stVals[tcRef]); + textureCoords.push_back(stVals[tcRef + 1]); + + normals.push_back(normVals[normRef]); + normals.push_back(normVals[normRef + 1]); + normals.push_back(normVals[normRef + 2]); + } + } + } +} +int ModelImporter::getNumVertices() { return (triangleVerts.size()/3); } +std::vector ModelImporter::getVertices() { return triangleVerts; } +std::vector ModelImporter::getTextureCoordinates() { return textureCoords; } +std::vector ModelImporter::getNormals() { return normals; } \ No newline at end of file diff --git a/Lighting/ImportedModel.h b/Lighting/ImportedModel.h new file mode 100644 index 0000000..17a123b --- /dev/null +++ b/Lighting/ImportedModel.h @@ -0,0 +1,35 @@ +#include + +class ImportedModel +{ +private: + int numVertices; + std::vector vertices; + std::vector texCoords; + std::vector normalVecs; +public: + ImportedModel(); + ImportedModel(const char *filePath); + int getNumVertices(); + std::vector getVertices(); + std::vector getTextureCoords(); + std::vector getNormals(); +}; + +class ModelImporter +{ +private: + std::vector vertVals; + std::vector triangleVerts; + std::vector textureCoords; + std::vector stVals; + std::vector normals; + std::vector normVals; +public: + ModelImporter(); + void parseOBJ(const char *filePath); + int getNumVertices(); + std::vector getVertices(); + std::vector getTextureCoordinates(); + std::vector getNormals(); +}; \ No newline at end of file diff --git a/Lighting/Lighting.vcxproj b/Lighting/Lighting.vcxproj new file mode 100644 index 0000000..0a27130 --- /dev/null +++ b/Lighting/Lighting.vcxproj @@ -0,0 +1,161 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {44a6fb06-4513-47de-b966-9ad000e9300d} + Lighting + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + C:\Users\sigon\Documents\OpenGL\include;%(AdditionalIncludeDirectories) + + + Console + true + C:\Users\sigon\Documents\OpenGL\lib;%(AdditionalLibraryDirectories) + glfw3.lib;glew32.lib;soil2-debug.lib;opengl32.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + C:\Users\sigon\Documents\OpenGL\include;%(AdditionalIncludeDirectories) + + + Console + true + true + true + C:\Users\sigon\Documents\OpenGL\lib;%(AdditionalLibraryDirectories) + glfw3.lib;glew32.lib;soil2-debug.lib;opengl32.lib;%(AdditionalDependencies) + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + C:\Users\sigon\Documents\OpenGL\include;%(AdditionalIncludeDirectories) + + + Console + true + C:\Users\sigon\Documents\OpenGL\lib;%(AdditionalLibraryDirectories) + glfw3.lib;glew32.lib;soil2-debug.lib;opengl32.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + C:\Users\sigon\Documents\OpenGL\include;%(AdditionalIncludeDirectories) + + + Console + true + true + true + C:\Users\sigon\Documents\OpenGL\lib;%(AdditionalLibraryDirectories) + glfw3.lib;glew32.lib;soil2-debug.lib;opengl32.lib;%(AdditionalDependencies) + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Lighting/Lighting.vcxproj.filters b/Lighting/Lighting.vcxproj.filters new file mode 100644 index 0000000..2fcafc0 --- /dev/null +++ b/Lighting/Lighting.vcxproj.filters @@ -0,0 +1,56 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + + + Resource Files + + + Resource Files + + + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + \ No newline at end of file diff --git a/Lighting/ShaderError.h b/Lighting/ShaderError.h new file mode 100644 index 0000000..8ae7d47 --- /dev/null +++ b/Lighting/ShaderError.h @@ -0,0 +1,39 @@ +#pragma once +#include +#include +#include + +namespace ErrorCheck{ + inline void printShaderLog(GLuint shader) { + int len = 0; + int chWrittn = 0; + char *log; + glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len); + if (len > 0) { + log = (char *)malloc(len); + glGetShaderInfoLog(shader, len, &chWrittn, log); + std::cout << "Shader Info Log: " << log << std::endl; + free(log); + } } + inline void printProgramLog(int prog) { + int len = 0; + int chWrittn = 0; + char *log; + glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &len); + if (len > 0) { + log = (char *)malloc(len); + glGetProgramInfoLog(prog, len, &chWrittn, log); + std::cout << "Program Info Log: " << log << std::endl; + free(log); + } } + inline bool checkOpenGLError() { + bool foundError = false; + int glErr = glGetError(); + while (glErr != GL_NO_ERROR) { + std::cout << "glError: " << glErr << std::endl; + foundError = true; + glErr = glGetError(); + } + return foundError; + } +} \ No newline at end of file diff --git a/Lighting/Sphere.cpp b/Lighting/Sphere.cpp new file mode 100644 index 0000000..d940f20 --- /dev/null +++ b/Lighting/Sphere.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +#include "Sphere.h" +using namespace std; + +Sphere::Sphere() { + init(48); +} + +Sphere::Sphere(int prec) { // prec is precision, or number of slices + init(prec); +} + +float Sphere::toRadians(float degrees) { return (degrees*2.0f*3.14159f)/360.0f; } + +void Sphere::init(int prec) { + numVertices = (prec+1)*(prec+1); + numIndices = prec*prec*6; + for(int i = 0; i Sphere::getIndices() { return indices; } +std::vector Sphere::getVertices() { return vertices; } +std::vector Sphere::getTexCoords() { return texCoords; } +std::vector Sphere::getNormals() { return normals; } \ No newline at end of file diff --git a/Lighting/Sphere.h b/Lighting/Sphere.h new file mode 100644 index 0000000..0e51ee9 --- /dev/null +++ b/Lighting/Sphere.h @@ -0,0 +1,25 @@ +#include +#include +#include + +class Sphere{ +private: + int numVertices; + int numIndices; + std::vector indices; + std::vector vertices; + std::vector texCoords; + std::vector normals; + void init(int); + float toRadians(float degrees); + +public: + Sphere(); + Sphere(int prec); + int getNumVertices(); + int getNumIndices(); + std::vector getIndices(); + std::vector getVertices(); + std::vector getTexCoords(); + std::vector getNormals(); +}; \ No newline at end of file diff --git a/Lighting/Torus.cpp b/Lighting/Torus.cpp new file mode 100644 index 0000000..ec77169 --- /dev/null +++ b/Lighting/Torus.cpp @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#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 Torus::getIndices() { return indices; } +std::vector Torus::getVertices() { return vertices; } +std::vector Torus::getTexCoords() { return texCoords; } +std::vector Torus::getNormals() { return normals; } +std::vector Torus::getStangents() { return sTangents; } +std::vector Torus::getTtangents() { return tTangents; } \ No newline at end of file diff --git a/Lighting/Torus.h b/Lighting/Torus.h new file mode 100644 index 0000000..0ed4957 --- /dev/null +++ b/Lighting/Torus.h @@ -0,0 +1,32 @@ +#include +#include +#include +class Torus +{ +private: + int numVertices; + int numIndices; + int prec; + float inner; + float outer; + std::vector indices; + std::vector vertices; + std::vector texCoords; + std::vector normals; + std::vector sTangents; + std::vector tTangents; + void init(); + float toRadians(float degrees); + +public: + Torus(); + Torus(float innerRadius,float outerRadius,int prec); + int getNumVertices(); + int getNumIndices(); + std::vector getIndices(); + std::vector getVertices(); + std::vector getTexCoords(); + std::vector getNormals(); + std::vector getStangents(); + std::vector getTtangents(); +}; \ No newline at end of file diff --git a/Lighting/brick1.jpg b/Lighting/brick1.jpg new file mode 100644 index 0000000..dbcb80f Binary files /dev/null and b/Lighting/brick1.jpg differ diff --git a/Lighting/earth.jpg b/Lighting/earth.jpg new file mode 100644 index 0000000..b40ae31 Binary files /dev/null and b/Lighting/earth.jpg differ diff --git a/Lighting/fragShader.glsl b/Lighting/fragShader.glsl new file mode 100644 index 0000000..87df112 --- /dev/null +++ b/Lighting/fragShader.glsl @@ -0,0 +1,8 @@ +#version 430 +layout (binding=0) uniform sampler2D samp; +in vec2 uv; +out vec4 color; +void main(void) +{ + color=texture(samp,uv); +} \ No newline at end of file diff --git a/Lighting/glew32.dll b/Lighting/glew32.dll new file mode 100644 index 0000000..04f9381 Binary files /dev/null and b/Lighting/glew32.dll differ diff --git a/Lighting/ice.jpg b/Lighting/ice.jpg new file mode 100644 index 0000000..17804a3 Binary files /dev/null and b/Lighting/ice.jpg differ diff --git a/Lighting/main.cpp b/Lighting/main.cpp new file mode 100644 index 0000000..011b5e1 --- /dev/null +++ b/Lighting/main.cpp @@ -0,0 +1,253 @@ +#include +#include +#include "utils.h" +#include +#include +#include +#include +#include +#include +#include +#include "Sphere.h" +#include "Torus.h" +#include "ImportedModel.h" + +#define numVAOs 1 +#define numVBOs 11 + +GLuint renderingProgram; +GLuint vao[numVAOs]; +GLuint vbo[numVBOs]; + +glm::vec3 camera; +GLuint mvLoc,projLoc; +int width, height; +float aspect; +glm::mat4 mMat, pMat, vMat, mvMat; +std::stacktransforms; + +GLuint pyrTex; + +Sphere sphere(48); +Torus torus(1.9,0.4,48); +ImportedModel shuttle("shuttle.obj"); + +void setupVertices(void) { + glGenVertexArrays(1, vao); + glBindVertexArray(vao[0]); + glGenBuffers(numVBOs, vbo); + + std::vectorindices=sphere.getIndices(); + std::vectorvertices=sphere.getVertices(); + std::vectoruvs=sphere.getTexCoords(); + std::vectornormals=sphere.getNormals(); + + std::vectorpvalues; + std::vectortvalues; + std::vectornvalues; + for(int i=0;i +#include +#include +#include "ShaderError.h" +#include + +#define PI 3.14159 + +class utils{ + inline static std::string readShaderSource(const char *filePath){ + std::string content; + std::ifstream fileStream(filePath, std::ios::in); + std::string line = ""; + if(fileStream.fail()){ + std::cout<<"Could not open shader "<