Sign in to follow this  

Sine wave

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 this post

Link to post
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?

 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 this post

Link to post
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

Share this post

Link to post
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 circle
float twoPi = 6.2831853f;
float delta = twoPi/32*periods; // defines in how many steps the sine curve is rendered
float sphereRadius = 5.0f; // is the radius of the circle the sine curve is wrapped around
float magnitude = 3.0f; // is the magnitude of the sine curve
for(phase=0; phase<twoPi; phase += delta) {

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

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this