Archived

This topic is now archived and is closed to further replies.

Texture Question

This topic is 6158 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all, i have a problem using textures with DX8 and VB. Well, i draw a triangle and a square,and since the square was made of two triangles(trianglestrip),that's the problem; only the half part of the square appears(one triangle)and not the second. Here is the complete code if you want to help me.Sorry for the size of this post. Thanks.(I also have a problem with my english
    
'Module Code
Option Explicit
Public objDX As DirectX8 'El objeto principal de donde se derivan los demas
Public objD3D As Direct3D8 'El objeto que controla las operaciones de graficos
Public objDevice As Direct3DDevice8 'El responsable de dibujar todas las imagenes
Public bInit As Boolean 'Nos dira si DirectX se inicializo con exito
Public bFin As Boolean 'Para saber si debemos terminar o no
Public m_vb As Direct3DVertexBuffer8 'Nuestro vertex buffer
Public D3DX As D3DX8 'libreria que nos ayuda bastante (Nuevo)
Public Texture As Direct3DTexture8 '(Nuevo)
'Estructura de nuestro vertice(Modificado)
Public Type CUSTOMVERTEX
    x As Single
    y As Single
    z As Single
    rhw As Single
    color As Long
    texu As Single
    texv As Single
End Type
'Nuestro FVF(Flexible Vertex Format), que describe la estructura del vertice CUSTOMVERTEX(Modificado)
Public Const D3DFVF_CUSTOMVERTEX = (D3DFVF_XYZRHW Or D3DFVF_DIFFUSE Or D3DFVF_TEX1)

'La funcion de inicializacion, aca empieza todo el proceso
Public Function DXInit()
    'Relativo a la informacion del modo de presentacion como color, tamaño de la pantalla,etc
    Dim m_D3DDisplayMode As D3DDISPLAYMODE
    'Aca iran las opciones que elegimos para que DirectX despliegue el grafico
    Dim m_D3DPresentParameters As D3DPRESENT_PARAMETERS
    On Error GoTo errorhandler
    'Creamos una instancia del objeto Direct3D
    Set objD3D = objDX.Direct3DCreate
    'Obtiene el modo de presentacion del adaptador actual y lo guarda en m_D3DDisplayMode
    Call objD3D.GetAdapterDisplayMode(D3DADAPTER_DEFAULT, m_D3DDisplayMode)
    'Especificamos los parametros de presentacion
    With m_D3DPresentParameters
        .Windowed = 1
        .SwapEffect = D3DSWAPEFFECT_COPY_VSYNC
        .BackBufferFormat = m_D3DDisplayMode.Format
    End With
    'Crea un dispositivo para el adaptador especificado
    Set objDevice = objD3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Form1.hWnd, _
                                        D3DCREATE_SOFTWARE_VERTEXPROCESSING, m_D3DPresentParameters)
    'Si no se creo el dispositivo generamos un error
    If objDevice Is Nothing Then GoTo errorhandler
    Set D3DX = New D3DX8 '(Nuevo)
    Set Texture = D3DX.CreateTextureFromFile(objDevice, App.Path & "\opentutor1.bmp") '(Nuevo)
    DXInit = True
    Exit Function
errorhandler:
    DXInit = False
End Function
'Crea y llena un vertex buffer con los vertices especificados
Public Function InitVertex() As Boolean
    'Inicializa tres vertices
    Dim Vertices(0 To 6) As CUSTOMVERTEX
    Dim VertexSizeInBytes As Long
    On Error GoTo errorhandler
    VertexSizeInBytes = Len(Vertices(0)) 'Tamaño de un vertice
    'Llena la estructura de nuestros vertices (triangulo)(Modificado)
    With Vertices(0): .x = 50: .y = 50: .z = 0.5: .rhw = 1: .color = &HFFFF0000: .texu = 0.5: .texv = 0: End With
    With Vertices(1): .x = 100: .y = 150: .z = 0.5: .rhw = 1: .color = &HFF00FF00: .texu = 1: .texv = 1: End With
    With Vertices(2): .x = 10: .y = 150: .z = 0.5: .rhw = 1: .color = &HFF00FFFF: .texu = 0: .texv = 1: End With
    'Cuadrado(Modificado)
    With Vertices(3): .x = 150: .y = 50: .z = 0.5: .rhw = 1: .color = RGB(255, 0, 0): .texu = 0: .texv = 0: End With
    With Vertices(4): .x = 250: .y = 50: .z = 0.5: .rhw = 1: .color = RGB(0, 255, 0): .texu = 1: .texv = 0: End With
    With Vertices(5): .x = 250: .y = 150: .z = 0.5: .rhw = 1: .color = RGB(0, 0, 180): .texu = 1: .texv = 1: End With
    With Vertices(6): .x = 150: .y = 150: .z = 0.5: .rhw = 1: .color = RGB(0, 255, 0): .texu = 0: .texv = 1: End With
    'Crea el vertex buffer con espacio suficiente para los siete vertices
    '   LengthInBytes= VertexSizeInBytes *7     (El tamaño total del buffer para los 7 vertices)
    '   fvf=D3DFVF_CUSTOMVERTEX                 (Describe que hay en los vertices)
    '   flags= 0                                (opciones por defecto)
    '   pool=D3DPOOL_DEFAULT                    (d3d decide que tipo de memoria usar)
    Set m_vb = objDevice.CreateVertexBuffer(VertexSizeInBytes * 7, _
                                            0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT)
    If m_vb Is Nothing Then GoTo errorhandler
    'Llenamos el vertex buffer
    '   VBuffer=m_VB                        El vertex buffer que queremos llenar
    '   Offset=0                            Queremos llenarlo desde el comienzo del buffer
    '   Size=VertexSizeInBytes*7              Copiamos 7 tipos CUSTOMVERTEX en el buffer
    '   flags=0                             Enviamos las opciones por defecto al lock
    '   data=Vertices(0)                    Enviamos el primer elemento del array
    D3DVertexBuffer8SetData m_vb, 0, VertexSizeInBytes * 7, 0, Vertices(0)

    InitVertex = True
    Exit Function
errorhandler:
    InitVertex = False
End Function

'FORM CODE
Private Sub Form_Load()
    Dim v As CUSTOMVERTEX
    Dim VertexSize As Long
    Me.Show
    Me.Caption = "OpenTutor - Inicializando DirectX..."
    'Creamos una nueva instancia del objeto DirectX8 para la variable
    Set objDX = New DirectX8
    bInit = DXInit()
    If bInit = False Then
        MsgBox "Error critico en DirectX Graphics", vbExclamation + vbOKOnly, "Error"
        End
    End If
    bInit = InitVertex()
    If bInit = False Then
        MsgBox "No se pudo crear el vertex buffer", vbExclamation + vbOKOnly, "Error"
        End
    End If
    Me.Caption = "OpenTutor - Texturas"
    Do
        'Limpia el viewport(area de visualizacion)
        Call objDevice.Clear(0, ByVal 0, D3DCLEAR_TARGET, &H44AA&, 1#, 0)
        objDevice.BeginScene
            VertexSize = Len(v) 'nuevo
            'Pasamos los vertices a un "stream",especificando el origen del stream
            'que es nuestro vertex buffer m_vb
            objDevice.SetStreamSource 0, m_vb, VertexSize
            'D3D debe saber que vertex shader usar, en la mayoria de los casos
            'el vertex shader es solo el FVF, para saber con que tipo de vertice
            'estamos tratando
            objDevice.SetVertexShader D3DFVF_CUSTOMVERTEX
            'objDevice.SetTexture 0, Texture 'Especificamos la textura a usar
            'Finalmente dibujamos un triangulo
            objDevice.DrawPrimitive D3DPT_TRIANGLELIST, 0, 1
            'y un cuadrado
            objDevice.DrawPrimitive D3DPT_TRIANGLESTRIP, 3, 2
        objDevice.EndScene 'Terminamos la escena
        'Presentamos el contenido del back buffer al front buffer
        Call objDevice.Present(ByVal 0, ByVal 0, 0, ByVal 0)
        DoEvents 'Le damos un respiro a windows para que haga sus cosas :)
    Loop Until bFin = True
    End
End Sub



Private Sub Form_Unload(Cancel As Integer)
    Set objD3D = Nothing
    Set objDevice = Nothing
    bFin = True
End Sub
    
Edited by - BlackStorm on January 30, 2001 2:47:54 AM

Share this post


Link to post
Share on other sites
I don''t use VB, so I didn''t read the code, but I know the problem. It occurs if you declare the quad completely clockwise / counterclockwise. Like this:
1 2

4 3
Because it''s a triangle strip, one side logically has to be on the wrong side, because once it''s rendered clockwise, and the second counterclockwise. This is the correct way to do it:
1 2

3 4
Works i all of my progs, HTH,,
NoNseNse

Share this post


Link to post
Share on other sites