Help us understand the problem. What is going on with this article?

openFrameworksでshaderにUniformArrayで渡す方法

arrayで渡す時にやり方がよくわからなかったのでメモ

コード

int/floatの場合

  • 1個目の要素のポインタを渡す。
main.cpp
ofShader shader;

vector <float> nums;

for (int i = 0; i < 10; i++) {
    nums.push_back(ofRandom(1));
}

// 第二引数にmatの1個目,第三引数にcountを渡す
shader.setUniform1fv("nums", &nums[0], nums.size());
fragment.glsl
#version 150

uniform float nums[10];

vecの場合

  • 1個目のvecのx要素のポインタを渡す。
main.cpp
ofShader shader;

vector <glm::vec4> vectors;

glm::vec4 vec1;
glm::vec4 vec2;
glm::vec4 vec3;

matrices.push_back(vec1);
matrices.push_back(vec2);
matrices.push_back(vec3);

// 第二引数にmatの1個目,第三引数にcountを渡す
shader.setUniform4fv("vectors", &vectors[0].x, matrices.size());
fragment.glsl
#version 150

uniform vec4 vectors[3];

matの場合

  • 1個目のmatを直接渡す。
main.cpp
ofShader shader;

vector <glm::mat4> matrices;

glm::mat4 mat1;
glm::mat4 mat2;
glm::mat4 mat3;

matrices.push_back(mat1);
matrices.push_back(mat2);
matrices.push_back(mat3);

// 第二引数にmatの1個目,第三引数にcountを渡す
shader.setUniformMatrix4f("matrices", matrices[0], matrices.size());
fragment.glsl
#version 150

uniform mat4 matrices[3];

まとめ

  • matの時だけmatそのものを渡す
  • それ以外は1個目の要素のポインタを渡す

 おまけ

openglの場合

main.cpp
ofShader shader;

vector <glm::mat4> matrices;

glm::mat4 mat1;
glm::mat4 mat2;
glm::mat4 mat3;

matrices.push_back(mat1);
matrices.push_back(mat2);
matrices.push_back(mat3);

// 第二引数にcountの1個目,第四引数に1個目のmatの[0][0](要するにGLfloatの配列の1個目)のポインタを渡す
glUniformMatrix3fv(shader.getUniformLocation("matrices"), matrices.size(), GL_FALSE, glm::value_ptr(matrices[0]));

// glm::value_ptr(matrices[0])はglm::value_ptr(matrices[0][0][0])やglm::value_ptr(matrices[0][0].x)と同義

以上です。間違ってたらコメントお願いします🙇

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away