Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
3
Help us understand the problem. What is going on with this article?
@73_ch

openFrameworksでshaderにUniformArrayで渡す方法

More than 1 year has passed since last update.

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)と同義

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

3
Help us understand the problem. What is going on with this article?
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
73_ch

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
3
Help us understand the problem. What is going on with this article?