Setup next chapter project files and experiment with active texture sample binding.
This commit is contained in:
parent
836b249bc6
commit
a14516f74f
157
LoadingModels/LoadingModels.vcxproj
Normal file
157
LoadingModels/LoadingModels.vcxproj
Normal file
@ -0,0 +1,157 @@
|
||||
<?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>{b5f65d11-bf58-4316-babc-df4d4105e9dc}</ProjectGuid>
|
||||
<RootNamespace>LoadingModels</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="main.cpp" />
|
||||
<ClCompile Include="Sphere.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="fragShader.glsl" />
|
||||
<None Include="vertShader.glsl" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="ShaderError.h" />
|
||||
<ClInclude Include="Sphere.h" />
|
||||
<ClInclude Include="utils.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
44
LoadingModels/LoadingModels.vcxproj.filters
Normal file
44
LoadingModels/LoadingModels.vcxproj.filters
Normal file
@ -0,0 +1,44 @@
|
||||
<?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="Sphere.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="ShaderError.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="utils.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Sphere.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
39
LoadingModels/ShaderError.h
Normal file
39
LoadingModels/ShaderError.h
Normal file
@ -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;
|
||||
}
|
||||
}
|
57
LoadingModels/Sphere.cpp
Normal file
57
LoadingModels/Sphere.cpp
Normal file
@ -0,0 +1,57 @@
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <glm/glm.hpp>
|
||||
#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; }
|
25
LoadingModels/Sphere.h
Normal file
25
LoadingModels/Sphere.h
Normal file
@ -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();
|
||||
};
|
BIN
LoadingModels/brick1.jpg
Normal file
BIN
LoadingModels/brick1.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 180 KiB |
8
LoadingModels/fragShader.glsl
Normal file
8
LoadingModels/fragShader.glsl
Normal file
@ -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);
|
||||
}
|
BIN
LoadingModels/glew32.dll
Normal file
BIN
LoadingModels/glew32.dll
Normal file
Binary file not shown.
BIN
LoadingModels/ice.jpg
Normal file
BIN
LoadingModels/ice.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 185 KiB |
147
LoadingModels/main.cpp
Normal file
147
LoadingModels/main.cpp
Normal file
@ -0,0 +1,147 @@
|
||||
#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"
|
||||
|
||||
#define numVAOs 1
|
||||
#define numVBOs 3
|
||||
|
||||
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);
|
||||
|
||||
void setupVertices(void) { // 36 vertices, 12 triangles, makes 2x2x2 cube placed at origin
|
||||
|
||||
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.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);
|
||||
}
|
||||
|
||||
glGenVertexArrays(1, vao);
|
||||
glBindVertexArray(vao[0]);
|
||||
glGenBuffers(numVBOs, vbo);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
|
||||
glBufferData(GL_ARRAY_BUFFER, pvalues.size()*sizeof(float), vertices.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);
|
||||
}
|
||||
|
||||
void setupTextures(){
|
||||
pyrTex=utils::loadTexture("brick1.jpg");
|
||||
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,2.0f,-10.0f};
|
||||
setupVertices();
|
||||
setupTextures();
|
||||
}
|
||||
|
||||
double lastTime=0;
|
||||
|
||||
void display(GLFWwindow* window, double currentTime) {
|
||||
double elapsedTime=currentTime-lastTime;
|
||||
lastTime=currentTime;
|
||||
|
||||
glClear(GL_DEPTH_BUFFER_BIT);
|
||||
glClearColor(0,0,0.2,1);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
glUseProgram(renderingProgram);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glDepthFunc(GL_LEQUAL);
|
||||
glEnableVertexAttribArray(0);
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
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});
|
||||
|
||||
|
||||
mMat=glm::translate(mMat,{0.f,0.f,0.f});
|
||||
mvMat=vMat*mMat;
|
||||
|
||||
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(0,2,GL_FLOAT,GL_FALSE,0,0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER,vbo[2]);
|
||||
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0);
|
||||
|
||||
glActiveTexture(GL_TEXTURE_2D);
|
||||
glBindTexture(GL_TEXTURE_2D,pyrTex);
|
||||
|
||||
glDrawArrays(GL_TRIANGLES,0,sphere.getNumIndices());
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
94
LoadingModels/utils.h
Normal file
94
LoadingModels/utils.h
Normal file
@ -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;
|
||||
}
|
||||
};
|
67
LoadingModels/vertShader.glsl
Normal file
67
LoadingModels/vertShader.glsl
Normal file
@ -0,0 +1,67 @@
|
||||
#version 430
|
||||
layout (location=0) in vec3 position;
|
||||
layout (location=1) in vec2 texCoords;
|
||||
uniform mat4 mv_matrix;
|
||||
uniform mat4 proj_matrix;
|
||||
out vec4 varyingColor;
|
||||
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;
|
||||
}
|
BIN
Models/ice.jpg
Normal file
BIN
Models/ice.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 185 KiB |
@ -23,11 +23,10 @@ float aspect;
|
||||
glm::mat4 mMat, pMat, vMat, mvMat;
|
||||
std::stack<glm::mat4>transforms;
|
||||
|
||||
GLuint cupYayTex;
|
||||
GLuint cupYayTex,iceTex;
|
||||
|
||||
void setupTextures(void){
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D,cupYayTex);
|
||||
}
|
||||
|
||||
void setupVertices(void) { // 36 vertices, 12 triangles, makes 2x2x2 cube placed at origin
|
||||
@ -81,6 +80,7 @@ void init(GLFWwindow* window) {
|
||||
setupVertices();
|
||||
|
||||
cupYayTex=utils::loadTexture("cupyay4.png");
|
||||
iceTex=utils::loadTexture("ice.jpg");
|
||||
setupTextures();
|
||||
}
|
||||
|
||||
@ -105,6 +105,7 @@ GLuint timeLoc;
|
||||
|
||||
GLuint mvLoc;
|
||||
float size=0;
|
||||
GLuint texLoc;
|
||||
|
||||
void display(GLFWwindow* window, double currentTime) {
|
||||
double elapsedTime=currentTime-lastTime;
|
||||
@ -157,6 +158,7 @@ void display(GLFWwindow* window, double currentTime) {
|
||||
|
||||
mvLoc=glGetUniformLocation(expandingPlaneProgram,"mv_matrix");
|
||||
projLoc=glGetUniformLocation(expandingPlaneProgram,"proj_matrix");
|
||||
texLoc=glGetUniformLocation(expandingPlaneProgram,"texture");
|
||||
glUniformMatrix4fv(mvLoc,1,GL_FALSE,glm::value_ptr(mvMat));
|
||||
glUniformMatrix4fv(projLoc,1,GL_FALSE,glm::value_ptr(pMat));
|
||||
glBindBuffer(GL_ARRAY_BUFFER,vbo[1]);
|
||||
@ -164,12 +166,27 @@ void display(GLFWwindow* window, double currentTime) {
|
||||
glBindBuffer(GL_ARRAY_BUFFER,vbo[2]);
|
||||
glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,0,0);
|
||||
|
||||
glActiveTexture(GL_TEXTURE_2D);
|
||||
glBindTexture(GL_TEXTURE_2D,cupYayTex);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D,iceTex);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glDepthFunc(GL_LEQUAL);
|
||||
|
||||
glDrawArrays(GL_TRIANGLES,0,6);
|
||||
mMat=glm::translate(glm::mat4{1.0},{10,0,0});
|
||||
mMat=glm::scale(mMat,{size,size,size});
|
||||
|
||||
mvMat=vMat*mMat;
|
||||
|
||||
mvLoc=glGetUniformLocation(expandingPlaneProgram,"mv_matrix");
|
||||
projLoc=glGetUniformLocation(expandingPlaneProgram,"proj_matrix");
|
||||
texLoc=glGetUniformLocation(expandingPlaneProgram,"texture");
|
||||
glUniformMatrix4fv(mvLoc,1,GL_FALSE,glm::value_ptr(mvMat));
|
||||
glUniformMatrix4fv(projLoc,1,GL_FALSE,glm::value_ptr(pMat));
|
||||
glBindBuffer(GL_ARRAY_BUFFER,vbo[1]);
|
||||
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER,vbo[2]);
|
||||
glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,0,0);
|
||||
glBindTexture(GL_TEXTURE_2D,cupYayTex);
|
||||
|
||||
glDrawArrays(GL_TRIANGLES,0,6);
|
||||
}
|
||||
|
||||
|
@ -11,6 +11,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Models", "Models\Models.vcx
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TextureMapping", "TextureMapping\TextureMapping.vcxproj", "{609A0750-8502-4F60-8E54-AD032D6E216B}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "LoadingModels", "LoadingModels\LoadingModels.vcxproj", "{B5F65D11-BF58-4316-BABC-DF4D4105E9DC}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|x64 = Debug|x64
|
||||
@ -51,6 +53,14 @@ Global
|
||||
{609A0750-8502-4F60-8E54-AD032D6E216B}.Release|x64.Build.0 = Release|x64
|
||||
{609A0750-8502-4F60-8E54-AD032D6E216B}.Release|x86.ActiveCfg = Release|Win32
|
||||
{609A0750-8502-4F60-8E54-AD032D6E216B}.Release|x86.Build.0 = Release|Win32
|
||||
{B5F65D11-BF58-4316-BABC-DF4D4105E9DC}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{B5F65D11-BF58-4316-BABC-DF4D4105E9DC}.Debug|x64.Build.0 = Debug|x64
|
||||
{B5F65D11-BF58-4316-BABC-DF4D4105E9DC}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{B5F65D11-BF58-4316-BABC-DF4D4105E9DC}.Debug|x86.Build.0 = Debug|Win32
|
||||
{B5F65D11-BF58-4316-BABC-DF4D4105E9DC}.Release|x64.ActiveCfg = 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.Build.0 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
Loading…
x
Reference in New Issue
Block a user