PDA

View Full Version : Small spheres in 3D



dt
02-09-2005, 03:51 AM
Open GL
I am using .NET open GL to try to draw a series of placed solid spheres in 3 dimensions onto a plane. However am having trouble doing this. I am using the standard opengl routines which have been ported over to windows and the glut library. I need to be able to use the routines glutsolidsphere to draw a sphere at a particular place in 3D. I need light to be abl to show that these sphere (I need to draw maybe 400) are in the right place. I just cant find the code to

1) position the spheres where I need them
2) put correct lighting effects on it.

Any help with example code would be gratefully recieved.

mikael_aronsson
02-09-2005, 05:59 AM
glPushMatrix();
glTranslate3d( x, y, z);
glutSolidSphere....
glPopMatrix();

and so on.

Lighting is a little more complicated, you might find it useful to read som tutorials on OpenGL lighting and have a peek at the OpenGL spec. before you give it a try.

Mikael

02-11-2005, 12:06 AM
Hi

Thanks for those comments. I know the basics but am having trouble with the fine detail. For example at the moment I have the ollowing code in my draw routine:

glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,1.0,1.0);
glLoadIdentity();
gluLookAt(0.0,1.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0); // Viewing postion
//glScalef(1.0,1.0,1.0); // Scale the cube up
//glutWireCube(1.0); // Show the cube

GLfloat mat_specular[] = {1.0, 1.0,1.0,1.0};
GLfloat mat_shininess[] = {50.0};
GLfloat light_position[] = {1.0,1.0,1.0,0.0};
glShadeModel(GL_SMOOTH);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
glMaterialfv(GL_LIGHT0,GL_POSITION,light_position) ;
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
GLUquadricObj *sphereObj;
sphereObj = gluNewQuadric();
glutSolidSphere(5.0,100,100);
glPopMatrix();
glFlush(); // Display

But it just does'nt seem to work!

@tomic
02-18-2005, 06:19 AM
I have loaded your code into visual studio and given it a few tweeks. It works fine. You have not filled in the blanks though.... Here is an example of how to get your code working.



#include <GL/glut.h>

GLfloat mat_specular[] = {1.0, 1.0,1.0,1.0};
GLfloat mat_shininess[] = {50.0};
GLfloat light_position[] = {1.0,1.0,1.0,0.0};

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0,1.0,1.0);
glLoadIdentity();
gluLookAt(0.0,1.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0);
glShadeModel(GL_SMOOTH);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
glLightfv(GL_LIGHT0,GL_POSITION,light_position); // This has to be light not material
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glTranslatef(0.5,0,0);
glutSolidSphere(0.5,100,100);
glTranslatef(-1.0,0,0);
glutSolidSphere(0.5,100,100);
glPopMatrix();
glFlush();
glutSwapBuffers();
}

void resize(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-2.0,2.0,-2.0*h/w,2.0*h/w,-10.0,10.0);
else
glOrtho(-2.0*w/h,2.0*w/h,-2.0,2.0,-10.0,10.0);
glMatrixMode(GL_MODELVIEW);
}

void main(int argc, char **argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("Spheres");
glutReshapeFunc(resize);
glutDisplayFunc(display);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}
This uses the glut library, but it is possible to do all of this with the standard gl library, you just need to look up some tutorials