# Thread: sin() function problems.

1. ## sin() function problems.

First of all, hello everyone. Been searching for a place of assistance for some time now.

My dilemma... I am trying to use the sin() function to modify a simple mesh (a giant quad made of a single degenerative triangle strip. Uses OpenGL, and is an indexed vertex array). I am wishing to loop through and make the mesh look wavy, similar to a sine wave. Unfortunately for me, things are going awry. If I use sin() to modify a single vertex, I can see some change though it sort of jumps and flickers. However, if I try to loop through the vertices, the entire mesh disappears. My default mesh is 40x40, but I made it easily modifiable and changed it to a 4x4, still experiencing the same issue.

Running the debugger, I find that after 5 or 6 iterations, the value that the function returns becomes -1.#IND000

I've tried several different inputs into the function, and it always starts returning the above value after 5 or 6 iterations. I've never really worked with the sin() function before, and am lost as to what that value means.

I know the following will not give me axactly what I need... in fact, it SHOULD modify all the values in the mesh the exact same way, giving me the appearance of a bouncing mesh. At current, the first 5 times returns 1.065..... etc, which is a decent number. I'm trying to keep with smaller values so that the verts stay in view. Current vert count is 100, which uNumPoints is equal to, in case it matters. The mesh does draw if I do not use the sin() function. I believe the -1.#IND000 is messing things up.

Code:
```void ShaderApplyBehavior (Int32 iShaderIdx, Float32 pPoints[NUMVERTS][3], Uint32 uNumPoints)
{
int i;
float deltaZ;
static float ftime = 0.0f;

ftime += 0.01f;
if(ftime > 1.0f)
ftime = -1.0f;

for(i = 0; i < uNumPoints; i++)
{
deltaZ = (float)sin(ftime) * 0.000000001f;
pPoints[i][2] = deltaZ;
}
}```
Also, just in case it lies here, which I doubt, but never know... my drawing is done here.

Code:
```void Draw()
{
Int32 i = 0;
Int32 j = 0;

glColor3f(1.0f, 1.0f, 1.0f);
ShaderBegin(0); // <- This function currently sets culling based on the parsed shader script. Culling is currently off.
glPushMatrix();
ShaderApplyBehavior(0, &vdata, totalVerts); // <- The above function.
glTranslatef(0.0f, 0.0f, -70.0f);
glRotatef(45.0f, -1.0f, 0.0f, 0.0f);
glRotatef(25.0f, 0.0f, 1.0f, 0.0f);
glVertexPointer(3, GL_FLOAT, 0, vdata);
glDrawElements(GL_TRIANGLE_STRIP, vertsStored, GL_UNSIGNED_INT, floorMesh);
glPopMatrix();
}```
Thanks in advance for any advice or suggestions you may have. I am utterly stuck until I get at least some functionality from the sin() function.

2. First note that sin() funtion receives an angle in radians. Making this
Code:
```if(ftime > 1.0f)
ftime = -1.0f;```
would make a ladder effect, in case you aren't aware of it. Use instead
Code:
```const double Pi = 2*acos(0.0);
if(ftime > 2*Pi)
ftime = 0;```
This will make things more wavy without bumps.
-1.#IND000 is an error value when division by zero ocurrs.
Plus this expression is constant
Code:
` deltaZ = (float)sin(ftime) * 0.000000001f;`
therefore you should remove it from the cicle to make it more efficiente and sin() wouldn't be called in each iteration. Only at the beginnig.

3. Thank you!

The reason I had it in the loop is because I want to next make the wave dependent on the X value. I am also seeing the value I am using is causing the value to continuously rise.

Is there a way to get an even mix of positive and negative values, or should I make some extra variables to multiply into the actual place where I alter my position?

4. sin acepts an angle in radians. Angles are ciclic: 0 and 2*Pi and are both the same. When ftime reaches 2*Pi, a complete loop has been reached.. you could continue to rise the ftime var, reseting it would be more easy to understand, debug...

Plus this assingment:
deltaZ = (float)sin(ftime) * 0.000000001f;
is independent from you data structures. It only depends on you ftime var which is constant during the for cicle
for(i = 0; i < uNumPoints; i++);

You may want to do something like:
Code:
```const double Pi = 2*acos(0.0);

void ShaderApplyBehavior (Int32 iShaderIdx, Float32 pPoints[NUMVERTS][3], Uint32 uNumPoints)
{
int i;
float deltaY;
static float ftime = 0.0;

if(ftime > 2*Pi)
ftime = 0;

deltaY = (float)sin(ftime) * 1.0;

for(i = 0; i < uNumPoints; i++){
pPoints[i][2] = deltaY;
}
}```

5. Thank you for the assist. I was able to talk to the lab instructors in class today, and getting nowhere with them I went to see if any peers had the situation solved. Essentially, it involved using the floor function on ftime to keep it as a floating point value. It's up and working quite well now. Horizontal, vertical, and diagonal positional waves. Per vertex color scaling to give the appearance of lighting, though I do not have one, and currently snapping a texture to the mesh. Things are good, and once again, thank you.

Popular pages Recent additions