重新定位形状/对象 - OpenGL

发布于 2024-12-09 23:02:27 字数 119 浏览 1 评论 0原文

我想重新定位圆形、三角形、直线和圆弧。我该怎么办?我在网上搜索了解决方案,但没有任何具体解决该问题的方法。

任何能引导我走向正确方向的意见都会有所帮助。

我正在使用 C++ 和 opengl。

I would like to reposition a circle, triangles, lines and an arc. How do I go about it? I've searched the web for solutions but nothing that address the issue specifically.

Any input that will lead me in the right direction will be helpful.

I'm using C++ with opengl.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我一直都在从未离去 2024-12-16 23:02:27

搜索函数 glTranslatef

作为旁注,您可能还想查看 glRotatefglScalef。如果您对翻译一无所知,请查找翻译矩阵,首先在 2D 中学习,然后在 3D 中学习。

Search for the function glTranslatef

As a side note, you might want to look at glRotatef and glScalef also. If you know nothing about translation, look for translation matrix, first learn it in 2D and then in 3D.

最终幸福 2024-12-16 23:02:27

我想重新定位圆形、三角形、直线和圆弧。

那么您已经绘制了场景,现在想要更改对象的位置?

那么,您需要了解的是,OpenGL 不维护某种场景图。提交绘制调用后,只会更新目标帧缓冲区,仅此而已。你想改变什么吗?清除屏幕,重新绘制所有内容,但现在应用了调整。

实际上,OpenGL 只是绘制东西,它只不过是操作系统(视口/窗口)提供的一些纸张的复杂画笔(纹理)和铅笔(基元)。

由于评论而编辑

通常带有动画的交互式OpenGL图形程序,用命令式编程语言编程,结构如下(伪代码)

float T = 0
float alpha = 0, beta = 0
float red = 0, green = 0, blue = 0

paused = false

on_mousemove(mousemove):
    alpha += mousemove.rel_x
    beta  += mousemove.rel_y

on_keypress(keypress):
    switch keypress.key:
        case Key_ESCAPE:
            queue_event(Quit)
            return

        case Key_PAUSE:
            paused = not paused

        case Key_R:
            red += 0.1
        case Key_r:
            red -= 0.1

        case Key_G:
            green += 0.1
        case Key_g:
            green -= 0.1

        case Key_B:
            blue += 0.1
        case Key_b:
            blue -= 0.1

    queue_event(Redraw)

render()
    glViewport(0, 0, window.width, window.height)

    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    apply_projection()

    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    apply_modelview()

    glColor3f(red, green, blue)
    glRotatef(T * x_revolutions_per_second_factor, 1, 0, 0)
    glRotatef(alpha, 0, 1, 0)
    glRotate(beta, 0, 0, 1)

    draw_object()

    SwapBuffers()

main:
    initialize_libraries()
    load_resources()
    create_window_and_OpenGL_context()

    previous_loop = get_time()

    loop 'eventloop':
        event = peek_event()
        switch event.type:
            case MouseMove:
                on_mousemove(event.mousemove)

            case KeyPress:
                on_keypress(event.keypress)

            case Quit:
                break 'eventloop'

            case Redraw:
                render()
                break

            case NoEvents:
                fallthrough
            default:
                if not paused
                    render()

        current_loop = get_time()
        if not paused:
            T += current_loop - previous_loop
        previous_loop = current_loop

I would like to reposition a circle, triangles, lines and an arc.

So you already did draw the scene and now want to change the position of the objects?

Well then, all you need to understand is, that OpenGL doesn't maintain a scene graph of sorts. After submitting a draw call, things get only updates on the target framebuffer and that's it. You want to change something? Clear the screen, redraw everything, but now with the adjustments applied.

Really, OpenGL just draws things, it's nothing more than sophisticated brushes (textures) and pencils (primitives) for some paper offered by the operating system (viewport/window).

EDIT due to comment

The usual interactive OpenGL graphics program with animations, programmed in a imperative programming language, is structured like this (pseudocode)

float T = 0
float alpha = 0, beta = 0
float red = 0, green = 0, blue = 0

paused = false

on_mousemove(mousemove):
    alpha += mousemove.rel_x
    beta  += mousemove.rel_y

on_keypress(keypress):
    switch keypress.key:
        case Key_ESCAPE:
            queue_event(Quit)
            return

        case Key_PAUSE:
            paused = not paused

        case Key_R:
            red += 0.1
        case Key_r:
            red -= 0.1

        case Key_G:
            green += 0.1
        case Key_g:
            green -= 0.1

        case Key_B:
            blue += 0.1
        case Key_b:
            blue -= 0.1

    queue_event(Redraw)

render()
    glViewport(0, 0, window.width, window.height)

    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    apply_projection()

    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    apply_modelview()

    glColor3f(red, green, blue)
    glRotatef(T * x_revolutions_per_second_factor, 1, 0, 0)
    glRotatef(alpha, 0, 1, 0)
    glRotate(beta, 0, 0, 1)

    draw_object()

    SwapBuffers()

main:
    initialize_libraries()
    load_resources()
    create_window_and_OpenGL_context()

    previous_loop = get_time()

    loop 'eventloop':
        event = peek_event()
        switch event.type:
            case MouseMove:
                on_mousemove(event.mousemove)

            case KeyPress:
                on_keypress(event.keypress)

            case Quit:
                break 'eventloop'

            case Redraw:
                render()
                break

            case NoEvents:
                fallthrough
            default:
                if not paused
                    render()

        current_loop = get_time()
        if not paused:
            T += current_loop - previous_loop
        previous_loop = current_loop
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文