summaryrefslogtreecommitdiff
path: root/gfx/gl/shader.cpp
blob: 06edc5c6d9a5c94e6a80af1f889223ce667aef98 (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
#include "shader.h"
#include "gfx/gl/glSource.h"
#include <array>
#include <cstddef>
#include <gfx/gl/shaders/fs-basicShader.h>
#include <gfx/gl/shaders/fs-landmassShader.h>
#include <gfx/gl/shaders/fs-waterShader.h>
#include <gfx/gl/shaders/vs-basicShader.h>
#include <gfx/gl/shaders/vs-landmassShader.h>
#include <gfx/gl/shaders/vs-waterShader.h>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtx/transform.hpp>
#include <location.hpp>
#include <maths.h>
#include <stdexcept>
#include <string>

Shader::ProgramHandle::ProgramHandle(GLuint vs, GLuint fs) : viewProjection_uniform {}, model_uniform {}
{
	glAttachShader(m_program, vs);
	glAttachShader(m_program, fs);

	glBindAttribLocation(m_program, 0, "position");
	glBindAttribLocation(m_program, 1, "texCoord");
	glBindAttribLocation(m_program, 2, "normal");

	glLinkProgram(m_program);
	GLsource::CheckShaderError(m_program, GL_LINK_STATUS, true, "Error linking shader program");

	glValidateProgram(m_program);
	GLsource::CheckShaderError(m_program, GL_VALIDATE_STATUS, true, "Invalid shader program");

	viewProjection_uniform = glGetUniformLocation(m_program, "viewProjection");
	model_uniform = glGetUniformLocation(m_program, "model");
}

Shader::Shader() :
	programs {{{
					   basicShader_vs.compile(),
					   basicShader_fs.compile(),
			   },
			{
					waterShader_vs.compile(),
					waterShader_fs.compile(),
			},
			{
					landmassShader_vs.compile(),
					landmassShader_fs.compile(),
			},
			{
					landmassShader_vs.compile(),
					basicShader_fs.compile(),
			}}}
{
}

void
Shader::setView(glm::mat4 proj) const
{
	for (const auto & prog : programs) {
		glUseProgram(prog.m_program);
		glUniformMatrix4fv(prog.viewProjection_uniform, 1, GL_FALSE, glm::value_ptr(proj));
	}
}

void
Shader::setUniform(const GLchar * uniform, glm::vec3 v) const
{
	for (const auto & prog : programs) {
		if (auto loc = glGetUniformLocation(prog.m_program, uniform); loc >= 0) {
			glUseProgram(prog.m_program);
			glUniform3fv(loc, 1, glm::value_ptr(v));
		}
	}
}

void
Shader::setModel(const Location & loc, Program pid) const
{
	auto & prog = programs[static_cast<std::size_t>(pid)];
	glUseProgram(prog.m_program);
	if (prog.model_uniform >= 0) {
		const auto model {glm::translate(loc.pos) * rotate_ypr(loc.rot)};
		glUniformMatrix4fv(prog.model_uniform, 1, GL_FALSE, glm::value_ptr(model));
	}
}