[EDIT] edit lib files
This commit is contained in:
52
src/FpsCam.cpp
Normal file
52
src/FpsCam.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#include "FpsCam.h"
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
FpsCam::FpsCam(GLFWwindow* window)
|
||||
{
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
if (glfwRawMouseMotionSupported())
|
||||
glfwSetInputMode(window, GLFW_RAW_MOUSE_MOTION, GLFW_TRUE);
|
||||
}
|
||||
|
||||
|
||||
glm::mat4 FpsCam::getMatrix()
|
||||
{
|
||||
glm::mat4 ret(1.0f);
|
||||
ret = glm::rotate(ret, rotation.x, glm::vec3(1, 0, 0));
|
||||
ret = glm::rotate(ret, rotation.y, glm::vec3(0, 1, 0));
|
||||
ret = glm::translate(ret, position);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void FpsCam::move(float angle, float fac)
|
||||
{
|
||||
position.x += (float)cos(rotation.y + glm::radians(angle)) * fac;
|
||||
position.z += (float)sin(rotation.y + glm::radians(angle)) * fac;
|
||||
}
|
||||
|
||||
|
||||
void FpsCam::update(GLFWwindow* window)
|
||||
{
|
||||
double x, y;
|
||||
glfwGetCursorPos(window, &x, &y);
|
||||
|
||||
static double lastX = x;
|
||||
static double lastY = y;
|
||||
|
||||
rotation.x -= (float)(lastY - y) / 100.0f;
|
||||
rotation.y -= (float)(lastX - x) / 100.0f;
|
||||
|
||||
lastX = x;
|
||||
lastY = y;
|
||||
|
||||
|
||||
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
||||
move(0, 0.05f);
|
||||
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
||||
move(180, 0.05f);
|
||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
||||
move(90, 0.05f);
|
||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
||||
move(-90, 0.05f);
|
||||
}
|
||||
21
src/FpsCam.h
Normal file
21
src/FpsCam.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
struct GLFWwindow;
|
||||
|
||||
class FpsCam
|
||||
{
|
||||
public:
|
||||
FpsCam(GLFWwindow*);
|
||||
|
||||
glm::mat4 getMatrix();
|
||||
void update(GLFWwindow*);
|
||||
|
||||
private:
|
||||
glm::vec3 position = glm::vec3(0, 0, 0);
|
||||
glm::vec2 rotation = glm::vec2(0, 0);
|
||||
|
||||
void move(float angle, float fac);
|
||||
};
|
||||
|
||||
|
||||
100
src/main.cpp
Normal file
100
src/main.cpp
Normal file
@@ -0,0 +1,100 @@
|
||||
#include <GL/glew.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include "tigl.h"
|
||||
#include "FpsCam.h"
|
||||
#include <iostream>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
using tigl::Vertex;
|
||||
|
||||
#pragma comment(lib, "glfw3.lib")
|
||||
#pragma comment(lib, "glew32s.lib")
|
||||
#pragma comment(lib, "opengl32.lib")
|
||||
|
||||
GLFWwindow* window;
|
||||
|
||||
void init();
|
||||
void update();
|
||||
void draw();
|
||||
|
||||
int main(void)
|
||||
{
|
||||
if (!glfwInit())
|
||||
throw "Could not initialize glwf";
|
||||
window = glfwCreateWindow(1400, 800, "Hello World", NULL, NULL);
|
||||
if (!window)
|
||||
{
|
||||
glfwTerminate();
|
||||
throw "Could not initialize glwf";
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
tigl::init();
|
||||
|
||||
init();
|
||||
|
||||
while (!glfwWindowShouldClose(window))
|
||||
{
|
||||
update();
|
||||
draw();
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
glfwTerminate();
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
FpsCam* camera;
|
||||
|
||||
void init()
|
||||
{
|
||||
glfwSetKeyCallback(window, [](GLFWwindow* window, int key, int scancode, int action, int mods)
|
||||
{
|
||||
if (key == GLFW_KEY_ESCAPE)
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
});
|
||||
camera = new FpsCam(window);
|
||||
}
|
||||
|
||||
|
||||
void update()
|
||||
{
|
||||
camera->update(window);
|
||||
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
glClearColor(0.3f, 0.4f, 0.6f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
int viewport[4];
|
||||
glGetIntegerv(GL_VIEWPORT, viewport);
|
||||
glm::mat4 projection = glm::perspective(glm::radians(75.0f), viewport[2] / (float)viewport[3], 0.01f, 100.0f);
|
||||
|
||||
tigl::shader->setProjectionMatrix(projection);
|
||||
tigl::shader->setViewMatrix(camera->getMatrix());
|
||||
tigl::shader->setModelMatrix(glm::mat4(1.0f));
|
||||
|
||||
tigl::shader->enableColor(true);
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
tigl::begin(GL_TRIANGLES);
|
||||
tigl::addVertex(Vertex::PC(glm::vec3(-2, -1, -4), glm::vec4(1, 0, 0, 1)));
|
||||
tigl::addVertex(Vertex::PC(glm::vec3(2, -1, -4), glm::vec4(0, 1, 0, 1)));
|
||||
tigl::addVertex(Vertex::PC(glm::vec3(0, 1, -4), glm::vec4(0, 0, 1, 1)));
|
||||
|
||||
|
||||
tigl::addVertex(Vertex::PC(glm::vec3(-10, -1, -10), glm::vec4(1, 1, 1, 1)));
|
||||
tigl::addVertex(Vertex::PC(glm::vec3(-10, -1, 10), glm::vec4(1, 1, 1, 1)));
|
||||
tigl::addVertex(Vertex::PC(glm::vec3(10, -1, 10), glm::vec4(1, 1, 1, 1)));
|
||||
|
||||
tigl::addVertex(Vertex::PC(glm::vec3(-10, -1, -10), glm::vec4(1, 1, 1, 1)));
|
||||
tigl::addVertex(Vertex::PC(glm::vec3(10, -1, -10), glm::vec4(1, 1, 1, 1)));
|
||||
tigl::addVertex(Vertex::PC(glm::vec3(10, -1, 10), glm::vec4(1, 1, 1, 1)));
|
||||
|
||||
tigl::end();
|
||||
}
|
||||
465
src/tigl.cpp
Normal file
465
src/tigl.cpp
Normal file
@@ -0,0 +1,465 @@
|
||||
#include "tigl.h"
|
||||
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
namespace tigl
|
||||
{
|
||||
class ShaderImpl : public internal::Shader
|
||||
{
|
||||
public:
|
||||
ShaderImpl();
|
||||
~ShaderImpl();
|
||||
void use();
|
||||
|
||||
void setProjectionMatrix(const glm::mat4& matrix);
|
||||
void setViewMatrix(const glm::mat4& matrix);
|
||||
void setModelMatrix(const glm::mat4& matrix);
|
||||
|
||||
void enableColor(bool enabled) { setUniform(Uniform::useColor, enabled); }
|
||||
void enableTexture(bool enabled) { setUniform(Uniform::useTexture, enabled); }
|
||||
void enableLighting(bool enabled) { setUniform(Uniform::useLighting, enabled); }
|
||||
void setLightCount(int count) { setUniform(Uniform::lightCount, count); }
|
||||
void setLightDirectional(int lightNr, bool isDirectional) { setUniform("lights[" + std::to_string(lightNr) + "].directional", isDirectional); }
|
||||
void setLightPosition(int lightNr, const glm::vec3& position) { setUniform("lights[" + std::to_string(lightNr) + "].position", position); }
|
||||
void setLightAmbient(int lightNr, const glm::vec3& color) { setUniform("lights[" + std::to_string(lightNr) + "].ambient", color); }
|
||||
void setLightDiffuse(int lightNr, const glm::vec3& color) { setUniform("lights[" + std::to_string(lightNr) + "].diffuse", color); }
|
||||
void setLightSpecular(int lightNr, const glm::vec3& color) { setUniform("lights[" + std::to_string(lightNr) + "].specular", color); }
|
||||
void setShinyness(float shinyness) { setUniform(Uniform::shinyness, shinyness); }
|
||||
|
||||
void enableColorMult(bool enabled) { setUniform(Uniform::useColorMult, enabled); }
|
||||
void setColorMult(const glm::vec4& color) { setUniform(Uniform::colorMult, color); }
|
||||
|
||||
void enableAlphaTest(bool enabled) { setUniform(Uniform::useAlphaTest, enabled); }
|
||||
|
||||
void enableFog(bool enabled) { setUniform(Uniform::useFog, enabled); }
|
||||
void setFogLinear(float begin, float end) {
|
||||
setUniform(Uniform::fogType, 0);
|
||||
setUniform(Uniform::fogLinNear, begin);
|
||||
setUniform(Uniform::fogLinFar, end);
|
||||
}
|
||||
void setFogExp(float density) {
|
||||
setUniform(Uniform::fogType, 1);
|
||||
setUniform(Uniform::fogExpDensity, density);
|
||||
}
|
||||
void setFogExp2(float density) {
|
||||
setUniform(Uniform::fogType, 2);
|
||||
setUniform(Uniform::fogExpDensity, density);
|
||||
}
|
||||
|
||||
private:
|
||||
void addShader(int shaderProgram, GLenum shaderType, const std::string& shader);
|
||||
GLuint programId;
|
||||
enum Uniform
|
||||
{
|
||||
//matrices
|
||||
ProjectionMatrix,
|
||||
ViewMatrix,
|
||||
ModelMatrix,
|
||||
NormalMatrix,
|
||||
//flags
|
||||
useColor,
|
||||
useColorMult,
|
||||
useTexture,
|
||||
useLighting,
|
||||
useAlphaTest,
|
||||
useFog,
|
||||
//parameters
|
||||
colorMult,
|
||||
lightCount,
|
||||
shinyness,
|
||||
cameraPosition,
|
||||
fogType,
|
||||
fogLinNear,
|
||||
fogLinFar,
|
||||
fogExpDensity,
|
||||
|
||||
|
||||
UniformMax
|
||||
};
|
||||
|
||||
int uniforms[UniformMax];
|
||||
|
||||
void setUniform(Uniform uniform, const glm::mat4& value);
|
||||
void setUniform(Uniform uniform, const glm::mat3& value);
|
||||
void setUniform(Uniform uniform, const glm::vec4& value);
|
||||
void setUniform(Uniform uniform, const glm::vec3& value);
|
||||
void setUniform(Uniform uniform, const glm::vec2& value);
|
||||
void setUniform(Uniform uniform, float value);
|
||||
void setUniform(Uniform uniform, int value);
|
||||
void setUniform(Uniform uniform, bool value);
|
||||
|
||||
//slower
|
||||
void setUniform(const std::string& uniform, const glm::mat4& value);
|
||||
void setUniform(const std::string& uniform, const glm::mat3& value);
|
||||
void setUniform(const std::string& uniform, const glm::vec4& value);
|
||||
void setUniform(const std::string& uniform, const glm::vec3& value);
|
||||
void setUniform(const std::string& uniform, const glm::vec2& value);
|
||||
void setUniform(const std::string& uniform, float value);
|
||||
void setUniform(const std::string& uniform, int value);
|
||||
void setUniform(const std::string& uniform, bool value);
|
||||
|
||||
glm::mat4 modelMatrix;
|
||||
glm::mat4 projectionMatrix;
|
||||
glm::mat4 viewMatrix;
|
||||
|
||||
};
|
||||
|
||||
std::unique_ptr<internal::Shader> shader;
|
||||
int attributePosition = 0;
|
||||
int attributeColor = 1;
|
||||
int attributeTexcoord = 2;
|
||||
int attributeNormal = 3;
|
||||
|
||||
|
||||
// Initializes shader used
|
||||
void init()
|
||||
{
|
||||
glewInit();
|
||||
shader.reset(new ShaderImpl());
|
||||
|
||||
glEnableVertexAttribArray(0);
|
||||
glEnableVertexAttribArray(1);
|
||||
glEnableVertexAttribArray(2);
|
||||
glEnableVertexAttribArray(3);
|
||||
}
|
||||
|
||||
std::vector<Vertex> vertices;
|
||||
GLenum shape = 0;
|
||||
|
||||
void begin(GLenum shape)
|
||||
{
|
||||
assert(tigl::shape == 0);
|
||||
tigl::shape = shape;
|
||||
vertices.clear();
|
||||
}
|
||||
void addVertex(const Vertex& vertex)
|
||||
{
|
||||
vertices.push_back(vertex);
|
||||
}
|
||||
void end()
|
||||
{
|
||||
assert(shape != 0);
|
||||
drawVertices(shape, vertices);
|
||||
shape = 0;
|
||||
}
|
||||
|
||||
void drawVertices(GLenum shape, const std::vector<Vertex>& vertices)
|
||||
{
|
||||
if (vertices.size() > 0)
|
||||
{
|
||||
glVertexAttribPointer(tigl::attributePosition, 3, GL_FLOAT, false, sizeof(Vertex), &vertices[0].position);
|
||||
glVertexAttribPointer(tigl::attributeColor, 4, GL_FLOAT, false, sizeof(Vertex), &vertices[0].color);
|
||||
glVertexAttribPointer(tigl::attributeTexcoord, 2, GL_FLOAT, false, sizeof(Vertex), &vertices[0].texcoord);
|
||||
glVertexAttribPointer(tigl::attributeNormal, 3, GL_FLOAT, false, sizeof(Vertex), &vertices[0].normal);
|
||||
glDrawArrays(shape, 0, (GLsizei)vertices.size());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
ShaderImpl::ShaderImpl()
|
||||
{
|
||||
this->programId = glCreateProgram();
|
||||
addShader(this->programId, GL_VERTEX_SHADER, R"ESC(#version 330
|
||||
|
||||
layout (location = 0) in vec3 a_position;
|
||||
layout (location = 1) in vec4 a_color;
|
||||
layout (location = 2) in vec2 a_texcoord;
|
||||
layout (location = 3) in vec3 a_normal;
|
||||
|
||||
uniform mat4 modelMatrix = mat4(1.0);
|
||||
uniform mat4 viewMatrix = mat4(1.0);
|
||||
uniform mat4 projectionMatrix = mat4(1.0);
|
||||
uniform mat3 normalMatrix = mat3(1.0);
|
||||
|
||||
out vec4 color;
|
||||
out vec2 texCoord;
|
||||
out vec3 normal;
|
||||
out vec3 position;
|
||||
|
||||
void main()
|
||||
{
|
||||
texCoord = a_texcoord;
|
||||
color = a_color;
|
||||
normal = normalMatrix * a_normal;
|
||||
position = vec3(modelMatrix * vec4(a_position,1));
|
||||
gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(a_position,1);
|
||||
}
|
||||
)ESC");
|
||||
addShader(this->programId, GL_FRAGMENT_SHADER, R"ESC(#version 330
|
||||
layout(location = 0) out vec4 fragColor;
|
||||
uniform sampler2D s_texture;
|
||||
|
||||
//flags
|
||||
uniform bool useColor = false;
|
||||
uniform bool useColorMult = false;
|
||||
uniform bool useTexture = false;
|
||||
uniform bool useLighting = false;
|
||||
uniform bool useAlphaTest = false;
|
||||
uniform bool useFog = false;
|
||||
//parameters
|
||||
uniform vec4 colorMult = vec4(1,1,1,1);
|
||||
uniform vec3 fogColor = vec3(1.0);
|
||||
uniform vec3 cameraPosition;
|
||||
|
||||
uniform int fogType = 0;
|
||||
uniform float fogLinNear = 0;
|
||||
uniform float fogLinFar = 100;
|
||||
uniform float fogExpDensity = 0;
|
||||
|
||||
|
||||
uniform float shinyness = 0;
|
||||
struct Light
|
||||
{
|
||||
bool directional;
|
||||
vec3 position;
|
||||
vec3 diffuse;
|
||||
vec3 ambient;
|
||||
vec3 specular;
|
||||
};
|
||||
uniform Light lights[5];
|
||||
uniform int lightCount = 1;
|
||||
|
||||
|
||||
in vec4 color;
|
||||
in vec2 texCoord;
|
||||
in vec3 normal;
|
||||
in vec3 position;
|
||||
|
||||
|
||||
float fogFactorLinear(const float dist, const float start, const float end) {
|
||||
return 1.0 - clamp((end - dist) / (end - start), 0.0, 1.0);
|
||||
}
|
||||
|
||||
float fogFactorExp2(const float dist, const float density) {
|
||||
const float LOG2 = -1.442695;
|
||||
float d = density * dist;
|
||||
return 1.0 - clamp(exp2(d * d * LOG2), 0.0, 1.0);
|
||||
}
|
||||
|
||||
float fogFactorExp(const float dist, const float density) {
|
||||
return 1.0 - clamp(exp(-density * dist), 0.0, 1.0);
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
vec4 outputColor = vec4(1,1,1,1);
|
||||
if(useColor)
|
||||
outputColor *= color;
|
||||
if(useColorMult)
|
||||
outputColor *= colorMult;
|
||||
if(useTexture)
|
||||
outputColor *= texture2D(s_texture, texCoord);
|
||||
|
||||
if(useLighting) {
|
||||
vec3 ambient;
|
||||
vec3 specular;
|
||||
vec3 diffuse;
|
||||
|
||||
for(int i = 0; i < lightCount; i++) {
|
||||
|
||||
vec3 lightDir = normalize(lights[i].position - position);
|
||||
|
||||
ambient += lights[i].ambient;
|
||||
|
||||
float diffuseFactor = max(0, dot(lightDir, normalize(normal)));
|
||||
diffuse += diffuseFactor * lights[i].diffuse;
|
||||
|
||||
vec3 reflectDir = reflect(-lightDir, normalize(normal));
|
||||
float specularFactor = pow(max(dot(normalize(cameraPosition-position), reflectDir), 0.0), shinyness);
|
||||
specular += specularFactor * lights[i].specular;
|
||||
}
|
||||
|
||||
outputColor.rgb = (ambient + specular + diffuse) * outputColor.rgb;
|
||||
}
|
||||
if(useFog) {
|
||||
float fogDistance = gl_FragCoord.z / gl_FragCoord.w;
|
||||
if(fogType == 0)
|
||||
outputColor.rgb = mix(outputColor.rgb, fogColor, fogFactorLinear(fogDistance, fogLinNear,fogLinFar));
|
||||
else if(fogType == 1)
|
||||
outputColor.rgb = mix(outputColor.rgb, fogColor, fogFactorExp(fogDistance, fogExpDensity));
|
||||
else if(fogType == 2)
|
||||
outputColor.rgb = mix(outputColor.rgb, fogColor, fogFactorExp2(fogDistance, fogExpDensity));
|
||||
|
||||
}
|
||||
|
||||
|
||||
if(useAlphaTest && outputColor.a < 0.01)
|
||||
discard;
|
||||
fragColor = outputColor;
|
||||
}
|
||||
)ESC");
|
||||
glLinkProgram(programId);
|
||||
|
||||
GLint status;
|
||||
glGetProgramiv(programId, GL_COMPILE_STATUS, &status);
|
||||
if (status == GL_FALSE)
|
||||
{
|
||||
int length, charsWritten;
|
||||
glGetProgramiv(programId, GL_INFO_LOG_LENGTH, &length);
|
||||
char* infolog = new char[length + 1];
|
||||
memset(infolog, 0, length + 1);
|
||||
glGetProgramInfoLog(programId, length, &charsWritten, infolog);
|
||||
std::cout << "Error compiling shader:\n" << infolog << std::endl;
|
||||
delete[] infolog;
|
||||
return;
|
||||
}
|
||||
|
||||
uniforms[Uniform::ModelMatrix] = glGetUniformLocation(programId, "modelMatrix");
|
||||
uniforms[Uniform::ViewMatrix] = glGetUniformLocation(programId, "viewMatrix");
|
||||
uniforms[Uniform::ProjectionMatrix] = glGetUniformLocation(programId, "projectionMatrix");
|
||||
uniforms[Uniform::NormalMatrix] = glGetUniformLocation(programId, "normalMatrix");
|
||||
uniforms[Uniform::useColor] = glGetUniformLocation(programId, "useColor");
|
||||
uniforms[Uniform::useColorMult] = glGetUniformLocation(programId, "useColorMult");
|
||||
uniforms[Uniform::useTexture] = glGetUniformLocation(programId, "useTexture");
|
||||
uniforms[Uniform::useLighting] = glGetUniformLocation(programId, "useLighting");
|
||||
uniforms[Uniform::useAlphaTest] = glGetUniformLocation(programId, "useAlphaTest");
|
||||
uniforms[Uniform::useFog] = glGetUniformLocation(programId, "useFog");
|
||||
uniforms[Uniform::colorMult] = glGetUniformLocation(programId, "colorMult");
|
||||
uniforms[Uniform::cameraPosition] = glGetUniformLocation(programId, "cameraPosition");
|
||||
uniforms[Uniform::shinyness] = glGetUniformLocation(programId, "shinyness");
|
||||
uniforms[Uniform::fogType] = glGetUniformLocation(programId, "fogType");
|
||||
uniforms[Uniform::fogLinNear] = glGetUniformLocation(programId, "fogLinNear");
|
||||
uniforms[Uniform::fogLinFar] = glGetUniformLocation(programId, "fogLinFar");
|
||||
uniforms[Uniform::fogExpDensity] = glGetUniformLocation(programId, "fogExpDensity");
|
||||
|
||||
use();
|
||||
}
|
||||
|
||||
ShaderImpl::~ShaderImpl()
|
||||
{
|
||||
|
||||
}
|
||||
void ShaderImpl::use()
|
||||
{
|
||||
glUseProgram(programId);
|
||||
}
|
||||
|
||||
void ShaderImpl::addShader(int shaderProgram, GLenum shaderType, const std::string& shader)
|
||||
{
|
||||
GLuint shaderId = glCreateShader(shaderType);
|
||||
const char* shaderSource = shader.c_str();
|
||||
glShaderSource(shaderId, 1, &shaderSource, NULL);
|
||||
glCompileShader(shaderId);
|
||||
GLint status;
|
||||
glGetShaderiv(shaderId, GL_COMPILE_STATUS, &status);
|
||||
if (status == GL_FALSE)
|
||||
{
|
||||
int length, charsWritten;
|
||||
glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &length);
|
||||
char* infolog = new char[length + 1];
|
||||
memset(infolog, 0, length + 1);
|
||||
glGetShaderInfoLog(shaderId, length, &charsWritten, infolog);
|
||||
std::cout << "Error compiling shader:\n" << infolog << std::endl;
|
||||
delete[] infolog;
|
||||
return;
|
||||
}
|
||||
glAttachShader(programId, shaderId);
|
||||
}
|
||||
|
||||
|
||||
void ShaderImpl::setProjectionMatrix(const glm::mat4& matrix)
|
||||
{
|
||||
this->projectionMatrix = matrix;
|
||||
setUniform(Uniform::ProjectionMatrix, matrix);
|
||||
}
|
||||
|
||||
void ShaderImpl::setViewMatrix(const glm::mat4& matrix)
|
||||
{
|
||||
this->viewMatrix = matrix;
|
||||
setUniform(Uniform::ViewMatrix, matrix);
|
||||
|
||||
glm::vec4 cameraPosition = glm::inverse(matrix) * glm::vec4(0, 0, 0, 1);
|
||||
setUniform(Uniform::cameraPosition, glm::vec3(cameraPosition));
|
||||
|
||||
}
|
||||
|
||||
void ShaderImpl::setModelMatrix(const glm::mat4& matrix)
|
||||
{
|
||||
this->modelMatrix = matrix;
|
||||
setUniform(Uniform::ModelMatrix, matrix);
|
||||
setUniform(Uniform::NormalMatrix, glm::mat3(glm::transpose(glm::inverse(modelMatrix))));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void ShaderImpl::setUniform(Uniform uniform, const glm::mat4& value)
|
||||
{
|
||||
glUniformMatrix4fv(uniforms[uniform], 1, false, glm::value_ptr(value));
|
||||
}
|
||||
|
||||
void ShaderImpl::setUniform(Uniform uniform, const glm::mat3& value)
|
||||
{
|
||||
glUniformMatrix3fv(uniforms[uniform], 1, false, glm::value_ptr(value));
|
||||
}
|
||||
|
||||
void ShaderImpl::setUniform(Uniform uniform, const glm::vec4& value)
|
||||
{
|
||||
glUniform4fv(uniforms[uniform], 1, glm::value_ptr(value));
|
||||
}
|
||||
void ShaderImpl::setUniform(Uniform uniform, const glm::vec3& value)
|
||||
{
|
||||
glUniform3fv(uniforms[uniform], 1, glm::value_ptr(value));
|
||||
}
|
||||
void ShaderImpl::setUniform(Uniform uniform, const glm::vec2& value)
|
||||
{
|
||||
glUniform2fv(uniforms[uniform], 1, glm::value_ptr(value));
|
||||
}
|
||||
|
||||
void ShaderImpl::setUniform(Uniform uniform, bool value)
|
||||
{
|
||||
glUniform1i(uniforms[uniform], value ? GL_TRUE : GL_FALSE);
|
||||
}
|
||||
void ShaderImpl::setUniform(Uniform uniform, int value)
|
||||
{
|
||||
glUniform1i(uniforms[uniform], value);
|
||||
}
|
||||
void ShaderImpl::setUniform(Uniform uniform, float value)
|
||||
{
|
||||
glUniform1f(uniforms[uniform], value);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void ShaderImpl::setUniform(const std::string& uniform, const glm::mat4& value)
|
||||
{
|
||||
glUniformMatrix4fv(glGetUniformLocation(programId, uniform.c_str()), 1, false, glm::value_ptr(value));
|
||||
}
|
||||
|
||||
void ShaderImpl::setUniform(const std::string& uniform, const glm::mat3& value)
|
||||
{
|
||||
glUniformMatrix3fv(glGetUniformLocation(programId, uniform.c_str()), 1, false, glm::value_ptr(value));
|
||||
}
|
||||
|
||||
void ShaderImpl::setUniform(const std::string& uniform, const glm::vec4& value)
|
||||
{
|
||||
glUniform4fv(glGetUniformLocation(programId, uniform.c_str()), 1, glm::value_ptr(value));
|
||||
}
|
||||
void ShaderImpl::setUniform(const std::string& uniform, const glm::vec3& value)
|
||||
{
|
||||
glUniform3fv(glGetUniformLocation(programId, uniform.c_str()), 1, glm::value_ptr(value));
|
||||
}
|
||||
void ShaderImpl::setUniform(const std::string& uniform, const glm::vec2& value)
|
||||
{
|
||||
glUniform2fv(glGetUniformLocation(programId, uniform.c_str()), 1, glm::value_ptr(value));
|
||||
}
|
||||
|
||||
|
||||
void ShaderImpl::setUniform(const std::string& uniform, bool value)
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(programId, uniform.c_str()), value ? GL_TRUE : GL_FALSE);
|
||||
}
|
||||
void ShaderImpl::setUniform(const std::string& uniform, int value)
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(programId, uniform.c_str()), value);
|
||||
}
|
||||
void ShaderImpl::setUniform(const std::string& uniform, float value)
|
||||
{
|
||||
glUniform1f(glGetUniformLocation(programId, uniform.c_str()), value);
|
||||
}
|
||||
|
||||
}
|
||||
148
src/tigl.h
Normal file
148
src/tigl.h
Normal file
@@ -0,0 +1,148 @@
|
||||
#pragma once
|
||||
|
||||
#include <GL/glew.h>
|
||||
#include <memory>
|
||||
#include <glm/glm.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace tigl
|
||||
{
|
||||
namespace internal
|
||||
{
|
||||
class Shader
|
||||
{
|
||||
public:
|
||||
virtual ~Shader() {};
|
||||
|
||||
// Sets the projection matrix
|
||||
virtual void setProjectionMatrix(const glm::mat4& matrix) = 0;
|
||||
|
||||
// Sets the view (camera) matrix
|
||||
virtual void setViewMatrix(const glm::mat4& matrix) = 0;
|
||||
|
||||
// Sets the model matrix
|
||||
virtual void setModelMatrix(const glm::mat4& matrix) = 0;
|
||||
|
||||
// enables the use of the colors set in vertices
|
||||
virtual void enableColor(bool enabled) = 0;
|
||||
|
||||
// enables the use of texture coordinats set in vertices, and uses textures set in texture sampler
|
||||
virtual void enableTexture(bool enabled) = 0;
|
||||
|
||||
// enables the lighting
|
||||
virtual void enableLighting(bool enabled) = 0;
|
||||
|
||||
// sets the number of lights
|
||||
virtual void setLightCount(int count) = 0;
|
||||
|
||||
// sets the light as directional or positional
|
||||
virtual void setLightDirectional(int lightNr, bool isDirectional) = 0;
|
||||
|
||||
// sets the position of the light. If the light is a directional light, the position is interpreted as a direction
|
||||
virtual void setLightPosition(int lightNr, const glm::vec3& position) = 0;
|
||||
|
||||
// sets the ambient color of a light
|
||||
virtual void setLightAmbient(int lightNr, const glm::vec3& color) = 0;
|
||||
|
||||
// sets the diffuse color of a light
|
||||
virtual void setLightDiffuse(int lightNr, const glm::vec3& color) = 0;
|
||||
|
||||
// sets the specular color of a light
|
||||
virtual void setLightSpecular(int lightNr, const glm::vec3& color) = 0;
|
||||
|
||||
// sets the shinyness of the material drawn. Used for specular calculations
|
||||
virtual void setShinyness(float shinyness) = 0;
|
||||
|
||||
// Enables color multiplication. If enabled, all colors (texture and vertex colors) will be multiplied by the color set by the setColorMult method
|
||||
virtual void enableColorMult(bool enabled) = 0;
|
||||
|
||||
// Changes the color that output will be multiplied with when enableColorMult is enabled
|
||||
virtual void setColorMult(const glm::vec4& color) = 0;
|
||||
|
||||
// Enables alpha testing. Will stop rendering everything with a low alpha value
|
||||
virtual void enableAlphaTest(bool enabled) = 0;
|
||||
|
||||
// Enables fog
|
||||
virtual void enableFog(bool enabled) = 0;
|
||||
|
||||
// Sets the fog to linear
|
||||
virtual void setFogLinear(float begin, float end) = 0;
|
||||
|
||||
// Sets the fog to Exponential
|
||||
virtual void setFogExp(float density) = 0;
|
||||
|
||||
// Sets the fog to Exponential
|
||||
virtual void setFogExp2(float density) = 0;
|
||||
};
|
||||
}
|
||||
// A simple structure to store vertices. Can store positions, normals, colors and texture coordinats
|
||||
struct Vertex
|
||||
{
|
||||
public:
|
||||
glm::vec3 position;
|
||||
glm::vec3 normal;
|
||||
glm::vec4 color;
|
||||
glm::vec2 texcoord;
|
||||
|
||||
// Creates a vertex with a position
|
||||
static Vertex P(const glm::vec3& position) {
|
||||
return { position, glm::vec3(0,1,0), glm::vec4(1,1,1,1), glm::vec2(0,0) };
|
||||
}
|
||||
|
||||
// Creates a vertex with a position and a color
|
||||
static Vertex PC(const glm::vec3& position, const glm::vec4& color) {
|
||||
return { position, glm::vec3(0,1,0), color, glm::vec2(0,0) };
|
||||
}
|
||||
|
||||
// Creates a vertex with a position and a texture coordinat
|
||||
static Vertex PT(const glm::vec3& position, const glm::vec2& texcoord) {
|
||||
return { position, glm::vec3(0,1,0), glm::vec4(1,1,1,1), texcoord };
|
||||
}
|
||||
|
||||
// Creates a vertex with a position and a normal
|
||||
static Vertex PN(const glm::vec3& position, const glm::vec3& normal) {
|
||||
return { position, normal, glm::vec4(1,1,1,1), glm::vec2(0,0) };
|
||||
}
|
||||
|
||||
// Creates a vertex with a position, a texture coordinat and a color
|
||||
static Vertex PTC(const glm::vec3& position, const glm::vec2& texcoord, const glm::vec4 &color) {
|
||||
return { position, glm::vec3(0,1,0), color, texcoord };
|
||||
}
|
||||
|
||||
|
||||
// Creates a vertex with a position, color and normal
|
||||
static Vertex PCN(const glm::vec3& position, const glm::vec4& color, const glm::vec3& normal) {
|
||||
return { position, normal, color, glm::vec2(0,0) };
|
||||
}
|
||||
|
||||
// Creates a vertex with a position, texture coordinat and normal
|
||||
static Vertex PTN(const glm::vec3& position, const glm::vec2& texcoord, const glm::vec3& normal) {
|
||||
return { position, normal, glm::vec4(1,1,1,1), texcoord };
|
||||
}
|
||||
|
||||
// Creates a vertex with a position, color, texture coordinat and normal
|
||||
static Vertex PCTN(const glm::vec3& position, const glm::vec4& color, const glm::vec2& texcoord, const glm::vec3& normal) {
|
||||
return { position, normal, color, texcoord };
|
||||
}
|
||||
};
|
||||
// Access point for the shader
|
||||
extern std::unique_ptr<internal::Shader> shader;
|
||||
|
||||
// Call to initialize. Loads the shader
|
||||
void init();
|
||||
|
||||
// Call to start a set of OpenGL primitives
|
||||
void begin(GLenum shape);
|
||||
|
||||
// Adds a single vertex to the set
|
||||
void addVertex(const Vertex& vertex);
|
||||
|
||||
// Finishes and draws the vertices given
|
||||
void end();
|
||||
|
||||
// Draws a full array of vertices
|
||||
void drawVertices(GLenum shape, const std::vector<Vertex> &vertices);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user