Jump to content
  • Advertisement
Sign in to follow this  
vbuser1338

OpenGL rotating 2d objects

This topic is 5018 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 am havind some trouble about how to rotate objects with NEHE tutorials using project builder on my mac. I can't get it to rotate any for some reason. Well here is my code can someone help me out?
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>



const int  kWindowWidth  =  1024;
const int kWindowHeight =   724;

void InitGL(GLvoid);
void DrawGLScene(void);
GLvoid ReSizeGLScene (int Width, int Height){};
GLfloat rTri = 1.0f;	//variable for rotating triangle
GLfloat rQuad;	//variable for rotating square

int main (int argc , char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);	//Sets up display mode
glutInitWindowSize (kWindowWidth, kWindowHeight);	//Sets window size
glutInitWindowPosition (0,0);	//Sets the position
glutCreateWindow ("GLUT OpenGL Learning");	//Creates the window
//glutCreateWindow (argv[0]);
InitGL();
glutDisplayFunc(DrawGLScene);
glutReshapeFunc(ReSizeGLScene);
                                            
glutMainLoop();	//The main loop

                                       
return 0;
}

void DrawGLScene(void)                                                 // Here's Where We Do All The Drawing
{

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);             // Clear The Screen And The Depth Buffer
        glLoadIdentity(); 			// Reset The View 
        glTranslatef(0.0f,0.0f,0.0f);  //Sets the postion

        glRotatef(rTri,0.0f,1.0f,0.0f);	//Rotate the triangle
                                         
glBegin(GL_TRIANGLES); 	//Start drawing the triangle
                                      
                    glColor3f (0.0, 0.0, 4.0);	//Set the color
                glVertex3f( 0.0f, 0.5f, 0.0f);                          // Top
                    glColor3f (1.0, 0.0, 0.0);	//Set the color
                glVertex3f(-0.5f,-0.5f, 0.0f);                          // Bottom Left
                    glColor3f (0.0, 1.0, 0.0);	//Set the color
                glVertex3f( 0.5f,-0.5f, 0.0f);                          // Bottom Right
        glEnd();                                                        // Finished Drawing The Triangle
       
       glLoadIdentity();	//Reset view
       glTranslatef(0.5f,0.0f,0.0f);                                   // Move Right 3 Units

glRotatef( rQuad, 1.0f, 0.0f, 0.0f );

        glBegin(GL_QUADS); 						// Draw A Quad
                glColor3f (1.0, 0.0, 0.0);	//Set the color                                 
                    glVertex3f(-0.1f,0.1f, 0.0f);                          // Top Left
                glColor3f (0.0, 1.0, 0.0); 	//Set the color 
                    glVertex3f( 0.1f, 0.1f, 0.0f);                          // Top Right
                glColor3f (3.0, 0.0, 0.0);  	//Set the color
                    glVertex3f( 0.1f,-0.1f, 0.0f);                          // Bottom Right
                glColor3f (0.0, 0.0, 4.0);  	//Set the color
                    glVertex3f(-0.1f,-0.1f, 0.0f);                          // Bottom Left
        glEnd();                                                        // Done Drawing The Quad
      rTri+= 0.2f;	//Increase the variable for rotating
      rQuad-= 0.15f;
                                                                                                   
      glFlush ();	//Clear the buffer
      
      
}

void InitGL(GLvoid)
{ 
glClearColor (0.0, 3.0, 0.0, 0.0);	//Clear screen Green
glShadeModel (GL_SMOOTH);	//Set the shader to allow smooth colour blending
glClearDepth( 1.0f );                     // Depth buffer setup
glEnable( GL_DEPTH_TEST );                // Enable depth testing
glDepthFunc( GL_LEQUAL );                 // Type of depth test to do
   // Really nice perspective calculations
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );

}                                         

Thanks

Share this post


Link to post
Share on other sites
Advertisement
try to use glutPostRedisplay() in the render function i am really not sure about it because i'm not using glut

Share this post


Link to post
Share on other sites
I found my problem. Pretty stupid one. The tutorial only rotates it 2 degrees. So I could not see any change. And since it only painted it once it would not add up. I'll make a little timer function that repaints the screen every second or so. Or is there a way it is supposed to repaint it? Well thanks for your help. Bye for now

Share this post


Link to post
Share on other sites
hello again i made a few updates in your code

#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>

const int kWindowWidth = 1024;
const int kWindowHeight = 724;
void spining(void);
void InitGL(GLvoid);
void DrawGLScene(void);
GLvoid ReSizeGLScene (int Width, int Height){};
GLfloat rTri = 1.0f; //variable for rotating triangle
GLfloat rQuad; //variable for rotating square

int main (int argc , char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); //Sets up display mode
glutInitWindowSize (kWindowWidth, kWindowHeight); //Sets window size
glutInitWindowPosition (0,0); //Sets the position
glutCreateWindow ("GLUT OpenGL Learning"); //Creates the window
//glutCreateWindow (argv[0]);
InitGL();
glutIdleFunc(spining);
glutDisplayFunc(DrawGLScene);
glutReshapeFunc(ReSizeGLScene);

glutMainLoop(); //The main loop


return 0;
}

void DrawGLScene(void) // Here's Where We Do All The Drawing
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View
glTranslatef(0.0f,0.0f,0.0f); //Sets the postion

glRotatef(rTri,0.0f,1.0f,0.0f); //Rotate the triangle

glBegin(GL_TRIANGLES); //Start drawing the triangle

glColor3f (0.0, 0.0, 4.0); //Set the color
glVertex3f( 0.0f, 0.5f, 0.0f); // Top
glColor3f (1.0, 0.0, 0.0); //Set the color
glVertex3f(-0.5f,-0.5f, 0.0f); // Bottom Left
glColor3f (0.0, 1.0, 0.0); //Set the color
glVertex3f( 0.5f,-0.5f, 0.0f); // Bottom Right
glEnd(); // Finished Drawing The Triangle

glLoadIdentity(); //Reset view
glTranslatef(0.5f,0.0f,0.0f); // Move Right 3 Units

glRotatef( rQuad, 1.0f, 0.0f, 0.0f );

glBegin(GL_QUADS); // Draw A Quad
glColor3f (1.0, 0.0, 0.0); //Set the color
glVertex3f(-0.1f,0.1f, 0.0f); // Top Left
glColor3f (0.0, 1.0, 0.0); //Set the color
glVertex3f( 0.1f, 0.1f, 0.0f); // Top Right
glColor3f (3.0, 0.0, 0.0); //Set the color
glVertex3f( 0.1f,-0.1f, 0.0f); // Bottom Right
glColor3f (0.0, 0.0, 4.0); //Set the color
glVertex3f(-0.1f,-0.1f, 0.0f); // Bottom Left
glEnd(); // Done Drawing The Quad

glutSwapBuffers();
// glFlush (); //Clear the buffer


}
void spining(){
rTri+= 0.2f; //Increase the variable for rotating
rQuad-= 0.15f;
glutPostRedisplay();
}
void InitGL(GLvoid)
{
glClearColor (0.0, 3.0, 0.0, 0.0); //Clear screen Green
glShadeModel (GL_SMOOTH); //Set the shader to allow smooth colour blending
glClearDepth( 1.0f ); // Depth buffer setup
glEnable( GL_DEPTH_TEST ); // Enable depth testing
glDepthFunc( GL_LEQUAL ); // Type of depth test to do
// Really nice perspective calculations
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );

}



ok all your objects are rotating

PS:"pleas rate me :P"

Share this post


Link to post
Share on other sites
Thanks I'll try that. I had them roatating but not constantly just at load time to what angle I wanted.
Thanks again I'd be lost without your guys help

Share this post


Link to post
Share on other sites
Sorry but I am having probelms with texture mapping. I can't find a place that I can load a bmp file for using as a texture. I looked at the one at nehe but it uses the glaux and I am trying to stay away from using that. I also tried downloading the macox/cocoa conversion but it didn't make any sense to me :S. Any one have a place with tutorials? I would really appreciate it. Oh ya is there a way to change the text of a opengl window after it is created! googled it and could not find an answer.
THANKS

[Edited by - vbuser1338 on January 17, 2005 7:48:57 PM]

Share this post


Link to post
Share on other sites
Are you sure it is SetWindowText()?? I had my framework in and the headers but I still get something like undefined function meaning its not delcared anywhere. Do you need the glaux header for that? Becuase I am not using that.
Thanks

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!