Editモードに入り、ループカット(Ctrl+R)で切れ目を入れ、Z方向に4だけ移動する。底面を選択し縮小した後、Editモードを抜ける。
[a]で全ての頂点を選択し、[+]ボタンでVertexGroupを作成し名前を「Deform」として[Assign]する。
現在、"Deform"グループには全ての頂点が登録されているので、「グループから除外したい頂点を選択し」[Remove]ボタンを押す。
モディファイアが多いので他のオブジェクトを用意しモディファイア群だけを複製する。
「①新しいモデル」「②Rockのモディファイア群を設定したモデル」の順番に選択し、[Ctrl+L]→Modifiresを選択しコピーする。
なおここではBlender 2.90で試す。
以降、以下のようにモディファイア(とObject Data Properties)を編集していく。
BaseDisplacementNoiseテクスチャを以下のように設定
SecondaryDisplacementNoiseテクスチャを設定。殆どBaseDisplacementNoiseと同じで、sizeを3.0に変更する
これは追加するだけで、パラメータの変更はしない。
形状はこれで(ほぼ)完了。めずらしくチュートリアルに極めて近い物ができた。次回は残っているライティングとかもやってみる。
テクスチャはポリゴンに貼るが、そのポリゴンの色が何色かで張り付けた結果に違いが出る。
以下は、テクスチャを一枚用意し、それをglColor3dでポリゴンの色を指定した上に張り付けた結果。
//描画関数 void disp(void) { glViewport(0, 0, winwidth, winheight); glClearColor(0.2, 0.2, 0.2, 1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_CULL_FACE); double v = 0.95;
//下地の色を設定 glColor3d(1, 0, 0);
//テクスチャを有効化 glBindTexture(GL_TEXTURE_2D, texname); glEnable(GL_TEXTURE_2D); glBegin(GL_QUADS); glTexCoord2d(0, 1); glVertex2d(-v, -v); glTexCoord2d(1, 1); glVertex2d(v, -v); glTexCoord2d(1, 0); glVertex2d(v, v); glTexCoord2d(0, 0); glVertex2d(-v, v); glEnd(); glFlush(); }
glActiveTextureを使うと、下地の色用のテクスチャと、張り付けたい画像のテクスチャを同時に指定できる。「同時に指定できる」とはつまり、普通glBindTextureを一度実行したらほかのテクスチャは無効になるが、glActiveTextureを使えば二つのglBindTextureを有効にできるという意味。
OpenGLにはテクスチャスロットのような概念がある。
glActiveTexture(GL_TEXTURE0);
で、「これからスロット名"GL_TEXTURE0"の設定をする」という意味になる。
そして、一つのスロットで一度に使えるテクスチャは一枚だけで、これは「最後にglBindTextureで指定したテクスチャ」だが、一度に複数のスロットを使えるので、各スロットで glEnable(GL_TEXTURE_2D)でそのスロットを有効にしてやれば、テクスチャを複数使うことができる。
#include <iostream> #include <Windows.h> #include <gl/glew.h>// glActiveTextureに必要 #include <gl/GL.h> #include <gl/GLU.h> #include <gl/freeglut.h> #pragma comment(lib,"glew32.lib") #define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" // freeglut: // http://freeglut.sourceforge.net/ //ウィンドウの幅と高さ int winwidth, winheight; GLuint texname_img; GLuint texname_block;
//下地用のテクスチャ void init_tex_block() { unsigned char pixels[] = { 255, 0, 0, 0,255, 0, 0, 0,255, 255,255,255 }; glGenTextures(1, &texname_block); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texname_block); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels ); }
//画像読み込み,テクスチャとしてロード //pngの読み込みに STBライブラリを使用 //https://suzulang.com/stb-library-image-read/ //https://github.com/nothings/stb void init_tex_img() { unsigned char* pixels; int bpp; int imgwidth; int imgheight; // ファイルを読み込み、画像データを取り出す // 最後の引数でピクセルあたりのバイト数を強制できる pixels = stbi_load(R"(C:\Users\szl\Desktop\bird-1232416_640.png)", &imgwidth, &imgheight, &bpp, 0); glGenTextures(1, &texname_img); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, texname_img); glPixelStorei(GL_UNPACK_ALIGNMENT, 4); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, imgwidth, imgheight, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels ); // メモリ上の画像データを破棄 stbi_image_free(pixels); }
//描画関数 void disp(void) { glViewport(0, 0, winwidth, winheight); glClearColor(0.2, 0.2, 0.2, 1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_CULL_FACE); double v = 0.95;
glActiveTexture(GL_TEXTURE0); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D,texname_block);
glActiveTexture(GL_TEXTURE1); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, texname_img);
glBegin(GL_QUADS); glMultiTexCoord2f(GL_TEXTURE0, 0, 1); glMultiTexCoord2f(GL_TEXTURE1, 0, 1); glVertex2d(-v, -v); glMultiTexCoord2f(GL_TEXTURE0, 1, 1); glMultiTexCoord2f(GL_TEXTURE1, 1, 1); glVertex2d(v, -v); glMultiTexCoord2f(GL_TEXTURE0, 1, 0); glMultiTexCoord2f(GL_TEXTURE1, 1, 0); glVertex2d(v, v); glMultiTexCoord2f(GL_TEXTURE0, 0, 0); glMultiTexCoord2f(GL_TEXTURE1, 0, 0); glVertex2d(-v, v); glEnd(); glActiveTexture(GL_TEXTURE1); glDisable(GL_TEXTURE_2D); glActiveTexture(GL_TEXTURE0); glDisable(GL_TEXTURE_2D); glFlush(); } //ウィンドウサイズの変化時に呼び出される void reshape(int w, int h) { winwidth = w; winheight = h; disp(); } //エントリポイント int main(int argc, char** argv) { glutInit(&argc, argv); glutInitWindowPosition(100, 50); glutInitWindowSize(640, 480); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA); glutCreateWindow("sample"); glutDisplayFunc(disp); glutReshapeFunc(reshape); glewInit(); init_tex_img(); init_tex_block(); glutMainLoop(); return 0; }
glMaterialfvで着色する場合、第一引数にGL_FRONTかGL_BACKを指定して切り替える
glColor3fで着色する場合、カリングを使い裏面のみ描画、表面のみ描画を切り替えて二回レンダリングする。
//ポリゴンを描画 void render() { float v = 0.7; glBegin(GL_QUADS); glVertex3f(-v, -v, 0.0); glVertex3f(v, -v, 0.0); glVertex3f(v, v, 0.0); glVertex3f(-v, v, 0.0); glEnd(); }
void draw_by_color() { glEnable(GL_CULL_FACE);//カリングを有効化 glCullFace(GL_FRONT); glColor4f(1, 0, 0, 1); render(); glCullFace(GL_BACK); glColor4f(0, 1, 0, 1); render(); }
void draw_by_material() { glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); GLfloat white[4]{ 1,1,1,1 }; GLfloat red[4]{ 1,0,0,1 }; GLfloat green[4]{ 0,1,0,1 }; GLfloat pos[4]{ 0,0,0,1 }; glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);//両面に光を当てる glLightfv(GL_LIGHT0, GL_POSITION, pos);//ライト位置設定 glLightfv(GL_LIGHT0, GL_AMBIENT, white);//環境光色設定 glMaterialfv(GL_FRONT, GL_AMBIENT, red);//表面の材質 glMaterialfv(GL_BACK, GL_AMBIENT, green);//裏面の材質 render();//オブジェクトのレンダリング glDisable(GL_LIGHTING); glDisable(GL_LIGHT0); }
void draw() { ///////////////////// // 描画 glViewport(0, 0, width, height); glClearColor(0.3, 0.3, 0.3, 1); glClear(GL_COLOR_BUFFER_BIT); Sleep(1); angle++; glPushMatrix(); glRotated(angle, 1, 0, 0); //draw_by_color();//この二つの関数切り替え draw_by_material(); glPopMatrix(); // ///////////////////// glFlush(); glfwSwapBuffers(_window); }
以前フレームバッファをやってみたときに、glFramebufferTexture2D関数を使ったが、第二引数に与えたGL_COLOR_ATTACHMENT0について言及しなかった(気がする)。当時は調べて納得はしていたが改めて読み返して混乱したのでここで明確にしておきたい。
glGenFramebuffers(1, &framebuffer); glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); /* ... */ glGenTextures(1, texture0); glGenTextures(1, texture1); /* ... */ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture0, 0); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, texture1, 0);
このように指定し、フラグメントシェーダ側で以下のように出力する。
#version 460 core layout (location = 0) out vec4 DefaultOut;// 0、つまりGL_COLOR_ATTACHMENT0 layout (location = 1) out vec4 RedOnly; // 1、つまりGL_COLOR_ATTACHMENT1 in vec4 vertexColor; void main() { DefaultOut = vertexColor; RedOnly = vec4(vertexColor[0],0,0,1); }
つまり、GL_COLOR_ATTACHMENT0はフラグメントシェーダのlocationを指定している。
#include <memory> #include <iostream> #include <vector> #include <algorithm> #include <array> #include<windows.h> #include <gl/glew.h> #include "CglfwBase.hpp" #pragma warning(disable:4996) #pragma comment(lib,"opengl32.lib") #pragma comment(lib,"glew32.lib")
//! @brief PPM(RGB各1byte,カラー,テキスト)を書き込む //! @param [in] fname ファイル名 //! @param [in] vmax 全てのRGBの中の最大値 //! @param [in] width 画像の幅 //! @param [in] height 画像の高さ //! @param [in] p 画像のメモリへのアドレス //! @details RGBRGBRGB....のメモリを渡すと、RGBテキストでファイル名fnameで書き込む void pnmP3_Write(const TCHAR* const fname, const int vmax, const int width, const int height, const unsigned char* const p) { // PPM ASCII FILE* fp = _wfopen(fname, L"wb"); fprintf(fp, "P3\n%d %d\n%d\n", width, height, vmax); size_t k = 0; for (size_t i = 0; i < (size_t)height; i++) { for (size_t j = 0; j < (size_t)width; j++) { fprintf(fp, "%d %d %d ", p[k * 3 + 0], p[k * 3 + 1], p[k * 3 + 2]); k++; } fprintf(fp, "\n"); } fclose(fp); }
//! @brief フレームバッファに関する変数と機能をまとめる class FramebufferTest { public: GLuint ID_texture_0; // テクスチャ1 GLuint ID_texture_1; // テクスチャ2 GLuint ID_fbo;// フレームバッファ int width, height;//テクスチャのサイズ private: GLuint ID_depth;
//! @brief テクスチャを一枚作成し、フレームバッファに関連付ける void generate_texture(GLuint *texname, GLenum attachment) { glGenTextures(1, texname); glBindTexture(GL_TEXTURE_2D, *texname); //テクスチャを転送する。 //ただし最後がnullptrなので実質転送しない glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr); //テクスチャのフィルタリングの設定 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); //描画先の設定 // attachmentへの書き込みで、texnameに書き込まれるようにする glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, *texname, 0); }
public:
// フレームバッファオブジェクトの作成と設定を行う // 途中で描画先のテクスチャも作成する void prepare(int WIDTH,int HEIGHT) { width = WIDTH; height = HEIGHT; ////////////////////////////////////////////////////// //フレームバッファの作成 // 削除はglDeleteFrameBuffersを使用する glGenFramebuffers(1, &ID_fbo); glBindFramebuffer(GL_FRAMEBUFFER, ID_fbo);
////////////////////////////////////////////////////// //テクスチャバッファの作成 // 削除はglDeleteTextures generate_texture(&ID_texture_0, GL_COLOR_ATTACHMENT0); generate_texture(&ID_texture_1, GL_COLOR_ATTACHMENT1); // fragment shaderの // layout (location = 0) ・・・ GL_COLOR_ATTACHMENT0 // layout (location = 1) ・・・ GL_COLOR_ATTACHMENT1 // が書き込み先であることを教える(?) GLenum DrawBuffers[2] = { GL_COLOR_ATTACHMENT0 , GL_COLOR_ATTACHMENT1 }; glDrawBuffers(2, DrawBuffers); // "2"はDrawBuffersのサイズです。
////////////////////////////////////////////////////// // デプスバッファの作成 // レンダーバッファの一種なので、削除はglDeleteRenderbuffersを使用する glGenRenderbuffers(1, &ID_depth); glBindRenderbuffer(GL_RENDERBUFFER, ID_depth); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, ID_depth); /////////////////////////////////////////////////// // フレームバッファのエラーチェック。 if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) return;/*エラーデータ*/ }
}; class MyWindow :public MyWindowBase { int width; int height; FramebufferTest myfb; GLuint vertexbufferName; GLuint colorbufferName; public:
//! @brief glew,glfwの設定 フレームバッファの初期化、シェーダの作成、データの作成 virtual void setting()override { glewInit(); glfwGetFramebufferSize(_window, &width, &height); //フレームバッファの初期化 myfb.prepare(width, height); //シェーダに渡す描画データの作成 prepare_data(); //シェーダの作成 prepare_shaders(); }
//! @brief キーイベント。フレームバッファによって書き込んだテクスチャデータをファイルへ出力 virtual void key(int key, int scancode, int action, int mods)override { //テクスチャ(ID_texture_output)に書き込んだデータを取り出す領域を用意 std::vector<GLubyte> texdata; texdata.resize(myfb.width*myfb.height * 3); //////////////////////////////////////////////////// //テクスチャのデータを取り出す glBindTexture(GL_TEXTURE_2D, myfb.ID_texture_0); glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, &texdata[0]); // 取り出したテクスチャをファイルに書き出す pnmP3_Write(LR"(C:\test\texture0.ppm)", 255, width, height, texdata.data()); //////////////////////////////////////////////////// //テクスチャのデータを取り出す glBindTexture(GL_TEXTURE_2D, myfb.ID_texture_1); glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, &texdata[0]); // 取り出したテクスチャをファイルに書き出す pnmP3_Write(LR"(C:\test\texture1.ppm)", 255, width, height, texdata.data()); }
//! @brief 描画関数 virtual void draw()override { //以後の描画はフレームバッファに対して行われる glBindFramebuffer(GL_FRAMEBUFFER, myfb.ID_fbo); draw_core(); //以後の描画は画面に対して行われる glBindFramebuffer(GL_FRAMEBUFFER, 0); draw_core(); glfwSwapBuffers(_window); }
//! @brief 描画関数本体
void draw_core() { ///////////////////// // 描画 glViewport(0, 0, width, height); glClearColor(0.3, 0.3, 0.3, 1); glClear(GL_COLOR_BUFFER_BIT); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glUseProgram(ProgramID); //gl_PointSizeを有効にする glEnable(GL_VERTEX_PROGRAM_POINT_SIZE); // 頂点バッファ glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, vertexbufferName); glVertexAttribPointer( 0, // 属性0 3, // 1要素の要素数 x y z GL_FLOAT, // タイプ GL_FALSE, // 正規化しない(データが整数型の時) 0, // ストライド (void*)0 // 配列バッファオフセット ); // カラーバッファ glEnableVertexAttribArray(1); glBindBuffer(GL_ARRAY_BUFFER, colorbufferName); glVertexAttribPointer( 1, // 属性1 4, // 1要素の要素数 r g b a GL_FLOAT, // タイプ GL_FALSE, // 正規化しない(データが整数型の時) 0, // ストライド (void*)0 // 配列バッファオフセット ); // 5頂点を描く glDrawArrays(GL_POINTS, 0, 5); glDisableVertexAttribArray(0); glDisableVertexAttribArray(1); // ///////////////////// glFlush(); } GLuint VertexShaderID; GLuint FragmentShaderID; GLuint ProgramID;
//! @brief シェーダの作成
//! シェーダコードを直接埋め込んで、compileshaderを呼び出している
void prepare_shaders() { const char* vshader = R"(
#version 460 core layout (location = 0) in vec3 aPos; layout (location = 1) in vec3 incolor; out vec4 vertexColor; uniform mat4 gl_ModelViewMatrix; uniform mat4 gl_ProjectionMatrix; void main() { gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * vec4(aPos, 1.0); vertexColor = vec4(incolor, 1.0); gl_PointSize=30.0; // 頂点のサイズ(ピクセル) }
)"; VertexShaderID = compileshader(GL_VERTEX_SHADER, vshader); const char* fshader = R"(
#version 460 core layout (location = 0) out vec4 DefaultOut;// 0、つまりGL_COLOR_ATTACHMENT0 layout (location = 1) out vec4 RedOnly; // 1、つまりGL_COLOR_ATTACHMENT1 //out vec4 FragColor; in vec4 vertexColor; void main() { DefaultOut = vertexColor; RedOnly = vec4(vertexColor[0],0,0,1); }
)"; FragmentShaderID = compileshader(GL_FRAGMENT_SHADER, fshader); GLint Result = GL_FALSE; int InfoLogLength; //////////////////////////////////////// // プログラムをリンクします。 fprintf(stdout, "Linking program\n"); ProgramID = glCreateProgram(); glAttachShader(ProgramID, VertexShaderID); glAttachShader(ProgramID, FragmentShaderID); glLinkProgram(ProgramID); //////////////////////////////////////// // プログラムをチェックします。 glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result); glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength); std::vector<char> ProgramErrorMessage((std::max)(InfoLogLength, int(1))); glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]); fprintf(stdout, "%s\n", &ProgramErrorMessage[0]); }
//! @brief シェーダに渡す描画データの作成 void prepare_data() { using point_t = std::array<GLfloat, 3>; //モデルの座標用 x y z using color_t = std::array<GLfloat, 4>;//テクスチャ座標用 r g b a ///////////////////////////////// //バッファとデータ point_t vertex[5]; color_t color[5]; const int elemcount = 5; ////////////////////////////////////////// vertex[0] = { -0.7, 0.7, 0 }; vertex[1] = { -0.7,-0.7, 0 }; vertex[2] = { 0.7,-0.7, 0 }; vertex[3] = { 0.7, 0.7, 0 }; vertex[4] = { 0 , 0 , 0 }; color[0] = { 1.0,0.0,0.0,1.0 }; color[1] = { 0.0,1.0,0.0,1.0 }; color[2] = { 0.0,0.0,1.0,1.0 }; color[3] = { 1.0,0.0,1.0,1.0 }; color[4] = { 1.0,1.0,1.0,1.0 }; ////////////////////////////////////////// // バッファの転送 glGenBuffers(1, &vertexbufferName); glBindBuffer(GL_ARRAY_BUFFER, vertexbufferName); glBufferData(GL_ARRAY_BUFFER, 3 * elemcount * sizeof(GLfloat), vertex, GL_STATIC_DRAW); glGenBuffers(1, &colorbufferName); glBindBuffer(GL_ARRAY_BUFFER, colorbufferName); glBufferData(GL_ARRAY_BUFFER, 4 * elemcount * sizeof(GLfloat), color, GL_STATIC_DRAW); }
//! @brief シェーダをコンパイル・リンク GLuint compileshader(GLenum type,const char* VertexShaderCode) { GLuint shaderID; ////////////////////////////////////////////// // シェーダを作ります // (作るといっても宣言みたいなもの) shaderID = glCreateShader(type); //////////////////////////////////////////// // 頂点シェーダをコンパイルします。 printf("Compiling shader... \n"); glShaderSource(shaderID, 1, &VertexShaderCode, NULL); glCompileShader(shaderID); //////////////////////////////////////////// // エラーチェック GLint Result = GL_FALSE; int InfoLogLength; // 頂点シェーダをチェックします。 glGetShaderiv(shaderID, GL_COMPILE_STATUS, &Result); glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); if (Result == FALSE) { std::vector<char> VertexShaderErrorMessage(InfoLogLength); glGetShaderInfoLog(shaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]); fprintf(stdout, "%s\n", &VertexShaderErrorMessage[0]); } return shaderID; }
virtual void framebuffer_size(int _width, int _height)override { width = _width; height = _height; } }; int main() { MyWindow win; win.init(600,600,"glfw window"); win.setting(); win.loop(); }
画面出力ができたら、キーを何か押すとc:\test\texture0.ppmとtexture1.ppmを出力する。
画像が逆転しているのはOpenGLは左下原点なので仕方がない。
glfwSetWindowUserPointerでthisポインタを指定し、非メンバ関数であるコールバック関数の第一引数からthisを取り出せるようにすれば、間接的にだが自作クラスのメンバ関数を呼び出す事ができる。
#pragma once #pragma comment(lib,"glfw3.lib") #include <gl/glfw/glfw3.h> class MyWindowBase { protected: GLFWwindow* _window; protected:
// コールバック関数から呼ばれるメンバ関数群 //////////////////////////
virtual void key(int key, int scancode, int action, int mods) {} virtual void mouse_button(int button, int action, int mods) {} virtual void mouse_move(double xpos, double ypos) {} virtual void mouse_wheel(double xoffset, double yoffset) {} virtual void framebuffer_size(int width, int height) {} ////////////////////////////////////////////// ////////////////////////////////////////////// virtual void setting() {} virtual void draw() {}
protected:
// GLFWに指定するコールバック関数(staticメンバ関数) ////////////////////////// // GLFWのコールバック関数にはこれを指定する。glfwSetWindowUserPointerで指定したthisポインタを // glfwGetWindowUserPointerで取得し、メンバ関数を呼び出す
static void key_callback(GLFWwindow* pwin, int key, int scancode, int action, int mods) { MyWindowBase* myw = static_cast<MyWindowBase*>(glfwGetWindowUserPointer(pwin)); myw->key(key, scancode, action, mods); } static void mouse_button_callback(GLFWwindow* pwin, int button, int action, int mods) { MyWindowBase* myw = static_cast<MyWindowBase*>(glfwGetWindowUserPointer(pwin)); myw->mouse_button(button, action, mods); } static void mouse_move_callback(GLFWwindow* pwin, double xpos, double ypos) { MyWindowBase* myw = static_cast<MyWindowBase*>(glfwGetWindowUserPointer(pwin)); myw->mouse_move(xpos, ypos); } static void mouse_wheel_callback(GLFWwindow* pwin, double xoffset, double yoffset) { MyWindowBase* myw = static_cast<MyWindowBase*>(glfwGetWindowUserPointer(pwin)); myw->mouse_wheel(xoffset, yoffset); } static void framebuffer_size_callback(GLFWwindow* pwin, int width, int height) { MyWindowBase* myw = static_cast<MyWindowBase*>(glfwGetWindowUserPointer(pwin)); myw->framebuffer_size(width, height); } static void draw_callback(GLFWwindow* pwin) { MyWindowBase* myw = static_cast<MyWindowBase*>(glfwGetWindowUserPointer(pwin)); myw->draw(); }
public:
// ここでGLFWの初期化を行う ///////////////////////////////// int init(int winwidth,int winheight,const char* wintitle) {
// GLFW の初期化 if (glfwInit() == GL_FALSE) { // 初期化に失敗したら終了 return 1; }
// ウィンドウを作成 _window = glfwCreateWindow( winwidth, //width winheight, //height wintitle,//title NULL, //monitor NULL //share ); //////////////////////////////////////////////////////////////////////////////// // ウィンドウを作成できなければ終了 if (_window == nullptr) { glfwTerminate(); return 1; }
//////////////////////////////////////////////////////////////////////////////// // 自身のポインタをGLFWwindowオブジェクトにセット glfwSetWindowUserPointer(_window, this);
//////////////////////////////////////////// // コールバック関数の設定 glfwSetKeyCallback(_window, MyWindowBase::key_callback); glfwSetMouseButtonCallback(_window, MyWindowBase::mouse_button_callback); glfwSetScrollCallback(_window, MyWindowBase::mouse_wheel_callback); glfwSetCursorPosCallback(_window, MyWindowBase::mouse_move_callback); glfwSetFramebufferSizeCallback(_window, MyWindowBase::framebuffer_size_callback); glfwSetWindowRefreshCallback(_window, MyWindowBase::draw_callback); glfwMakeContextCurrent(_window); }
// イベントループ /////////////////////////////////
void loop() { while (glfwWindowShouldClose(_window) == GL_FALSE) { draw(); // イベント取得 glfwPollEvents(); } glfwTerminate(); }
};
#include <memory> #include <iostream> #include<windows.h> #include "CglfwBase.hpp" #pragma comment(lib,"opengl32.lib") class MyWindow :public MyWindowBase { int width; int height; public:
virtual void setting()override { glfwGetFramebufferSize(_window, &width, &height); } virtual void key(int key, int scancode, int action, int mods)override { // https://www.glfw.org/docs/latest/group__keys.html#ga99aacc875b6b27a072552631e13775c7 // action ... GLFW_PRESS , GLFW_REPEAT , GLFW_RELEASE //特殊キー if (key == GLFW_KEY_UP && action == GLFW_PRESS) { } if (key == GLFW_KEY_DOWN && action == GLFW_PRESS) { } if (key == GLFW_KEY_LEFT && action == GLFW_PRESS) { } if (key == GLFW_KEY_RIGHT && action == GLFW_PRESS) { } if (key >= GLFW_KEY_A && key <= GLFW_KEY_Z) { if (action == GLFW_PRESS) { const char* key_name = glfwGetKeyName(key, 0); printf("key - %s\n", key_name); } } } virtual void mouse_button(int button, int action, int mods)override { double xpos, ypos; glfwGetCursorPos(_window, &xpos, &ypos); if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) { printf("mouse L pressed\n"); } if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE) { printf("mouse L released\n"); } if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) { } if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_RELEASE) { } if (button == GLFW_MOUSE_BUTTON_MIDDLE && action == GLFW_PRESS) { } draw(); } virtual void mouse_move(double xpos, double ypos)override { printf("mouse is moving\n"); draw(); } // マウスホイールを処理するコールバック関数 virtual void mouse_wheel(double xoffset, double yoffset)override { if (yoffset < 0) { } if (yoffset > 0) { } draw(); } virtual void framebuffer_size(int _width, int _height)override { width = _width; height = _height; } virtual void draw()override { ///////////////////// // 描画 glViewport(0, 0, width, height); glClearColor(0.3, 0.3, 0.3, 1); glClear(GL_COLOR_BUFFER_BIT); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); float v = 0.7; glBegin(GL_TRIANGLES); glColor3f(0, 0, 1); glVertex3f(-v, -v, 0.0); glColor3f(1, 0, 1); glVertex3f(v, -v, 0.0); glColor3f(1, 1, 1); glVertex3f(v, v, 0.0); glEnd(); // ///////////////////// glFlush(); glfwSwapBuffers(_window); }
}; int main() { MyWindow win; win.init(600,600,"glfw window"); win.setting(); win.loop(); }
import bpy import mathutils
# 行列をモデルに適用するボタン class execButton(bpy.types.Operator): bl_idname = "szl.multmatrixbutton" bl_label = "multi matrix" def execute(self, context): M11 = context.scene.myinputs.m11 M12 = context.scene.myinputs.m12 M13 = context.scene.myinputs.m13 M14 = context.scene.myinputs.m14 ####################################### M21 = context.scene.myinputs.m21 M22 = context.scene.myinputs.m22 M23 = context.scene.myinputs.m23 M24 = context.scene.myinputs.m24 ####################################### M31 = context.scene.myinputs.m31 M32 = context.scene.myinputs.m32 M33 = context.scene.myinputs.m33 M34 = context.scene.myinputs.m34 ####################################### M41 = context.scene.myinputs.m41 M42 = context.scene.myinputs.m42 M43 = context.scene.myinputs.m43 M44 = context.scene.myinputs.m44 ####################################### #apply matrix matrix = mathutils.Matrix(( (M11 ,M12 ,M13 ,M14), (M21 ,M22 ,M23 ,M24), (M31 ,M32 ,M33 ,M34), (M41 ,M42 ,M43 ,M44))) msh = bpy.context.active_object for vt in msh.data.vertices: src = mathutils.Vector((vt.co[0],vt.co[1],vt.co[2],1)) dst = matrix @ src vt.co[0] = dst[0] vt.co[1] = dst[1] vt.co[2] = dst[2] return{'FINISHED'}
# 行列を単位行列にする class execButtonLoadIdentity(bpy.types.Operator): bl_idname = "szl.loadidentitybutton" bl_label = "load identity" def execute(self, context): context.scene.myinputs.m11 =1.0 context.scene.myinputs.m12 =0.0 context.scene.myinputs.m13 =0.0 context.scene.myinputs.m14 =0.0 ####################################### context.scene.myinputs.m21 =0.0 context.scene.myinputs.m22 =1.0 context.scene.myinputs.m23 =0.0 context.scene.myinputs.m24 =0.0 ####################################### context.scene.myinputs.m31 =0.0 context.scene.myinputs.m32 =0.0 context.scene.myinputs.m33 =1.0 context.scene.myinputs.m34 =0.0 ####################################### context.scene.myinputs.m41 =0.0 context.scene.myinputs.m42 =0.0 context.scene.myinputs.m43 =0.0 context.scene.myinputs.m44 =1.0 return{'FINISHED'}
#テキストボックスを定義 class myCInputs(bpy.types.PropertyGroup): m11: bpy.props.FloatProperty(name="m11") m12: bpy.props.FloatProperty(name="m12") m13: bpy.props.FloatProperty(name="m13") m14: bpy.props.FloatProperty(name="m14") ####################################### m21: bpy.props.FloatProperty(name="m21") m22: bpy.props.FloatProperty(name="m22") m23: bpy.props.FloatProperty(name="m23") m24: bpy.props.FloatProperty(name="m24") ####################################### m31: bpy.props.FloatProperty(name="m31") m32: bpy.props.FloatProperty(name="m32") m33: bpy.props.FloatProperty(name="m33") m34: bpy.props.FloatProperty(name="m34") ####################################### m41: bpy.props.FloatProperty(name="m41") m42: bpy.props.FloatProperty(name="m42") m43: bpy.props.FloatProperty(name="m43") m44: bpy.props.FloatProperty(name="m44")
class myCTool_PT_panel(bpy.types.Panel): bl_label = "行列適用" bl_category = "行列適用" bl_space_type = "VIEW_3D" bl_region_type = "UI" def draw(self, context): layout = self.layout column = layout.column(align=True) # テキストボックスを並べる row = column.row(align=True) row.prop(context.scene.myinputs, "m11", text="") row.prop(context.scene.myinputs, "m12", text="") row.prop(context.scene.myinputs, "m13", text="") row.prop(context.scene.myinputs, "m14", text="") row = column.row(align=True) row.prop(context.scene.myinputs, "m21", text="") row.prop(context.scene.myinputs, "m22", text="") row.prop(context.scene.myinputs, "m23", text="") row.prop(context.scene.myinputs, "m24", text="") row = column.row(align=True) row.prop(context.scene.myinputs, "m31", text="") row.prop(context.scene.myinputs, "m32", text="") row.prop(context.scene.myinputs, "m33", text="") row.prop(context.scene.myinputs, "m34", text="") row = column.row(align=True) row.prop(context.scene.myinputs, "m41", text="") row.prop(context.scene.myinputs, "m42", text="") row.prop(context.scene.myinputs, "m43", text="") row.prop(context.scene.myinputs, "m44", text="") # ボタンを配置 layout.operator("szl.loadidentitybutton") layout.operator("szl.multmatrixbutton")
classes = ( myCInputs, execButton, execButtonLoadIdentity, myCTool_PT_panel ) def register(): for cls in classes: bpy.utils.register_class(cls) bpy.types.Scene.myinputs = bpy.props.PointerProperty(type=myCInputs) def unregister(): for cls in classes: bpy.utils.unregister_class(cls) del bpy.types.Scene.myinputs if __name__ == "__main__": register()
Blender PythonでUIにGUI部品を追加するときは、「チェックボックス」「エディットボックス」として追加するのではなく、
「String型のプロパティ」を登録するとエディットボックスとして表示され、
「Bool型のプロパティ」を登録するとチェックボックスとして表示される。
import bpy #テキストボックスを定義 class myCInputs(bpy.types.PropertyGroup):
myTextField: bpy.props.StringProperty( name="テキスト", description="説明文", default="", maxlen=1024, # 最大文字数 subtype="NONE" )
myCheckField: bpy.props.BoolProperty( name="チェック", default=True )
class myCTool_PT_panel(bpy.types.Panel): bl_label = "パネルタイトル" bl_category = "タブタイトル" bl_space_type = "VIEW_3D" bl_region_type = "UI" def draw(self, context): layout = self.layout #横に並べるならrow.propを使う # row = layout.row() layout.prop(context.scene.myinputs, "myTextField") layout.prop(context.scene.myinputs, "myCheckField") classes = ( myCInputs, myCTool_PT_panel ) def register(): for cls in classes: bpy.utils.register_class(cls) bpy.types.Scene.myinputs = bpy.props.PointerProperty(type=myCInputs) def unregister(): for cls in classes: bpy.utils.unregister_class(cls) del bpy.types.Scene.myinputs if __name__ == "__main__": register()
import bpy
# ボタンを定義 class execButton(bpy.types.Operator): bl_idname = "szl.button" bl_label = "access to property" def execute(self, context): print( context.scene.myinputs.myTextField)# 他のGUI部品の値を取得 return{'FINISHED'}
# ボタン以外のUIを定義 class myCInputs(bpy.types.PropertyGroup): myTextField: bpy.props.StringProperty( name="テキスト", description="説明文", default="", maxlen=1024, # 最大文字数 subtype="NONE" ) myCheckField: bpy.props.BoolProperty( name="チェック", default=True )
class myCTool_PT_panel(bpy.types.Panel): bl_label = "パネルタイトル" bl_category = "タブタイトル" bl_space_type = "VIEW_3D" bl_region_type = "UI" def draw(self, context): layout = self.layout #横に並べるならrow.propを使う # row = layout.row() layout.prop(context.scene.myinputs, "myTextField") layout.prop(context.scene.myinputs, "myCheckField") layout.operator("szl.button")
classes = ( myCInputs, execButton, myCTool_PT_panel, ) def register(): for cls in classes: bpy.utils.register_class(cls) bpy.types.Scene.myinputs = bpy.props.PointerProperty(type=myCInputs) def unregister(): for cls in classes: bpy.utils.unregister_class(cls) del bpy.types.Scene.myinputs if __name__ == "__main__": register()
テキストフィールドを追加するときはpropを使用する。
import bpy
class myCInputs(bpy.types.PropertyGroup): # テキストボックスを定義 myTextField: bpy.props.StringProperty( name="タイトル", description="説明文", default="", maxlen=1024, # 最大文字数 subtype="NONE" ) # subtype: # ['FILE_PATH', 'DIR_PATH', # 'FILE_NAME', 'BYTE_STRING', # 'PASSWORD', 'NONE']
class myCTool_PT_panel(bpy.types.Panel): bl_label = "パネルタイトル" bl_category = "タブタイトル" bl_space_type = "VIEW_3D" bl_region_type = "UI" def draw(self, context): layout = self.layout #横に並べるならrow.propを使う # row = layout.row() layout.prop(context.scene.myinputs, "myTextField")
classes = ( myCInputs, myCTool_PT_panel ) def register(): for cls in classes: bpy.utils.register_class(cls) # bpy.types.Scene.myinputs変数を作成し、テキストボックスを代入 bpy.types.Scene.myinputs = bpy.props.PointerProperty(type=myCInputs) def unregister(): for cls in classes: bpy.utils.unregister_class(cls) del bpy.types.Scene.myinputs if __name__ == "__main__": register()
import bpy import mathutils msh = bpy.context.active_object Sx = 1 Sy = 1 Sz = 2 Tx = 1 Ty = 1 Tz = 1 Tmatrix = mathutils.Matrix(( (Sx ,0 , 0 , 0), (0 ,Sy , 0 , 0), (0 ,0 , Sz , 0), (0 ,0 , 0 , 1))) Smatrix = mathutils.Matrix(( (1 ,0 ,0 ,Tx), (0 ,1 ,0 ,Ty), (0 ,0 ,1 ,Tz), (0 ,0 ,0 , 1))) Matrix = Tmatrix @ Smatrix for vt in msh.data.vertices: src = mathutils.Vector((vt.co[0],vt.co[1],vt.co[2],1)) dst = Matrix @ src vt.co[0] = dst[0] vt.co[1] = dst[1] vt.co[2] = dst[2]