Textured Tri Strip Prob

Started by
6 comments, last by Steve5050 15 years, 8 months ago
Hi, I have a triangle strip grid that I'm trying to put a texture on, I'm close but not close enough. Can you see what I'm doing wrong?

 Private Const ZMIN As Single = -10
    Private Const ZMAX As Single = 10
    Private Const XMIN As Single = -10
    Private Const XMAX As Single = 10
    Private Const NUM_TRIANGLE_STRIPS As Integer = (ZMAX - ZMIN)
    Private Const NUM_TRIANGLES_PER_STRIP As Integer = 2 * (XMAX - XMIN)
    Private Const NUM_POINTS_PER_STRIP As Integer = NUM_TRIANGLES_PER_STRIP + 2
    Private Const NUM_POINTS As Integer = NUM_TRIANGLE_STRIPS * NUM_POINTS_PER_STRIP
    Private Vb As VertexBuffer = Nothing
    Private Tex As Texture
    Private Mat1 As Matrix
    Private Mat2 As Matrix
    Private S_Color_Min As Integer = Color.FromArgb(255, 10, 20).ToArgb() 'Color.FromArgb(10, 10, 20).ToArgb()
    Private S_Color_Mid As Integer = Color.FromArgb(10, 255, 20).ToArgb() 'Color.FromArgb(15, 15, 40).ToArgb()
    Private S_Color_Max As Integer = Color.FromArgb(10, 10, 255).ToArgb() 'Color.FromArgb(20, 20, 60).ToArgb()
    Private x, z As Integer
    Private Sub MakeTriangle(ByVal vertices() As CustomVertex.PositionNormalTextured, ByRef i As Integer, ByVal x0 As Single, ByVal y0 As Single, ByVal z0 As Single, ByVal x1 As Single, ByVal y1 As Single, ByVal z1 As Single, ByVal x2 As Single, ByVal y2 As Single, ByVal z2 As Single)

        vertices(i).X = x0
        vertices(i).Y = y0
        vertices(i).Z = z0
       
        i += 1

        vertices(i).X = x1
        vertices(i).Y = y1
        vertices(i).Z = z1
       
        i += 1

        vertices(i).X = x2
        vertices(i).Y = y2
        vertices(i).Z = z2
       
        i += 1
    End Sub


    Private Sub MakePoint(ByVal vertices() As CustomVertex.PositionNormalTextured, ByRef i As Integer, ByVal x0 As Single, ByVal y0 As Single, ByVal z0 As Single, ByVal tu As Single, ByVal tv As Single)

        vertices(i).X = x0
        vertices(i).Y = y0
        vertices(i).Z = z0
        vertices(i).Tu = tu
        vertices(i).Tv = tv

        i += 1
    End Sub
    Public Sub New(ByVal Device As Device)
        Tex = TextureLoader.FromFile(Device, MediaUtilities.FindFile("W.jpg"))
        Me.Device = Device
        Vb = New VertexBuffer(GetType(CustomVertex.PositionNormalTextured), NUM_POINTS, Device, 0, CustomVertex.PositionNormalTextured.Format, Pool.Default)

        
        Dim vertices As CustomVertex.PositionNormalTextured() = CType(Vb.Lock(0, 0), CustomVertex.PositionNormalTextured())
        Dim i As Integer = 0
        For z As Integer = ZMIN To ZMAX - 1
            For x As Integer = XMIN To XMAX
                MakePoint(vertices, i, x, F(x, z), z, x / NUM_POINTS, z / NUM_POINTS)
                MakePoint(vertices, i, x, F(x, z + 1), z + 1, (x + 1) / NUM_POINTS, (z + 1) / NUM_POINTS)
            Next x
        Next z

        Vb.Unlock()
        With Mat1
            .M11 = 1.0 : .M12 = 0.0 : .M13 = 0.0 : .M14 = 0.0
            .M21 = 0.0 : .M22 = 1.0 : .M23 = 0.0 : .M24 = 0.0
            .M31 = 0.0 : .M32 = 0.0 : .M33 = 1.0 : .M34 = 0.0
            .M41 = 0.0 : .M42 = 0.0 : .M43 = 0.0 : .M44 = 1.0
        End With
        Mat2 = Mat1
    End Sub
       Public Sub Render()

        'Me.Device.RenderState.ZBufferEnable = True
        Device.RenderState.CullMode = Cull.CounterClockwise
        Me.Device.RenderState.Lighting = False
        Device.Transform.World = Mat1

        Device.SetTexture(0, Tex)
        Device.SetStreamSource(0, Vb, 0)
        Device.VertexFormat = CustomVertex.PositionNormalTextured.Format
        Dim index As Integer = 0
        For i As Integer = 1 To NUM_TRIANGLE_STRIPS
            Device.DrawPrimitives(PrimitiveType.TriangleStrip, index, NUM_TRIANGLES_PER_STRIP)
            index += NUM_POINTS_PER_STRIP
        Next i

    End Sub



Thanks Steve [Edited by - Steve5050 on August 2, 2008 9:19:32 AM]
Advertisement
What do you mean you're "close"? Can we see a screenshot of what that code generates, and a description of how it's wrong?
I thought it might be something that was obvious.
I'll have to find somewhere to upload screenshot
from. I will also have to download a tool to get
a screenshot because the tool that I always used
is on broken hard drive that I replaced.
MakePoint(vertices, i, x, F(x, z), z, x / NUM_POINTS, z / NUM_POINTS)


I don't have a clue how you want the texture displayed. But, if you want it spread uniformly over the grid, I would think you'd want:
MakePoint(vertices, i, x, F(x, z), z, x / XMAX, z / ZMAX)

or something similar.

Also, I think you want:
For x As Integer = XMIN To XMAX-1


Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Hi,
I've tried your solutions plus I've been farting
around for hours trying to get this to work.
The best I could get was using:

Private Sub MakePoint(ByVal vertices() As CustomVertex.PositionNormalTextured, ByRef i As Integer, ByVal x0 As Single, ByVal y0 As Single, ByVal z0 As Single)
vertices(i).X = x0
vertices(i).Y = y0
vertices(i).Z = z0
vertices(i).Nx = 0
vertices(i).Ny = 0
vertices(i).Nz = 1
vertices(i).Tu = vertices(i).X / XMAX
vertices(i).Tv = vertices(i).Z / ZMAX
i += 1
End Sub

The code works the same in the sub, I took the tu,tv out of the args.

Changing:
For x As Integer = XMIN To XMAX-1
That just added a jagged plane in the Y direction.

I did make an image to see what was going on.
The image I made looks like:
|
|
9 | 9
________|_________
|
|
9 | 9
|

Using the code above it looks like:

e | ee | e
_______|____|_______
e | ee | e
_____e_|_ee_|_e_____
| |
e | ee | e

I used "e" to represent the backward"9".
I could see the lines in the image,in alot of the other ways I
tried I couldn't see them.
I'll leave it at that for know, In the mean time I'm going to work on
getting a screen shot posted.
Ok heres the screen shot.
In the lower left corner is the image I used.
Screen Shot

Two things:

1. It appears your texture is upsidedown. To correct that, subtract the calculated tex coords from 1.0f.

2. It appears your texture is rendered at half the size you want. To correct that, divide by XMAX-XMIN rather than just XMAX.

Something like:

Tu = (x-XMIN)/(XMAX-XMIN);
Tv = 1.0f - (z-ZMIN)/(ZMAX-ZMIN);

See if that's closer to what you want.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

That wasn't close "It was dead on"
You've done it again.
I can't thank you enough!

Thanks
Steve

This topic is closed to new replies.

Advertisement