Welcome! Log In Create A New Profile

Advanced

why don't my spheres render properly?

Posted by chaospixel 
Announcements Last Post
Announcement myUnisa availability 21 to 24 March 2019 03/25/2019 07:26PM
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
why don't my spheres render properly?
August 03, 2006 05:50PM
the "tops" of the spheres seem to be backwards and other strange lighting anomolies become apparent if you rotate around the scene a bit.

here's the code:

#include <GL/glut.h>

const unsigned int FRAME_RATE = 1000 / 18; // 18 fps

int spin = 0.0;
GLfloat aspect = 1.0;

int mousex = 0;
int mousey = 0;

void draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

// draw scene

glutSolidSphere(1.0, 10, 10);

glRotatef(spin, 1.0, 0.0, 0.0);
glTranslatef(0.0, 0.0, 3.0);

glutSolidSphere(1.0, 10, 10);


glFlush();
glutSwapBuffers();
}

void tick(int value)
{
spin = (spin + 3) % 360;

glutPostRedisplay();
glutTimerFunc(FRAME_RATE, tick, 0);
}

void resize(int w, int h)
{
if (w >= h) aspect = (GLfloat)w / (GLfloat)h;
else aspect = (GLfloat)h / (GLfloat)w;

glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(60.0, aspect, 0.0, 50.0);
glTranslatef(0.0, 0.0, -8.0);

static const GLfloat lightpos[] = {4.0, 0.0, 0.0, 0.0};
glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
}

void motion(int x, int y)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(60.0, aspect, 0.0, 50.0);
glTranslatef(0.0, 0.0, -8.0);
glRotatef( (x - mousex) % 360 , 0.0, 1.0, 0.0);
glRotatef( (y - mousey) % 360, 1.0, 0.0, 0.0);

glutPostRedisplay();
}

void mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
mousex = x;
mousey = y;
}
}

void init()
{
glShadeModel(GL_SMOOTH);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}


int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(600, 400);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH | GLUT_STENCIL);
glutCreateWindow("My Shadow Sample"winking smiley;

init();

glutDisplayFunc(draw);
glutReshapeFunc(resize);
glutTimerFunc(FRAME_RATE, tick, 0);
glutMouseFunc(mouse);
glutMotionFunc(motion);

glutMainLoop();

return 0;
}
Anonymous User
Re: why don't my spheres render properly?
August 04, 2006 05:37PM
Where did you get this code?
Re: why don't my spheres render properly?
August 10, 2006 07:26PM
i wrote it. why?
Anonymous User
Re: why don't my spheres render properly?
August 11, 2006 12:17AM
You are missing glEnable( GL_DEPTH_TEST ); in the init()

and your resize function is wrong
use
void resize(int w, int h)
{
float aspectRatio;
h = (h == 0) ? 1 : h;
w = (w == 0) ? 1 : w;
glViewport( 0, 0, w, h ); // View port uses whole window
aspectRatio = (float)w/(float)h;

// Set up the projection view matrix (not very well!)
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 60.0, aspectRatio, 1.0, 30.0 );

// Select the Modelview matrix
glMatrixMode( GL_MODELVIEW );


}

In draw()

remove
glMatrixMode(GL_MODELVIEW);

and after glLoadIdentity();
add

glTranslatef ( 0.0, 0.0, -8.0 );

glRotatef( 15.0, 1.0, 0.0, 0.0 );



Re: why don't my spheres render properly?
August 12, 2006 03:43PM
thanks!

just figured out the depth_test bit and the problem with gluPerspective. was about to post my answer smiling smiley.

I don't really understand _why_ I can't use 0.0 for the 3rd parameter to gluPerspective though.
Re: why don't my spheres render properly?
August 12, 2006 03:46PM
thanks!

just figured out the depth_test bit and the problem with gluPerspective. was about to post my answer smiling smiley.

I don't really understand _why_ I can't use 0.0 for the 3rd parameter to gluPerspective though.
Anonymous User
Re: why don't my spheres render properly?
August 16, 2006 07:38PM
found the answer why glRotatef(spin, 1.0, 0.0, 0.0); rotates the y axis its because if you use gluPerspective(60.0, aspect, 0.0, 50.0); instead of ortho the y-axis become the z-axis and the z-axis become the x-axis and the x-axis become the y-axis.

See chap 3 of theredbook

void gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar);

Creates a matrix for a symmetric perspective-view frustum and multiplies the current matrix by it. The fovy argument is the angle of the field of view in the x-z plane; its value must be in the range [0.0,180.0]. The aspect ratio is the width of the frustum divided by its height. The zNear and zFar values are the distances between the viewpoint and the clipping planes, along the negative z-axis. They should always be positive.

Sorry, only registered users may post in this forum.

Click here to login