0

介绍

我目前正在使用 TAO 框架在 OpenGL 中绘制图形。下面列出的是我使用纹理或颜色绘制任何大小的框的代码,无论用户指定什么。这在绘制时也非常有效,如下所示。

该项目是用 VB .NET 2010 编写的,但由于大部分是在 C# 的帮助下编写的,因此我将其发布在两种语言下,因为答案可能是 C#,然后翻译成 VB .NET。

问题

现在我正在尝试开发的下一部分是选择对象并旋转/平移它的能力。我不知道该怎么做。我在网上查看过,只发现 C# 中的示例很好,但大多数方法都不会产生正确的结果。我确实知道我需要命名我相信我已经完成的对象。所以我的问题是如何做到这一点?

我也可以完全放弃绘制所有内容的现有代码。我不知道。

现有程序

在此处输入图像描述

代码

这是我的初始加载功能:

Private Sub DrawInit()
    Gl.glClearColor(0.0F, 0.0F, 0.0F, 0.0F)
    Gl.glShadeModel(Gl.GL_SMOOTH)
    Gl.glClearDepth(1.0#)
    Gl.glEnable(Gl.GL_DEPTH_TEST)
    Gl.glDepthFunc(Gl.GL_LESS)
    Gl.glEnable(Gl.GL_STENCIL_TEST)
    Gl.glHint(Gl.GL_PERSPECTIVE_CORRECTION_HINT, Gl.GL_NICEST)

    Draw_Shapes()

End Sub

我的绘图形状功能

 Private Sub Draw_Shapes()
    draw_boxes()
End Sub

我的 draw_box 函数

Private Sub draw_boxes()



    Dim i As Integer = 0

    Dim startx As Integer = 0
    Dim starty As Integer = 0
    Dim startz As Integer = 0

    Dim deltax As Integer = 0
    Dim deltay As Integer = 0
    Dim deltaz As Integer = 0

    Dim front_face_color As Color
    Dim back_face_color As Color
    Dim top_face_color As Color
    Dim bottom_face_color As Color
    Dim left_face_color As Color
    Dim right_face_color As Color

    Dim front_face_texture As Bitmap
    Dim back_face_texture As Bitmap
    Dim top_face_texture As Bitmap
    Dim bottom_face_texture As Bitmap
    Dim left_face_texture As Bitmap
    Dim right_face_texture As Bitmap

    Dim front_face_num_textures As Point
    Dim back_face_num_textures As Point
    Dim top_face_num_textures As Point
    Dim bottom_face_num_textures As Point
    Dim left_face_num_textures As Point
    Dim right_face_num_textures As Point

    Dim front_texture As Boolean
    Dim back_texture As Boolean
    Dim top_texture As Boolean
    Dim bottom_texture As Boolean
    Dim left_texture As Boolean
    Dim right_texture As Boolean


    Do Until i = boxes.Length - 1
        startx = boxes(i).start_x
        starty = boxes(i).start_y
        startz = boxes(i).start_z
        deltax = boxes(i).delta_x
        deltay = boxes(i).delta_y
        deltaz = boxes(i).delta_z
        front_face_color = boxes(i).front_face_color
        back_face_color = boxes(i).back_face_color
        top_face_color = boxes(i).top_face_color
        bottom_face_color = boxes(i).bottom_face_color
        left_face_color = boxes(i).left_face_color
        right_face_color = boxes(i).right_face_color


        front_face_texture = boxes(i).front_face_texture
        back_face_texture = boxes(i).back_face_texture
        top_face_texture = boxes(i).top_face_texture
        bottom_face_texture = boxes(i).bottom_face_texture
        left_face_texture = boxes(i).left_face_texture
        right_face_texture = boxes(i).right_face_texture

        front_face_num_textures = boxes(i).front_face_num_textures
        back_face_num_textures = boxes(i).back_face_num_textures
        top_face_num_textures = boxes(i).top_face_num_textures
        bottom_face_num_textures = boxes(i).bottom_face_num_textures
        left_face_num_textures = boxes(i).left_face_num_textures
        right_face_num_textures = boxes(i).right_face_num_textures


        If front_face_texture Is Nothing Then
            front_texture = False
        Else
            front_texture = True
        End If

        If back_face_texture Is Nothing Then
            back_texture = False
        Else
            back_texture = True
        End If

        If top_face_texture Is Nothing Then
            top_texture = False
        Else
            top_texture = True
        End If

        If bottom_face_texture Is Nothing Then
            bottom_texture = False
        Else
            bottom_texture = True
        End If

        If left_face_texture Is Nothing Then
            left_texture = False
        Else
            left_texture = True
        End If

        If right_face_texture Is Nothing Then
            right_texture = False
        Else
            right_texture = True
        End If

        Gl.glLoadName(num_shapes)
        Gl.glNewList(i + 1, Gl.GL_COMPILE)



        draw_rectangular_face(front_texture, front_face_color, front_face_texture, New ThreeDPoint(startx, starty, startz), New ThreeDPoint(startx + deltax, starty, startz), New ThreeDPoint(startx + deltax, starty + deltay, startz), New ThreeDPoint(startx, starty + deltay, startz), front_face_num_textures)

        draw_rectangular_face(back_texture, back_face_color, back_face_texture, New ThreeDPoint(startx, starty, startz), New ThreeDPoint(startx + deltax, starty, startz), New ThreeDPoint(startx + deltax, starty, startz + deltaz), New ThreeDPoint(startx, starty, startz + deltaz), back_face_num_textures)

        draw_rectangular_face(top_texture, top_face_color, top_face_texture, New ThreeDPoint(startx, starty, startz + deltaz), New ThreeDPoint(startx + deltax, starty, startz + deltaz), New ThreeDPoint(startx + deltax, starty + deltay, startz + deltaz), New ThreeDPoint(startx, starty + deltay, startz + deltaz), top_face_num_textures)

        draw_rectangular_face(bottom_texture, bottom_face_color, bottom_face_texture, New ThreeDPoint(startx, starty + deltay, startz), New ThreeDPoint(startx + deltax, starty + deltay, startz), New ThreeDPoint(startx + deltax, starty + deltay, startz + deltaz), New ThreeDPoint(startx, starty + deltay, startz + deltaz), bottom_face_num_textures)

        draw_rectangular_face(left_texture, left_face_color, left_face_texture, New ThreeDPoint(startx + deltax, starty, startz), New ThreeDPoint(startx + deltax, starty + deltay, startz), New ThreeDPoint(startx + deltax, starty + deltay, startz + deltaz), New ThreeDPoint(startx + deltax, starty, startz + deltaz), left_face_num_textures)

        draw_rectangular_face(right_texture, right_face_color, right_face_texture, New ThreeDPoint(startx, starty, startz), New ThreeDPoint(startx, starty + deltay, startz), New ThreeDPoint(startx, starty + deltay, startz + deltaz), New ThreeDPoint(startx, starty, startz + deltaz), right_face_num_textures)


        Gl.glEndList()

        num_shapes = num_shapes + 1

        i = i + 1
    Loop

我的画脸功能

Private Sub draw_rectangular_face(ByVal texture As Boolean, ByVal face_color As Color, ByVal texture_image As Bitmap, ByVal point1 As ThreeDPoint, ByVal point2 As ThreeDPoint, ByVal point3 As ThreeDPoint, ByVal point4 As ThreeDPoint, ByVal num_textures As Point)

    If texture = True Then
        load_textures(texture_image)
    End If


    Gl.glBindTexture(Gl.GL_TEXTURE_2D, 1)

    If texture = True Then
        Gl.glEnable(Gl.GL_TEXTURE_2D) 'turn on for texture
    Else
        Gl.glDisable(Gl.GL_TEXTURE_2D) 'turn on for color
    End If


    Gl.glBegin(Gl.GL_QUADS)

    If texture = True Then
        Gl.glColor3f(1, 1, 1) 'turn on for texture
    Else
        Gl.glColor3f(face_color.R / 256, face_color.G / 256, face_color.B / 256) 'turn on for color
    End If


    Gl.glTexCoord2f(0, 0) : Gl.glVertex3f(point1.x, point1.y, point1.z)
    Gl.glTexCoord2f(num_textures.X, 0) : Gl.glVertex3f(point2.x, point2.y, point2.z)
    Gl.glTexCoord2f(num_textures.X, num_textures.Y) : Gl.glVertex3f(point3.x, point3.y, point3.z)
    Gl.glTexCoord2f(0, num_textures.Y) : Gl.glVertex3f(point4.x, point4.y, point4.z)

    Gl.glEnd()
End Sub

我的 DrawScene 事件用于平移和旋转世界:

Private Sub DrawScene()

    Gl.glClear(Gl.GL_COLOR_BUFFER_BIT Or Gl.GL_DEPTH_BUFFER_BIT Or Gl.GL_STENCIL_BUFFER_BIT)
    Gl.glLoadIdentity()
    'move reference?
    Gl.glTranslatef(camera_xloc, camera_yloc, camera_zloc)
    ' Gl.glTranslatef(0.0F, 0.0F, -20.0F)


    'rotate
    Gl.glRotatef(camera_xrot, 1, 0, 0)
    Gl.glRotatef(camera_yrot, 0, 1, 0)
    Gl.glRotatef(camera_zrot, 0, 0, 1)

    Dim i As Integer = 1
    Do Until i > num_shapes
        Gl.glCallList(i)
        i = i + 1
    Loop
    Gl.glFlush()
    Application.DoEvents()
    Gdi.SwapBuffers(hDC)



    If intialload = 0 Then
        intialload = 1
        RaiseEvent initial_load_completed()
    End If
End Sub

我尝试执行以下操作将形状#1 转换为 1000,1000,1000,但没有成功:

Gl.glLoadName(num_shapes)
        Gl.glNewList(i + 1, Gl.GL_COMPILE)



        If num_shapes = 1 Then
            Gl.glPushMatrix()
            Gl.glTranslatef(1000, 1000, 1000)
        End If


        draw_rectangular_face(front_texture, front_face_color, front_face_texture, New ThreeDPoint(startx, starty, startz), New ThreeDPoint(startx + deltax, starty, startz), New ThreeDPoint(startx + deltax, starty + deltay, startz), New ThreeDPoint(startx, starty + deltay, startz), front_face_num_textures)

        draw_rectangular_face(back_texture, back_face_color, back_face_texture, New ThreeDPoint(startx, starty, startz), New ThreeDPoint(startx + deltax, starty, startz), New ThreeDPoint(startx + deltax, starty, startz + deltaz), New ThreeDPoint(startx, starty, startz + deltaz), back_face_num_textures)

        draw_rectangular_face(top_texture, top_face_color, top_face_texture, New ThreeDPoint(startx, starty, startz + deltaz), New ThreeDPoint(startx + deltax, starty, startz + deltaz), New ThreeDPoint(startx + deltax, starty + deltay, startz + deltaz), New ThreeDPoint(startx, starty + deltay, startz + deltaz), top_face_num_textures)

        draw_rectangular_face(bottom_texture, bottom_face_color, bottom_face_texture, New ThreeDPoint(startx, starty + deltay, startz), New ThreeDPoint(startx + deltax, starty + deltay, startz), New ThreeDPoint(startx + deltax, starty + deltay, startz + deltaz), New ThreeDPoint(startx, starty + deltay, startz + deltaz), bottom_face_num_textures)

        draw_rectangular_face(left_texture, left_face_color, left_face_texture, New ThreeDPoint(startx + deltax, starty, startz), New ThreeDPoint(startx + deltax, starty + deltay, startz), New ThreeDPoint(startx + deltax, starty + deltay, startz + deltaz), New ThreeDPoint(startx + deltax, starty, startz + deltaz), left_face_num_textures)

        draw_rectangular_face(right_texture, right_face_color, right_face_texture, New ThreeDPoint(startx, starty, startz), New ThreeDPoint(startx, starty + deltay, startz), New ThreeDPoint(startx, starty + deltay, startz + deltaz), New ThreeDPoint(startx, starty, startz + deltaz), right_face_num_textures)

        If num_shapes = 1 Then
            Gl.glPopMatrix()
        End If



        Gl.glEndList()
4

0 回答 0