Sign in to follow this  

Why doesn't this code work:

This topic is 4869 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

//It should make the triangles rotate, but it doesn't work on my pc, when I compile it using VC++ 6, the triangles just stay were they are #include <gl/glut.h> #include <gl/glu.h> #include <gl/gl.h> GLfloat counter = 1; void renderScene(void) { glClear(GL_COLOR_BUFFER_BIT); glRotatef(counter/10,1.0,1.0,0.0); glBegin(GL_TRIANGLES); glColor3f(1.0f,0.0f,0.0f); //glTranslatef(0,0,0.5); glVertex3f(0,0,0); glVertex3f(-0.5,0.5,-0.5); glVertex3f(0.5,0.5,-0.5); glColor3f(1.0f,1.0f,0.0f); glVertex3f(0,0,0); glVertex3f(0.5,0.5,-0.5); glVertex3f(0,-0.5,-0.5); glColor3f(1.0f,0.0f,1.0f); glVertex3f(0,0,0); glVertex3f(0,-0.5,-0.5); glVertex3f(-0.5,0.5,-0.5); glColor3f(0,0.0f,1.0f); glVertex3f(-0.5,0.5,-0.5); glVertex3f(0,-0.5,-0.5); glVertex3f(0.5,0.5,-0.5); glEnd(); counter = counter + 1; glFlush(); } void main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA); glutInitWindowPosition(100,100); glutInitWindowSize(320,320); glutCreateWindow("3D Tech- GLUT Tutorial"); glutDisplayFunc(renderScene); glutMainLoop(); }

Share this post


Link to post
Share on other sites
try this:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

instead of this:

glClear(GL_COLOR_BUFFER_BIT);

EDIT: i don't think that's the problem but you can just try it once you get the rotating done to see the difference.

And the rotation normal you specify (glRotatef(float, nx, ny, nz)) isn't normalized (=magnitude=1.0f). I don't know if that might be a problem, try 1,0,0 instead of 1,1,0 and see if that works.

And as a sidenote, you can't translate the scene (glTranslatef) when you are drawing a primitive (between glBegin and glEnd). You have it commented now so there's no problem.

Share this post


Link to post
Share on other sites
Well, you need to call glutPostRedisplay() now and then, to tell GLUT that the scene needs rendering. My suggestion is at the end of renderScene().
You should also have a glLoadIdentity() before the glRotatef(), so that the previous transformation is cleared before doing a new one.
Finally, you'd probably want double buffering. Change GLUT_SINGLE to GLUT_DOUBLE in glutInitDisplayMode() and cal glutSwapBuffers() instead of glFlush().

EDIT: And if you want depth testing (which you do), you should enable it (glEnable(GL_DEPTH_TEST)) and then clear as Tree Penguin said

Share this post


Link to post
Share on other sites
and now it works, but there must be something wrong with the way I draw the piramid:
//////////////
#include <gl/glut.h>
#include <gl/glu.h>
#include <gl/gl.h>
GLfloat counter = 1;

void renderScene(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(counter/2,0.0,0.0,1.0);

glBegin(GL_TRIANGLES);
glColor3f(1.0f,0.0f,0.0f);

glVertex3f(0,0,0);
glVertex3f(-0.5,0.5,-0.5);
glVertex3f(0.5,0.5,-0.5);
glColor3f(1.0f,1.0f,0.0f);
glVertex3f(0,0,0);
glVertex3f(0.5,0.5,-0.5);
glVertex3f(0,-0.5,-0.5);
glColor3f(1.0f,0.0f,1.0f);
glVertex3f(0,0,0);
glVertex3f(0,-0.5,-0.5);
glVertex3f(-0.5,0.5,-0.5);
glColor3f(0,0.0f,1.0f);
glVertex3f(-0.5,0.5,-0.5);
glVertex3f(0,-0.5,-0.5);
glVertex3f(0.5,0.5,-0.5);
glEnd();
counter = counter + 1;
glutSwapBuffers();
glutPostRedisplay();
}

void main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(320,320);
glutCreateWindow("3D Tech- GLUT Tutorial");
glutDisplayFunc(renderScene);
glutMainLoop();
}

Share this post


Link to post
Share on other sites
Quote:
Original post by TomasH
EDIT: And if you want depth testing (which you do), you should enable it (glEnable(GL_DEPTH_TEST)) and then clear as Tree Penguin said

Add it before the call to glutMainLoop().
If that doesn't fix it, you'll have to be a little bit more descriptive than "there must be something wrong"

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

one thing that might have stopped it working is
glRotatef(counter/10,1.0,1.0,0.0);
counter/10 <- int
so it would be round to nearest int which could be 0 stopping it rotating. 10 as a float should be 10.0f

when you changed your could to counter/2 it could be working because it rounds to an integer higher than (or lower than) 0

Share this post


Link to post
Share on other sites

This topic is 4869 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this