Skip to main content
GameDev.net gamedev.net
🔒 Locked

Drawing a curve in OpenGLES - how?

Started by norbak May 7, 2009 at 1:22 AM 4 replies 18.5k views
Original Post
norbak
norbak
Hi y'all :) Me and my mate would like to implement a curve into our OpenGL|ES code. glMap1 is not available so we guess it has to be hardcoded? But then our newbieness takes over: how? We're developing a game for iPhone and need to a draw curve and afterwards check intersections (we'll talk about intersection another time :)). Do you guys have any code example when it comes to drawing a curve in OpenGL|ES? Any help is very appreciated :) E.g. Bezier curve etc. -kenneth
V-man
V-man
If you are rendering a line curve, then you render many GL_LINES (or GL_LINE_STRIP). If it is a mesh curve, then GL_TRIANGLES.
I'm sure you can find some NURBS C++ code and use that to generate lines or triangles for rendering.
baw
baw
Bezier curves (and especially surfaces) look daunting, but they are actually very easy to implement yourself. You'll be surprised.

In fact you can grab the required formulas right out of the Wikipedia article on Bezier curves. What you'll probably use most are quadratic (3 control points) and cubic (4 control points) Bezier curves.

The formulas are (images from the Wikipedia article):

Cubic:


Quadratic:


In case of the cubic formula P0, P1 and P2 are your control points. t has to be a value between 0 and 1 and represents the "position" on the curve. By incrementing t step by step you'll get several points you can use to actually draw the curve.

So using the above formula you could do something like

glBegin(GL_LINE_STRIP);for(float t=0; t <= 1; t += 0.1) {     float x = (1-t)*(1-t)*p0.x + 2(1-t)*t*p1.x + t*t*p2.x;     float y = (1-t)*(1-t)*p0.y + 2(1-t)*t*p1.y + t*t*p2.y;     float z = (1-t)*(1-t)*p0.z + 2(1-t)*t*p1.z + t*t*p2.z;     glVertex3f(x, y, z);}glEnd();


The smaller the steps you use for t the smoother the curve will become. That's it. Really.
robhasacamera
robhasacamera
I just wanted to let you know I've seen this formula time and time again and have never gotten a clearer explanation then the one you just gave.
CrazyCdn
CrazyCdn
To speed up the code a bit never declare variables inside the for loop. Move x, y and z outside. Just a small help but if your doing this often it will slow you down. I know, I know, don't optimize early either but this is something most people should do in general anyways.

Great explanation too.
"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin
V-man
V-man
This is a 2 year old thread and BAW is long gone and dead
===
Last Active:user_off.png Feb 19 2011 10:58 PM

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.