Sign in to follow this  
nicolasbrown

it wont rotate

Recommended Posts

do ya have any section for real noobs(like me)? i am wondering why this triangle won,t rotate.Can someone plz tell me why? #include <windows.h> #include <gl\gl.h> #include <gl\glut.h> void init(void); void display(void); int main (int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(400, 400); glutInitWindowPosition(100, 100); glutCreateWindow("Nicolas window"); init(); glutDisplayFunc(display); glutMainLoop(); return 0; } void init(void) { glClearColor(0.0, 0.0, 0.0, 0.0); glColor3f(0.5, 0.60, 1.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-10.0, 10.0, -10.0, 10.0, -10.0, 1); } void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glRotatef(10,0.0f,1.0f,0.0f); glBegin(GL_TRIANGLES); glVertex3f( 0.0f, 1.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f); glVertex3f( 2.0f,-1.0f, 0.0f); glEnd(); glutSwapBuffers(); }

Share this post


Link to post
Share on other sites
You have no variable in your whole code. Every loop sets the rotation to the same value. I think you got it wrong. Rotation doesn't rotate from the current rotated position but from the original one. To rotate something, you have to continually increase one of the values in your rotation.
I have no idea about glut but I just assume something like

glRotatef(angle,0.0f,1.0f,0.0f);
angle += 0.001f;

might help.
Also consider using [ source ]
[ /source ] tags when posting code. It keeps the indentation and makes the code more readable

Share this post


Link to post
Share on other sites
Since you aren't calling glLoadIdentity() after each rendering, I imagine that you ought to see a rotating triangle. It may be that the screen is not being refreshed. You might try:

------------------------------------------
glutSwapBuffers();
//followed by
glutPostRedisplay();

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

In any case, you will eventually want to do what BiGFOOT suggested if you start making a game, as well as use time-based updates to object positions and animations.

Share this post


Link to post
Share on other sites
Just signed up to forum guys so Hi All! :D it's funny this question cropt up i have just done this in my university course and to start with i had the same problem as you. What bigfoot said is true you need to increment the angle variable every frame so that the new position after the frame is that of the starting position of the triangle plus that of the variable(which gets bigger).

I'd also suggest using glPushMatrix(); and glPopMatrix(); as shown below;

glPushMatrix();
glRotatef(angle, 0.45, 0.45, 0.45);
glBegin(GL_TRIANGLES);
glVertex2f(0.6f,-0.6f);
glVertex2f(1.0f,-0.4f);
glVertex2f(0.8f,-1.0f);
glEnd();
glPopMatrix();

but this may be irrelevant if your only using/creating one shape.

Hope this helps :D

Share this post


Link to post
Share on other sites
wow, they really teach this in university.Back here in jamaica i dont think they teach thats kinda stuff.Although i really want to do it.
Oh well, i am just 15, i got lots of time to choose my career.
Thanks guys

here it is now:

#include <windows.h> /* obviously change this to your native library
if you're compiling under unix */
#include <gl\gl.h>
#include <gl\glut.h>

void init(void);
void display(void);
float rtri;

int main (int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutInitWindowPosition(100, 100);
glutCreateWindow("Nicolas window");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glColor3f(0.5, 0.60, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10.0, 10.0, -10.0, 10.0, -10.0, 1);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Move Left 1.5 Units And Into The Screen 6.0
glRotatef(rtri,0.0f,1.0f,0.0f); // Rotate The Triangle On The Y axis ( NEW )
glBegin(GL_TRIANGLES); // Start Drawing A Triangle
glColor3f(1.0f,0.0f,0.0f); // Red
glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Front)
glColor3f(0.0f,1.0f,0.0f); // Green
glVertex3f(-1.0f,-1.0f, 1.0f); // Left Of Triangle (Front)
glColor3f(0.0f,0.0f,1.0f); // Blue
glVertex3f( 1.0f,-1.0f, 1.0f); // Right Of Triangle (Front)
glColor3f(1.0f,0.0f,0.0f); // Red
glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Right)
glColor3f(0.0f,0.0f,1.0f); // Blue
glVertex3f( 1.0f,-1.0f, 1.0f); // Left Of Triangle (Right)
glColor3f(0.0f,1.0f,0.0f); // Green
glVertex3f( 1.0f,-1.0f, -1.0f); // Right Of Triangle (Right)
glColor3f(1.0f,0.0f,0.0f); // Red
glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Back)
glColor3f(0.0f,1.0f,0.0f); // Green
glVertex3f( 1.0f,-1.0f, -1.0f); // Left Of Triangle (Back)
glColor3f(0.0f,0.0f,1.0f); // Blue
glVertex3f(-1.0f,-1.0f, -1.0f); // Right Of Triangle (Back)
glColor3f(1.0f,0.0f,0.0f); // Red
glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Left)
glColor3f(0.0f,0.0f,1.0f); // Blue
glVertex3f(-1.0f,-1.0f,-1.0f); // Left Of Triangle (Left)
glColor3f(0.0f,1.0f,0.0f); // Green
glVertex3f(-1.0f,-1.0f, 1.0f); // Right Of Triangle (Left)
glEnd(); // Done Drawing The Pyramid
rtri = 0.2;
glutSwapBuffers();
glutPostRedisplay();
}

Share this post


Link to post
Share on other sites

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