Welcome! Log In Create A New Profile

Advanced

question8: last year's exam

Posted by B Kad 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
question8: last year's exam
October 05, 2006 11:57AM
Hi everyone,
Please can someone help with question 8 of last year's exam?
I seem unable to position the light point correctly!! It is rotated with the sphere!!
I am looking forward to your answer...
Re: question8: last year's exam
October 13, 2006 09:17PM
#include <GL/glut.h>
#include <math.h>

void earth()
{
float x, y, z, thet, phi;
float c = 3.14159/180.0;

for (phi = -90.0; phi <= 90.0; phi += 20.0)
{
glBegin(GL_QUAD_STRIP);
for (thet = -180.0; thet <= 180.0; thet += 20.0)
{
x = sin(c*thet)*cos(c*phi);
y = cos(c*thet)*cos(c*phi);
z = sin(c*phi);
glNormal3f(x, y, z); // just need 1 normal for flat shading
glVertex3f(x, y, z);

x = sin(c*thet) * cos(c*(phi+20.0));
y = cos(c*thet) * cos(c*(phi+20.0));
z = sin(c*(phi+20.0));
glVertex3f(x, y, z);
}
glEnd();
}
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();
glRotatef(105, 1.0, 0.0, 0.0);
earth();

glFlush();
}

void reshape(int w, int h)
{
glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

if (w <= h)
glOrtho(-2.0, 2.0, -2.0*(GLfloat)h/w, 2.0*(GLfloat)h/w, -10.0, 10.0);
else
glOrtho(-2.0*(GLfloat)w/h, 2.0*(GLfloat)w/h, -2.0, 2.0, -10.0, 10.0);

glMatrixMode(GL_MODELVIEW);
}

void init()
{
glEnable(GL_DEPTH_TEST);

// enable lighting
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

// set the shading model to flat
glShadeModel(GL_FLAT);

// position the light before any transformations take place
GLfloat pos[] = {0.0, 0.0, 2.0, 0.0};
glLightfv(GL_LIGHT0, GL_POSITION, pos);

glClearColor(1.0, 1.0, 1.0, 1.0);
// glColor3f(0.0, 0.0, 0.0); // optional

// glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // optional
glPolygonMode(GL_FRONT, GL_FILL);
}

int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);

glutCreateWindow("sphere"winking smiley;

glutReshapeFunc(reshape);
glutDisplayFunc(display);

init();

glutMainLoop();

return 0;
}
Sorry, only registered users may post in this forum.

Click here to login