PDA

View Full Version : Circle Code



BeeNew
10-13-2001, 11:51 AM
Can someone post code to draw a circle, a cone, and a cylinder using triangles?
What is wrong in this code?
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#include <windows.h>
#include <winbase.h>
#define ESC
#define Space
int Height=500, Width=500;
int depth=0;
static float vdata[4][3] = {{1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {-1.0, 0.0, 0.0}, {0.0, -1.0, 0.0}};
void normalize(float v[3]){
GLfloat d = sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]);
if(d==0.0){
// error("zero length vector");
return;
}
v[0] /=d; v[1] /=d; v[2] /=d;
}

void drawtriangle(float*v1,float*v2, float*v3){
glBegin(GL_POLYGON);
glNormal3fv(v1); glVertex3fv(v1);
glNormal3fv(v2); glVertex3fv(v2);
glNormal3fv(v3); glVertex3fv(v3);
glFinish();
}

void subdivideCircle(int radius,float *v1,float *v2,int depth){
float v11[3],v22[3],v00[3]={0,0,0},v12[3];
int i;
if (depth==0){
glColor3f(v1[0]*v1[0],v1[1]*v1[1],v1[2]*v1[2]);
for(i=0;i<3;i++){
v11[i]=v1[i];
v22[i]=v2[i];
drawtriangle(v11,v22,v00);
v00[2]=1;
drawtriangle(v11,v22,v00);
v12[i]=v11[i]=v1[i];
v12[i]=v22[i]=v2[i];
}
v12[2]=v12[2]=1;
v12[0]=v1[0]+v2[0];
v12[1]=v1[1]+v2[1];
v12[2]=v1[2]+v2[2];
normalize (v12);
subdivideCircle(radius, v1, v2, depth-1);
subdivideCircle(radius, v1, v2, depth-1);
return;
}
}

void drawCircle(int CircleRadius){
subdivideCircle(CircleRadius,vdata[0],vdata[1],depth);
subdivideCircle(CircleRadius,vdata[1],vdata[2],depth);
subdivideCircle(CircleRadius,vdata[2],vdata[3],depth);
subdivideCircle(CircleRadius,vdata[3],vdata[0],depth);
}

void display(void){ //used to display a circle
int CircleRadius=0;
int cnt=0;
if(CircleRadius>Width/2&#0124; &#0124;CircleRadius==1);
{
cnt=-cnt;
depth++;
depth=depth%5;
}
CircleRadius+=cnt;
glClear(GL_COLOR_BUFFER_BIT);
drawCircle(CircleRadius);
glutSwapBuffers();
}

static void reshape(int w, int h)
{
glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT);
Width = w;
Height = h;
glViewport (0, 0, Width, Height);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho(-Width/2, Width/2, -Height/2, Height/2, -1.0, 1.0);
}

static void Key(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
case 32:
glutIdleFunc(NULL);
display();
break;
default:
glutIdleFunc(display);
}
}

void myInit()
{
glClearColor (0.0, 0.0, 0.0, 1.0);
glutIdleFunc(display);
}

int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
glutInitWindowSize(Width, Height);
glutCreateWindow("Circle.");
myInit();
glutKeyboardFunc(Key);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}



[This message has been edited by BeeNew (edited 10-14-2001).]

martinzwigl
10-14-2001, 01:39 AM
circle (pseudo-code)
--------------------

glvertex(circle_middpoint)

glbegin(triangle-fan)
for i= 0 to 2*pi
begin
x= cos(i) * radius
z= sin(i) * radius
glvertex(x, height, z)
end
glend;

produces a circle in the x,z-plane

cheers,
martin

BeeNew
10-14-2001, 02:31 AM
Thanks for comments. I am trying to draw circle from triangles. Can you look in to subdivideCircle function

[This message has been edited by BeeNew (edited 10-16-2001).]