summaryrefslogtreecommitdiff
path: root/gfx/gl/sceneShader.cpp
blob: cdb8d2f1ce874112673fc743b61573079b886d25 (plain)
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
#include "sceneShader.h"
#include <array>
#include <gfx/gl/shaders/fs-landmass.h>
#include <gfx/gl/shaders/fs-material.h>
#include <gfx/gl/shaders/fs-pointLight.h>
#include <gfx/gl/shaders/fs-spotLight.h>
#include <gfx/gl/shaders/fs-water.h>
#include <gfx/gl/shaders/gs-pointLight.h>
#include <gfx/gl/shaders/gs-spotLight.h>
#include <gfx/gl/shaders/vs-dynamicPoint.h>
#include <gfx/gl/shaders/vs-dynamicPointInst.h>
#include <gfx/gl/shaders/vs-fixedPoint.h>
#include <gfx/gl/shaders/vs-pointLight.h>
#include <gfx/gl/shaders/vs-spotLight.h>
#include <gfx/gl/shaders/vs-water.h>
#include <gfx/gl/vertexArrayObject.h>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtx/transform.hpp>
#include <location.h>
#include <maths.h>

SceneShader::SceneShader() :
	basicInst {dynamicPointInst_vs, material_fs}, landmass {fixedPoint_vs, landmass_fs},
	absolute {fixedPoint_vs, material_fs}, spotLightInst {spotLight_vs, spotLight_gs, spotLight_fs}
{
}

void
SceneShader::setViewProjection(const GlobalPosition3D & viewPoint, const glm::mat4 & viewProjection) const
{
	for (const auto & prog : std::initializer_list<const SceneProgram *> {
				 &basic, &basicInst, &water, &landmass, &absolute, &pointLight, &spotLightInst}) {
		prog->setViewProjection(viewPoint, viewProjection);
	}
}

void
SceneShader::setViewPort(const ViewPort & viewPort) const
{
	for (const auto & prog : std::initializer_list<const SceneProgram *> {
				 &basic, &basicInst, &water, &landmass, &absolute, &pointLight, &spotLightInst}) {
		prog->setViewPort(viewPort);
	}
}

void
SceneShader::SceneProgram::setViewProjection(const GlobalPosition3D & viewPoint, const glm::mat4 & viewProjection) const
{
	glUseProgram(*this);
	glUniformMatrix4fv(viewProjectionLoc, 1, GL_FALSE, glm::value_ptr(viewProjection));
	glUniform3iv(viewPointLoc, 1, glm::value_ptr(viewPoint));
}

void
SceneShader::SceneProgram::setViewPort(const ViewPort & viewPort) const
{
	if (viewPortLoc >= 0) {
		glUseProgram(*this);
		glUniform4iv(viewPortLoc, 1, glm::value_ptr(viewPort));
	}
}

SceneShader::BasicProgram::BasicProgram() :
	SceneProgram {dynamicPoint_vs, material_fs}, modelLoc {*this, "model"}, modelPosLoc {*this, "modelPos"}
{
}

void
SceneShader::BasicProgram::setModel(Location const & location) const
{
	glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(rotate_ypr(location.rot)));
	glUniform3iv(modelPosLoc, 1, glm::value_ptr(location.pos));
}

void
SceneShader::BasicProgram::use(Location const & location) const
{
	Program::use();
	setModel(location);
}

SceneShader::WaterProgram::WaterProgram() : SceneProgram {water_vs, water_fs}, waveLoc {*this, "waves"} { }

void
SceneShader::WaterProgram::use(float waveCycle) const
{
	Program::use();
	glUniform1f(waveLoc, waveCycle);
}

SceneShader::PointLightShader::PointLightShader() :
	SceneProgram {pointLight_vs, pointLight_gs, pointLight_fs}, colourLoc {*this, "colour"}, kqLoc {*this, "kq"},
	viewPointLoc {*this, "viewPoint"}
{
	VertexArrayObject {va}.addAttribs<Position3D>(b);
}

void
SceneShader::PointLightShader::add(const Position3D & position, const RGB & colour, const float kq) const
{
	Program::use();
	glBindVertexArray(va);
	glBindBuffer(GL_ARRAY_BUFFER, b);
	glUniform3fv(colourLoc, 1, glm::value_ptr(colour));
	glUniform1f(kqLoc, kq);
	glBufferData(GL_ARRAY_BUFFER, sizeof(Position3D), glm::value_ptr(position), GL_DYNAMIC_DRAW);
	glDrawArrays(GL_POINTS, 0, 1);
}