#### Archived

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

# Converting c++ to VB

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

## Recommended Posts

I need to convert the c++ code written by druid called "Spectral Synthesis for Creating Terrain" located here on this website http://www.gamedev.net/reference/programming/features/noiseterrain It has been a long time since I have worked with c++ and I just cannot remember/grasp some of the concepts. I have converted almost all of it (it is pretty short), but I still have a few questions. Will anyone help me? Chris

##### Share on other sites
Yes, if you post your question.

Qui fut tout, et qui ne fut rien

##### Share on other sites
(help == do_it_for_me)

##### Share on other sites
Here is the code... There is a function that he uses called spline() that I found in some of his other code.. it is at the bottom. He also uses SRANDOM. I found the definition of it in some of this other code... it is this...

  #define RANDMASK RAND_MAX#define MRANDOM (float)(rand() & (RANDMASK))/(RANDMASK)#define SRANDOM ((MRANDOM) * 2) - 1// Here is the main code./* * fracSynthPass(...) * *   generate basic points *   interpolate along spline & scale *   add to existing hbuffer */void fracSynthPass( float *hbuf, float freq, float zscale, int xres, int zres ){   int i;   int x, z;   float *val;   int max;   float dfx, dfz;   float *zknots, *knots;   float xk, zk;   float *hlist;   float *buf;   // how many to generate (need 4 extra for smooth 2d spline interpolation)   max = freq + 2;   // delta x and z - pixels per spline segment   dfx = xres / (freq-1);   dfz = zres / (freq-1);   // the generated values - to be equally spread across buf   val = (float*)calloc( sizeof(float)*max*max, 1 );   // intermediately calculated spline knots (for 2d)   zknots = (float*)calloc( sizeof(float)*max, 1 );   // horizontal lines through knots   hlist = (float*)calloc( sizeof(float)*max*xres, 1 );   // local buffer - to be added to hbuf   buf = (float*)calloc( sizeof(float)*xres*zres, 1 );   // start at -dfx, -dfz - generate knots   for( z=0; z < max; z++ )   {      for( x=0;x < max;x++ )      {         val[z*max+x] = SRANDOM;      }   }   // interpolate horizontal lines through knots   for( i=0;i < max;i++ )   {      knots = &val[i*max];      xk = 0;      for( x=0;x < xres;x++ )      {         hlist[i*xres+x] = spline( xk/dfx, 4, knots );         xk += 1;         if( xk >= dfx )         {            xk -= dfx;            knots++;         }      }   }   // interpolate all vertical lines   for( x=0;x < xres;x++ )   {      zk = 0;      knots = zknots;      // build knot list      for( i=0;i < max;i++ )      {         knots[i] = hlist[i*xres+x];      }      for( z=0;z < zres;z++ )      {         buf[z*xres+x] = spline( zk/dfz, 4, knots ) * zscale;         zk += 1;         if( zk >= dfz )         {            zk -= dfz;            knots++;         }      }   }   // update hbuf   for( z=0;z < zres;z++ )      for( x=0;x < xres;x++ )         hbuf[z*xres+x] += buf[z*xres+x];   free( val );   free( buf );   free( hlist );   free( zknots );}// here is the spline function codefloat spline( float x, int nknots, float *knot ){    int span;    int nspans = nknots - 3;    float c0, c1, c2, c3;   // coefficients of the cubic    if( nspans < 1)    {        // illegal        return 0;    }    // find the appropriate 4-point span of the spline    x = (x < 0 ? 0 : (x > 1 ? 1 : x)) * nspans;    span = (int) x;    if( span >= nknots -3 )        span = nknots - 3;    x -= span;    knot += span;    // evaluate the span cubic at x using horner's rule    c3 = CR00*knot[0] + CR01*knot[1]        + CR02*knot[2] + CR03*knot[3];    c2 = CR10*knot[0] + CR11*knot[1]        + CR12*knot[2] + CR13*knot[3];    c1 = CR20*knot[0] + CR21*knot[1]        + CR22*knot[2] + CR23*knot[3];    c0 = CR30*knot[0] + CR31*knot[1]        + CR32*knot[2] + CR33*knot[3];    return ((c3*x + c2)*x + c1)*x + c0;}// here are the definitions of CR00 thru CR33#define CR00	-0.5#define CR01	1.5#define CR02	-1.5#define CR03	0.5#define CR10	1.0#define CR11	-2.5#define CR12	2.0#define CR13	-0.5#define CR20	-0.5#define CR21	0.0#define CR22	0.5#define CR23	0.0#define CR30	0.0#define CR31	1.0#define CR32	0.0#define CR33	0.0

I hope I provided enough info.

Chris

[edited by - Yann L on May 7, 2003 6:38:15 PM]

##### Share on other sites
Here is what I have so far...
I know it is not the most optimized, but I can do that later.

Thank you for any help

  Public Sub fracSynthPass(ByRef hbuf() As Double, freq As Double, zscale As Double, xres As Integer, zres As Integer)    Dim SRANDOM As Double    SRANDOM = (((Rnd And (32767)) / (32767)) * 2) - 1    Dim i As Integer    Dim x As Integer    Dim z As Integer    Dim val() As Double    Dim max As Integer    Dim dfx As Double    Dim dfz As Double    Dim zknots() As Double    Dim knots() As Double    Dim xk As Double    Dim zk As Double    Dim hlist() As Double    Dim buf() As Double    max = freq + 2'   // delta x and z - pixels per spline segment    dfx = xres / (freq - 1)    dfz = zres / (freq - 1)''   // the generated values - to be equally spread across buf    ReDim val(max * max)'   // intermediately calculated spline knots (for 2d)    ReDim zknots(max)''   // horizontal lines through knots    ReDim hlist(max * xres)'   // local buffer - to be added to hbuf    ReDim buf(xres * zres)'    For z = 0 To max        For x = 0 To max            val(z * max + x) = (((Rnd And (32767)) / (32767)) * 2) - 1        Next    Next    Dim knotscounter As Integer    knotscounter = 0    For i = 0 To max        knots(xk) = val(i * max)        xk = 0        For x = 0 To xres            hlist(i * xres + x) = spline(xk / dfx, 4, knots)            xk = xk + 1            If xk >= dfx Then                xk = xk - dfx                knotscounter = knotscounter + 1            End If        Next    Next    knotscounter = 0    For x = 0 To xres        zk = 0        ReDim knots(UBound(zknots, 1))        For i = 0 To UBound(zknots, 1)            knots(i) = zknots(i)        Next        For i = 0 To max            knots(i) = hlist(i * xres + x)        Next        For z = 0 To zres            buf(z * xres + x) = spline(zk / dfz, 4, knots) * zscale            zk = zk + 1            If zk >= dfz Then                zk = zk - dfz                knotscounter = knotscounter + 1            End If        Next    Next    For z = 0 To zres        For x = 0 To xres            hbuf(z * xres + x) = hbuf(z * xres + x) + buf(z * xres + x)        Next    NextEnd SubFunction spline(x As Double, nknots As Integer, knot() As Double) As Double    '// basis matrix for spline interpolation    Dim CR00 As Double    Dim CR01 As Double    Dim CR02 As Double    Dim CR03 As Double    Dim CR10 As Double    Dim CR11 As Double    Dim CR12 As Double    Dim CR13 As Double    Dim CR20 As Double    Dim CR21 As Double    Dim CR22 As Double    Dim CR23 As Double    Dim CR30 As Double    Dim CR31 As Double    Dim CR32 As Double    Dim CR33 As Double    CR00 = -0.5    CR01 = 1.5    CR02 = -1    CR03 = 0.5    CR10 = 1    CR11 = -2    CR12 = 2    CR13 = -0    CR20 = -0    CR21 = 0    CR22 = 0.5    CR23 = 0    CR30 = 0    CR31 = 1    CR32 = 0        Dim span As Integer    Dim nspans As Integer    nspans = nknots - 3    Dim c0 As Double    Dim c1 As Double    Dim c2 As Double    Dim c3 As Double        If nspans < 1 Then        'illegal        spline = 0        Exit Function    End If        If x < 0 Then        x = 0    Else        If x > 1 Then            x = 1 * nspans        Else            x = x * nspans        End If    End If        span = CInt(x)        If span >= nknots - 3 Then        span = nknots - 3    End If        x = x - span    knot = knot + span    '    // evaluate the span cubic at x using horner's rule    c3 = CR00 * knot(0) + CR01 * knot(1) + CR02 * knot(2) + CR03 * knot(3)    c2 = CR10 * knot(0) + CR11 * knot(1) + CR12 * knot(2) + CR13 * knot(3)    c1 = CR20 * knot(0) + CR21 * knot(1) + CR22 * knot(2) + CR23 * knot(3)    c0 = CR30 * knot(0) + CR31 * knot(1) + CR32 * knot(2) + CR33 * knot(3)        spline = ((c3 * x + c2) * x + c1) * x + c0End Function

[edited by - Yann L on May 7, 2003 6:39:05 PM]

##### Share on other sites
Mine eyes. I''ll give you the best advice I can. Not intending to be a putz or anything, but it truly is the best way to go about it.

Use C++.

##### Share on other sites
nexguy, if you''re posting more than a few lines of code, then please use the source tags. Thanks.

Other than that, I really can''t help you, sorry. I''ve never used VB in my life...

##### Share on other sites
Edward... Can you help me convert it?

##### Share on other sites
quote:
Original post by nexguy
Here is what I have so far...
I know it is not the most optimized, but I can do that later.

Thank you for any help

      Public Sub fracSynthPass(ByRef hbuf() As Double, freq As Double, zscale As Double, xres As Integer, _ zres As Integer) 'when text goes out of the immediate window(not debug mode), _ you can use the underscore char to tell the _ compiler that the statement continues on the next line    Dim SRANDOM As Double    SRANDOM = (((Rnd And (32767)) / (32767)) * 2) - 1    Dim i As Integer    Dim x As Integer    Dim z As Integer    Dim val() As Double    Dim max As Integer    Dim dfx As Double    Dim dfz As Double    Dim zknots() As Double    Dim knots() As Double    Dim xk As Double    Dim zk As Double    Dim hlist() As Double    Dim buf() As Double    max = freq + 2'   // delta x and z - pixels per spline segment    dfx = xres / (freq - 1)    dfz = zres / (freq - 1)''   // the generated values - to be equally spread across buf    ReDim val(max * max)'   // intermediately calculated spline knots (for 2d)    ReDim zknots(max)''   // horizontal lines through knots    ReDim hlist(max * xres)'   // local buffer - to be added to hbuf    ReDim buf(xres * zres)'    For z = 0 To max        For x = 0 To max            val(z * max + x) = (((Rnd And (32767)) / (32767)) * 2) - 1        Next    Next    Dim knotscounter As Integer    knotscounter = 0    For i = 0 To max        knots(xk) = val(i * max)        xk = 0        For x = 0 To xres            hlist(i * xres + x) = spline(xk / dfx, 4, knots)            xk = xk + 1            If xk >= dfx Then                xk = xk - dfx                knotscounter = knotscounter + 1            End If        Next    Next    knotscounter = 0    For x = 0 To xres        zk = 0        ReDim knots(UBound(zknots, 1))        For i = 0 To UBound(zknots, 1)            knots(i) = zknots(i)        Next        For i = 0 To max            knots(i) = hlist(i * xres + x)        Next        For z = 0 To zres            buf(z * xres + x) = spline(zk / dfz, 4, knots) * zscale            zk = zk + 1            If zk >= dfz Then                zk = zk - dfz                knotscounter = knotscounter + 1            End If        Next    Next    For z = 0 To zres        For x = 0 To xres            hbuf(z * xres + x) = hbuf(z * xres + x) + buf(z * xres + x)        Next    NextEnd SubFunction spline(x As Double, nknots As Integer, knot() As Double) As Double    '// basis matrix for spline interpolation    Dim CR00 As Double    Dim CR01 As Double    Dim CR02 As Double    Dim CR03 As Double    Dim CR10 As Double    Dim CR11 As Double    Dim CR12 As Double    Dim CR13 As Double    Dim CR20 As Double    Dim CR21 As Double    Dim CR22 As Double    Dim CR23 As Double    Dim CR30 As Double    Dim CR31 As Double    Dim CR32 As Double    Dim CR33 As Double    CR00 = -0.5    CR01 = 1.5    CR02 = -1    CR03 = 0.5    CR10 = 1    CR11 = -2    CR12 = 2    CR13 = -0    CR20 = -0    CR21 = 0    CR22 = 0.5    CR23 = 0    CR30 = 0    CR31 = 1    CR32 = 0        Dim span As Integer    Dim nspans As Integer    nspans = nknots - 3    Dim c0 As Double    Dim c1 As Double    Dim c2 As Double    Dim c3 As Double        If nspans < 1 Then        'illegal        spline = 0        Exit Function    End If        If x < 0 Then        x = 0    Else        If x > 1 Then            x = 1 * nspans        Else            x = x * nspans        End If    End If        span = CInt(x)        If span >= nknots - 3 Then        span = nknots - 3    End If        x = x - span    knot = knot + span    '    // evaluate the span cubic at x using horner's rule    c3 = CR00 * knot(0) + CR01 * knot(1) + CR02 * knot(2) + CR03 * knot(3)    c2 = CR10 * knot(0) + CR11 * knot(1) + CR12 * knot(2) + CR13 * knot(3)    c1 = CR20 * knot(0) + CR21 * knot(1) + CR22 * knot(2) + CR23 * knot(3)    c0 = CR30 * knot(0) + CR31 * knot(1) + CR32 * knot(2) + CR33 * knot(3)        spline = ((c3 * x + c2) * x + c1) * x + c0End Function

[edited by - Yann L on May 7, 2003 6:39:05 PM]

and a little bit of thought: vb uses broken english, except for function calls not made by you, or other people/companies ie:

list1.additem "vb uses english for basic function calls"
list1.refresh

end sub

------------------------------------------------------------
I really need to update this signature...

[edited by - demonrazor2003 on May 8, 2003 9:44:51 AM]

##### Share on other sites

This code doesn''t compile does it? Because ''val'' is also a VB-function converting strings to doubles.

Just my 2 cents,

Edo

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 13
• 9
• 12
• 9
• ### Forum Statistics

• Total Topics
631437
• Total Posts
3000063
×