Public Group

# Sine wave

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

## Recommended Posts

Does anyone know how I could plot a sine wave in OpenGL, I've been trying to mkae it work by glBegin(GL_LINE_STRIP); for (x = 0; x <= 100; x += 10) glVertex2f(x, sin(x)); end glEnd() but I end up with a slightly slanted line

##### Share on other sites
The sin function takes its argument in radians, so I'm not sure that a step of 10 radians is what you want there. Why don't you try a smaller step and see if that works?

i.e.
 for (x = 0; x <= 100; x += 0.1)

(I'm assuming x is a float or double)

Also, you might need to stick a multiplier in front of the sin(x), as the function will only provide you values between -1 and 1. Then again, depending on how you set up your camera, that might be what you want.

Hopefully that will be of some help!

##### Share on other sites
Ok I got it to work already, now I'm trying to wrap that sine wave around a sphere?
How can I do that?

Here's what I'm trying to do
http://s24.yousendit.com/d.aspx?id=1PP2FFNBU0A0T3B2SANZDFW1JP

##### Share on other sites
You have to go from 2D vertex to 3D vertex, and adapt two of the dimensions.
Something like this wraps a sine curve to a circle:

float phase;float periods = 5.0f; // how many periods the sine curve shows around the circlefloat twoPi = 6.2831853f;float delta = twoPi/32*periods; // defines in how many steps the sine curve is renderedfloat sphereRadius = 5.0f; // is the radius of the circle the sine curve is wrapped aroundfloat magnitude = 3.0f; // is the magnitude of the sine curveglBegin(GL_LINE_STRIP);for(phase=0; phase<twoPi; phase += delta) {   glVertex3f(      sphereRadius*cos(phase),      sphereRadius*sin(phase),      magnitude*sin(phase*periods));}glEnd();

EDIT: Other variations, e.g. sloping the sine to the center of the circle, are possible by choosing other combinations of the co-ordinates.

1. 1
Rutin
26
2. 2
JoeJ
20
3. 3
4. 4
5. 5

• 10
• 10
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
631751
• Total Posts
3002086
×