JeffBla
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # OpenGL NCKU HW3 # 作業環境 vscode / glfw version 3.3.6 / cmake version 3.31.4 / GNU Make 4.3 / g++ (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3. / Linux 6.11.0-19-generic #19~24.04.1-Ubuntu SMP PREEMPT_DYNAMIC Mon Feb 17 11:51:52 UTC 2 x86_64 x86_64 x86_64 GNU/Linux # 方法說明 動作呈現是以 interpolation 與 frame 方式進行。 其 interpolation: ```cpp glm::vec3 interpolatedOffset = glm::mix(prevFrameData->offset, FrameData->offset, progress); glm::vec3 interpolatedRotation = glm::mix(prevFrameData->rotation, FrameData->rotation, progress); glm::vec3 interpolatedSize = glm::mix(prevFrameData->size, FrameData->size, progress); ``` 而每個 frame 為 hard-code ,並以位移、旋轉、縮放,對該物件進行操作。 ```cpp transformFrames_[TransformFrame::FRAME1] = { {"torso", std::make_shared<TransformFrameData>(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(40.0f, 0.0f, 0.0f), glm::vec3(1.0f, 1.5f, 0.5f))}, {"abdomen", std::make_shared<TransformFrameData>(glm::vec3(0.0f, -0.1f, 0.5f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.5f, 1.0f, 0.4f))}, {"abdomenRight", std::make_shared<TransformFrameData>(glm::vec3(-0.6f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.5f, 1.0f, 0.4f))}, {"abdomenLeft", std::make_shared<TransformFrameData>(glm::vec3(0.6f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.5f, 1.0f, 0.4f))}, {"tail", std::make_shared<TransformFrameData>(glm::vec3(0.0f, -1.5f, -0.3f), glm::vec3(40.0f, 0.0f, 0.0f), glm::vec3(0.1f, 0.5f, 0.1f))}, {"head", std::make_shared<TransformFrameData>(glm::vec3(0.0f, 2.2f, 0.0f), glm::vec3(-35.0f, 0.0f, 0.0f), glm::vec3(0.75f, 0.75f, 0.75f))}, {"nose", std::make_shared<TransformFrameData>(glm::vec3(0.0f, -0.3f, 0.75f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.4f, 0.4f, 0.2f))}, {"leftShoulder", std::make_shared<TransformFrameData>(glm::vec3(-1.2f, 1.0f, 0.0f), glm::vec3(-65.0f, 0.0f, 0.0f), glm::vec3(0.25f, 0.4f, 0.25f))}, {"leftArm", std::make_shared<TransformFrameData>(glm::vec3(0.0f, -0.8f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.25f, 0.4f, 0.25f))}, {"leftHand", std::make_shared<TransformFrameData>(glm::vec3(0.0f, -0.7f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.3f, 0.3f, 0.3f))}, {"rightShoulder", std::make_shared<TransformFrameData>(glm::vec3(1.2f, 1.0f, 0.0f), glm::vec3(-65.0f, 0.0f, 0.0f), glm::vec3(0.25f, 0.4f, 0.25f))}, {"rightArm", std::make_shared<TransformFrameData>(glm::vec3(0.0f, -0.8f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.25f, 0.4f, 0.25f))}, {"rightHand", std::make_shared<TransformFrameData>(glm::vec3(0.0f, -0.7f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.3f, 0.3f, 0.3f))}, {"leftHip", std::make_shared<TransformFrameData>(glm::vec3(-0.4f, -2.0f, 0.3f), glm::vec3(-30.0f, 0.0f, 0.0f), glm::vec3(0.25f, 0.5f, 0.25f))}, {"leftLeg", std::make_shared<TransformFrameData>(glm::vec3(0.0f, -1.0f, 0.0f), glm::vec3(15.0f, 0.0f, 0.0f), glm::vec3(0.25f, 0.5f, 0.25f))}, {"leftFoot", std::make_shared<TransformFrameData>(glm::vec3(0.0f, -0.7f, 0.1f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.3f, 0.2f, 0.5f))}, {"rightHip", std::make_shared<TransformFrameData>(glm::vec3(0.4f, -1.0f, 0.7f), glm::vec3(-90.f, 0.0f, 0.0f), glm::vec3(0.25f, 0.5f, 0.25f))}, {"rightLeg", std::make_shared<TransformFrameData>(glm::vec3(0.0f, -1.0f, 0.0f), glm::vec3(15.0f, 0.0f, 0.0f), glm::vec3(0.25f, 0.5f, 0.25f))}, {"rightFoot", std::make_shared<TransformFrameData>(glm::vec3(0.0f, -0.7f, 0.1f), glm::vec3(25.0f, 0.0f, 0.0f), glm::vec3(0.3f, 0.2f, 0.5f))} }; ``` 骨架為先創建枝幹,接著按照親代關係建立結構。 ```cpp std::shared_ptr<Joint> Animal::createBoneHierarchy() { // Create individual models for each body part with different sizes and textures // Create torso (root) auto torsoModel = createBodyPartModel("torso", glm::vec3(1.0f, 1.5f, 0.5f), texturePath); auto torso = std::make_unique<Joint>("torso", glm::vec3(0.0f, 0.0f, 0.0f), torsoModel); auto abdomenModel = createBodyPartModel("abdomen", glm::vec3(0.5f, 0.5f, 0.2f), texturePath); auto abdomen = std::make_unique<Joint>("abdomen", glm::vec3(0.0f, -0.5f, 0.5f), abdomenModel); ... // Build hierarchy (bottom-up) leftArm->addChild(std::move(leftHand)); leftShoulder->addChild(std::move(leftArm)); rightArm->addChild(std::move(rightHand)); rightShoulder->addChild(std::move(rightArm)); leftLeg->addChild(std::move(leftFoot)); leftHip->addChild(std::move(leftLeg)); rightLeg->addChild(std::move(rightFoot)); rightHip->addChild(std::move(rightLeg)); head->addChild(std::move(nose)); // Add all to torso torso->addChild(std::move(abdomen)); torso->addChild(std::move(abdomenLeft)); torso->addChild(std::move(abdomenRight)); torso->addChild(std::move(tail)); torso->addChild(std::move(head)); torso->addChild(std::move(leftShoulder)); torso->addChild(std::move(rightShoulder)); torso->addChild(std::move(leftHip)); torso->addChild(std::move(rightHip)); return torso; } ``` ## Bonus - Using texture. - Camera look at and translation. - Free Camera: WASD and mouse control. - Model control. - Animation control. ### 使用 texture 利用 ```cpp glBufferData(static_cast<GLenum>(type_), size, data, static_cast<GLenum>(usagePattern_)); ``` 與 ```cpp glVertexAttribPointer(index, size, type, normalized, stride, reinterpret_cast<void *>(offset)); ``` 將 texture Coordinates 的資料給儲存並設定性質,並在渲染時 `glActiveTexture` 與 `glBindTexture`。 ### Camera 移動與關注物體 根據[文檔](https://glm.g-truc.net/0.9.9/api/a00668.html#gaa64aa951a0e99136bba9008d2b59c78e)設定相機位置與關注目標物體。 ```cpp GLM_FUNC_DECL mat<4, 4, T, Q> glm::lookAt(vec< 3, T, Q > const &eye, vec< 3, T, Q > const &center, vec< 3, T, Q > const &up) ``` ### Free camera WASD移動 ```cpp bool wasFreeCamera_ = true; bool isFirstFreeCamera_ = true; bool isFreeCamera_ = false; glm::vec3 lookAt_; glm::vec3 cameraPosition_ = glm::vec3(0.0f, 0.0f, 3.0f); glm::vec3 cameraFront_ = glm::vec3(0.0f, 0.0f, -1.0f); glm::vec3 cameraUp_ = glm::vec3(0.0f, 1.0f, 0.0f); float cameraSpeedFactor_ = 2.5f; float deltaTime_ = 0.0f; // time between current frame and last frame float lastFrame_ = 0.0f; static bool mouseCaptured_; float mouse_lastX_ = 400, mouse_lastY_ = 300; float mouse_yaw_ = -90.0f; float mouse_pitch_ = 0.0f; float mouse_fov_ = 45.0f; float sensitivity_ = 1.0f; ``` 紀錄相機座標、速度、視角目標與紀錄時間變化讓移動運算合理可行,使用 `glfwGetKey` 接受按鍵事件,並針對其控制相機位置 ### Free camera 滑鼠控制視角 增加滑鼠位置之 callback 函式,使用 mouseCaptured 與按鍵 c 確保滑鼠指標能夠隨時使用與拖離控制視角滑鼠鎖定 `GLFW_CURSOR_DISABLED` 並確定事件觸發為一組按壓與施放操作,以免在按下後取消與進入控制視角模式不斷切換。 ```c if (glfwGetKey(window_, GLFW_KEY_C) == GLFW_PRESS) { if (!mouseCaptured) { mouseCaptured = true; if (glfwGetInputMode(window_, GLFW_CURSOR) == GLFW_CURSOR_DISABLED) { glfwSetInputMode(window_, GLFW_CURSOR, GLFW_CURSOR_NORMAL); glfwSetCursorPosCallback(window_, nullptr); wasFreeCamera_ = true; } else { glfwSetInputMode(window_, GLFW_CURSOR, GLFW_CURSOR_DISABLED); glfwSetCursorPosCallback(window_, Detail::mouseCallback); } } } else { mouseCaptured = false; } firstEnterMouseCallback(window_, mouse_lastX_, mouse_lastY_, isFirstFreeCamera_); cameraDirectionLoop(window_, mouse_lastX_, mouse_lastY_); } ``` `firstEnterMouseCallback` 確保相機視角不會因為第一次進入自由相機模式或者第一次移動滑鼠而造成相機大幅度改變視角 使用 `cameraDirectionLoop` 進行滑鼠移動運算。 ```cpp void OpenGLWindow::cameraDirectionLoop(GLFWwindow *window, double xpos, double ypos) { float xoffset = xpos - mouse_lastX_; float yoffset = mouse_lastY_ - ypos; mouse_lastX_ = xpos; mouse_lastY_ = ypos; xoffset *= sensitivity_; yoffset *= sensitivity_; mouse_yaw_ += xoffset; mouse_pitch_ += yoffset; // Add constraints to prevent camera flipping if (mouse_pitch_ > 89.0f) mouse_pitch_ = 89.0f; if (mouse_pitch_ < -89.0f) mouse_pitch_ = -89.0f; glm::vec3 front; front.x = cos(glm::radians(mouse_pitch_)) * cos(glm::radians(mouse_yaw_)); front.y = sin(glm::radians(mouse_pitch_)); front.z = cos(glm::radians(mouse_pitch_)) * sin(glm::radians(mouse_yaw_)); cameraFront_ = glm::normalize(front); lookAt_ = cameraPosition_ + cameraFront_; } ``` ### 模型控制 能夠自由的更改每個階層模型的位置、大小與旋轉。 位置及旋轉為 local 操作且子代會受到影響,而大小只跟自己有關,調整大小並不會影響其他模型的大小,方便物件移動觀察。 額外建立一個 `Joint` 物件,並將 scale 的計算分開,以達成大小與其他物件無關。 ```cpp class Joint { ... glm::mat4 getLocalTransform() const; void draw(glm::mat4 parentTransform, glm::mat4 &view, glm::mat4 &projection); ... }; ``` ```cpp void Joint::draw(glm::mat4 parentTransform, glm::mat4 &view, glm::mat4 &projection) { glm::mat4 localTransform = getLocalTransform(); glm::mat4 worldTransformNoScale = parentTransform * localTransform; glm::mat4 worldTransform = glm::scale(worldTransformNoScale, size_); if (model_) { model_->setModelMatrix(worldTransform); model_->draw(view, projection); } for (auto& child : children_) { child->draw(worldTransformNoScale, view, projection); } } ``` 在 GUI 中利用迭代與遞迴展開骨架 ```cpp void OpenGLWindow::windowImguiModelSetting() { ... auto animal_root = animal_->getRootJoint(); _windowImguiModelSetting(animal_root); } ``` ```cpp void OpenGLWindow::_windowImguiModelSetting(std::shared_ptr<Joint> &joint) { ImGui::Text("%s", joint->getName().c_str()); ImGui::SliderFloat3(("Position##" + joint->getName()).c_str(), glm::value_ptr(joint->getOffset()), -10.0f, 10.0f); ImGui::SliderFloat3(("Rotation##" + joint->getName()).c_str(), glm::value_ptr(joint->getRotation()), -180.0f, 180.0f); ImGui::SliderFloat3(("Scale##" + joint->getName()).c_str(), joint.get(), &Joint::getSize, &Joint::setSize, 0.1f, 5.0f); ImGui::Separator(); for(auto& child : joint->getChildren()) { _windowImguiModelSetting(child); } } ``` ### 動畫控制 由於我們動畫是一幀幀組成,只要將幀數們紀錄在變數中並由 GUI 呈現,即可監控動畫。至於連續循環動畫則是透過餘數運算進行頭對尾的動畫連接。 ```cpp void Animal::TransformToFrame(TransformFrame frame) { if(currentFrame == frame) { return; } currentFrame = frame; transformationProgress_ = 0.0001f; } ``` GUI ```cpp ImGui::RadioButton("Transform 1", (int *) &animal_->getCurrentFrame(), 0); ImGui::SameLine(); ImGui::RadioButton("Transform 2", (int *) &animal_->getCurrentFrame(), 1); ImGui::SameLine(); ImGui::RadioButton("Transform 3", (int *) &animal_->getCurrentFrame(), 2); animal_->TransformToFrame(static_cast<Animal::TransformFrame>(animal_->getCurrentFrame())); ``` 更新動畫的算法,當正在運行動畫且該影格運行結束,則播放下一個影格。 ```cpp if(isAnimating_ && transformationProgress_ >= 1.0f) { currentFrame = static_cast<TransformFrame>((currentFrame + 1) % transformFrameSize); transformationProgress_ = 0.0001f; } ``` # 程式如何執行 ```shell $ cd "out/build/GCC 13.3.0 x86_64-linux-gnu/bin/Debug" $ ./Homework03 ``` 程式一開始會先建立 Animal 物件,Animal 物件會創建 shader 並連結到一個 shaerprogram ,接著建立 cube 物件們組成人體,cube 物件會設定 texture 相關訊息, VAO 、 VBO 也設置好後,即可透過 Window 將貼好 texture 的 model 顯示出來。 呈現 model 後,若要產生動作,則利用旋轉與縮放達成。

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully