PDA

View Full Version : Why isn't my cube showing up?



Dogcow
09-29-2002, 05:54 AM
I've used lines to draw a cube, and am under the impression that it should be viewable now. But when my window comes up I have a white background with nothing on it. What am I doing wrong? My code is here (http://www.theunderground.eon.com.au/code/) . Pick your flavor (.txt,.cpp,.pdf). It's quite lengthy so I didn't want to post it in this thread.

Thanks in advance,
-Dogcow "moof!"
Visit The Underground (http://theunderground.eon.com.au)

tinak
09-29-2002, 06:07 AM
Well, I wouldn't have done it that way.. but anyways, when I altered the translate and scale values to the following it appeared...

translate(50.0,50.0,0);
scale(200.0,200.0,0.0);
rotate(0.0,0.0,0.0);

Hope that helps you

Tina

Dogcow
09-29-2002, 06:49 AM
How should I fix the camera so that it works? The translate, scale methods are supposed to simply move the cube around in front of the camera. I'm supposed to have the camera "lookat" a point using lookat, eyepoint, and up. How do I do this with OpenGL? (my teacher is using Java3D) Also, how does one define the angle of view?

Thanks,
-Dogcow "moof!"
Visit The Underground (http://theunderground.eon.com.au)

[This message has been edited by Dogcow (edited 09-29-2002).]

tinak
09-29-2002, 08:57 AM
You may find it useful to play with Nehe's initial tutorials to give you an idea on what can be done with OpenGL.

nehe.gamedev.net


But your code is okay for an initial OpenGL program... perhaps as you progress you will see ways of improving the code and format.

First things first is that you seem to be using Translate,Rotate and Scale to update the values in a matrix. You don't really need to do this but unless you recode all of your scene you can't really alter this. Otherwise just using the normal glRotate,glTranslate and glScale commands will carry out these commands for you for the whole scene.

To have the camera lookat a point you can simply use the glLookAt code which, if you hadn't used it before, takes a position (xyz), view (xyz) and up (xyz). This can be used with or without the earlier mentioned gl routines to make the view move in conjunction with the camera.

My C++ environment isn't behaving itself at the moment so I'll have to reboot to rig up a simple GLUT program highlighting these functions for you in its simplest sense.

The Angle of View is setup when you use gluPerspective instead of glOrtho. I will highlight these in my program as well...

Tina

Dogcow
09-29-2002, 10:12 AM
Yes, if you can suggest ways to clean up my code I'd really appreciate it. I'm sure there's a more efficient method of storing the values of the coordinate system, but I'm a fish out of the water when it comes to C. I used my own translate and scale methods because I *think* my professor wants me to do that. I'm not positive though, so I may end up using the glut ones...

Is there some sort of point object (I know c isn't object oriented...) that I can store the x,y,z coordinates in? I hope to god there's a better way to do it than I did...

An example program would be very much appreciated.
-Dogcow "moof!"
Visit The Underground (http://theunderground.eon.com.au)

tinak
09-29-2002, 10:40 AM
Well, before I answer your questions the code below is a working OpenGL program that uses the standard GL rotation,translate and scale commands as well as the LookAt command.

For xyz points I tend towards the struct command so something like this could suffice.

struct coords
{
int x;
int y;
int z;
};

You could then create variables as follows:
coords campos,camview,camup;
coords c1,c2;

campos.x = 0
campos.y = 0
campos.z = -100
Could be used to initialise the camera position and then used in the glLookAt command or the initial translate command as follow:

glLookAt(campos.x,campos.y,campos.z,0,0,0,0,1,0)
glTranslated(campos.x,campos.y,campos.z);


But it you are supposed to be using your own methods then it may be just the case that you have to use your existing code and find out the value ranges to use and why... trial and error may well help you out there...

Tina


Example GLUT Program
====================

#include <GL/glut.h>

char glMode;
float rotx=0.0,xrot=0;

void drawCenteredSquare(int width,int height)
{
//Center the Square on the x,y axis points sent to the routine
glBegin(GL_QUADS);
glVertex2d(-width/2,-height/2);
glVertex2d(-width/2, height/2);
glVertex2d( width/2, height/2);
glVertex2d( width/2,-height/2);
glEnd();
}

void drawSquare(int width,int height)
{
glBegin(GL_QUADS);
glVertex2d(0, 0);
glVertex2d(0, height);
glVertex2d(width,height);
glVertex2d(width,0);
glEnd();
}

void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (glMode == 'O') gluOrtho2D(0,w, 0,h);
if (glMode == 'P') gluPerspective(45,1,1,1000);
glMatrixMode(GL_MODELVIEW);
glClearColor(0.0,0.0,0.0,0.0);
glLoadIdentity();
glutPostRedisplay();
}

void Draw3DScene()
{
glPushMatrix();
glColor3ub(255,255,255);
glRotatef(rotx,1,0,0); // Rotate on the x-axis
drawSquare(10,10);
glScalef(0.5,2.0,1.0); // Scale Object half its width,twice its height and the same depth
glTranslated(5,5,0); // Move the view 5 pixels up and across
drawSquare(10,10);
rotx+=0.5;
glPopMatrix();

glPushMatrix();
glTranslatef(0,0,-50);
glRotatef(xrot,1,0,0);

glBegin(GL_TRIANGLES);
glColor3f(1,0,0); glVertex3f(0,1,1);
glColor3f(0,1,0); glVertex3f(1,0,2);
glColor3f(0,0,1); glVertex3f(-1,0,2);

glColor3f(0,0,1); glVertex3f(-1,0,2);
glColor3f(0,1,0); glVertex3f(1,0,2);
glColor3f(1,0,1); glVertex3f(0,0,0);

glColor3f(1,0,0); glVertex3f(0,1,1);
glColor3f(0,0,1); glVertex3f(-1,0,2);
glColor3f(1,0,1); glVertex3f(0,0,0);

glColor3f(1,0,0); glVertex3f(0,1,1);
glColor3f(0,1,0); glVertex3f(1,0,2);
glColor3f(1,0,1); glVertex3f(0,0,0);
glEnd();
xrot++;
glPopMatrix();
}

void Draw2DScene()
{
glPushMatrix();
glColor3ub(255,255,255);
glTranslated(50,50,0);
glRotatef(rotx,0,1,0);
drawSquare(100,100);
rotx++;
glPopMatrix();
}

void display ()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
gluLookAt(0,0,-100,0,0,0,0,1,0); // place camera 100 pixel back with y being up
// or
//glTranslated(0,0,-100); // place view 100 pixels into the screen
if (glMode=='P') Draw3DScene();
if (glMode=='O') Draw2DScene();
glPopMatrix();
glFlush();
glutSwapBuffers();

}


/*--------------------*
* The main routine: *
*--------------------*/
int main (int argc, char **argv)
{
glMode = 'P';
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(640, 480);
glutCreateWindow("Simple GLUT Program");
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
return 0;
}