Where can i find actual coding implemnetation for isosurface or object generation base on formula such as sphere with the formula of
R^2 = X^2 + Y^2 + Z^2
because I noticed that isosurface is an object with a surface of equal values. And I really got problem in generating object with mathematical equations (don’t know how to and where to start). Correct me if I’m wrong and Thanks You in Advance =)
The problem with implicit surfaces or isosurfaces is that you can’t draw them directly. You have to find some parametric representation for them.
For example the parametric representation for a sphere is:
x = r * cos u * cos v
y = r * sin u * cos v
z = r * sin v
with u running from 0 to 2 pi and v running from -pi/2 to pi/2
Once you have found that representation, you can just use two nested loops to iterate over u and v in some arbitrary stepsize to get all surface vertices.
The problem is that there is no general rule for finding these representations, let alone an algorithm that can run automatically.
Another possibility is the marching cubes algorithm. I can’t really give you a coding example because while it’s really a simple algorithm, its implementation is rather complex:
Basically you split the region into a regular sized 3d grid of cubes. Then, for each cube, you evaluate the function at the corners of the cube and look if the result is above or below the target value. From this you can determine how the surface runs through this cube, there are only a few possibilities, search the web to find a list of them. The exact location of the vertices can be determined by extrapolation…
I’m sure google will find many websites with the details of the algorithm. But for simple surfaces you should really try to find a parametric equation, because this will always be faster and more accurate.
Originally posted by Overmind:
For example the parametric representation for a sphere is:
x = r * cos u * cos v
y = r * sin u * cos v
z = r * sin v
with u running from 0 to 2 pi and v running from -pi/2 to pi/2
hi there overmind,
Do you mind giving more example the best is that give me some coding example cause implementing mathematical function is not like writing and calculating using our brain, and I hope you understand =). Thanks You.
// increase/decrease these for more quality/performance
#define STEPS_U 20
#define STEPS_V 20
double v0, v1 = - M_PI / 2;
for(int j = 1; j <= STEPS_V; j++) {
v0 = v1;
// j is running from 0 to STEPS_V
// v should be running from -pi/2 to pi/2
v1 = (double)(j+1) * M_PI / STEPS_V - M_PI / 2;
glBegin(GL_TRIANGLE_STRIP);
for(int i = 0; i <= STEPS_V; i++) {
// i is running from 0 to STEPS_U
// u should be running from 0 to 2pi
double u = (double)i * 2 * M_PI / STEPS_U;
glVertex3f(r * cos(u) * cos(v0),
r * sin(u) * cos(v0),
r * sin(v0));
glVertex3f(r * cos(u) * cos(v1),
r * sin(u) * cos(v1),
r * sin(v1));
}
glEnd();
}
If you look at this code, you see that the outer loop iterates over v and the inner loop iterates over u. It draws a triangle strip between the (j-1)th row and the jth row,
Basically you can use this loop with any formula you like, not just with a sphere.
In a real program it’s not good to write it exactly like this, it would be better to run these loops only once at load time and store the result somewhere, for example in a vertex array or display list.
I’ve tried out the source code you given me… it is sad to said that the code is not working cause nothing seems to appear on the screen, and I hope you can fix it asap. Thanks you very much
sure and here it is
</font><blockquote><font size=“1” face=“Verdana, Arial”>code:</font><hr /><pre style=“font-size:x-small; font-family: monospace;”>#include <iostream>
using namespace std;
#include <GL\glut.h> #include <GL\gl.h>
#define STEPS_U 20 #define STEPS_V 20
#define M_PI 3.14159265358979323846
GLint width = 500, height = 500;
void DisplayFunc(void) {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
double v0, v1 = - M_PI / 2;
float r = 1.0;
for(int j = 1; j <= STEPS_V; j++) {
v0 = v1;
// j is running from 0 to STEPS_V
// v should be running from -pi/2 to pi/2
v1 = (double)(j+1) * M_PI / STEPS_V - M_PI / 2;
glBegin(GL_TRIANGLE_STRIP);
for(int i = 0; i <= STEPS_V; i++) {
// i is running from 0 to STEPS_U
// u should be running from 0 to 2pi
double u = (double)i * 2 * M_PI / STEPS_U;
glVertex3f((GLfloat)r * cos(u) * cos(v0),
(GLfloat)r * sin(u) * cos(v0),
(GLfloat)r * sin(v0));
glVertex3f((GLfloat)r * cos(u) * cos(v1),
(GLfloat)r * sin(u) * cos(v1),
(GLfloat)r * sin(v1));
}
glVertex3f(0.0, 250.0, 0.0);
glVertex3f(250.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glEnd();
glVertex3f(0.0, 250.0, 0.0);
glVertex3f(250.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glutSwapBuffers();
}
}
void ReshapeFunc(GLsizei w, GLsizei h) {
if(width != w