fip-ems / fip

3 stars 1 forks source link

I want to add text and legend in the view. #2

Open lj-cug opened 2 months ago

lj-cug commented 2 months ago

I try my best to add text rendering in the view after the code of learnopengl,but I failed! I cannot render the text in the code. Have you some tips. Yours, Li jian

arnerak commented 1 month ago

Hello Li,

Apologies for the late response. We use imgui for rendering user interface elements. Please see examples of how text is rendered here: https://github.com/fip-ems/fip/blob/main/src/vis.cpp#L373

Best regards Arne Rak

lj-cug commented 1 month ago

It's a misunderstanding. I mean how can I render text, for example the name of hydrological station, in the rendered terrain window?Thank you for your contribution.Li中国地质大学-------- 原始邮件 --------发件人: arnerak @.>日期: 2024年9月25日周三 凌晨1:23收件人: fip-ems/fip @.>抄送: lj-cug @.>, Author @.>主 题: Re: [fip-ems/fip] I want to add text and legend in the view. (Issue #2) Hello Li, Apologies for the late response. We use imgui for rendering user interface elements. Please see examples of how text is rendered here: https://github.com/fip-ems/fip/blob/main/src/vis.cpp#L373 Best regards Arne Rak

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>

arnerak commented 1 month ago

To achieve this, you need a function that translates terrain coordinates to screen coordinates. Here is an example that you can add to vis.cpp:

vec3_t terrainToScreen(int terrainX, int terrainY, const mat4_t& view, const mat4_t& projection, float terrainScale, int screenWidth, int screenHeight) {
    mat4_t model = m4_translation({ -0.5f * SimData::W, 0.f, -0.5f * SimData::H });

    float terrainZ = SimData::h_sohle[terrainY * SimData::W + terrainX];

    vec3_t worldPos = m4_mul_pos(model, { (float)terrainX, terrainScale * terrainZ, SimData::H - (float)terrainY  });
    vec3_t viewPos = m4_mul_pos(view, worldPos);
    vec3_t clipPos = m4_mul_pos(projection, viewPos);

    return { (clipPos.x + 1.0f) * 0.5f * screenWidth, (1.f - clipPos.y) * 0.5f * screenHeight, 1.f };
}

Example usage ( add in https://github.com/fip-ems/fip/blob/main/src/vis.cpp#L428 )

{
    mat4_t viewMat4;
    memcpy(&viewMat4.m00, viewMat, 16 * sizeof(float));
    auto screenPos = terrainToScreen(500, 400, viewMat4, projection, terrainScale, display_w, display_h);
    auto drawList = ImGui::GetForegroundDrawList();
    drawList->AddText({ screenPos.x, screenPos.y }, 0xFF0000FF, "test");
}
lj-cug commented 1 month ago

Thank you. Now it can work!