terravisor/source/uniforms.cpp

70 lines
3.1 KiB
C++

#include "uniforms.h"
#include <GL/glew.h>
namespace Uniforms
{
SceneUniforms SceneData;
LightUniforms LightData;
MaterialUniforms MaterialData;
GeoUniforms GeoData;
//IDs for the buffer objects holding the uniform block data
GLuint sceneUbo = 0;
GLuint lightUbo = 0;
GLuint materialUbo = 0;
GLuint geoUbo = 0;
namespace UboBinding
{
//These values come from the binding value specified in the shader block layout
int scene = 0;
int light = 1;
int material = 2;
int geometry = 4;
};
//Locations for the uniforms which are not in uniform blocks
namespace UniformLocs
{
int M = 0; //model matrix
int time = 1;
};
void Init()
{
//Create and initialize uniform buffers
glGenBuffers(1, &Uniforms::sceneUbo);
glBindBuffer(GL_UNIFORM_BUFFER, sceneUbo);
glBufferData(GL_UNIFORM_BUFFER, sizeof(SceneUniforms), nullptr, GL_STREAM_DRAW); //Allocate memory for the buffer, but don't copy (since pointer is null).
glBindBufferBase(GL_UNIFORM_BUFFER, UboBinding::scene, sceneUbo); //Associate this uniform buffer with the uniform block in the shader that has the same binding.
glGenBuffers(1, &lightUbo);
glBindBuffer(GL_UNIFORM_BUFFER, lightUbo);
glBufferData(GL_UNIFORM_BUFFER, sizeof(LightUniforms), &LightData, GL_STREAM_DRAW); //Allocate memory for the buffer, but don't copy (since pointer is null).
glBindBufferBase(GL_UNIFORM_BUFFER, UboBinding::light, lightUbo); //Associate this uniform buffer with the uniform block in the shader that has the same binding.
glGenBuffers(1, &materialUbo);
glBindBuffer(GL_UNIFORM_BUFFER, materialUbo);
glBufferData(GL_UNIFORM_BUFFER, sizeof(MaterialUniforms), &MaterialData, GL_STREAM_DRAW); //Allocate memory for the buffer, but don't copy (since pointer is null).
glBindBufferBase(GL_UNIFORM_BUFFER, UboBinding::material, materialUbo); //Associate this uniform buffer with the uniform block in the shader that has the same binding.
glGenBuffers(1, &geoUbo);
glBindBuffer(GL_UNIFORM_BUFFER, geoUbo);
glBufferData(GL_UNIFORM_BUFFER, sizeof(GeoUniforms), &GeoData, GL_STREAM_DRAW); //Allocate memory for the buffer, but don't copy (since pointer is null).
glBindBufferBase(GL_UNIFORM_BUFFER, UboBinding::geometry, geoUbo); //Associate this uniform buffer with the uniform block in the shader that has the same binding.
glBindBuffer(GL_UNIFORM_BUFFER, 0);
}
void BufferSceneData()
{
glBindBuffer(GL_UNIFORM_BUFFER, sceneUbo); //Bind the OpenGL UBO before we update the data.
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(SceneData), &SceneData); //Upload the new uniform values.
glBindBuffer(GL_UNIFORM_BUFFER, 0); //unbind the ubo
glBindBuffer(GL_UNIFORM_BUFFER, geoUbo); //Bind the OpenGL UBO before we update the data.
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(GeoData), &GeoData); //Upload the new uniform values.
glBindBuffer(GL_UNIFORM_BUFFER, 0); //unbind the ubo
}
};