Wednesday, April 17, 2013

glRotated example c c++ java objc



Name

glRotated — multiply the current matrix by a rotation matrix

C Specification

void glRotated(GLdouble  angle,
GLdouble  x,
GLdouble  y,
GLdouble  z);
void glRotatef(GLfloat  angle,
GLfloat  x,
GLfloat  y,
GLfloat  z);

Parameters


angle
Specifies the angle of rotation, in degrees.
xyz
Specify the xy, and z coordinates of a vector, respectively.

Description

glRotated produces a rotation of angle degrees around the vector x y z . The current matrix (see glMatrixMode) is multiplied by a rotation matrix with the product replacing the current matrix, as if glMultMatrix were called with the following matrix as its argument:
x 2 ⁡ 1 - c + c x ⁢ y ⁡ 1 - c - z ⁢ s x ⁢ z ⁡ 1 - c + y ⁢ s 0 y ⁢ x ⁡ 1 - c + z ⁢ s y 2 ⁡ 1 - c + c y ⁢ z ⁡ 1 - c - x ⁢ s 0 x ⁢ z ⁡ 1 - c - y ⁢ s y ⁢ z ⁡ 1 - c + x ⁢ s z 2 ⁡ 1 - c + c 0 0 0 0 1
Where c = cos ⁡ angle , s = sin ⁡ angle , and x y z = 1 (if not, the GL will normalize this vector).
If the matrix mode is either GL_MODELVIEW or GL_PROJECTION, all objects drawn after glRotate is called are rotated. Use glPushMatrix and glPopMatrix to save and restore the unrotated coordinate system.

Notes

This rotation follows the right-hand rule, so if the vector x y z points toward the user, the rotation will be counterclockwise.

Errors

GL_INVALID_OPERATION is generated if glRotate is executed between the execution of glBegin and the corresponding execution of glEnd.

Associated Gets

glGet with argument GL_MATRIX_MODE
glGet with argument GL_COLOR_MATRIX
glGet with argument GL_MODELVIEW_MATRIX
glGet with argument GL_PROJECTION_MATRIX
glGet with argument GL_TEXTURE_MATRIX

Copyright

Copyright © 1991-2006 Silicon Graphics, Inc. This document is licensed under the SGI Free Software B License. For details, see http://oss.sgi.com/projects/FreeB/.

Example

#include <GL/glut.h>    // includes gl.h glu.h
#include <iostream>
#include <windows.h>
#include <math.h>
void SetupRC();
void RenderScene(void);
void Keyboard (unsigned char key, int x, int y);
void ChangeSize(int w, int h);
float xRot =0.0f;
float yRot =0.0f;
float zRot =0.0f;

int main(int argc, char* argv[])
{
 glutInit(&argc, argv);
 glutInitDisplayMode( GLUT_SINGLE | GLUT_RGBA );
 glutCreateWindow ("Cube Drawing");
 glutReshapeFunc(ChangeSize);
 glutDisplayFunc(RenderScene);
 glutKeyboardFunc(Keyboard);
 SetupRC();
 glutMainLoop();
 return 0;
}
void SetupRC()
{

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


 glColor3f(0.0f, 1.0f, 0.0f);
 //glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
}
void RenderScene(void)
{
 glClear (GL_COLOR_BUFFER_BIT);
 /*
 glBegin(GL_POINTS);
 //glVertex3i(0,0,0);
 //glVertex3i(100,0,0);
 glEnd();
 */
 glPushMatrix ();
 glRotated(xRot, 1.0, 0.0, 0.0);
 glRotated(yRot, 0.0, 1.0, 0.0);
 glRotatef(zRot, 0.0f, 0.0f, 1.0f);
 // draw Cube
 glutWireCube (50.0);
 glPopMatrix ();
 glFlush ();
}
void ChangeSize(int w, int h)
{
 GLfloat nRange = 100.0f;
 // h가 0일 때
 if(h == 0)
  h = 1;


 glViewport(0, 0, w, h);


 glMatrixMode (GL_PROJECTION);
 //gluLookAt (0.0, 0.0, 0.0, 0.0, 0.0, -100.0, 0.0, 1.0, 0.0);
 gluPerspective (0.0, w/h, 0.0, 100.0);

 glLoadIdentity();

 if (w <= h)
  glOrtho
  (-nRange, nRange,
  -nRange*h/w, nRange*h/w,
  -nRange, nRange);
 else
  glOrtho
  (-nRange*w/h, nRange*w/h,
  - nRange, nRange,
  -nRange, nRange);


 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();
}
void Keyboard (unsigned char key, int x, int y)
{
 if(key == 'x'){
  xRot+=0.5;
  glutPostRedisplay();
 }
 if(key == 'y'){
  yRot+=0.5;
  glutPostRedisplay();
 }
 if(key == 'z'){
   zRot+=0.5;
  glutPostRedisplay();
 }
}