PDA

View Full Version : Using glutSolidTorus or glutWireTorus?



Andrew
11-26-2000, 12:29 PM
I have been trying to draw a simple torus in an OpenGL window, and for some reason it is not working. Obviously, I an very new to using the OpenGL API, but I am slowly learning. Any help would be greatly appreciated.

All this code is attempting to do is draw a blue wireframe torus on a black background.

Andrew. http://www.opengl.org/discussion_boards/ubb/confused.gif
---


/*****************
torus.c
******************/

#include <GL/gl.h>
#include <GL/glut.h>

void init(void)
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glEnable(GL_DEPTH_TEST);
}

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

glColor3f(0.0, 0.0, 1.0);

glPushMatrix();
glutWireTorus(2.0, 5.0, 20, 20);
glPopMatrix();

glutSwapBuffers();

//glFlush();
}


int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);
glutInitWindowPosition(100, 100);

glutCreateWindow("A Simple OpenGL Torus");
init();

glutDisplayFunc(display);
glutMainLoop();

return 0;
}

MrShoe
11-26-2000, 05:43 PM
You made a very simple mistake, you forgot to add
glutIdleFunc(...);

ipo
11-26-2000, 05:50 PM
See code below.




/*
You need to do several things to make your code work. These are as follows:

1. Write an initialization function where you can specify clearing colors, depth
tests, etc. This is what you have done but look at the way I setup the depth test.
In the future you may want to use this function to setup other things such as
antialiasing, lighting modes and the like.

2. Write a resize function and let glut know about it. This function will be called
when the application first starts and whenever the drawing window is resized.
To let glut know abot the function, call glutReshapeFunc() as I've done in main().
In this function you need to setup a viewport transformation (e.g. glViewport())
and a projection transformation (e.g. gluPerspective()).

3. Define the display function and let glut know about it as you've done in your code.
However, here you need to apply a viewing transformation (e.g. gluLookAt()) and a
modelling transformation (ie. any set of calls to glTranslatef(), glRotatef() and glScalef()
taking place after the viewing transformation). In the code below, the modelling transformation
consists of a single call to glRotatef(). After setting up the viewing and modelling transformations
you can begin drawing.

Note that the viewing and modelling transformations can be a bit confusing in the beginning since
you can actually create a viewing transformation by means of calls to glTranslatef() and
glRotatef(). In fact this is what gluLookAt() does for you. In the code below I could have
avoided the call to gluLookAt() by replacing it with glTranslatef(0.0, 0.0, -15.0).
*/

#include <windows.h>
#include <gl/gl.h>
#include <gl/glut.h>

void initGL(void) {
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);

/* depth buffer test */

glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

void resizeScene(GLsizei width, GLsizei height) {
if (height == 0.0)
height = 1;

/* apply viewport transformation */

glViewport(0, 0, width, height);

/* apply projection transformation */

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat) width / (GLfloat) height, 1.0, 100.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

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

glLoadIdentity(); /* reset MODELVIEW matrix */
gluLookAt(0.0, 0.0, 15.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); /* apply viewing transformation */

glRotatef(45.0, 1.0, 0.0, 0.0); /* apply modelling transformation */

glColor3f(0.0, 0.0, 1.0); /* draw torus with given color */
glutWireTorus(2.0, 5.0, 20, 20);

glutSwapBuffers();
}

int main(int argc, char** argv) {
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);
glutInitWindowPosition(100, 100);
glutCreateWindow("A Simple OpenGL Torus");

initGL();

glutDisplayFunc(display);
glutReshapeFunc(resizeScene);
glutMainLoop();

return(0);
}



[This message has been edited by ipo (edited 11-26-2000).]

[This message has been edited by ipo (edited 11-26-2000).]

MrShoe
11-26-2000, 07:09 PM
I would again like to point out that if you are using a double buffer you need to have an
glutIdleFunc(...);
Without it you will get one frame displayed and then the screen or window will go blank!

Andrew
11-27-2000, 06:19 AM
Thanks for all your help, I really appreciate it!
http://www.opengl.org/discussion_boards/ubb/smile.gif
Andrew.

Glossifah
11-27-2000, 07:02 AM
MrShoe,

All due respect sir, but glutIdleFunc() is a tool for animations and the like. It is not required, however, to have a scene which updates itself. This happens with glutMainLoop(), which tells the scene to continue cycling through whatever function specified by glutDisplayFunc().

Though it adds some great functionality with limited complexity, glutIdleFunc() isn't needed to create a scene which is updated consistently.

Regards,

Glossifah