Setup Lighting Project

master
sigonasr2 1 year ago
parent a6a1f61e22
commit ec680f42ca
  1. 91
      Lighting/ImportedModel.cpp
  2. 35
      Lighting/ImportedModel.h
  3. 161
      Lighting/Lighting.vcxproj
  4. 56
      Lighting/Lighting.vcxproj.filters
  5. 39
      Lighting/ShaderError.h
  6. 56
      Lighting/Sphere.cpp
  7. 25
      Lighting/Sphere.h
  8. 89
      Lighting/Torus.cpp
  9. 32
      Lighting/Torus.h
  10. BIN
      Lighting/brick1.jpg
  11. BIN
      Lighting/earth.jpg
  12. 8
      Lighting/fragShader.glsl
  13. BIN
      Lighting/glew32.dll
  14. BIN
      Lighting/ice.jpg
  15. 253
      Lighting/main.cpp
  16. BIN
      Lighting/saturn.jpg
  17. BIN
      Lighting/spstob_1.jpg
  18. 94
      Lighting/utils.h
  19. 67
      Lighting/vertShader.glsl
  20. 10
      OpenGLTemplate.sln

@ -0,0 +1,91 @@
#include <fstream>
#include <sstream>
#include <glm/glm.hpp>
#include "ImportedModel.h"
using namespace std;
ImportedModel::ImportedModel() {}
ImportedModel::ImportedModel(const char *filePath) {
ModelImporter modelImporter = ModelImporter();
modelImporter.parseOBJ(filePath);
numVertices = modelImporter.getNumVertices();
std::vector<float> verts = modelImporter.getVertices();
std::vector<float> tcs = modelImporter.getTextureCoordinates();
std::vector<float> 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<glm::vec3> ImportedModel::getVertices() { return vertices; }
std::vector<glm::vec2> ImportedModel::getTextureCoords() { return texCoords; }
std::vector<glm::vec3> 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<float> ModelImporter::getVertices() { return triangleVerts; }
std::vector<float> ModelImporter::getTextureCoordinates() { return textureCoords; }
std::vector<float> ModelImporter::getNormals() { return normals; }

@ -0,0 +1,35 @@
#include <vector>
class ImportedModel
{
private:
int numVertices;
std::vector<glm::vec3> vertices;
std::vector<glm::vec2> texCoords;
std::vector<glm::vec3> normalVecs;
public:
ImportedModel();
ImportedModel(const char *filePath);
int getNumVertices();
std::vector<glm::vec3> getVertices();
std::vector<glm::vec2> getTextureCoords();
std::vector<glm::vec3> getNormals();
};
class ModelImporter
{
private:
std::vector<float> vertVals;
std::vector<float> triangleVerts;
std::vector<float> textureCoords;
std::vector<float> stVals;
std::vector<float> normals;
std::vector<float> normVals;
public:
ModelImporter();
void parseOBJ(const char *filePath);
int getNumVertices();
std::vector<float> getVertices();
std::vector<float> getTextureCoordinates();
std::vector<float> getNormals();
};

@ -0,0 +1,161 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{44a6fb06-4513-47de-b966-9ad000e9300d}</ProjectGuid>
<RootNamespace>Lighting</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>C:\Users\sigon\Documents\OpenGL\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>C:\Users\sigon\Documents\OpenGL\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>glfw3.lib;glew32.lib;soil2-debug.lib;opengl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>C:\Users\sigon\Documents\OpenGL\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>C:\Users\sigon\Documents\OpenGL\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>glfw3.lib;glew32.lib;soil2-debug.lib;opengl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>C:\Users\sigon\Documents\OpenGL\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>C:\Users\sigon\Documents\OpenGL\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>glfw3.lib;glew32.lib;soil2-debug.lib;opengl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>C:\Users\sigon\Documents\OpenGL\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>C:\Users\sigon\Documents\OpenGL\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>glfw3.lib;glew32.lib;soil2-debug.lib;opengl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="ImportedModel.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="Sphere.cpp" />
<ClCompile Include="Torus.cpp" />
</ItemGroup>
<ItemGroup>
<None Include="fragShader.glsl" />
<None Include="vertShader.glsl" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="ImportedModel.h" />
<ClInclude Include="ShaderError.h" />
<ClInclude Include="Sphere.h" />
<ClInclude Include="Torus.h" />
<ClInclude Include="utils.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

@ -0,0 +1,56 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="ImportedModel.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Sphere.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Torus.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="fragShader.glsl">
<Filter>Resource Files</Filter>
</None>
<None Include="vertShader.glsl">
<Filter>Resource Files</Filter>
</None>
</ItemGroup>
<ItemGroup>
<ClInclude Include="ImportedModel.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="ShaderError.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Sphere.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Torus.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="utils.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

@ -0,0 +1,39 @@
#pragma once
#include <GL/glew.h>
#include <cstdlib>
#include <iostream>
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;
}
}

@ -0,0 +1,56 @@
#include <cmath>
#include <vector>
#include <iostream>
#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<numVertices; i++) { vertices.push_back(glm::vec3()); } // std::vector::push_back()
for(int i = 0; i<numVertices; i++) { texCoords.push_back(glm::vec2()); } // inserts new element at
for(int i = 0; i<numVertices; i++) { normals.push_back(glm::vec3()); } // the end of a vector and
for(int i = 0; i<numIndices; i++) { indices.push_back(0); } // increases the vector size by 1
// calculate triangle vertices
for(int i = 0; i<=prec; i++) {
for(int j = 0; j<=prec; j++) {
float y = (float)cos(toRadians(180.0f-i*180.0f/prec));
float x = -(float)cos(toRadians(j*360.0f/prec))*(float)abs(cos(asin(y)));
float z = (float)sin(toRadians(j*360.0f/prec))*(float)abs(cos(asin(y)));
vertices[i*(prec+1)+j] = glm::vec3(x,y,z);
texCoords[i*(prec+1)+j] = glm::vec2(((float)j/prec),((float)i/prec));
normals[i*(prec+1)+j] = glm::vec3(x,y,z);
}
}
// calculate triangle indices
for(int i = 0; i<prec; i++) {
for(int j = 0; j<prec; j++) {
indices[6*(i*prec+j)+0] = i*(prec+1)+j;
indices[6*(i*prec+j)+1] = i*(prec+1)+j+1;
indices[6*(i*prec+j)+2] = (i+1)*(prec+1)+j;
indices[6*(i*prec+j)+3] = i*(prec+1)+j+1;
indices[6*(i*prec+j)+4] = (i+1)*(prec+1)+j+1;
indices[6*(i*prec+j)+5] = (i+1)*(prec+1)+j;
}
}
}
// accessors
int Sphere::getNumVertices() { return numVertices; }
int Sphere::getNumIndices() { return numIndices; }
std::vector<int> Sphere::getIndices() { return indices; }
std::vector<glm::vec3> Sphere::getVertices() { return vertices; }
std::vector<glm::vec2> Sphere::getTexCoords() { return texCoords; }
std::vector<glm::vec3> Sphere::getNormals() { return normals; }

@ -0,0 +1,25 @@
#include <cmath>
#include <vector>
#include <glm/glm.hpp>
class Sphere{
private:
int numVertices;
int numIndices;
std::vector<int> indices;
std::vector<glm::vec3> vertices;
std::vector<glm::vec2> texCoords;
std::vector<glm::vec3> normals;
void init(int);
float toRadians(float degrees);
public:
Sphere();
Sphere(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();
};

@ -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();
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 180 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 836 KiB

@ -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);
}

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 185 KiB

@ -0,0 +1,253 @@
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "utils.h"
#include <algorithm>
#include <vector>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <array>
#include <stack>
#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::stack<glm::mat4>transforms;
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::vector<int>indices=sphere.getIndices();
std::vector<glm::vec3>vertices=sphere.getVertices();
std::vector<glm::vec2>uvs=sphere.getTexCoords();
std::vector<glm::vec3>normals=sphere.getNormals();
std::vector<float>pvalues;
std::vector<float>tvalues;
std::vector<float>nvalues;
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);
}
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
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()*sizeof(float), tvalues.data(),GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vbo[2]);
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);
vertices=shuttle.getVertices();
uvs=shuttle.getTextureCoords();
normals=shuttle.getNormals();
pvalues.clear();
tvalues.clear();
nvalues.clear();
for(int i=0;i<shuttle.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[7]);
glBufferData(GL_ARRAY_BUFFER, pvalues.size()*sizeof(float), pvalues.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vbo[8]);
glBufferData(GL_ARRAY_BUFFER, tvalues.size()*sizeof(float), tvalues.data(),GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vbo[9]);
glBufferData(GL_ARRAY_BUFFER, nvalues.size()*sizeof(float), nvalues.data(), GL_STATIC_DRAW);
}
void setupTextures(){
pyrTex=utils::loadTexture("spstob_1.jpg");
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,pyrTex);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
void init(GLFWwindow* window) {
renderingProgram=utils::createShaderProgram("vertShader.glsl","fragShader.glsl");
camera={0.0f,5.0f,-10.0f};
setupVertices();
setupTextures();
}
void DrawSphere(){
glUniformMatrix4fv(mvLoc,1,GL_FALSE,glm::value_ptr(mvMat));
glUniformMatrix4fv(projLoc,1,GL_FALSE,glm::value_ptr(pMat));
glBindBuffer(GL_ARRAY_BUFFER,vbo[0]);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0);
glBindBuffer(GL_ARRAY_BUFFER,vbo[1]);
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]);
glDrawElements(GL_TRIANGLES,torus.getNumIndices(),GL_UNSIGNED_INT,0);
}
void DrawShuttle(){
glUniformMatrix4fv(mvLoc,1,GL_FALSE,glm::value_ptr(mvMat));
glUniformMatrix4fv(projLoc,1,GL_FALSE,glm::value_ptr(pMat));
glBindBuffer(GL_ARRAY_BUFFER,vbo[7]);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0);
glBindBuffer(GL_ARRAY_BUFFER,vbo[8]);
glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,0,0);
glBindBuffer(GL_ARRAY_BUFFER,vbo[9]);
glVertexAttribPointer(2,3,GL_FLOAT,GL_FALSE,0,0);
glDrawArrays(GL_TRIANGLES,0,shuttle.getNumVertices());
}
double lastTime=0;
void display(GLFWwindow* window, double currentTime) {
double elapsedTime=currentTime-lastTime;
lastTime=currentTime;
glClear(GL_DEPTH_BUFFER_BIT);
glClearColor(0.2,0,0.2,1);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(renderingProgram);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
mvLoc=glGetUniformLocation(renderingProgram,"mv_matrix");
projLoc=glGetUniformLocation(renderingProgram,"proj_matrix");
glfwGetFramebufferSize(window,&width,&height);
aspect=(float)width/(float)height;
pMat=glm::perspective(utils::degToRad(60),aspect,0.1f,1000.f);
vMat=glm::translate(glm::mat4(1.0f),glm::vec3{camera.x,camera.y,camera.z});
vMat=glm::lookAt(glm::vec3{camera.x,camera.y,camera.z},{0,-3,0},{0,1,0});
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::scale(mMat,{8,8,8});
mvMat=vMat*mMat;
//DrawSphere();
DrawShuttle();
mMat=glm::translate(glm::mat4(1.0f),{0.f,-3.f,0.f});
mMat=glm::rotate(mMat,float(currentTime/4),{0.f,1.f,0.4f});
mMat=glm::scale(mMat,{3,0.1,3});
mvMat=vMat*mMat;
//DrawTorus();
}
int main(void) {
if (!glfwInit()) { exit(EXIT_FAILURE); }
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
GLFWwindow* window = glfwCreateWindow(1920, 1080, "Texture Mapping!", NULL, NULL);
glfwSetWindowPos(window,1400,180);
glfwMakeContextCurrent(window);
if (glewInit() != GLEW_OK) { exit(EXIT_FAILURE); }
glfwSwapInterval(1);
init(window);
while (!glfwWindowShouldClose(window)) {
display(window,glfwGetTime());
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 269 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 555 KiB

@ -0,0 +1,94 @@
#pragma once
#include <string>
#include <fstream>
#include <GL/glew.h>
#include "ShaderError.h"
#include <SOIL2/SOIL2.h>
#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 "<<filePath<<"!"<<std::endl;
throw;
}
while (fileStream.good()) {
getline(fileStream, line);
content.append(line + "\n");
}
fileStream.close();
return content;
}
inline static void CompileShader(GLuint shader,std::string type){
GLint compiled;
glCompileShader(shader);
ErrorCheck::checkOpenGLError();
glGetShaderiv(shader,GL_COMPILE_STATUS,&compiled);
if(compiled!=1){
std::cout<<type<<" Compilation Failed!"<<std::endl;
ErrorCheck::printShaderLog(shader);
throw;
}
}
inline static void CreateShader(GLuint type,std::string shaderName,const char*source,GLuint vfProgram){
if(source!=nullptr){
GLuint vShader=glCreateShader(type);
std::string shaderFile=readShaderSource(source);
const char*vertShaderSrc=shaderFile.c_str();
glShaderSource(vShader,1,&vertShaderSrc,NULL);
CompileShader(vShader,shaderName);
glAttachShader(vfProgram,vShader);
}
};
public:
inline static GLuint createShaderProgram(const char*vertex,const char*fragment){
return createShaderProgram(vertex,nullptr,nullptr,nullptr,fragment);
}
inline static GLuint createShaderProgram(const char*vertex,const char*geometry,const char*fragment){
return createShaderProgram(vertex,nullptr,nullptr,geometry,fragment);
}
inline static GLuint createShaderProgram(const char*vertex,const char*tesselationCS,const char*tesselationES,const char*fragment){
return createShaderProgram(vertex,tesselationCS,tesselationES,nullptr,fragment);
}
inline static GLuint createShaderProgram(const char*vertex,const char*tesselationCS,const char*tesselationES,const char*geometry,const char*fragment){
GLuint vfProgram=glCreateProgram();
CreateShader(GL_VERTEX_SHADER,"Vertex",vertex,vfProgram);
CreateShader(GL_FRAGMENT_SHADER,"Fragment",fragment,vfProgram);
CreateShader(GL_TESS_CONTROL_SHADER,"Tesselation Control",tesselationCS,vfProgram);
CreateShader(GL_TESS_EVALUATION_SHADER,"Tesselation Evaluation",tesselationES,vfProgram);
CreateShader(GL_GEOMETRY_SHADER,"Geometry",geometry,vfProgram);
GLint linked;
glLinkProgram(vfProgram);
ErrorCheck::checkOpenGLError();
glGetProgramiv(vfProgram,GL_LINK_STATUS,&linked);
if(linked!=1){
std::cout<<"Linking Failed!"<<std::endl;
ErrorCheck::printProgramLog(vfProgram);
}
return vfProgram;
}
inline static float degToRad(float deg){
return deg*(PI/180);
}
inline static float radToDeg(float rad){
return rad*57.2957795130823208767;
}
inline static GLuint loadTexture(const char*file){
GLuint textureID;
textureID=SOIL_load_OGL_texture(file,SOIL_LOAD_AUTO,SOIL_CREATE_NEW_ID,SOIL_FLAG_INVERT_Y);
if(!textureID){
std::cout<<"Could not load texture file "<<file<<std::endl;
throw;
}
return textureID;
}
};

@ -0,0 +1,67 @@
#version 430
layout (location=0) in vec3 position;
layout (location=1) in vec2 texCoords;
layout (location=2) in vec3 normals;
uniform mat4 mv_matrix;
uniform mat4 proj_matrix;
out vec2 uv;
mat4 buildTranslate(float x, float y, float z);
mat4 buildRotateX(float rad);
mat4 buildRotateY(float rad);
mat4 buildRotateZ(float rad);
void main(void)
{
gl_Position=proj_matrix*mv_matrix*vec4(position,1.0);
uv=texCoords;
}
// builds and returns a translation matrix
mat4 buildTranslate(float x, float y, float z)
{
mat4 trans = mat4(1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
x, y, z, 1.0 );
return trans;
}
// builds and returns a matrix that performs a rotation around the X axis
mat4 buildRotateX(float rad)
{
mat4 xrot = mat4(1.0, 0.0, 0.0, 0.0,
0.0, cos(rad), -sin(rad), 0.0,
0.0, sin(rad), cos(rad), 0.0,
0.0, 0.0, 0.0, 1.0 );
return xrot;
}
// builds and returns a matrix that performs a rotation around the Y axis
mat4 buildRotateY(float rad)
{
mat4 yrot = mat4(cos(rad), 0.0, sin(rad), 0.0,
0.0, 1.0, 0.0, 0.0,
-sin(rad), 0.0, cos(rad), 0.0,
0.0, 0.0, 0.0, 1.0 );
return yrot;
}
// builds and returns a matrix that performs a rotation around the Z axis
mat4 buildRotateZ(float rad)
{
mat4 zrot = mat4(cos(rad), -sin(rad), 0.0, 0.0,
sin(rad), cos(rad), 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0 );
return zrot;
}
// builds and returns a scale matrix
mat4 buildScale(float x, float y, float z)
{
mat4 scale = mat4(x, 0.0, 0.0, 0.0,
0.0, y, 0.0, 0.0,
0.0, 0.0, z, 0.0,
0.0, 0.0, 0.0, 1.0 );
return scale;
}

@ -13,6 +13,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TextureMapping", "TextureMa
EndProject EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LoadingModels", "LoadingModels\LoadingModels.vcxproj", "{B5F65D11-BF58-4316-BABC-DF4D4105E9DC}" Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LoadingModels", "LoadingModels\LoadingModels.vcxproj", "{B5F65D11-BF58-4316-BABC-DF4D4105E9DC}"
EndProject EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Lighting", "Lighting\Lighting.vcxproj", "{44A6FB06-4513-47DE-B966-9AD000E9300D}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64 Debug|x64 = Debug|x64
@ -61,6 +63,14 @@ Global
{B5F65D11-BF58-4316-BABC-DF4D4105E9DC}.Release|x64.Build.0 = Release|x64 {B5F65D11-BF58-4316-BABC-DF4D4105E9DC}.Release|x64.Build.0 = Release|x64
{B5F65D11-BF58-4316-BABC-DF4D4105E9DC}.Release|x86.ActiveCfg = Release|Win32 {B5F65D11-BF58-4316-BABC-DF4D4105E9DC}.Release|x86.ActiveCfg = Release|Win32
{B5F65D11-BF58-4316-BABC-DF4D4105E9DC}.Release|x86.Build.0 = Release|Win32 {B5F65D11-BF58-4316-BABC-DF4D4105E9DC}.Release|x86.Build.0 = Release|Win32
{44A6FB06-4513-47DE-B966-9AD000E9300D}.Debug|x64.ActiveCfg = Debug|x64
{44A6FB06-4513-47DE-B966-9AD000E9300D}.Debug|x64.Build.0 = Debug|x64
{44A6FB06-4513-47DE-B966-9AD000E9300D}.Debug|x86.ActiveCfg = Debug|Win32
{44A6FB06-4513-47DE-B966-9AD000E9300D}.Debug|x86.Build.0 = Debug|Win32
{44A6FB06-4513-47DE-B966-9AD000E9300D}.Release|x64.ActiveCfg = Release|x64
{44A6FB06-4513-47DE-B966-9AD000E9300D}.Release|x64.Build.0 = Release|x64
{44A6FB06-4513-47DE-B966-9AD000E9300D}.Release|x86.ActiveCfg = Release|Win32
{44A6FB06-4513-47DE-B966-9AD000E9300D}.Release|x86.Build.0 = Release|Win32
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

Loading…
Cancel
Save