OpenGL

Oct 14, 2011 at 11:17pm
I'm attempting to draw saturn and I have the sphere but I'm having a little trouble with the ring. I keep getting uninitialized local variables as well. Here is the code.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <gl/glut.h>


void display(void)
{
double phi, phir, phir20;
double theta, thetar, c, c80;
double x, y, z;
const double M_PI = 3.14159265358979323846;
c=M_PI/180.0;
c80=c*80.0;

//Clear openGL Window
glClear(GL_COLOR_BUFFER_BIT);

// glColro3f(1.0, 1.0, 1.0);
glColor3f(1.0f, 0.25, 0.25f);

glPointSize(2.0f);
glLineWidth(2.0f);

//select a polygon rasterization mode
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
//glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
//glLoadIdentity();
// viewing transformation
/* 1.position of eye, 2.at, where pointing at, 3.up vector of the camera */
gluLookAt (1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

// sphere
for (phi=-80.0;phi<=80.0; phi+=20.0)
{
phir=c*phi;
phir20 = c*(phi+20);

glBegin(GL_TRIANGLE_STRIP);
for (theta=-180.0; theta<=180.0; theta+=20.0)
{
thetar=c*theta;
x=sin(thetar)*cos(phir);
y=cos(thetar)*cos(phir);
z=sin(phir);
glVertex3d(x,y,z);
x=sin(thetar)*cos(phir20);
y=cos(thetar)*cos(phir20);
z=sin(phir20);
glVertex3d(x,y,z);
}
glEnd();
}

//South pole and north pole
glBegin(GL_TRIANGLE_FAN);
glVertex3d(0.0, 0.0, 1.0);


z=sin(c80);
for(theta=-180.0; theta<=180.0; theta+=20.0)
{
thetar=c*theta;
x=sin(thetar)*cos(c80);
y=cos(thetar)*cos(c80);
glVertex3d(x,y,z);
}
glEnd();

glBegin(GL_TRIANGLE_FAN);
glVertex3d(0.0, 0.0, -1.0);
z=-sin(c80);
for (theta=-180.0; theta<=180.0; theta+=20.0)
{
thetar=c*theta;
x=sin(thetar)*cos(c80);
y=cos(thetar)*cos(c80);
glVertex3d(x,y,z);
}
glEnd();



//Output everything
glFlush();
}
//Draws Ring
void Ring(void)
{
long i, di, j, NumWraps, NumPerWrap;
double wrapFrac, wrapFracTex, phi, thetaFrac, thetaFracTex, theta;
double MinorRadius, MajorRadius, TextureWrapVert, TextureWrapHoriz;
double x, y, z, r;
glBegin(GL_QUAD_STRIP);
for(di=0; di<NumWraps; di++)
{
for(j=0; j<NumPerWrap; j++)
{
for(i=di+1; i>=di; i--)
{
wrapFrac = (j%NumPerWrap)/NumPerWrap;
wrapFracTex = j/NumPerWrap;
phi = wrapFrac;
thetaFrac = ((i % NumWraps)+wrapFracTex)/NumWraps;
thetaFracTex = (i+wrapFracTex)/NumWraps;
theta = thetaFrac;
r = MajorRadius + MinorRadius*cos(phi);
x = sin(theta)*r;
z = cos(theta)*r;
y = MinorRadius*sin(phi);
glTexCoord2f(thetaFracTex*TextureWrapVert,
wrapFracTex*TextureWrapHoriz);
glVertex3f(x,y,z);
printf("%f, %f, %f\n", x, y, z);
}
}
glEnd();
glFlush();
}
}

void init(void){
// glClearColor(0.5, 0.5, 0.5, 0.0);
glClearColor(0.95f, 0.95f, 0.95f, 1.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2.0, 2.0, -2.0, 2.0, -2.0, 2.0);
// gluOrtho2D(0.0, 10.0, 0.0, 10.0);
}

int main(int argc, char** argv)
{
// Initialize OpenGL
glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);

// Create a window
glutCreateWindow("hello");

init();

// Tell OpenGL which is the drawing function
glutDisplayFunc(display);
glutDisplayFunc(Ring);

// Start the event loop
glutMainLoop();
return 0;
}
Oct 15, 2011 at 12:19am
1
2
glutDisplayFunc(display);
glutDisplayFunc(Ring);

If you want to draw Saturn and then draw the Ring, you should just have glutDisplayFunc(display); and call Ring() at the end of display(). The way you have it here is it sets Ring to the display function (overwriting display() as the display function), and when it calls Ring(), there is no view setup so it won't display even if you fix the uninitialized variable errors (which are happening because you haven't given NumWraps, NumPerWrap, MinorRadius, MajorRadius, TextureWrapVert, or TextureWrapHoriz any values).
Oct 18, 2011 at 1:43am
Ok got it, wow I did not realize anybody would get back to me this fast. I use this site often but never posted before. Thank you again.
Topic archived. No new replies allowed.