提示コードですがコメント部内部の関数ですがこれは二つの頂点を使って線を描画しているのですがその頂点の真ん中を中心に頂点を回転させるにはどうすればいいのでしょうか?
Github: https://github.com/Shigurechan/GL (Line.cpp Draw()関数部)
下記のようにやってみましたが上手く実装できません。
// ##################################### 描画 ##################################### void FrameWork::Line::Draw(glm::vec2 start, glm::vec2 end,glm::vec4 color,unsigned short width, float r) { glBindVertexArray(vao); glBindBuffer(GL_ARRAY_BUFFER, vbo); vertex->resize(2); glLineWidth(width); //太さ glm::vec2 size = glm::abs(glm::vec2(end.x - start.x,end.y - start.y)) / 2.0f; glm::vec2 center = glm::vec2(end.x + start.x,end.y + start.y) / 2.0f; vertex->at(0).position[0] = start.x; vertex->at(0).position[1] = start.y; vertex->at(1).position[0] = +end.x; vertex->at(1).position[1] = +end.y; if(rotate != r) { //setPosition(center); //座標 rotate = r; vertex->at(0).position[0] = (cos(r) * size.x) * center.x; vertex->at(0).position[1] = (sin(r) * size.y) * center.y; vertex->at(1).position[0] = (cos(r) * size.x) * center.x; vertex->at(1).position[1] = (sin(r) * size.y) * center.y; vertex->at(0).position[0] += start.x; vertex->at(0).position[1] += start.y; vertex->at(1).position[0] += end.x; vertex->at(1).position[1] += end.y; } else { } setPosition(glm::vec2(0,0)); //座標 //Transform setScale(glm::vec2(1,1)); //スケール setRotate(0); //回転 shader->setEnable(); shader->setUniformMatrix4fv("uTranslate", getMatTranslation()); shader->setUniformMatrix4fv("uRotate", getMatRotate()); shader->setUniformMatrix4fv("uScale", getMatScale()); shader->setUniformMatrix4fv("uViewProjection", glm::ortho(0.0f, FrameWork::windowContext->getSize().x, FrameWork::windowContext->getSize().y, 0.0f, -1.0f, 1.0f)); shader->setUniform4f("uFragment",color); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(VertexAttribute) * vertex->size(), vertex->data()); glDrawArrays(GL_LINES, 0, vertex->size()); shader->setDisable(); //バインド解除 glBindVertexArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindTexture(GL_TEXTURE_2D, 0); }
// ##################################### 描画 ##################################### void FrameWork::Line::Draw(glm::vec2 start, glm::vec2 end,glm::vec4 color,unsigned short width, float r) { glBindVertexArray(vao); glBindBuffer(GL_ARRAY_BUFFER, vbo); vertex->resize(2); glLineWidth(width); //太さ glm::vec2 size = glm::abs(end - start) / 2.0f; vertex->at(0).position[0] = start.x; vertex->at(0).position[1] = start.y; vertex->at(1).position[0] = +end.x; vertex->at(1).position[1] = +end.y; //////////////////////////////////////////////////////////////////////////////////////////////////// //Transform setPosition(glm::vec2(0,0)); //座標 setScale(glm::vec2(1,1)); //スケール setRotate(r); //回転 //////////////////////////////////////////////////////////////////////////////////////////////////// shader->setEnable(); shader->setUniformMatrix4fv("uTranslate", getMatTranslation()); shader->setUniformMatrix4fv("uRotate", getMatRotate()); shader->setUniformMatrix4fv("uScale", getMatScale()); shader->setUniformMatrix4fv("uViewProjection", glm::ortho(0.0f, FrameWork::windowContext->getSize().x, FrameWork::windowContext->getSize().y, 0.0f, -1.0f, 1.0f)); shader->setUniform4f("uFragment",color); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(VertexAttribute) * vertex->size(), vertex->data()); glDrawArrays(GL_LINES, 0, vertex->size()); shader->setDisable(); //バインド解除 glBindVertexArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindTexture(GL_TEXTURE_2D, 0); } // ##################################### デストラクタ ##################################### FrameWork::Line::~Line() { }
回答1件
あなたの回答
tips
プレビュー