繁体   English   中英

使用 glm::ortho(-width / 2, width / 2, -height/2, height, 0.1f, 10.0f) 时 glm::ortho() 拉伸;

[英]glm::ortho() streches when using glm::ortho(-width / 2, width / 2, -height/2, height, 0.1f, 10.0f);

我一直在尝试 3d 投影,

但我一直使用 -1 1 范围内的顶点我想使用 1 像素大的单位,所以我使用 glm::ortho(-width / 2, width / 2, -height/2, height, 0.1f, 10.0 F);

但这不起作用

下面是 glm::ortho(-1.0f, 1.0f, -1.0f, 1.0f, 0.1f,1.0f) 的代码:

#include "Engine.h"
#include "vendor/stb/stb_image.h"

float width = 1200;
float height = 750;

// edits in imgui files have farouk comment before them


int main(void)
{
    initGLFW();

GLFWimage icon;
icon.pixels = stbi_load("data/Images/Icon.png", &icon.width, &icon.height, nullptr, 4);

GLFWimage image;
image.pixels = stbi_load("data/Images/Cursor.png", &image.width, &image.height, nullptr, 4);
GLFWcursor * cursor = glfwCreateCursor(&image, image.width / 2, image.height / 2);

glfwWindowHint(GLFW_SAMPLES, 6);
glEnable(GL_MULTISAMPLE);

Window window("OGL", width, height, MVP());
Renderer renderer;

renderer.GenerateFonts({"data/fonts/arial.ttf", "data/fonts/Pixelboy.ttf"}, {"arial", "roboto"}, {64, 64});

// set perspective projection & set xyz ranges from -1, 1 to respective ranges
window.mvp.projection = glm::ortho(-1.0f, 1.0f, -1.0f, 1.0f, 0.1f, 10.0f);

window.createWindow("data/Images/Cursor.png", "data/Images/Icon.png");

glfwSetWindowIcon(window.window, 1, &icon);
glfwSetCursor(window.window, cursor);

stbi_image_free(image.pixels);
stbi_image_free(icon.pixels);

glfwMakeContextCurrent(window.window);
initGLEW(true);
glDepthMask(GL_TRUE);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);

renderer.loadFonts({"arial", "roboto"});
initImGUI(window.window, "data/fonts/arial.ttf");

Shader shader("data/shaders/base.shader");


// TODO: Add Multiple Window Support (Window Manager)
// Addd .txt file for text with Characters map insteadt loaf Fonts
// texture in 3d space & 3d textures
// map -1, 1 to -width/2 , width/2


std::vector <Obj> cubes = {
{
        {
            { {-0.5, -0.5, -1.1}, {1,1,1,1} },
            { {-0.5, 0.5, -1.1}, {1,1,1,1} },
            { {0.5, 0.5, -1.1}, {1,1,1,1} },
            { {0.5, -0.5, -1.1}, {1,1,1,1} },

            { {-0.5, -0.5, -2.1}, {0,1,1,1} },
            { {-0.5, 0.5, -2.1}, {0,1,1,1} },
            { {0.5, 0.5, -2.1}, {0,1,1,1} },
            { {0.5, -0.5, -2.1}, {0,1,1,1} },

        },
        {
            0,1,2, 2,3,0, 4,5,6, 6,7,4, 0,4,5, 1,5,0
        },
        true
    }
};

float tx = 0;
float ty = 0;
float tz = 0;

while (!glfwWindowShouldClose(window.window) )
{
    glfwMakeContextCurrent(window.window);
    renderer.Clear({0.0, 0.0, 0.0, 1});


    // feed inputs to dear imgui, start new frame
    ImGui_ImplOpenGL3_NewFrame();
    ImGui_ImplGlfw_NewFrame();
    ImGui::NewFrame();

    renderer.Draw(shader, window.mvp, cubes);
    window.mvp.view = glm::translate(glm::mat4(1), glm::vec3(0, 0, -1.6)) * glm::rotate(glm::mat4(1), glm::radians(tx), glm::vec3(1,0,0)) * glm::rotate(glm::mat4(1), glm::radians(ty), glm::vec3(0,1,0)) * glm::rotate(glm::mat4(1), glm::radians(tz), glm::vec3(0,0,1));
    window.mvp.view = glm::translate(window.mvp.view, glm::vec3(0, 0, 1.6));
    renderer.Text(shader, MVP(glm::mat4(1.0), glm::mat4(1.0),glm::ortho(0.0, (double)width, 0.0, (double)height)), {(TextObj){"roboto", "FPS:" + std::to_string((int)ImGui::GetIO().Framerate), 0.01424501424f * height, 0.01424501424f * height, 0, 0.02849002849f * height, 0, {1,1,1,1}}});

    // render your GUI
    ImGui::Begin("Demo window");
    ImGui::Button("Hello!");
    ImGui::SliderFloat("x", &tx, 0, 360, "%.0f", 1.0f);
    ImGui::SliderFloat("y", &ty, 0, 360, "%.0f", 1.0f);
    ImGui::SliderFloat("z", &tz, 0, 360, "%.0f", 1.0f);
    ImGui::End();

    // Render dear imgui into screen
    ImGui::Render();
    ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());


    int display_w, display_h;
    glfwGetFramebufferSize(window.window, &display_w, &display_h);
    glViewport(0, 0, display_w, display_h);
    glfwSwapBuffers(window.window);
    glfwSwapInterval(1);
    glfwPollEvents();
}

ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();

return 0;
}

和输出:

在此处输入图像描述

下面是 glm::ortho(-width / 2, width / 2, -height/2, height, 0.1f, 10.0f) 的代码:

#include "Engine.h"
#include "vendor/stb/stb_image.h"

float width = 1200;
float height = 750;

// edits in imgui files have farouk comment before them


int main(void)
{
    initGLFW();
GLFWimage icon;
icon.pixels = stbi_load("data/Images/Icon.png", &icon.width, &icon.height, nullptr, 4);

GLFWimage image;
image.pixels = stbi_load("data/Images/Cursor.png", &image.width, &image.height, nullptr, 4);
GLFWcursor * cursor = glfwCreateCursor(&image, image.width / 2, image.height / 2);

glfwWindowHint(GLFW_SAMPLES, 6);
glEnable(GL_MULTISAMPLE);

Window window("OGL", width, height, MVP());
Renderer renderer;

renderer.GenerateFonts({"data/fonts/arial.ttf", "data/fonts/Pixelboy.ttf"}, {"arial", "roboto"}, {64, 64});

// set perspective projection & set xyz ranges from -1, 1 to respective ranges
window.mvp.projection = glm::ortho(-width / 2, width / 2, -height/2, height, 0.1f, 10.0f);

window.createWindow("data/Images/Cursor.png", "data/Images/Icon.png");

glfwSetWindowIcon(window.window, 1, &icon);
glfwSetCursor(window.window, cursor);

stbi_image_free(image.pixels);
stbi_image_free(icon.pixels);

glfwMakeContextCurrent(window.window);
initGLEW(true);
glDepthMask(GL_TRUE);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);

renderer.loadFonts({"arial", "roboto"});
initImGUI(window.window, "data/fonts/arial.ttf");

Shader shader("data/shaders/base.shader");


// TODO: Add Multiple Window Support (Window Manager)
// Addd .txt file for text with Characters map insteadt loaf Fonts
// texture in 3d space & 3d textures
// map -1, 1 to -width/2 , width/2


std::vector <Obj> cubes = {
{
        {
            { {-300, -300, -1.1}, {1,1,1,1} },
            { {-300, 300, -1.1}, {1,1,1,1} },
            { {300, 300, -1.1}, {1,1,1,1} },
            { {300, -300, -1.1}, {1,1,1,1} },

            { {-300, -300, -2.1}, {0,1,1,1} },
            { {-300, 300, -2.1}, {0,1,1,1} },
            { {300, 300, -2.1}, {0,1,1,1} },
            { {300, -300, -2.1}, {0,1,1,1} },

        },
        {
            0,1,2, 2,3,0, 4,5,6, 6,7,4, 0,4,5, 1,5,0
        },
        true
    }
};

float tx = 0;
float ty = 0;
float tz = 0;

while (!glfwWindowShouldClose(window.window) )
{
    glfwMakeContextCurrent(window.window);
    renderer.Clear({0.0, 0.0, 0.0, 1});


    // feed inputs to dear imgui, start new frame
    ImGui_ImplOpenGL3_NewFrame();
    ImGui_ImplGlfw_NewFrame();
    ImGui::NewFrame();

    renderer.Draw(shader, window.mvp, cubes);
    window.mvp.view = glm::translate(glm::mat4(1), glm::vec3(0, 0, -1.6)) * glm::rotate(glm::mat4(1), glm::radians(tx), glm::vec3(1,0,0)) * glm::rotate(glm::mat4(1), glm::radians(ty), glm::vec3(0,1,0)) * glm::rotate(glm::mat4(1), glm::radians(tz), glm::vec3(0,0,1));
    window.mvp.view = glm::translate(window.mvp.view, glm::vec3(0, 0, 1.6));
    renderer.Text(shader, MVP(glm::mat4(1.0), glm::mat4(1.0),glm::ortho(0.0, (double)width, 0.0, (double)height)), {(TextObj){"roboto", "FPS:" + std::to_string((int)ImGui::GetIO().Framerate), 0.01424501424f * height, 0.01424501424f * height, 0, 0.02849002849f * height, 0, {1,1,1,1}}});

    // render your GUI
    ImGui::Begin("Demo window");
    ImGui::Button("Hello!");
    ImGui::SliderFloat("x", &tx, 0, 360, "%.0f", 1.0f);
    ImGui::SliderFloat("y", &ty, 0, 360, "%.0f", 1.0f);
    ImGui::SliderFloat("z", &tz, 0, 360, "%.0f", 1.0f);
    ImGui::End();

    // Render dear imgui into screen
    ImGui::Render();
    ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());


    int display_w, display_h;
    glfwGetFramebufferSize(window.window, &display_w, &display_h);
    glViewport(0, 0, display_w, display_h);
    glfwSwapBuffers(window.window);
    glfwSwapInterval(1);
    glfwPollEvents();
}

ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();

return 0;
}

和输出: 在此处输入图像描述

请注意,我将顶点从 0.5 更改为 300,所以这不是问题

obj结构的解释

{

// 顶点 -> vbo

{ {-0.5, -0.5, -1.1}, {1,1,1,1} }, glm::vec3 pos, glm::vec4 col

{ {-0.5, 0.5, -1.1}, {1,1,1,1} },

{ {0.5, 0.5, -1.1}, {1,1,1,1} },

{ {0.5, -0.5, -1.1}, {1,1,1,1} },

{ {-0.5, -0.5, -2.1}, {0,1,1,1} },

{ {-0.5, 0.5, -2.1}, {0,1,1,1} },

{ {0.5, 0.5, -2.1}, {0,1,1,1} },

{ {0.5, -0.5, -2.1}, {0,1,1,1} },

},

{ // indexex -> ibo

0,1,2, 2,3,0, 4,5,6, 6,7,4, 0,4,5, 1,5,0

},

true // 填充 -> GL_TRIANGLES 或 GL_LINES

}

感谢@derhass 的澄清,我绘制的长方体太薄,我的 Z 远值太短我已经将它调整为 1000.0f,现在一切正常

这是他的评论:

“问题是立方体变成了一条线”。 你的顶点数据中没有立方体,有一个大小为 600x600x1 的长方体,所以如果你从某些角度看它,它当然会看起来很薄。 此外,由于 z 范围只有 10 个单位,您会遇到剪裁问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM