/
hge-sun-shader.cpp
114 lines (114 loc) · 4.28 KB
/
hge-sun-shader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "hge-sun-shader.hpp"
#include "hge-shader-engine.hpp"
#include <iostream>
#include <cassert>
#define HGEPRINTCODELINE std::cout << "Debugging: file:" << __FILE__ << " line:" << __LINE__ << std::endl << std::flush;
hge::shader::SunShader::SunShader(const glm::vec3 &sunDirection):
shaderProgram(render::ShaderEngine::createProgram())
{
std::string pVS(
"#version 430\n"
"layout (location = 0) in vec3 vertex;"
//All of the normals must be normalized.
"layout (location = 1) in vec3 normal;"
"layout (location = 2) in vec2 uv;"
"out vec2 outUV;"
"out float lightIntensity;"
"uniform mat4 mvp;"
"uniform mat4 m;"
//Sun direction must be normalized.
"uniform vec3 s;"
"void main()"
"{"
"lightIntensity = float(max(float(dot(s, vec3(m * vec4(normal, 1.0)))), 0.1));"
"gl_Position = mvp * vec4(vertex, 1.0);"
"outUV = uv;"
"}");
///////////////////////////////////////////////////////////////////////
std::string pFS(
"#version 430\n"
"in vec2 outUV;"
"out vec4 fragColor;"
"in float lightIntensity;"
"uniform sampler2D textureSampler;"
"void main()"
"{"
"fragColor=texture2D(textureSampler,outUV.xy)*lightIntensity;"
"};");
///////////////////////////////////////////////////////////////////////
vertexShaderProgram = render::ShaderEngine::addShaderToProgram(pVS, GL_VERTEX_SHADER, shaderProgram);
fragmentShaderProgram = render::ShaderEngine::addShaderToProgram(pFS, GL_FRAGMENT_SHADER, shaderProgram);
hge::render::ShaderEngine::run(shaderProgram);
modelMatrixUniformLocation = render::ShaderEngine::getUniformLocation(std::string("m"), shaderProgram);
assert(modelMatrixUniformLocation != 0xFFFFFFFF);
modelViewProjectionMatrixUniformLocation = render::ShaderEngine::getUniformLocation(std::string("mvp"), shaderProgram);
assert(modelViewProjectionMatrixUniformLocation != 0xFFFFFFFF);
sunLightDirectionUniformLocation = render::ShaderEngine::getUniformLocation(std::string("s"), shaderProgram);
assert(sunLightDirectionUniformLocation != 0xFFFFFFFF);
textureSamplerLocation = render::ShaderEngine::getUniformLocation(std::string("textureSampler"), shaderProgram);
assert(textureSamplerLocation != 0xFFFFFFFF);
glUniform1i(textureSamplerLocation, 0);
}
hge::shader::SunShader::~SunShader()
{
hge::render::ShaderEngine::endObject(vertexShaderProgram);
hge::render::ShaderEngine::endObject(fragmentShaderProgram);
hge::render::ShaderEngine::endProgram(shaderProgram);
}
void hge::shader::SunShader::setModelMatrix(const glm::mat4 &modelMatrix)
{
glUniformMatrix4fv(modelMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(modelMatrix));
}
void hge::shader::SunShader::setModelViewProjectionMatrix(const glm::mat4 &modelViewProjectionMatrix)
{
glUniformMatrix4fv(modelViewProjectionMatrixUniformLocation, 1, GL_FALSE, glm::value_ptr(modelViewProjectionMatrix));
}
void hge::shader::SunShader::setLODNumber(const GLuint& lodNumber)
{
std::cerr << "Error this shader does not have LOD system." << std::endl;
HGEPRINTCODELINE
std::terminate();
}
void hge::shader::SunShader::setSunDirection(const glm::vec3 &sunDirection)
{
glUniform3fv(sunLightDirectionUniformLocation, 1, glm::value_ptr(sunDirection));
}
void hge::shader::SunShader::use()
{
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*) 0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(3 * sizeof(GLfloat)));
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(6 * sizeof(GLfloat)));
}
GLuint hge::shader::SunShader::getModelViewProjectionMatrixUniformLocation()
{
return modelViewProjectionMatrixUniformLocation;
}
GLuint hge::shader::SunShader::getModelMatrixUniformLoaction()
{
return modelMatrixUniformLocation;
}
GLuint hge::shader::SunShader::getVertexShaderProgram()
{
return vertexShaderProgram;
}
GLuint hge::shader::SunShader::getGeometryShaderProgram()
{
std::cerr << "Error this shader program does not have geometry shader." << std::endl;
HGEPRINTCODELINE
std::terminate();
}
GLuint hge::shader::SunShader::getFragmentShaderProgram()
{
return fragmentShaderProgram;
}
GLuint hge::shader::SunShader::getShaderProgram()
{
return shaderProgram;
}
GLuint hge::shader::SunShader::getTextureSamplerLocation()
{
return textureSamplerLocation;
}