Jump to content


Help in displaying a sphere pls ...


11 replies to this topic

#1 c00guy

    New Member

  • Members
  • Pip
  • 6 posts

Posted 25 May 2007 - 05:17 AM

I want to display a simple sphere but the code isn't working... Please help me out ... there are no errors .. I just get a blue background window with no sphere in it !! wheres the sphere???

here is the code

// glutTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <GL/glut.h>

void displayFunction(void)
{
glClearColor(0.0, 0.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);



glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, 1, -200, 200);

GLUquadricObj *obj;
obj = gluNewQuadric();
gluSphere(obj, GLdouble(100), GLint(20), GLint(20));
glMatrixMode(GL_MODELVIEW);

glPushMatrix();

glTranslatef(100,100,-20);

glPopMatrix();
glutSwapBuffers();

glFlush();
}

void menuHandler(int val)
{

if(val == 1)
{

}

}

int main(int argc, char* argv[])
{
glutInitWindowSize(500, 500);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glMatrixMode(GL_PROJECTION); /* Setup perspective projection. */
glLoadIdentity();
glOrtho(-210, 210, -162.5, 257.5, 232.5, -187.5);

glShadeModel(GL_SMOOTH);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();


glutCreateWindow("Test Application ");
glutDisplayFunc(displayFunction); /* Setup GLUT callbacks. */

glutCreateMenu(menuHandler);
glutAddMenuEntry("About", 1);
glutAddMenuEntry("Quit", 0);
glutAttachMenu(GLUT_RIGHT_BUTTON);

glutMainLoop();

return 0;
}

#2 Nicholas Christopher

    Member

  • Members
  • PipPip
  • 77 posts

Posted 25 May 2007 - 12:12 PM

one thing at a time: Start by fixing your near and far clipping planes.

glOrtho(-210, 210, -162.5, 257.5, 232.5, -187.5);

void glOrtho(
GLdouble left,
GLdouble right,
GLdouble bottom,
GLdouble top,
GLdouble zNear,
GLdouble zFar
);

zNear = 0
zFar = a large positive number

also replace

glOrtho( 0, width, 0, height, ... ); of the display size

Nicholas

#3 Kenneth Gorking

    Senior Member

  • Members
  • PipPipPipPip
  • 939 posts

Posted 27 May 2007 - 05:33 PM

You are setting up your matrix after you have drawn the object. Try this instead:

glMatrixMode(GL_MODELVIEW); 

glPushMatrix();

glTranslatef(100,100,-20);


GLUquadricObj *obj;

obj = gluNewQuadric();

gluSphere(obj, 100.0, 20, 20);


glPopMatrix();

glutSwapBuffers();


On a sidenote, you shouldn't call gluNewQuadric() every frame without deleting it, or you will get memoryleaks. Make the pointer global and allocate it in main() instead, and delete it when you are done with it.
"Stupid bug! You go squish now!!" - Homer Simpson

#4 c00guy

    New Member

  • Members
  • Pip
  • 6 posts

Posted 28 May 2007 - 12:40 AM

I tried changing the code as you told me but it still isn't working!!
The sphere isnt coming up ..
Here is the updated code ...

// graphicstest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <GL/glut.h>

void displayFunction(void)
{
glClearColor(0.0, 0.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);



glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, 1, -200, 200);

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(100,100,-20);

GLUquadricObj *obj;
obj = gluNewQuadric();
gluSphere(obj, 100.0, 20, 20);

glPopMatrix();
glutSwapBuffers();

glFlush();
}

void menuHandler(int val)
{

if(val == 1)
{

}

}

int main(int argc, char* argv[])
{
glutInitWindowSize(500, 500);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glMatrixMode(GL_PROJECTION); /* Setup perspective projection. */
glLoadIdentity();
glOrtho(0, 210, 0, 257.5, 0, 232.5);

glShadeModel(GL_SMOOTH);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();


glutCreateWindow("Test Application ");
glutDisplayFunc(displayFunction); /* Setup GLUT callbacks. */

glutCreateMenu(menuHandler);
glutAddMenuEntry("About", 1);
glutAddMenuEntry("Quit", 0);
glutAttachMenu(GLUT_RIGHT_BUTTON);

glutMainLoop();

return 0;
}

#5 Reedbeta

    DevMaster Staff

  • Administrators
  • 5305 posts
  • LocationBellevue, WA

Posted 28 May 2007 - 02:02 AM

c00guy, please use [code]...[/code] tags to post code in this forum.
reedbeta.com - developer blog, OpenGL demos, and other projects

#6 c00guy

    New Member

  • Members
  • Pip
  • 6 posts

Posted 28 May 2007 - 05:26 AM

I tried changing the code as you told me but it still isn't working!!
The sphere isnt coming up ..
Here is the updated code ...


// graphicstest.cpp : Defines the entry point for the console application.

//


#include "stdafx.h"

#include <GL/glut.h>


void displayFunction(void) 

{ 

glClearColor(0.0, 0.0, 1.0, 1.0);

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0, 0.0, 0.0);




glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluPerspective(60, 1, -200, 200);


glMatrixMode(GL_MODELVIEW); 

glPushMatrix();

glTranslatef(100,100,-20);


GLUquadricObj *obj;

obj = gluNewQuadric();

gluSphere(obj, 100.0, 20, 20);


glPopMatrix();

glutSwapBuffers();


glFlush();

}


void menuHandler(int val)

{


if(val == 1)

{


}


}


int main(int argc, char* argv[])

{

glutInitWindowSize(500, 500);

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

glMatrixMode(GL_PROJECTION); /* Setup perspective projection. */

glLoadIdentity(); 

glOrtho(0, 210, 0, 257.5, 0, 232.5);


glShadeModel(GL_SMOOTH);


glMatrixMode(GL_MODELVIEW); 

glLoadIdentity();



glutCreateWindow("Test Application ");

glutDisplayFunc(displayFunction); /* Setup GLUT callbacks. */


glutCreateMenu(menuHandler);

glutAddMenuEntry("About", 1);

glutAddMenuEntry("Quit", 0);

glutAttachMenu(GLUT_RIGHT_BUTTON);


glutMainLoop();


return 0;

} 



#7 Kenneth Gorking

    Senior Member

  • Members
  • PipPipPipPip
  • 939 posts

Posted 28 May 2007 - 10:49 AM

You can't have a negative near-plane, change your perspective setup to gluPerspective(60, 1, 1, 200);
You will now be able to see a bit of the sphere. Use glTranslatef(0, 0, -20) to center it on the screen.
Now your screen will be completely red, because you are rendering a sphere with a radius of 100 units 20 units away from you. Try gluSphere(obj, 2.0, 20, 20) instead, and you're all set.

The modified code:
#include "stdafx.h"

#include <GL/glut.h>


GLUquadricObj *obj = 0;


void displayFunction(void) 

{ 

	glClearColor(0.0, 0.0, 1.0, 1.0);

	glClear(GL_COLOR_BUFFER_BIT);


	glMatrixMode(GL_MODELVIEW); 

	glPushMatrix();

	glTranslatef(0,0,-20);


	glColor3f(1.0, 0.0, 0.0);

	gluSphere(obj, 2.0, 20, 20);


	glPopMatrix();

	glutSwapBuffers();

}


int main(int argc, char* argv[])

{

	glutInitWindowSize(500, 500);

	glutInit(&argc, argv);

	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

	glShadeModel(GL_SMOOTH);


	glutCreateWindow("Test Application ");

	glutDisplayFunc(displayFunction); /* Setup GLUT callbacks. */


	glMatrixMode(GL_PROJECTION);

	glLoadIdentity();

	gluPerspective(60, 1, 1, 200);

	glMatrixMode(GL_MODELVIEW); 

	glLoadIdentity();


	obj = gluNewQuadric();


/*	glutCreateMenu(menuHandler);

	glutAddMenuEntry("About", 1);

	glutAddMenuEntry("Quit", 0);

	glutAttachMenu(GLUT_RIGHT_BUTTON);

*/

	glutMainLoop();


	gluDeleteQuadric(obj);


	return 0;

}


"Stupid bug! You go squish now!!" - Homer Simpson

#8 c00guy

    New Member

  • Members
  • Pip
  • 6 posts

Posted 29 May 2007 - 01:48 AM

Thanks mate ... the code is workin fine now ... :)

#9 c00guy

    New Member

  • Members
  • Pip
  • 6 posts

Posted 29 May 2007 - 01:49 AM

by the way is there any way i cud give it a better 3D look ? .. its still looking like a 2D object ..

#10 Reedbeta

    DevMaster Staff

  • Administrators
  • 5305 posts
  • LocationBellevue, WA

Posted 29 May 2007 - 03:36 AM

You'll need to set up a light and enable lighting for it to look more 3D.

By the way, it's "could" not "cud". Cud is what cows chew.
reedbeta.com - developer blog, OpenGL demos, and other projects

#11 c00guy

    New Member

  • Members
  • Pip
  • 6 posts

Posted 29 May 2007 - 05:02 AM

yeah .. just used to short forms and sms script ... dont mind it ... :D

#12 Kenneth Gorking

    Senior Member

  • Members
  • PipPipPipPip
  • 939 posts

Posted 29 May 2007 - 03:02 PM

Check out Nehe for all your OpenGL needs :)
"Stupid bug! You go squish now!!" - Homer Simpson





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users