5 #ifndef CNOID_BASE_GLSL_PROGRAM_H
6 #define CNOID_BASE_GLSL_PROGRAM_H
9 #include <cnoid/EigenTypes>
13 #include "exportdecl.h"
26 void loadShader(
const char* filename,
int shaderType);
31 GLint
handle()
const {
return programHandle; }
35 return glGetUniformLocation(programHandle, name);
38 return glGetUniformLocation(programHandle, name.c_str());
41 #ifdef CNOID_GL_CORE_4_4
42 return glGetSubroutineIndex(programHandle, shaderType, name);
48 #ifdef CNOID_GL_CORE_4_4
49 return glUniformSubroutinesuiv(shaderType, count, indices);
65 bool initialize(
GLSLProgram& program,
const std::string& blockName);
70 GLuint checkUniform(
const char* name);
72 GLuint checkUniformMatrix(
const char* name);
75 GLuint blockIndex = glGetUniformBlockIndex(program.
handle(), blockName.c_str());
76 glUniformBlockBinding(program.
handle(), blockIndex, bindingPoint);
80 glBindBufferBase(GL_UNIFORM_BUFFER, bindingPoint, uboHandle);
83 void write(GLuint index,
float v){
84 std::memcpy(&localBuffer[infos[index].offset], &v,
sizeof(v));
87 void write(GLuint index,
const Vector3f& v){
88 std::memcpy(&localBuffer[infos[index].offset], v.data(),
sizeof(v));
91 void write(GLuint index,
const Vector4f& v){
92 std::memcpy(&localBuffer[infos[index].offset], v.data(),
sizeof(v));
95 void write(GLuint index,
const Matrix3f& M){
96 const UniformInfo& info = infos[index];
97 const int size = 3 *
sizeof(Matrix3f::Scalar);
98 std::memcpy(&localBuffer[info.offset], &M(0, 0), size);
99 std::memcpy(&localBuffer[info.offset + info.matrixStrides], &M(0, 1), size);
100 std::memcpy(&localBuffer[info.offset + info.matrixStrides * 2], &M(0, 2), size);
103 void write(GLuint index,
const Matrix4f& M){
104 std::memcpy(&localBuffer[infos[index].offset], M.data(),
sizeof(M));
107 void write(GLuint index,
const Affine3f& T){
108 std::memcpy(&localBuffer[infos[index].offset], T.matrix().data(),
sizeof(T));
112 glBindBuffer(GL_UNIFORM_BUFFER, uboHandle);
113 glBufferSubData(GL_UNIFORM_BUFFER, 0, localBuffer.size(), &localBuffer[0]);
118 std::vector<GLubyte> localBuffer;
123 std::vector<UniformInfo> infos;
124 GLuint lastProgramHandle;
125 std::string blockName;