Fixed Lab06, Assignment 3 Completed.
This commit is contained in:
parent
af6aa58f4c
commit
97066ef640
28 changed files with 13535 additions and 1 deletions
1
A3/.gitignore
vendored
Normal file
1
A3/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
build/
|
127
A3/CMakeLists.txt
Normal file
127
A3/CMakeLists.txt
Normal file
|
@ -0,0 +1,127 @@
|
||||||
|
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
|
||||||
|
|
||||||
|
# Name of the project
|
||||||
|
PROJECT(A3)
|
||||||
|
|
||||||
|
# FOR LAB MACHINES ONLY!
|
||||||
|
# DO NOT EDIT
|
||||||
|
SET(DEF_DIR_GLM "C:\\c++\\glm")
|
||||||
|
SET(DEF_DIR_GLFW "C:\\c++\\glfw-3.2.1")
|
||||||
|
SET(DEF_DIR_GLEW "C:\\c++\\glew-2.0.0")
|
||||||
|
|
||||||
|
# Is this the solution?
|
||||||
|
# Override with `cmake -DSOL=ON ..`
|
||||||
|
OPTION(SOL "Solution" OFF)
|
||||||
|
|
||||||
|
# Use glob to get the list of all source files.
|
||||||
|
# We don't really need to include header and resource files to build, but it's
|
||||||
|
# nice to have them also show up in IDEs.
|
||||||
|
IF(${SOL})
|
||||||
|
FILE(GLOB_RECURSE SOURCES "src0/*.cpp")
|
||||||
|
FILE(GLOB_RECURSE HEADERS "src0/*.h")
|
||||||
|
FILE(GLOB_RECURSE GLSL "resources0/*.glsl")
|
||||||
|
ELSE()
|
||||||
|
FILE(GLOB_RECURSE SOURCES "src/*.cpp")
|
||||||
|
FILE(GLOB_RECURSE HEADERS "src/*.h")
|
||||||
|
FILE(GLOB_RECURSE GLSL "resources/*.glsl")
|
||||||
|
ENDIF()
|
||||||
|
|
||||||
|
# Set the executable.
|
||||||
|
ADD_EXECUTABLE(${CMAKE_PROJECT_NAME} ${SOURCES} ${HEADERS} ${GLSL})
|
||||||
|
|
||||||
|
# Get the GLM environment variable. Since GLM is a header-only library, we
|
||||||
|
# just need to add it to the include directory.
|
||||||
|
SET(GLM_INCLUDE_DIR "$ENV{GLM_INCLUDE_DIR}")
|
||||||
|
IF(NOT GLM_INCLUDE_DIR)
|
||||||
|
# The environment variable was not set
|
||||||
|
SET(ERR_MSG "Please point the environment variable GLM_INCLUDE_DIR to the root directory of your GLM installation.")
|
||||||
|
IF(WIN32)
|
||||||
|
# On Windows, try the default location
|
||||||
|
MESSAGE(STATUS "Looking for GLM in ${DEF_DIR_GLM}")
|
||||||
|
IF(IS_DIRECTORY ${DEF_DIR_GLM})
|
||||||
|
MESSAGE(STATUS "Found!")
|
||||||
|
SET(GLM_INCLUDE_DIR ${DEF_DIR_GLM})
|
||||||
|
ELSE()
|
||||||
|
MESSAGE(FATAL_ERROR ${ERR_MSG})
|
||||||
|
ENDIF()
|
||||||
|
ELSE()
|
||||||
|
MESSAGE(FATAL_ERROR ${ERR_MSG})
|
||||||
|
ENDIF()
|
||||||
|
ENDIF()
|
||||||
|
INCLUDE_DIRECTORIES(${GLM_INCLUDE_DIR})
|
||||||
|
|
||||||
|
# Get the GLFW environment variable. There should be a CMakeLists.txt in the
|
||||||
|
# specified directory.
|
||||||
|
SET(GLFW_DIR "$ENV{GLFW_DIR}")
|
||||||
|
IF(NOT GLFW_DIR)
|
||||||
|
# The environment variable was not set
|
||||||
|
SET(ERR_MSG "Please point the environment variable GLFW_DIR to the root directory of your GLFW installation.")
|
||||||
|
IF(WIN32)
|
||||||
|
# On Windows, try the default location
|
||||||
|
MESSAGE(STATUS "Looking for GLFW in ${DEF_DIR_GLFW}")
|
||||||
|
IF(IS_DIRECTORY ${DEF_DIR_GLFW})
|
||||||
|
MESSAGE(STATUS "Found!")
|
||||||
|
SET(GLFW_DIR ${DEF_DIR_GLFW})
|
||||||
|
ELSE()
|
||||||
|
MESSAGE(FATAL_ERROR ${ERR_MSG})
|
||||||
|
ENDIF()
|
||||||
|
ELSE()
|
||||||
|
MESSAGE(FATAL_ERROR ${ERR_MSG})
|
||||||
|
ENDIF()
|
||||||
|
ENDIF()
|
||||||
|
OPTION(GLFW_BUILD_EXAMPLES "GLFW_BUILD_EXAMPLES" OFF)
|
||||||
|
OPTION(GLFW_BUILD_TESTS "GLFW_BUILD_TESTS" OFF)
|
||||||
|
OPTION(GLFW_BUILD_DOCS "GLFW_BUILD_DOCS" OFF)
|
||||||
|
IF(CMAKE_BUILD_TYPE MATCHES Release)
|
||||||
|
ADD_SUBDIRECTORY(${GLFW_DIR} ${GLFW_DIR}/release)
|
||||||
|
ELSE()
|
||||||
|
ADD_SUBDIRECTORY(${GLFW_DIR} ${GLFW_DIR}/debug)
|
||||||
|
ENDIF()
|
||||||
|
INCLUDE_DIRECTORIES(${GLFW_DIR}/include)
|
||||||
|
TARGET_LINK_LIBRARIES(${CMAKE_PROJECT_NAME} glfw ${GLFW_LIBRARIES})
|
||||||
|
|
||||||
|
# Get the GLEW environment variable.
|
||||||
|
SET(GLEW_DIR "$ENV{GLEW_DIR}")
|
||||||
|
IF(NOT GLEW_DIR)
|
||||||
|
# The environment variable was not set
|
||||||
|
SET(ERR_MSG "Please point the environment variable GLEW_DIR to the root directory of your GLEW installation.")
|
||||||
|
IF(WIN32)
|
||||||
|
# On Windows, try the default location
|
||||||
|
MESSAGE(STATUS "Looking for GLEW in ${DEF_DIR_GLEW}")
|
||||||
|
IF(IS_DIRECTORY ${DEF_DIR_GLEW})
|
||||||
|
MESSAGE(STATUS "Found!")
|
||||||
|
SET(GLEW_DIR ${DEF_DIR_GLEW})
|
||||||
|
ELSE()
|
||||||
|
MESSAGE(FATAL_ERROR ${ERR_MSG})
|
||||||
|
ENDIF()
|
||||||
|
ELSE()
|
||||||
|
MESSAGE(FATAL_ERROR ${ERR_MSG})
|
||||||
|
ENDIF()
|
||||||
|
ENDIF()
|
||||||
|
INCLUDE_DIRECTORIES(${GLEW_DIR}/include)
|
||||||
|
IF(WIN32)
|
||||||
|
# With prebuilt binaries
|
||||||
|
TARGET_LINK_LIBRARIES(${CMAKE_PROJECT_NAME} ${GLEW_DIR}/lib/Release/Win32/glew32s.lib)
|
||||||
|
ELSE()
|
||||||
|
TARGET_LINK_LIBRARIES(${CMAKE_PROJECT_NAME} ${GLEW_DIR}/lib/libGLEW.a)
|
||||||
|
ENDIF()
|
||||||
|
|
||||||
|
# OS specific options and libraries
|
||||||
|
IF(WIN32)
|
||||||
|
# c++11 is enabled by default.
|
||||||
|
# -Wall produces way too many warnings.
|
||||||
|
# -pedantic is not supported.
|
||||||
|
# Disable warning 4996.
|
||||||
|
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4996")
|
||||||
|
TARGET_LINK_LIBRARIES(${CMAKE_PROJECT_NAME} opengl32.lib)
|
||||||
|
ELSE()
|
||||||
|
# Enable all pedantic warnings.
|
||||||
|
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -pedantic")
|
||||||
|
IF(APPLE)
|
||||||
|
# Add required frameworks for GLFW.
|
||||||
|
TARGET_LINK_LIBRARIES(${CMAKE_PROJECT_NAME} "-framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo")
|
||||||
|
ELSE()
|
||||||
|
#Link the Linux OpenGL library
|
||||||
|
TARGET_LINK_LIBRARIES(${CMAKE_PROJECT_NAME} "GL")
|
||||||
|
ENDIF()
|
||||||
|
ENDIF()
|
4
A3/README.txt
Normal file
4
A3/README.txt
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
Alexander Huddleston
|
||||||
|
I downloaded the code from the lab pages and worked from there.
|
||||||
|
|
||||||
|
I didn't notice you implemented a "toggle key" functionality in the main and instead coded if statements in the char callback to do what I wanted. The functionality remains the same, but I thought it was worth noting.
|
17
A3/resources/.frag.glsl
Normal file
17
A3/resources/.frag.glsl
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
#version 120
|
||||||
|
|
||||||
|
uniform vec3 lightPos;
|
||||||
|
uniform vec3 ka;
|
||||||
|
uniform vec3 kd;
|
||||||
|
uniform vec3 ks;
|
||||||
|
uniform float s;
|
||||||
|
|
||||||
|
varying vec3 n; // passed from the vertex shader
|
||||||
|
varying vec3 p; // passed from the vertex shader
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
n = normalize(normal);
|
||||||
|
vec3 color = 0.5 * (n + 1.0);
|
||||||
|
gl_FragColor = vec4(color.r, color.g, color.b, 1.0);
|
||||||
|
}
|
17
A3/resources/.vert.glsl
Normal file
17
A3/resources/.vert.glsl
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
#version 120
|
||||||
|
|
||||||
|
uniform mat4 P;
|
||||||
|
uniform mat4 MV;
|
||||||
|
|
||||||
|
attribute vec4 aPos; // in object space
|
||||||
|
attribute vec3 aNor; // in object space
|
||||||
|
|
||||||
|
varying vec3 p; // passed to fragment shader
|
||||||
|
varying vec3 n; // passed to fragment shader
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
ugl_Position = P * MV * aPos;
|
||||||
|
p = MV * aPos;
|
||||||
|
n = MV * aNor;
|
||||||
|
}
|
9977
A3/resources/bunny.obj
Normal file
9977
A3/resources/bunny.obj
Normal file
File diff suppressed because it is too large
Load diff
29
A3/resources/cube.obj
Normal file
29
A3/resources/cube.obj
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
# Blender v2.71 (sub 0) OBJ File: ''
|
||||||
|
# www.blender.org
|
||||||
|
v 0.500000 0.500000 -0.500000
|
||||||
|
v 0.500000 -0.500000 -0.500000
|
||||||
|
v -0.500000 -0.500000 -0.500000
|
||||||
|
v -0.500000 0.500000 -0.500000
|
||||||
|
v 0.500000 0.500000 0.500000
|
||||||
|
v 0.500000 -0.500000 0.500000
|
||||||
|
v -0.500000 -0.500000 0.500000
|
||||||
|
v -0.500000 0.500000 0.500000
|
||||||
|
vn 0.000000 0.000000 -1.000000
|
||||||
|
vn 0.000000 0.000000 1.000000
|
||||||
|
vn 1.000000 -0.000000 -0.000000
|
||||||
|
vn -0.000000 -1.000000 -0.000000
|
||||||
|
vn -1.000000 0.000000 -0.000000
|
||||||
|
vn 0.000000 1.000000 0.000000
|
||||||
|
s off
|
||||||
|
f 2//1 3//1 4//1
|
||||||
|
f 8//2 7//2 6//2
|
||||||
|
f 1//3 5//3 6//3
|
||||||
|
f 2//4 6//4 7//4
|
||||||
|
f 7//5 8//5 4//5
|
||||||
|
f 1//6 4//6 8//6
|
||||||
|
f 1//1 2//1 4//1
|
||||||
|
f 5//2 8//2 6//2
|
||||||
|
f 2//3 1//3 6//3
|
||||||
|
f 3//4 2//4 7//4
|
||||||
|
f 3//5 7//5 4//5
|
||||||
|
f 5//6 1//6 8//6
|
48
A3/resources/frag.glsl
Normal file
48
A3/resources/frag.glsl
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
#version 120
|
||||||
|
|
||||||
|
uniform vec3 lightPos1;
|
||||||
|
uniform vec3 lightPos2;
|
||||||
|
uniform vec3 ka;
|
||||||
|
uniform vec3 kd;
|
||||||
|
uniform vec3 ks;
|
||||||
|
uniform float s;
|
||||||
|
uniform float i1;
|
||||||
|
uniform float i2;
|
||||||
|
|
||||||
|
varying vec3 color; // passed from the vertex shader
|
||||||
|
varying vec4 p;
|
||||||
|
varying vec4 n;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
vec4 normal = normalize(n);
|
||||||
|
vec3 norm = vec3(normal.x, normal.y, normal.z);
|
||||||
|
|
||||||
|
vec4 npos = normalize(p);
|
||||||
|
vec3 pos = vec3(npos.x, npos.y, npos.z);
|
||||||
|
|
||||||
|
vec3 light1 = lightPos1 - vec3(p.x, p.y, p.z);
|
||||||
|
vec3 lnorm1 = normalize(light1);
|
||||||
|
float temp1 = dot(lnorm1, norm);
|
||||||
|
vec3 cd1 = kd*max(0, temp1);
|
||||||
|
|
||||||
|
|
||||||
|
vec3 h1 = normalize(lnorm1 - pos);
|
||||||
|
vec3 cs1 = ks*pow(max(0, dot(h1, norm)), s);
|
||||||
|
|
||||||
|
vec3 light2 = lightPos2 - vec3(p.x, p.y, p.z);
|
||||||
|
vec3 lnorm2 = normalize(light2);
|
||||||
|
float temp2 = dot(lnorm2, norm);
|
||||||
|
vec3 cd2 = kd*max(0, temp2);
|
||||||
|
|
||||||
|
|
||||||
|
vec3 h2 = normalize(lnorm2 - pos);
|
||||||
|
vec3 cs2 = ks*pow(max(0, dot(h2, norm)), s);
|
||||||
|
|
||||||
|
|
||||||
|
vec4 c = vec4(ka.r + cd1.r + cs1.r, ka.g + cd1.g + cs1.g, ka.b + cd1.b + cs1.b, 1.0);
|
||||||
|
vec4 color1 = i1*vec4(ka.r + cd1.r + cs1.r, ka.g + cd1.g + cs1.g, ka.b + cd1.b + cs1.b, 1.0);
|
||||||
|
vec4 color2 = i2*vec4(ka.r + cd2.r + cs2.r, ka.g + cd2.g + cs2.g, ka.b + cd2.b + cs2.b, 1.0);
|
||||||
|
|
||||||
|
gl_FragColor = color1 + color2;
|
||||||
|
}
|
20
A3/resources/sil.glsl
Normal file
20
A3/resources/sil.glsl
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
#version 120
|
||||||
|
|
||||||
|
varying vec4 p;
|
||||||
|
varying vec4 n;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
vec4 normal = normalize(n);
|
||||||
|
vec3 norm = vec3(normal.x, normal.y, normal.z);
|
||||||
|
|
||||||
|
vec4 npos = normalize(p);
|
||||||
|
vec3 pos = vec3(npos.x, npos.y, npos.z);
|
||||||
|
|
||||||
|
float product = dot(norm, pos);
|
||||||
|
|
||||||
|
if(product <= 0.3 && product >= -0.3)
|
||||||
|
gl_FragColor = vec4(0.0f, 0.0f, 0.0f, 1.0f);
|
||||||
|
else
|
||||||
|
gl_FragColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
|
||||||
|
}
|
23
A3/resources/vert.glsl
Normal file
23
A3/resources/vert.glsl
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
#version 120
|
||||||
|
|
||||||
|
uniform mat4 P;
|
||||||
|
uniform mat4 MV;
|
||||||
|
uniform vec3 lightPos1;
|
||||||
|
uniform vec3 lightPos2;
|
||||||
|
uniform float i1;
|
||||||
|
uniform float i2;
|
||||||
|
|
||||||
|
attribute vec4 aPos; // in object space
|
||||||
|
attribute vec3 aNor; // in object space
|
||||||
|
|
||||||
|
varying vec3 color; // Pass to fragment shader
|
||||||
|
varying vec4 p;
|
||||||
|
varying vec4 n;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
gl_Position = P * MV * aPos;
|
||||||
|
p = MV * aPos;
|
||||||
|
n = MV * vec4(aNor, 0.0);
|
||||||
|
color = vec3(0.5, 0.5, 0.5);
|
||||||
|
}
|
BIN
A3/shadow8t4.zip
Normal file
BIN
A3/shadow8t4.zip
Normal file
Binary file not shown.
66
A3/src/Camera.cpp
Normal file
66
A3/src/Camera.cpp
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
#include "Camera.h"
|
||||||
|
#include "MatrixStack.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
|
|
||||||
|
Camera::Camera() :
|
||||||
|
aspect(1.0f),
|
||||||
|
fovy(45.0f),
|
||||||
|
znear(0.1f),
|
||||||
|
zfar(1000.0f),
|
||||||
|
rotations(0.0, 0.0),
|
||||||
|
translations(0.0f, 0.0f, -5.0f),
|
||||||
|
rfactor(0.01f),
|
||||||
|
tfactor(0.001f),
|
||||||
|
sfactor(0.005f)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Camera::~Camera()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void Camera::mouseClicked(float x, float y, bool shift, bool ctrl, bool alt)
|
||||||
|
{
|
||||||
|
mousePrev.x = x;
|
||||||
|
mousePrev.y = y;
|
||||||
|
if(shift) {
|
||||||
|
state = Camera::TRANSLATE;
|
||||||
|
} else if(ctrl) {
|
||||||
|
state = Camera::SCALE;
|
||||||
|
} else {
|
||||||
|
state = Camera::ROTATE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Camera::mouseMoved(float x, float y)
|
||||||
|
{
|
||||||
|
glm::vec2 mouseCurr(x, y);
|
||||||
|
glm::vec2 dv = mouseCurr - mousePrev;
|
||||||
|
switch(state) {
|
||||||
|
case Camera::ROTATE:
|
||||||
|
rotations += rfactor * dv;
|
||||||
|
break;
|
||||||
|
case Camera::TRANSLATE:
|
||||||
|
translations.x -= translations.z * tfactor * dv.x;
|
||||||
|
translations.y += translations.z * tfactor * dv.y;
|
||||||
|
break;
|
||||||
|
case Camera::SCALE:
|
||||||
|
translations.z *= (1.0f - sfactor * dv.y);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
mousePrev = mouseCurr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Camera::applyProjectionMatrix(std::shared_ptr<MatrixStack> P) const
|
||||||
|
{
|
||||||
|
// Modify provided MatrixStack
|
||||||
|
P->multMatrix(glm::perspective(fovy, aspect, znear, zfar));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Camera::applyViewMatrix(std::shared_ptr<MatrixStack> MV) const
|
||||||
|
{
|
||||||
|
MV->translate(translations);
|
||||||
|
MV->rotate(rotations.y, glm::vec3(1.0f, 0.0f, 0.0f));
|
||||||
|
MV->rotate(rotations.x, glm::vec3(0.0f, 1.0f, 0.0f));
|
||||||
|
}
|
47
A3/src/Camera.h
Normal file
47
A3/src/Camera.h
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
#pragma once
|
||||||
|
#ifndef __Camera__
|
||||||
|
#define __Camera__
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#define GLM_FORCE_RADIANS
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
|
class MatrixStack;
|
||||||
|
|
||||||
|
class Camera
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
enum {
|
||||||
|
ROTATE = 0,
|
||||||
|
TRANSLATE,
|
||||||
|
SCALE
|
||||||
|
};
|
||||||
|
|
||||||
|
Camera();
|
||||||
|
virtual ~Camera();
|
||||||
|
void setInitDistance(float z) { translations.z = -std::abs(z); }
|
||||||
|
void setAspect(float a) { aspect = a; };
|
||||||
|
void setRotationFactor(float f) { rfactor = f; };
|
||||||
|
void setTranslationFactor(float f) { tfactor = f; };
|
||||||
|
void setScaleFactor(float f) { sfactor = f; };
|
||||||
|
void mouseClicked(float x, float y, bool shift, bool ctrl, bool alt);
|
||||||
|
void mouseMoved(float x, float y);
|
||||||
|
void applyProjectionMatrix(std::shared_ptr<MatrixStack> P) const;
|
||||||
|
void applyViewMatrix(std::shared_ptr<MatrixStack> MV) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
float aspect;
|
||||||
|
float fovy;
|
||||||
|
float znear;
|
||||||
|
float zfar;
|
||||||
|
glm::vec2 rotations;
|
||||||
|
glm::vec3 translations;
|
||||||
|
glm::vec2 mousePrev;
|
||||||
|
int state;
|
||||||
|
float rfactor;
|
||||||
|
float tfactor;
|
||||||
|
float sfactor;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
152
A3/src/GLSL.cpp
Normal file
152
A3/src/GLSL.cpp
Normal file
|
@ -0,0 +1,152 @@
|
||||||
|
//
|
||||||
|
// Many useful helper functions for GLSL shaders - gleaned from various sources including orange book
|
||||||
|
// Created by zwood on 2/21/10.
|
||||||
|
// Modified by sueda 10/15/15.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "GLSL.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <cassert>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
namespace GLSL {
|
||||||
|
|
||||||
|
const char * errorString(GLenum err)
|
||||||
|
{
|
||||||
|
switch(err) {
|
||||||
|
case GL_NO_ERROR:
|
||||||
|
return "No error";
|
||||||
|
case GL_INVALID_ENUM:
|
||||||
|
return "Invalid enum";
|
||||||
|
case GL_INVALID_VALUE:
|
||||||
|
return "Invalid value";
|
||||||
|
case GL_INVALID_OPERATION:
|
||||||
|
return "Invalid operation";
|
||||||
|
case GL_STACK_OVERFLOW:
|
||||||
|
return "Stack overflow";
|
||||||
|
case GL_STACK_UNDERFLOW:
|
||||||
|
return "Stack underflow";
|
||||||
|
case GL_OUT_OF_MEMORY:
|
||||||
|
return "Out of memory";
|
||||||
|
default:
|
||||||
|
return "No error";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void checkVersion()
|
||||||
|
{
|
||||||
|
int major, minor;
|
||||||
|
major = minor = 0;
|
||||||
|
const char *verstr = (const char *)glGetString(GL_VERSION);
|
||||||
|
|
||||||
|
if((verstr == NULL) || (sscanf(verstr, "%d.%d", &major, &minor) != 2)) {
|
||||||
|
printf("Invalid GL_VERSION format %d.%d\n", major, minor);
|
||||||
|
}
|
||||||
|
if(major < 2) {
|
||||||
|
printf("This shader example will not work due to the installed Opengl version, which is %d.%d.\n", major, minor);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void checkError(const char *str)
|
||||||
|
{
|
||||||
|
GLenum glErr = glGetError();
|
||||||
|
if(glErr != GL_NO_ERROR) {
|
||||||
|
if(str) {
|
||||||
|
printf("%s: ", str);
|
||||||
|
}
|
||||||
|
printf("GL_ERROR = %s.\n", errorString(glErr));
|
||||||
|
assert(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void printShaderInfoLog(GLuint shader)
|
||||||
|
{
|
||||||
|
GLint infologLength = 0;
|
||||||
|
GLint charsWritten = 0;
|
||||||
|
GLchar *infoLog = 0;
|
||||||
|
|
||||||
|
checkError(GET_FILE_LINE);
|
||||||
|
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infologLength);
|
||||||
|
checkError(GET_FILE_LINE);
|
||||||
|
|
||||||
|
if(infologLength > 0) {
|
||||||
|
infoLog = (GLchar *)malloc(infologLength);
|
||||||
|
if(infoLog == NULL) {
|
||||||
|
puts("ERROR: Could not allocate InfoLog buffer");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
glGetShaderInfoLog(shader, infologLength, &charsWritten, infoLog);
|
||||||
|
checkError(GET_FILE_LINE);
|
||||||
|
printf("Shader InfoLog:\n%s\n\n", infoLog);
|
||||||
|
free(infoLog);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void printProgramInfoLog(GLuint program)
|
||||||
|
{
|
||||||
|
GLint infologLength = 0;
|
||||||
|
GLint charsWritten = 0;
|
||||||
|
GLchar *infoLog = 0;
|
||||||
|
|
||||||
|
checkError(GET_FILE_LINE);
|
||||||
|
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infologLength);
|
||||||
|
checkError(GET_FILE_LINE);
|
||||||
|
|
||||||
|
if(infologLength > 0) {
|
||||||
|
infoLog = (GLchar *)malloc(infologLength);
|
||||||
|
if(infoLog == NULL) {
|
||||||
|
puts("ERROR: Could not allocate InfoLog buffer");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
glGetProgramInfoLog(program, infologLength, &charsWritten, infoLog);
|
||||||
|
checkError(GET_FILE_LINE);
|
||||||
|
printf("Program InfoLog:\n%s\n\n", infoLog);
|
||||||
|
free(infoLog);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
char *textFileRead(const char *fn)
|
||||||
|
{
|
||||||
|
FILE *fp;
|
||||||
|
char *content = NULL;
|
||||||
|
int count = 0;
|
||||||
|
if(fn != NULL) {
|
||||||
|
fp = fopen(fn,"rt");
|
||||||
|
if(fp != NULL) {
|
||||||
|
fseek(fp, 0, SEEK_END);
|
||||||
|
count = (int)ftell(fp);
|
||||||
|
rewind(fp);
|
||||||
|
if(count > 0) {
|
||||||
|
content = (char *)malloc(sizeof(char) * (count+1));
|
||||||
|
count = (int)fread(content,sizeof(char),count,fp);
|
||||||
|
content[count] = '\0';
|
||||||
|
}
|
||||||
|
fclose(fp);
|
||||||
|
} else {
|
||||||
|
printf("error loading %s\n", fn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
int textFileWrite(const char *fn, const char *s)
|
||||||
|
{
|
||||||
|
FILE *fp;
|
||||||
|
int status = 0;
|
||||||
|
if(fn != NULL) {
|
||||||
|
fp = fopen(fn,"w");
|
||||||
|
if(fp != NULL) {
|
||||||
|
if(fwrite(s,sizeof(char),strlen(s),fp) == strlen(s)) {
|
||||||
|
status = 1;
|
||||||
|
}
|
||||||
|
fclose(fp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return(status);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
40
A3/src/GLSL.h
Normal file
40
A3/src/GLSL.h
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
//
|
||||||
|
// Many useful helper functions for GLSL shaders - gleaned from various sources including orange book
|
||||||
|
// Created by zwood on 2/21/10.
|
||||||
|
// Modified by sueda 10/15/15.
|
||||||
|
//
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#ifndef __GLSL__
|
||||||
|
#define __GLSL__
|
||||||
|
|
||||||
|
#define GLEW_STATIC
|
||||||
|
#include <GL/glew.h>
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
// For printing out the current file and line number //
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
std::string NumberToString(T x)
|
||||||
|
{
|
||||||
|
std::ostringstream ss;
|
||||||
|
ss << x;
|
||||||
|
return ss.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GET_FILE_LINE (std::string(__FILE__) + ":" + NumberToString(__LINE__)).c_str()
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
namespace GLSL {
|
||||||
|
|
||||||
|
void checkVersion();
|
||||||
|
void checkError(const char *str = 0);
|
||||||
|
void printProgramInfoLog(GLuint program);
|
||||||
|
void printShaderInfoLog(GLuint shader);
|
||||||
|
int textFileWrite(const char *filename, const char *s);
|
||||||
|
char *textFileRead(const char *filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
39
A3/src/Light.cpp
Normal file
39
A3/src/Light.cpp
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
#include "Light.h"
|
||||||
|
|
||||||
|
Light::Light()
|
||||||
|
{
|
||||||
|
pos = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||||
|
intensity = 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
Light::Light(const Light &l)
|
||||||
|
{
|
||||||
|
pos = l.pos;
|
||||||
|
intensity = l.intensity;
|
||||||
|
}
|
||||||
|
|
||||||
|
Light::Light(glm::vec3 p, float i)
|
||||||
|
{
|
||||||
|
pos = p;
|
||||||
|
intensity = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Light::setPos(glm::vec3 p)
|
||||||
|
{
|
||||||
|
this->pos = p;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Light::setIntensity(float i)
|
||||||
|
{
|
||||||
|
this->intensity = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
glm::vec3 Light::getPos()
|
||||||
|
{
|
||||||
|
return this->pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
float Light::getIntensity()
|
||||||
|
{
|
||||||
|
return this->intensity;
|
||||||
|
}
|
27
A3/src/Light.h
Normal file
27
A3/src/Light.h
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
#include <cmath>
|
||||||
|
#include <iostream>
|
||||||
|
#include <GL/glew.h>
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include <glm/gtc/type_ptr.hpp>
|
||||||
|
|
||||||
|
#include "GLSL.h"
|
||||||
|
#include "Camera.h"
|
||||||
|
#include "Shape.h"
|
||||||
|
#include "MatrixStack.h"
|
||||||
|
|
||||||
|
class Light
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
glm::vec3 pos;
|
||||||
|
float intensity;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Light();
|
||||||
|
Light(const Light &l);
|
||||||
|
Light(glm::vec3 p, float i);
|
||||||
|
void setPos(glm::vec3 p);
|
||||||
|
void setIntensity(float i);
|
||||||
|
glm::vec3 getPos();
|
||||||
|
float getIntensity();
|
||||||
|
};
|
39
A3/src/Material.cpp
Normal file
39
A3/src/Material.cpp
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
#include "Material.h"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
Material::Material()
|
||||||
|
{
|
||||||
|
this->ca = glm::vec3(0.3f,0.3f,0.3f);
|
||||||
|
this->cd = glm::vec3(0.3f,0.3f,0.3f);
|
||||||
|
this->cs = glm::vec3(1.0f,1.0f,1.0f);
|
||||||
|
this->shine = 0.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Material::setMaterial(glm::vec3 a, glm::vec3 d, glm::vec3 s, float sh)
|
||||||
|
{
|
||||||
|
this->ca = a;
|
||||||
|
this->cd = d;
|
||||||
|
this->cs = s;
|
||||||
|
this->shine = sh;
|
||||||
|
}
|
||||||
|
|
||||||
|
glm::vec3 Material::getAmbient()
|
||||||
|
{
|
||||||
|
return this->ca;
|
||||||
|
}
|
||||||
|
|
||||||
|
glm::vec3 Material::getDiffuse()
|
||||||
|
{
|
||||||
|
return this->cd;
|
||||||
|
}
|
||||||
|
|
||||||
|
glm::vec3 Material::getSpecular()
|
||||||
|
{
|
||||||
|
return this->cs;
|
||||||
|
}
|
||||||
|
|
||||||
|
float Material::getShiny()
|
||||||
|
{
|
||||||
|
return this->shine;
|
||||||
|
}
|
41
A3/src/Material.h
Normal file
41
A3/src/Material.h
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
#include <cmath>
|
||||||
|
#include <iostream>
|
||||||
|
#include <GL/glew.h>
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include <glm/gtc/type_ptr.hpp>
|
||||||
|
|
||||||
|
#include "GLSL.h"
|
||||||
|
#include "Camera.h"
|
||||||
|
#include "Shape.h"
|
||||||
|
#include "MatrixStack.h"
|
||||||
|
|
||||||
|
class Material
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
glm::vec3 ca;
|
||||||
|
glm::vec3 cd;
|
||||||
|
glm::vec3 cs;
|
||||||
|
float shine;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Material();
|
||||||
|
Material(const Material &m)
|
||||||
|
{
|
||||||
|
ca = m.ca;
|
||||||
|
cd = m.cd;
|
||||||
|
cs = m.cs;
|
||||||
|
}
|
||||||
|
Material(glm::vec3 a, glm::vec3 d, glm::vec3 s, float sh)
|
||||||
|
{
|
||||||
|
ca = a;
|
||||||
|
cd = d;
|
||||||
|
cs = s;
|
||||||
|
shine = sh;
|
||||||
|
}
|
||||||
|
void setMaterial(glm::vec3 a, glm::vec3 d, glm::vec3 s, float sh);
|
||||||
|
glm::vec3 getAmbient();
|
||||||
|
glm::vec3 getDiffuse();
|
||||||
|
glm::vec3 getSpecular();
|
||||||
|
float getShiny();
|
||||||
|
};
|
114
A3/src/MatrixStack.cpp
Normal file
114
A3/src/MatrixStack.cpp
Normal file
|
@ -0,0 +1,114 @@
|
||||||
|
#include "MatrixStack.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <cassert>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#define GLM_FORCE_RADIANS
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include <glm/gtx/transform.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
MatrixStack::MatrixStack()
|
||||||
|
{
|
||||||
|
mstack = make_shared< stack<glm::mat4> >();
|
||||||
|
mstack->push(glm::mat4(1.0));
|
||||||
|
}
|
||||||
|
|
||||||
|
MatrixStack::~MatrixStack()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void MatrixStack::pushMatrix()
|
||||||
|
{
|
||||||
|
const glm::mat4 &top = mstack->top();
|
||||||
|
mstack->push(top);
|
||||||
|
assert(mstack->size() < 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MatrixStack::popMatrix()
|
||||||
|
{
|
||||||
|
assert(!mstack->empty());
|
||||||
|
mstack->pop();
|
||||||
|
// There should always be one matrix left.
|
||||||
|
assert(!mstack->empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
void MatrixStack::loadIdentity()
|
||||||
|
{
|
||||||
|
glm::mat4 &top = mstack->top();
|
||||||
|
top = glm::mat4(1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MatrixStack::translate(const glm::vec3 &t)
|
||||||
|
{
|
||||||
|
glm::mat4 &top = mstack->top();
|
||||||
|
top *= glm::translate(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MatrixStack::translate(float x, float y, float z)
|
||||||
|
{
|
||||||
|
translate(glm::vec3(x, y, z));
|
||||||
|
}
|
||||||
|
|
||||||
|
void MatrixStack::scale(const glm::vec3 &s)
|
||||||
|
{
|
||||||
|
glm::mat4 &top = mstack->top();
|
||||||
|
top *= glm::scale(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MatrixStack::scale(float x, float y, float z)
|
||||||
|
{
|
||||||
|
scale(glm::vec3(x, y, z));
|
||||||
|
}
|
||||||
|
|
||||||
|
void MatrixStack::scale(float s)
|
||||||
|
{
|
||||||
|
scale(glm::vec3(s, s, s));
|
||||||
|
}
|
||||||
|
|
||||||
|
void MatrixStack::rotate(float angle, const glm::vec3 &axis)
|
||||||
|
{
|
||||||
|
glm::mat4 &top = mstack->top();
|
||||||
|
top *= glm::rotate(angle, axis);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MatrixStack::rotate(float angle, float x, float y, float z)
|
||||||
|
{
|
||||||
|
rotate(angle, glm::vec3(x, y, z));
|
||||||
|
}
|
||||||
|
|
||||||
|
void MatrixStack::multMatrix(const glm::mat4 &matrix)
|
||||||
|
{
|
||||||
|
glm::mat4 &top = mstack->top();
|
||||||
|
top *= matrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
const glm::mat4 &MatrixStack::topMatrix() const
|
||||||
|
{
|
||||||
|
return mstack->top();
|
||||||
|
}
|
||||||
|
|
||||||
|
void MatrixStack::print(const glm::mat4 &mat, const char *name)
|
||||||
|
{
|
||||||
|
if(name) {
|
||||||
|
printf("%s = [\n", name);
|
||||||
|
}
|
||||||
|
for(int i = 0; i < 4; ++i) {
|
||||||
|
for(int j = 0; j < 4; ++j) {
|
||||||
|
// mat[j] returns the jth column
|
||||||
|
printf("%- 5.2f ", mat[j][i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
if(name) {
|
||||||
|
printf("];");
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void MatrixStack::print(const char *name) const
|
||||||
|
{
|
||||||
|
print(mstack->top(), name);
|
||||||
|
}
|
50
A3/src/MatrixStack.h
Normal file
50
A3/src/MatrixStack.h
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
#pragma once
|
||||||
|
#ifndef _MatrixStack_H_
|
||||||
|
#define _MatrixStack_H_
|
||||||
|
|
||||||
|
#include <stack>
|
||||||
|
#include <memory>
|
||||||
|
#include <glm/fwd.hpp>
|
||||||
|
|
||||||
|
class MatrixStack
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
MatrixStack();
|
||||||
|
virtual ~MatrixStack();
|
||||||
|
|
||||||
|
// glPushMatrix(): Copies the current matrix and adds it to the top of the stack
|
||||||
|
void pushMatrix();
|
||||||
|
// glPopMatrix(): Removes the top of the stack and sets the current matrix to be the matrix that is now on top
|
||||||
|
void popMatrix();
|
||||||
|
|
||||||
|
// glLoadIdentity(): Sets the top matrix to be the identity
|
||||||
|
void loadIdentity();
|
||||||
|
// glMultMatrix(): Right multiplies the top matrix
|
||||||
|
void multMatrix(const glm::mat4 &matrix);
|
||||||
|
|
||||||
|
// glTranslate(): Right multiplies the top matrix by a translation matrix
|
||||||
|
void translate(const glm::vec3 &trans);
|
||||||
|
void translate(float x, float y, float z);
|
||||||
|
// glScale(): Right multiplies the top matrix by a scaling matrix
|
||||||
|
void scale(const glm::vec3 &scale);
|
||||||
|
void scale(float x, float y, float z);
|
||||||
|
// glScale(): Right multiplies the top matrix by a scaling matrix
|
||||||
|
void scale(float size);
|
||||||
|
// glRotate(): Right multiplies the top matrix by a rotation matrix (angle in radians)
|
||||||
|
void rotate(float angle, const glm::vec3 &axis);
|
||||||
|
void rotate(float angle, float x, float y, float z);
|
||||||
|
|
||||||
|
// glGet(GL_MODELVIEW_MATRIX): Gets the top matrix
|
||||||
|
const glm::mat4 &topMatrix() const;
|
||||||
|
|
||||||
|
// Prints out the specified matrix
|
||||||
|
static void print(const glm::mat4 &mat, const char *name = 0);
|
||||||
|
// Prints out the top matrix
|
||||||
|
void print(const char *name = 0) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::shared_ptr< std::stack<glm::mat4> > mstack;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
126
A3/src/Program.cpp
Normal file
126
A3/src/Program.cpp
Normal file
|
@ -0,0 +1,126 @@
|
||||||
|
#include "Program.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
#include "GLSL.h"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
Program::Program() :
|
||||||
|
vShaderName(""),
|
||||||
|
fShaderName(""),
|
||||||
|
pid(0),
|
||||||
|
verbose(true)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Program::~Program()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void Program::setShaderNames(const string &v, const string &f)
|
||||||
|
{
|
||||||
|
vShaderName = v;
|
||||||
|
fShaderName = f;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Program::init()
|
||||||
|
{
|
||||||
|
GLint rc;
|
||||||
|
|
||||||
|
// Create shader handles
|
||||||
|
GLuint VS = glCreateShader(GL_VERTEX_SHADER);
|
||||||
|
GLuint FS = glCreateShader(GL_FRAGMENT_SHADER);
|
||||||
|
|
||||||
|
// Read shader sources
|
||||||
|
const char *vshader = GLSL::textFileRead(vShaderName.c_str());
|
||||||
|
const char *fshader = GLSL::textFileRead(fShaderName.c_str());
|
||||||
|
glShaderSource(VS, 1, &vshader, NULL);
|
||||||
|
glShaderSource(FS, 1, &fshader, NULL);
|
||||||
|
|
||||||
|
// Compile vertex shader
|
||||||
|
glCompileShader(VS);
|
||||||
|
glGetShaderiv(VS, GL_COMPILE_STATUS, &rc);
|
||||||
|
if(!rc) {
|
||||||
|
if(isVerbose()) {
|
||||||
|
GLSL::printShaderInfoLog(VS);
|
||||||
|
cout << "Error compiling vertex shader " << vShaderName << endl;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compile fragment shader
|
||||||
|
glCompileShader(FS);
|
||||||
|
glGetShaderiv(FS, GL_COMPILE_STATUS, &rc);
|
||||||
|
if(!rc) {
|
||||||
|
if(isVerbose()) {
|
||||||
|
GLSL::printShaderInfoLog(FS);
|
||||||
|
cout << "Error compiling fragment shader " << fShaderName << endl;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the program and link
|
||||||
|
pid = glCreateProgram();
|
||||||
|
glAttachShader(pid, VS);
|
||||||
|
glAttachShader(pid, FS);
|
||||||
|
glLinkProgram(pid);
|
||||||
|
glGetProgramiv(pid, GL_LINK_STATUS, &rc);
|
||||||
|
if(!rc) {
|
||||||
|
if(isVerbose()) {
|
||||||
|
GLSL::printProgramInfoLog(pid);
|
||||||
|
cout << "Error linking shaders " << vShaderName << " and " << fShaderName << endl;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
GLSL::checkError(GET_FILE_LINE);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Program::bind()
|
||||||
|
{
|
||||||
|
glUseProgram(pid);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Program::unbind()
|
||||||
|
{
|
||||||
|
glUseProgram(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Program::addAttribute(const string &name)
|
||||||
|
{
|
||||||
|
attributes[name] = glGetAttribLocation(pid, name.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
void Program::addUniform(const string &name)
|
||||||
|
{
|
||||||
|
uniforms[name] = glGetUniformLocation(pid, name.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
GLint Program::getAttribute(const string &name) const
|
||||||
|
{
|
||||||
|
map<string,GLint>::const_iterator attribute = attributes.find(name.c_str());
|
||||||
|
if(attribute == attributes.end()) {
|
||||||
|
if(isVerbose()) {
|
||||||
|
cout << name << " is not an attribute variable" << endl;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return attribute->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
GLint Program::getUniform(const string &name) const
|
||||||
|
{
|
||||||
|
map<string,GLint>::const_iterator uniform = uniforms.find(name.c_str());
|
||||||
|
if(uniform == uniforms.end()) {
|
||||||
|
if(isVerbose()) {
|
||||||
|
cout << name << " is not a uniform variable" << endl;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return uniform->second;
|
||||||
|
}
|
44
A3/src/Program.h
Normal file
44
A3/src/Program.h
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
#pragma once
|
||||||
|
#ifndef __Program__
|
||||||
|
#define __Program__
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#define GLEW_STATIC
|
||||||
|
#include <GL/glew.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An OpenGL Program (vertex and fragment shaders)
|
||||||
|
*/
|
||||||
|
class Program
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Program();
|
||||||
|
virtual ~Program();
|
||||||
|
|
||||||
|
void setVerbose(bool v) { verbose = v; }
|
||||||
|
bool isVerbose() const { return verbose; }
|
||||||
|
|
||||||
|
void setShaderNames(const std::string &v, const std::string &f);
|
||||||
|
virtual bool init();
|
||||||
|
virtual void bind();
|
||||||
|
virtual void unbind();
|
||||||
|
|
||||||
|
void addAttribute(const std::string &name);
|
||||||
|
void addUniform(const std::string &name);
|
||||||
|
GLint getAttribute(const std::string &name) const;
|
||||||
|
GLint getUniform(const std::string &name) const;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
std::string vShaderName;
|
||||||
|
std::string fShaderName;
|
||||||
|
|
||||||
|
private:
|
||||||
|
GLuint pid;
|
||||||
|
std::map<std::string,GLint> attributes;
|
||||||
|
std::map<std::string,GLint> uniforms;
|
||||||
|
bool verbose;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
165
A3/src/Shape.cpp
Normal file
165
A3/src/Shape.cpp
Normal file
|
@ -0,0 +1,165 @@
|
||||||
|
#include "Shape.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "GLSL.h"
|
||||||
|
#include "Program.h"
|
||||||
|
|
||||||
|
#define GLM_FORCE_RADIANS
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
|
#define TINYOBJLOADER_IMPLEMENTATION
|
||||||
|
#include "tiny_obj_loader.h"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
Shape::Shape() :
|
||||||
|
posBufID(0),
|
||||||
|
norBufID(0),
|
||||||
|
texBufID(0)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Shape::~Shape()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void Shape::loadMesh(const string &meshName)
|
||||||
|
{
|
||||||
|
// Load geometry
|
||||||
|
tinyobj::attrib_t attrib;
|
||||||
|
std::vector<tinyobj::shape_t> shapes;
|
||||||
|
std::vector<tinyobj::material_t> materials;
|
||||||
|
string errStr;
|
||||||
|
bool rc = tinyobj::LoadObj(&attrib, &shapes, &materials, &errStr, meshName.c_str());
|
||||||
|
if(!rc) {
|
||||||
|
cerr << errStr << endl;
|
||||||
|
} else {
|
||||||
|
// Some OBJ files have different indices for vertex positions, normals,
|
||||||
|
// and texture coordinates. For example, a cube corner vertex may have
|
||||||
|
// three different normals. Here, we are going to duplicate all such
|
||||||
|
// vertices.
|
||||||
|
// Loop over shapes
|
||||||
|
for(size_t s = 0; s < shapes.size(); s++) {
|
||||||
|
// Loop over faces (polygons)
|
||||||
|
size_t index_offset = 0;
|
||||||
|
for(size_t f = 0; f < shapes[s].mesh.num_face_vertices.size(); f++) {
|
||||||
|
size_t fv = shapes[s].mesh.num_face_vertices[f];
|
||||||
|
// Loop over vertices in the face.
|
||||||
|
for(size_t v = 0; v < fv; v++) {
|
||||||
|
// access to vertex
|
||||||
|
tinyobj::index_t idx = shapes[s].mesh.indices[index_offset + v];
|
||||||
|
posBuf.push_back(attrib.vertices[3*idx.vertex_index+0]);
|
||||||
|
posBuf.push_back(attrib.vertices[3*idx.vertex_index+1]);
|
||||||
|
posBuf.push_back(attrib.vertices[3*idx.vertex_index+2]);
|
||||||
|
if(!attrib.normals.empty()) {
|
||||||
|
norBuf.push_back(attrib.normals[3*idx.normal_index+0]);
|
||||||
|
norBuf.push_back(attrib.normals[3*idx.normal_index+1]);
|
||||||
|
norBuf.push_back(attrib.normals[3*idx.normal_index+2]);
|
||||||
|
}
|
||||||
|
if(!attrib.texcoords.empty()) {
|
||||||
|
texBuf.push_back(attrib.texcoords[2*idx.texcoord_index+0]);
|
||||||
|
texBuf.push_back(attrib.texcoords[2*idx.texcoord_index+1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
index_offset += fv;
|
||||||
|
// per-face material (IGNORE)
|
||||||
|
shapes[s].mesh.material_ids[f];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Shape::fitToUnitBox()
|
||||||
|
{
|
||||||
|
// Scale the vertex positions so that they fit within [-1, +1] in all three dimensions.
|
||||||
|
glm::vec3 vmin(posBuf[0], posBuf[1], posBuf[2]);
|
||||||
|
glm::vec3 vmax(posBuf[0], posBuf[1], posBuf[2]);
|
||||||
|
for(int i = 0; i < (int)posBuf.size(); i += 3) {
|
||||||
|
glm::vec3 v(posBuf[i], posBuf[i+1], posBuf[i+2]);
|
||||||
|
vmin.x = min(vmin.x, v.x);
|
||||||
|
vmin.y = min(vmin.y, v.y);
|
||||||
|
vmin.z = min(vmin.z, v.z);
|
||||||
|
vmax.x = max(vmax.x, v.x);
|
||||||
|
vmax.y = max(vmax.y, v.y);
|
||||||
|
vmax.z = max(vmax.z, v.z);
|
||||||
|
}
|
||||||
|
glm::vec3 center = 0.5f*(vmin + vmax);
|
||||||
|
glm::vec3 diff = vmax - vmin;
|
||||||
|
float diffmax = diff.x;
|
||||||
|
diffmax = max(diffmax, diff.y);
|
||||||
|
diffmax = max(diffmax, diff.z);
|
||||||
|
float scale = 1.0f / diffmax;
|
||||||
|
for(int i = 0; i < (int)posBuf.size(); i += 3) {
|
||||||
|
posBuf[i ] = (posBuf[i ] - center.x) * scale;
|
||||||
|
posBuf[i+1] = (posBuf[i+1] - center.y) * scale;
|
||||||
|
posBuf[i+2] = (posBuf[i+2] - center.z) * scale;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Shape::init()
|
||||||
|
{
|
||||||
|
// Send the position array to the GPU
|
||||||
|
glGenBuffers(1, &posBufID);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, posBufID);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, posBuf.size()*sizeof(float), &posBuf[0], GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
// Send the normal array to the GPU
|
||||||
|
if(!norBuf.empty()) {
|
||||||
|
glGenBuffers(1, &norBufID);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, norBufID);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, norBuf.size()*sizeof(float), &norBuf[0], GL_STATIC_DRAW);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send the texture array to the GPU
|
||||||
|
if(!texBuf.empty()) {
|
||||||
|
glGenBuffers(1, &texBufID);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, texBufID);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, texBuf.size()*sizeof(float), &texBuf[0], GL_STATIC_DRAW);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unbind the arrays
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
|
|
||||||
|
GLSL::checkError(GET_FILE_LINE);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Shape::draw(const shared_ptr<Program> prog) const
|
||||||
|
{
|
||||||
|
// Bind position buffer
|
||||||
|
int h_pos = prog->getAttribute("aPos");
|
||||||
|
glEnableVertexAttribArray(h_pos);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, posBufID);
|
||||||
|
glVertexAttribPointer(h_pos, 3, GL_FLOAT, GL_FALSE, 0, (const void *)0);
|
||||||
|
|
||||||
|
// Bind normal buffer
|
||||||
|
int h_nor = prog->getAttribute("aNor");
|
||||||
|
if(h_nor != -1 && norBufID != 0) {
|
||||||
|
glEnableVertexAttribArray(h_nor);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, norBufID);
|
||||||
|
glVertexAttribPointer(h_nor, 3, GL_FLOAT, GL_FALSE, 0, (const void *)0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bind texcoords buffer
|
||||||
|
int h_tex = prog->getAttribute("aTex");
|
||||||
|
if(h_tex != -1 && texBufID != 0) {
|
||||||
|
glEnableVertexAttribArray(h_tex);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, texBufID);
|
||||||
|
glVertexAttribPointer(h_tex, 2, GL_FLOAT, GL_FALSE, 0, (const void *)0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw
|
||||||
|
int count = posBuf.size()/3; // number of indices to be rendered
|
||||||
|
glDrawArrays(GL_TRIANGLES, 0, count);
|
||||||
|
|
||||||
|
// Disable and unbind
|
||||||
|
if(h_tex != -1) {
|
||||||
|
glDisableVertexAttribArray(h_tex);
|
||||||
|
}
|
||||||
|
if(h_nor != -1) {
|
||||||
|
glDisableVertexAttribArray(h_nor);
|
||||||
|
}
|
||||||
|
glDisableVertexAttribArray(h_pos);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
|
|
||||||
|
GLSL::checkError(GET_FILE_LINE);
|
||||||
|
}
|
37
A3/src/Shape.h
Normal file
37
A3/src/Shape.h
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
#pragma once
|
||||||
|
#ifndef _SHAPE_H_
|
||||||
|
#define _SHAPE_H_
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
class Program;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A shape defined by a list of triangles
|
||||||
|
* - posBuf should be of length 3*ntris
|
||||||
|
* - norBuf should be of length 3*ntris (if normals are available)
|
||||||
|
* - texBuf should be of length 2*ntris (if texture coords are available)
|
||||||
|
* posBufID, norBufID, and texBufID are OpenGL buffer identifiers.
|
||||||
|
*/
|
||||||
|
class Shape
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Shape();
|
||||||
|
virtual ~Shape();
|
||||||
|
void loadMesh(const std::string &meshName);
|
||||||
|
void fitToUnitBox();
|
||||||
|
void init();
|
||||||
|
void draw(const std::shared_ptr<Program> prog) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<float> posBuf;
|
||||||
|
std::vector<float> norBuf;
|
||||||
|
std::vector<float> texBuf;
|
||||||
|
unsigned posBufID;
|
||||||
|
unsigned norBufID;
|
||||||
|
unsigned texBufID;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
362
A3/src/main.cpp
Normal file
362
A3/src/main.cpp
Normal file
|
@ -0,0 +1,362 @@
|
||||||
|
#include <cassert>
|
||||||
|
#include <cstring>
|
||||||
|
#define _USE_MATH_DEFINES
|
||||||
|
#include <cmath>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#define GLEW_STATIC
|
||||||
|
#include <GL/glew.h>
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
|
#define GLM_FORCE_RADIANS
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include <glm/gtc/type_ptr.hpp>
|
||||||
|
|
||||||
|
#include "Camera.h"
|
||||||
|
#include "GLSL.h"
|
||||||
|
#include "MatrixStack.h"
|
||||||
|
#include "Program.h"
|
||||||
|
#include "Shape.h"
|
||||||
|
#include "Material.h"
|
||||||
|
#include "Light.h"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
GLFWwindow *window; // Main application window
|
||||||
|
string RESOURCE_DIR = "./"; // Where the resources are loaded from
|
||||||
|
|
||||||
|
shared_ptr<Camera> camera;
|
||||||
|
shared_ptr<Program> prog;
|
||||||
|
shared_ptr<Program> sprog;
|
||||||
|
shared_ptr<Shape> shape;
|
||||||
|
|
||||||
|
bool keyToggles[256] = {false}; // only for English keyboards!
|
||||||
|
|
||||||
|
Material m[3];
|
||||||
|
Light l[2];
|
||||||
|
int ind = 0;
|
||||||
|
int lind = 0;
|
||||||
|
int sind = 0;
|
||||||
|
|
||||||
|
unsigned int pid0;
|
||||||
|
unsigned int pid1;
|
||||||
|
|
||||||
|
// This function is called when a GLFW error occurs
|
||||||
|
static void error_callback(int error, const char *description)
|
||||||
|
{
|
||||||
|
cerr << description << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// This function is called when a key is pressed
|
||||||
|
static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
|
||||||
|
{
|
||||||
|
if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
|
||||||
|
glfwSetWindowShouldClose(window, GL_TRUE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// This function is called when the mouse is clicked
|
||||||
|
static void mouse_button_callback(GLFWwindow *window, int button, int action, int mods)
|
||||||
|
{
|
||||||
|
// Get the current mouse position.
|
||||||
|
double xmouse, ymouse;
|
||||||
|
glfwGetCursorPos(window, &xmouse, &ymouse);
|
||||||
|
// Get current window size.
|
||||||
|
int width, height;
|
||||||
|
glfwGetWindowSize(window, &width, &height);
|
||||||
|
if(action == GLFW_PRESS) {
|
||||||
|
bool shift = (mods & GLFW_MOD_SHIFT) != 0;
|
||||||
|
bool ctrl = (mods & GLFW_MOD_CONTROL) != 0;
|
||||||
|
bool alt = (mods & GLFW_MOD_ALT) != 0;
|
||||||
|
camera->mouseClicked((float)xmouse, (float)ymouse, shift, ctrl, alt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// This function is called when the mouse moves
|
||||||
|
static void cursor_position_callback(GLFWwindow* window, double xmouse, double ymouse)
|
||||||
|
{
|
||||||
|
int state = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT);
|
||||||
|
if(state == GLFW_PRESS) {
|
||||||
|
camera->mouseMoved((float)xmouse, (float)ymouse);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void char_callback(GLFWwindow *window, unsigned int key)
|
||||||
|
{
|
||||||
|
keyToggles[key] = !keyToggles[key];
|
||||||
|
char cp = (char)key;
|
||||||
|
|
||||||
|
if(cp == 'S')
|
||||||
|
{
|
||||||
|
sind++;
|
||||||
|
if(sind > 1)
|
||||||
|
{
|
||||||
|
sind = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(cp == 's')
|
||||||
|
{
|
||||||
|
sind--;
|
||||||
|
if(sind < 0)
|
||||||
|
{
|
||||||
|
sind = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(cp == 'M')
|
||||||
|
{
|
||||||
|
ind++;
|
||||||
|
if(ind > 2)
|
||||||
|
{
|
||||||
|
ind = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(cp == 'm')
|
||||||
|
{
|
||||||
|
ind --;
|
||||||
|
if(ind < 0)
|
||||||
|
{
|
||||||
|
ind = 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(cp == 'L')
|
||||||
|
{
|
||||||
|
lind++;
|
||||||
|
if(lind > 1)
|
||||||
|
{
|
||||||
|
lind = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(cp == 'l')
|
||||||
|
{
|
||||||
|
lind--;
|
||||||
|
if(lind < 0)
|
||||||
|
{
|
||||||
|
lind = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(cp == 'X')
|
||||||
|
{
|
||||||
|
glm::vec3 temp = l[lind].getPos();
|
||||||
|
l[lind].setPos(glm::vec3(temp.x + 0.1f, temp.y, temp.z));
|
||||||
|
}
|
||||||
|
if(cp == 'x')
|
||||||
|
{
|
||||||
|
glm::vec3 temp = l[lind].getPos();
|
||||||
|
l[lind].setPos(glm::vec3(temp.x - 0.1f, temp.y, temp.z));
|
||||||
|
}
|
||||||
|
|
||||||
|
if(cp == 'Y')
|
||||||
|
{
|
||||||
|
glm::vec3 temp = l[lind].getPos();
|
||||||
|
l[lind].setPos(glm::vec3(temp.x, temp.y + 0.1f, temp.z));
|
||||||
|
}
|
||||||
|
if(cp == 'y')
|
||||||
|
{
|
||||||
|
glm::vec3 temp = l[lind].getPos();
|
||||||
|
l[lind].setPos(glm::vec3(temp.x, temp.y - 0.1f, temp.z));
|
||||||
|
}
|
||||||
|
|
||||||
|
if(cp == 'Z')
|
||||||
|
{
|
||||||
|
glm::vec3 temp = l[lind].getPos();
|
||||||
|
l[lind].setPos(glm::vec3(temp.x, temp.y, temp.z + 0.1f));
|
||||||
|
}
|
||||||
|
if(cp == 'z')
|
||||||
|
{
|
||||||
|
glm::vec3 temp = l[lind].getPos();
|
||||||
|
l[lind].setPos(glm::vec3(temp.x, temp.y, temp.z - 0.1f));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the window is resized, capture the new size and reset the viewport
|
||||||
|
static void resize_callback(GLFWwindow *window, int width, int height)
|
||||||
|
{
|
||||||
|
glViewport(0, 0, width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
// This function is called once to initialize the scene and OpenGL
|
||||||
|
static void init()
|
||||||
|
{
|
||||||
|
// Initialize time.
|
||||||
|
glfwSetTime(0.0);
|
||||||
|
|
||||||
|
// Set background color.
|
||||||
|
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
|
||||||
|
// Enable z-buffer test.
|
||||||
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
|
||||||
|
sprog = make_shared<Program>();
|
||||||
|
sprog->setShaderNames(RESOURCE_DIR + "vert.glsl", RESOURCE_DIR + "sil.glsl");
|
||||||
|
sprog->setVerbose(false);
|
||||||
|
sprog->init();
|
||||||
|
sprog->addAttribute("aPos");
|
||||||
|
sprog->addAttribute("aNor");
|
||||||
|
sprog->addUniform("MV");
|
||||||
|
sprog->addUniform("P");
|
||||||
|
|
||||||
|
prog = make_shared<Program>();
|
||||||
|
prog->setShaderNames(RESOURCE_DIR + "vert.glsl", RESOURCE_DIR + "frag.glsl");
|
||||||
|
prog->setVerbose(false);
|
||||||
|
prog->init();
|
||||||
|
prog->addAttribute("aPos");
|
||||||
|
prog->addAttribute("aNor");
|
||||||
|
prog->addUniform("MV");
|
||||||
|
prog->addUniform("P");
|
||||||
|
prog->addUniform("lightPos1");
|
||||||
|
prog->addUniform("lightPos2");
|
||||||
|
prog->addUniform("ka");
|
||||||
|
prog->addUniform("kd");
|
||||||
|
prog->addUniform("ks");
|
||||||
|
prog->addUniform("s");
|
||||||
|
prog->addUniform("i1");
|
||||||
|
prog->addUniform("i2");
|
||||||
|
|
||||||
|
camera = make_shared<Camera>();
|
||||||
|
camera->setInitDistance(2.0f);
|
||||||
|
|
||||||
|
shape = make_shared<Shape>();
|
||||||
|
shape->loadMesh(RESOURCE_DIR + "bunny.obj");
|
||||||
|
shape->fitToUnitBox();
|
||||||
|
shape->init();
|
||||||
|
Material m1;
|
||||||
|
m1.setMaterial(glm::vec3(0.0f, 0.0f, 0.4f), glm::vec3(0.2f, 0.1f, 0.7f), glm::vec3(0.5f, 0.5f, 0.5f), 200.0f);
|
||||||
|
Material m2;
|
||||||
|
m2.setMaterial(glm::vec3(0.3f, 0.3f, 0.4f), glm::vec3(0.2f, 0.2f, 0.3f), glm::vec3(0.1f, 0.1f, 0.1f), 10.0f);
|
||||||
|
Material m3;
|
||||||
|
m3.setMaterial(glm::vec3(0.2f, 0.2f, 0.2f), glm::vec3(0.8f, 0.7f, 0.7f), glm::vec3(1.0f, 0.9f, 0.8f), 200.0f);
|
||||||
|
m[0] = m1;
|
||||||
|
m[1] = m2;
|
||||||
|
m[2] = m3;
|
||||||
|
|
||||||
|
Light l1(glm::vec3(1.0f, 1.0f, 1.0f), 0.8f);
|
||||||
|
Light l2(glm::vec3(-1.0f, 1.0f, 1.0f), 0.2f);
|
||||||
|
l[0] = l1;
|
||||||
|
l[1] = l2;
|
||||||
|
|
||||||
|
GLSL::checkError(GET_FILE_LINE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// This function is called every frame to draw the scene.
|
||||||
|
static void render()
|
||||||
|
{
|
||||||
|
// Clear framebuffer.
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
if(keyToggles[(unsigned)'c']) {
|
||||||
|
glEnable(GL_CULL_FACE);
|
||||||
|
} else {
|
||||||
|
glDisable(GL_CULL_FACE);
|
||||||
|
}
|
||||||
|
// Get current frame buffer size.
|
||||||
|
int width, height;
|
||||||
|
glfwGetFramebufferSize(window, &width, &height);
|
||||||
|
camera->setAspect((float)width/(float)height);
|
||||||
|
|
||||||
|
// Matrix stacks
|
||||||
|
auto P = make_shared<MatrixStack>();
|
||||||
|
auto MV = make_shared<MatrixStack>();
|
||||||
|
|
||||||
|
// Apply camera transforms
|
||||||
|
P->pushMatrix();
|
||||||
|
camera->applyProjectionMatrix(P);
|
||||||
|
MV->pushMatrix();
|
||||||
|
camera->applyViewMatrix(MV);
|
||||||
|
|
||||||
|
glm::vec3 ambient = m[ind].getAmbient();
|
||||||
|
glm::vec3 diffuse = m[ind].getDiffuse();
|
||||||
|
glm::vec3 specular = m[ind].getSpecular();
|
||||||
|
float shine = m[ind].getShiny();
|
||||||
|
|
||||||
|
if(sind == 1)
|
||||||
|
{
|
||||||
|
sprog->bind();
|
||||||
|
glUniformMatrix4fv(sprog->getUniform("P"), 1, GL_FALSE, glm::value_ptr(P->topMatrix()));
|
||||||
|
glUniformMatrix4fv(sprog->getUniform("MV"), 1, GL_FALSE, glm::value_ptr(MV->topMatrix()));
|
||||||
|
shape->draw(sprog);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(sind == 0)
|
||||||
|
{
|
||||||
|
prog->bind();
|
||||||
|
glUniformMatrix4fv(prog->getUniform("P"), 1, GL_FALSE, glm::value_ptr(P->topMatrix()));
|
||||||
|
glUniformMatrix4fv(prog->getUniform("MV"), 1, GL_FALSE, glm::value_ptr(MV->topMatrix()));
|
||||||
|
glUniform3f(prog->getUniform("ka"), ambient.r, ambient.g, ambient.b);
|
||||||
|
glUniform3f(prog->getUniform("lightPos1"), l[0].getPos().x, l[0].getPos().y, l[0].getPos().z);
|
||||||
|
glUniform3f(prog->getUniform("lightPos2"), l[1].getPos().x, l[1].getPos().y, l[1].getPos().z);
|
||||||
|
glUniform3f(prog->getUniform("kd"), diffuse.r, diffuse.g, diffuse.b);
|
||||||
|
glUniform3f(prog->getUniform("ks"), specular.r, specular.g, specular.b);
|
||||||
|
glUniform1f(prog->getUniform("s"), shine);
|
||||||
|
glUniform1f(prog->getUniform("i1"), l[0].getIntensity());
|
||||||
|
glUniform1f(prog->getUniform("i2"), l[1].getIntensity());
|
||||||
|
shape->draw(prog);
|
||||||
|
}
|
||||||
|
|
||||||
|
MV->popMatrix();
|
||||||
|
P->popMatrix();
|
||||||
|
|
||||||
|
GLSL::checkError(GET_FILE_LINE);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
if(argc < 2) {
|
||||||
|
cout << "Please specify the resource directory." << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
RESOURCE_DIR = argv[1] + string("/");
|
||||||
|
|
||||||
|
// Set error callback.
|
||||||
|
glfwSetErrorCallback(error_callback);
|
||||||
|
// Initialize the library.
|
||||||
|
if(!glfwInit()) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
// Create a windowed mode window and its OpenGL context.
|
||||||
|
window = glfwCreateWindow(640, 480, "YOUR NAME", NULL, NULL);
|
||||||
|
if(!window) {
|
||||||
|
glfwTerminate();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
// Make the window's context current.
|
||||||
|
glfwMakeContextCurrent(window);
|
||||||
|
// Initialize GLEW.
|
||||||
|
glewExperimental = true;
|
||||||
|
if(glewInit() != GLEW_OK) {
|
||||||
|
cerr << "Failed to initialize GLEW" << endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
glGetError(); // A bug in glewInit() causes an error that we can safely ignore.
|
||||||
|
cout << "OpenGL version: " << glGetString(GL_VERSION) << endl;
|
||||||
|
cout << "GLSL version: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << endl;
|
||||||
|
GLSL::checkVersion();
|
||||||
|
// Set vsync.
|
||||||
|
glfwSwapInterval(1);
|
||||||
|
// Set keyboard callback.
|
||||||
|
glfwSetKeyCallback(window, key_callback);
|
||||||
|
// Set char callback.
|
||||||
|
glfwSetCharCallback(window, char_callback);
|
||||||
|
// Set cursor position callback.
|
||||||
|
glfwSetCursorPosCallback(window, cursor_position_callback);
|
||||||
|
// Set mouse button callback.
|
||||||
|
glfwSetMouseButtonCallback(window, mouse_button_callback);
|
||||||
|
// Set the window resize call back.
|
||||||
|
glfwSetFramebufferSizeCallback(window, resize_callback);
|
||||||
|
// Initialize scene.
|
||||||
|
init();
|
||||||
|
// Loop until the user closes the window.
|
||||||
|
while(!glfwWindowShouldClose(window)) {
|
||||||
|
// Render scene.
|
||||||
|
render();
|
||||||
|
// Swap front and back buffers.
|
||||||
|
glfwSwapBuffers(window);
|
||||||
|
// Poll for and process events.
|
||||||
|
glfwPollEvents();
|
||||||
|
}
|
||||||
|
// Quit program.
|
||||||
|
glfwDestroyWindow(window);
|
||||||
|
glfwTerminate();
|
||||||
|
return 0;
|
||||||
|
}
|
1922
A3/src/tiny_obj_loader.h
Normal file
1922
A3/src/tiny_obj_loader.h
Normal file
File diff suppressed because it is too large
Load diff
|
@ -5,7 +5,7 @@ varying vec3 vMyColor;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
if(distance(vec2(middle, vec2(gl_FragCoord.x, gl_FragCoord.y)) < 20)
|
if(distance(middle, vec2(gl_FragCoord.x, gl_FragCoord.y)) < 20)
|
||||||
{
|
{
|
||||||
discard;
|
discard;
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue