• Advertisement

Archived

This topic is now archived and is closed to further replies.

cube will not rotate, have no idea why?

This topic is 5491 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m sorta of new, its been a while, why isn''t this cube rotating on the screen, here is the code #include <windows.h> #include <gl/glut.h> void display(void); void ChangeSize(GLsizei w, GLsizei h); double rot = 45.0f; int main(int argc, char** argv) { glutInit (&argc, argv) ; glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB) ; glutInitWindowSize (800, 600) ; glutInitWindowPosition (100, 100) ; glutCreateWindow ("First Chapter - Opening an OpenGL Window") ; glClearColor(0.0f,0.0f,0.0f,1.0f); glutDisplayFunc(display); glutReshapeFunc(ChangeSize); glutMainLoop(); return 0 ; } void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer glLoadIdentity(); // Done Drawing The Quad glTranslatef(500.0f,300.0f,0.0f); glRotatef(rot,0.0,1.0,0.0); glBegin(GL_QUADS); //top glColor3f(1.0f,0.0f,0.0f); glVertex3f(0.0f,0.0f,0.0f); glVertex3f(0.0f,0.0f,-100.0f); glVertex3f(100.0f,0.0f,-100.0f); glVertex3f(100.0f,0.0f,0.0f); //bottom glColor3f(0.0f,1.0f,0.0f); glVertex3f(0.0f,-100.0f,0.0f); glVertex3f(0.0f,-100.0f,-100.0f); glVertex3f(100.0f,-100.0f,-100.0f); glVertex3f(100.0f,-100.0f,0.0f); //front glColor3f(0.0f,0.0f,1.0f); glVertex3f(0.0f,0.0f,0.0f); glVertex3f(100.0f,0.0f,0.0f); glVertex3f(100.0f,-100.0f,0.0f); glVertex3f(0.0f,-100.0f,0.0f); //back glColor3f(1.0f,1.0f,0.0f); glVertex3f(0.0f,0.0f,-100.0f); glVertex3f(100.0f,0.0f,-100.0f); glVertex3f(100.0f,-100.0f,-100.0f); glVertex3f(0.0f,-100.0f,-100.0f); //left glColor3f(1.0f,0.0f,1.0f); glVertex3f(0.0f,0.0f,0.0f); glVertex3f(0.0f,0.0f,-100.0f); glVertex3f(0.0f,-100.0f,-100.0f); glVertex3f(0.0f,-100.0f,0.0f); //right glColor3f(0.0f,1.0f,1.0f); glVertex3f(100.0f,0.0f,0.0f); glVertex3f(100.0f,0.0f,-100.0f); glVertex3f(100.0f,-100.0f,-100.0f); glVertex3f(100.0f,-100.0f,0.0f); glEnd(); rot+=2.0f; glutSwapBuffers(); } void ChangeSize(GLsizei w, GLsizei h) { if(h == 0) h = 1; glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if(w<=h) glOrtho(0,800,0,600*h/w,1.0f,-1.0f); else glOrtho(0,800*w/h,0,600,1.0f,-1.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); }

Share this post


Link to post
Share on other sites
Advertisement
I''ll help you fix this right now. After glutSwapBuffers, put glutPostRedisplay(). It will work just fine.

Share this post


Link to post
Share on other sites
thank you, now it looks like it is rotating, but it does not look like a cube rotating, does anyone have any idea why

Share this post


Link to post
Share on other sites
xg0blin is here to save the day . In your initialization function, put glEnable(GL_DEPTH_TEST).

Share this post


Link to post
Share on other sites
thanks goblin, i put what u said, but it still does not look like a cube rotating, here is the updated code

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

void display(void);
void ChangeSize(GLsizei w, GLsizei h);
double rot = 0.0f;
int main(int argc, char** argv)

{

glutInit (&argc, argv) ;

glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB) ;

glutInitWindowSize (800, 600) ;

glutInitWindowPosition (100, 100) ;

glutCreateWindow ("First Chapter - Opening an OpenGL Window") ;

glClearColor(0.0f,0.0f,0.0f,1.0f);

glEnable(GL_DEPTH_TEST);

glutDisplayFunc(display);

glutReshapeFunc(ChangeSize);



glutMainLoop();



return 0 ;

}

void display()

{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Done Drawing The Quad
glTranslatef(500.0f,300.0f,0.0f);
if(rot == 360.0)
rot = 0.0;
glRotatef(rot,0.0,1.0,0.0);
glBegin(GL_QUADS);
//top
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,-100.0f);
glVertex3f(100.0f,0.0f,-100.0f);
glVertex3f(100.0f,0.0f,0.0f);
//bottom
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(0.0f,-100.0f,0.0f);
glVertex3f(0.0f,-100.0f,-100.0f);
glVertex3f(100.0f,-100.0f,-100.0f);
glVertex3f(100.0f,-100.0f,0.0f);
//front
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(100.0f,0.0f,0.0f);
glVertex3f(100.0f,-100.0f,0.0f);
glVertex3f(0.0f,-100.0f,0.0f);
//back
glColor3f(1.0f,1.0f,0.0f);
glVertex3f(0.0f,0.0f,-100.0f);
glVertex3f(100.0f,0.0f,-100.0f);
glVertex3f(100.0f,-100.0f,-100.0f);
glVertex3f(0.0f,-100.0f,-100.0f);
//left
glColor3f(1.0f,0.0f,1.0f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,-100.0f);
glVertex3f(0.0f,-100.0f,-100.0f);
glVertex3f(0.0f,-100.0f,0.0f);
//right
glColor3f(0.0f,1.0f,1.0f);
glVertex3f(100.0f,0.0f,0.0f);
glVertex3f(100.0f,0.0f,-100.0f);
glVertex3f(100.0f,-100.0f,-100.0f);
glVertex3f(100.0f,-100.0f,0.0f);
glEnd();
rot+=0.25f;
glutSwapBuffers();
glutPostRedisplay();
}

void ChangeSize(GLsizei w, GLsizei h)
{
if(h == 0)
h = 1;

glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

if(w<=h)
glOrtho(0,800,0,600*h/w,1.0f,-1.0f);
else
glOrtho(0,800*w/h,0,600,1.0f,-1.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

Share this post


Link to post
Share on other sites
It appears that it would be because you are not drawing a cube at all. I have a little tutorial I wrote (for credit hours, I'm working on more) that covers just this kind of thing. See
rattler.cameron.edu\ogl
For further reference when you are posting on these forums, you can put source code inbetween source and /source


[edited by - xg0blin on February 8, 2003 3:55:33 PM]

Share this post


Link to post
Share on other sites
I tried using your cube source and it does the same thing, have any ideas, im sure im drawing a cube

Share this post


Link to post
Share on other sites
You used my source and it doesn''t work? You must be doing something weird then. You took every bit of it word for word? It works great for me, so that''s really weird.

Share this post


Link to post
Share on other sites
Perhaps it isnt being projected properly, I tried it using my cube code and it has simular results

Share this post


Link to post
Share on other sites
It was because you were using Orthographic mode, i think... lol anyway this works

-----------------------------------------------------------------

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

void display(void);
void ChangeSize(GLsizei w, GLsizei h);
void DrawBox(float size);
double rot = 0.0f;

int main(int argc, char** argv)
{
glutInit (&argc, argv) ;
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB) ;
glutInitWindowSize (800, 600) ;
glutInitWindowPosition (100, 100) ;
glutCreateWindow ("First Chapter - Opening an OpenGL Window") ;

glClearColor(0.0f,0.0f,0.0f,1.0f);
glEnable(GL_DEPTH_TEST);

glutDisplayFunc(display);
glutReshapeFunc(ChangeSize);
glutMainLoop();
return 0 ;
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer

glLoadIdentity();
glTranslatef(0.0f,0.0f,-10.0f);

rot+=1.0f;
if(rot >= 360.0)rot -= 360.0;
glRotatef(rot, 0.0,1.0,0.0);
// glRotatef(rot*2.0f,0.0,0.0,1.0); // uncomment to rotate on the z-axis

DrawBox(2.0f);

glutSwapBuffers();
glutPostRedisplay();
}

void ChangeSize(GLsizei w, GLsizei h)
{
if (h==0) // Prevent A Divide By Zero By
{
h=1; // Making Height Equal One
}

glViewport(0, 0, w, h); // Reset The Current Viewport

glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix

// Calculate The Aspect Ratio Of The Window
// (Field of view, aspect ratio, near, far)
gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,100.0f);

glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}

void DrawBox(float size)
{
glBegin(GL_QUADS);
// Front Face
glColor3f(0.0f,1.0f,1.0f);
glVertex3f(-size, -size, size); // Bottom Left Of the Quad
glVertex3f( size, -size, size); // Bottom Right Of the Quad
glVertex3f( size, size, size); // Top Right Of The the Quad
glVertex3f(-size, size, size); // Top Left Of the Quad
// Back Face
glColor3f(1.0f,0.0f,1.0f);
glVertex3f(-size, -size, -size); // Bottom Right Of the Quad
glVertex3f(-size, size, -size); // Top Right Of the Quad
glVertex3f( size, size, -size); // Top Left Of the Quad
glVertex3f( size, -size, -size); // Bottom Left Of the Quad
// Top Face
glColor3f(1.0f,1.0f,0.0f);
glVertex3f(-size, size, -size); // Top Left Of the Quad
glVertex3f(-size, size, size); // Bottom Left Of the Quad
glVertex3f( size, size, size); // Bottom Right Of the Quad
glVertex3f( size, size, -size); // Top Right Of the Quad
// Bottom Face
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-size, -size, -size); // Top Right Of the Quad
glVertex3f( size, -size, -size); // Top Left Of the Quad
glVertex3f( size, -size, size); // Bottom Left Of the Quad
glVertex3f(-size, -size, size); // Bottom Right Of the Quad
// Right face
glColor3f(1.0f,0.0f,0.0f);
glVertex3f( size, -size, -size); // Bottom Right Of the Quad
glVertex3f( size, size, -size); // Top Right Of the Quad
glVertex3f( size, size, size); // Top Left Of the Quad
glVertex3f( size, -size, size); // Bottom Left Of the Quad
// Left Face
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(-size, -size, -size); // Bottom Left Of the Quad
glVertex3f(-size, -size, size); // Bottom Right Of the Quad
glVertex3f(-size, size, size); // Top Right Of the Quad
glVertex3f(-size, size, -size); // Top Left Of the Quad
glEnd();
}

[edited by - Thorin_nz on February 8, 2003 7:19:26 PM]

Share this post


Link to post
Share on other sites
Remember that if it''s a wire frame cube, there are two ways of looking at it. The wrong way makes it look distorted and warping when it''s rotating. I didn''t read your code, sorry.

Share this post


Link to post
Share on other sites
Its because it was being displayed 2d due to the projection matrix used, When a proper 3d projection matrix is used its fine

Share this post


Link to post
Share on other sites
ok i changed it to perspective view, but now I don''t see the cube, I used the code posted above

/source void ChangeSize(GLsizei w, GLsizei h)
{
if (h==0) // Prevent A Divide By Zero By
{
h=1; // Making Height Equal One
}

glViewport(0, 0, w, h); // Reset The Current Viewport

glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix

// Calculate The Aspect Ratio Of The Window
// (Field of view, aspect ratio, near, far)
gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,100.0f);

glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
} /source

Share this post


Link to post
Share on other sites
Well, im translating to near the middle of the screen and rotating on that central axis, but i tried it anyway, the cube does not show

Share this post


Link to post
Share on other sites

  • Advertisement