重新定位形状/对象 - OpenGL

时间:2011-10-14 21:00:09

标签: c++ opengl

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

任何能引导我朝着正确方向前进的输入都会有所帮助。

我正在使用带有opengl的C ++。

2 个答案:

答案 0 :(得分:2)

搜索功能glTranslatef

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

答案 1 :(得分:0)

  

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

所以你已经绘制了场景,现在想要改变对象的位置?

那么,您需要了解的是,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