For loops and other general issues.

This semester I’m taking a Computer Graphics course focused around OpenGL. The professor spend his lectures talking about theoretical’s and concepts. Never shows any code what-so-ever. I’ve never used C before, but I’m pretty familiar with C++/C#. I’ve been coding in VS2008pro.

For our second assignment he wants us to draw a sphere using triangle fans and quad strips. While he did kind of talk about what fans and strips are he never showed us how to do it. So after digging on the net, I found the method and played around with it for a bit.

I’ve been trying to use a for loop to create my fan for me, using some trig functions, but for whatever the reason, I cannot.

Here’s what I’ve been trying to do.

#include <windows.h>
#include <math.h>
#include <stdio.h>
#include <glut.h>
#include <GL/glu.h>
#include <GL/gl.h>


void init(void)
{
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glShadeModel (GL_FLAT);

}

void display()
{
	glBegin (GL_TRIANGLE_FAN);
		glVertex3f(0., 1.0, 0.);
		for (int i=0; i<20; i++)
		{
			float angle = 2. * (float)i*3.14/(float)20;
			glVertex3f(0.5*cos(angle), 0.0, 0.5*sin(angle));
		}
 
	glEnd();
	glFlush();	
}

void reshape(int w, int h)
{
	glViewport (0,0,(GLsizei) w, (GLsizei) h);
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity();
	glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutCreateWindow(argv[0]);
	init();
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutMainLoop();
	return 0;
}

The errors I get are all similar to this, just moving down token by token telling me I have a syntax error.

Error 1 error C2143: syntax error : missing ‘;’ before ‘type’ line 33

I’ve searched and searched for a syntax error but I can’t find one. I thought that perhaps you couldn’t have a for loop within a glBegin()/glEnd() sequence, but I see it as an example in my text book. Not to mention that a for loop will not work anywhere within my code.

Anyway, if any of you can help fill me in on what I’m doing wrong, I’d greatly appreciate it.

Thanks.

i dont c anything wrong. did u visit the msdn page for compiler error number C2143 ? if no :
http://msdn.microsoft.com/en-us/library/0afb82ta(VS.80).aspx

btw did u try to remove the spaces between the functions name and the left parenthesis, and the for loop?

Here on Linux, if I paste this into a file – getting rid of that useless and problematic #include <windows.h> and changing the glut.h include to #include <GL/glut.h> – and compile it, it compiles and runs fine. So it’s likely that the windows.h include or just the silly MS compiler is screwing things up. I’ve seen that lame windows.h include screw a number of things up before, so it’s possible that that is it.

However, the code doesn’t do quite what you’re shooting for. First, add this:

glClear( GL_COLOR_BUFFER_BIT );

to the beginning of the display() function so you can actually see what you’re doing. Second, you’re trying to display a circle (using triangle fans) on the screen in the XY plane (by default), so you need draw your circle in the XY plane (using XY coordinates, not XZ). So change your second glVertex3f call to this:

          glVertex3f(0.5*cos(angle), 0.5*sin(angle), 0.0 );

(basically just swap the Y and Z), and then you’ll get … an upside down ice cream cone? Oh yeah, you need to start your fan in the middle of the circle, so change the first glVertex3f call to:

      glVertex3f(0., 0.0, 0.);

because your circle edge points are all centered about 0,0.
Then the only thing left to do is to change your loop so that it terminates at 20/20 instead of 19/20 so you get that last pie slice of the circular fan in there:

for (int i=0; i<=20; i++)

Depending on the compiler


		for (int i=0; i<20; i++)

could cause problems too in C.


		int i;
		for (i=0; i<20; i++)

would work better.

Thanks for all of the responses.

I tried everyone’s advice and never could get it to work.

I just ended up taking the code I showed above and pasted it into a new project and it worked fine.

Still trying to figure out how to complete this assignment though.

I’m trying to figure out how glutWireSphere pulls it off, but I can’t find the algorithm or code anywhere.

Again, thanks for all of the help.

You can not find the code for glutWireSphere ?
Well … What can I say …

http://lmgtfy.com/?q=glut+source

Heh, thanks.

I’ve been looking through that source code for a little while now. Still can’t find it in there.

The only thing I see is…

glutWireSphere(GLdouble radius, GLint slices, GLint stacks)
{
  QUAD_OBJ_INIT();
  gluQuadricDrawStyle(quadObj, GLU_LINE);
  gluQuadricNormals(quadObj, GLU_SMOOTH);
  /* If we ever changed/used the texture or orientation state
     of quadObj, we'd need to change it to the defaults here
     with gluQuadricTexture and/or gluQuadricOrientation. */
  gluSphere(quadObj, radius, slices, stacks);
}

located in glut_shapes.c.

then go for gluSphere source :slight_smile:
http://cgit.freedesktop.org/mesa/mesa/tree/src/glu/mesa/quadric.c