opengl入門系列2- 模型檢視矩陣變換
阿新 • • 發佈:2019-02-16
#include
<GL/glut.h>
#include <stdlib.h>
/* Initialize material property and light source.
*/
void init (void)
{
//Set up the environment light intensity
GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
//Set up the diffuse light intensity
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
//Set up the specular light intensity
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
//light_position is NOT default value
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
glLightfv (GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv (GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv (GL_LIGHT0, GL_POSITION, light_position);
glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
void display (void)
{
GLfloat M[4][4];
GLfloat V[4][4];
//--------Debug---
glGetFloatv(GL_MODELVIEW_MATRIX, M);
glGetFloatv(GL_PROJECTION_MATRIX, V);
//---------Debug---
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix ();
//沿著x軸旋轉20度,畫面向眼睛方向傾斜
glRotatef (20.0, 1.0, 0.0, 0.0); //MC=M1
//--------Debug---
glGetFloatv(GL_MODELVIEW_MATRIX, M);
glGetFloatv(GL_PROJECTION_MATRIX, V);
//---------Debug---
glPushMatrix ();
//向x移動-0.75, 向y移動0.5
glTranslatef (-0.75, 0.5, 0.0); //MC=M1*M2
//--------Debug---
glGetFloatv(GL_MODELVIEW_MATRIX, M);
glGetFloatv(GL_PROJECTION_MATRIX, V);
//---------Debug---
//沿著x軸旋轉90度
glRotatef (90.0, 1.0, 0.0, 0.0); //MC=M1*M2*M3
//--------Debug---
glGetFloatv(GL_MODELVIEW_MATRIX, M);
glGetFloatv(GL_PROJECTION_MATRIX, V);
//---------Debug---
glutSolidTorus (0.275, 0.85, 15, 15);
glPopMatrix ();
//--------Debug---
glGetFloatv(GL_MODELVIEW_MATRIX, M);
glGetFloatv(GL_PROJECTION_MATRIX, V);
//---------Debug---
glPushMatrix ();
glTranslatef (-0.75, -0.5, 0.0); //MC=M1*M2
glRotatef (270.0, 1.0, 0.0, 0.0); //MC=M1*M2*M3
glutSolidCone (1.0, 2.0, 15, 15);
glPopMatrix ();
glPushMatrix ();
glTranslatef (0.75, 0.0, -1.0);
glutSolidSphere (1.0, 15, 15);
glPopMatrix ();
glPopMatrix ();
glFlush ();
}
void reshape(int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
if (w <= h)
glOrtho (-2.5, 2.5, -2.5*(GLfloat)h/(GLfloat)w,
2.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
else
glOrtho (-2.5*(GLfloat)w/(GLfloat)h,
2.5*(GLfloat)w/(GLfloat)h, -2.5, 2.5, -10.0, 10.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}
/* Main Loop
* Open window with initial window size, title bar,
* RGBA display mode, and handle input events.
*/
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutCreateWindow (argv[0]);
init ();
glutReshapeFunc (reshape);
glutDisplayFunc(display);
glutKeyboardFunc (keyboard);
glutMainLoop();
return 0;
}
#include <stdlib.h>
/* Initialize material property and light source.
*/
void init (void)
{
//Set up the environment light intensity
GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
//Set up the diffuse light intensity
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
//Set up the specular light intensity
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
//light_position is NOT default value
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
glLightfv (GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv (GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv (GL_LIGHT0, GL_POSITION, light_position);
glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
void display (void)
{
GLfloat M[4][4];
GLfloat V[4][4];
//--------Debug---
glGetFloatv(GL_MODELVIEW_MATRIX, M);
glGetFloatv(GL_PROJECTION_MATRIX, V);
//---------Debug---
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix ();
//沿著x軸旋轉20度,畫面向眼睛方向傾斜
glRotatef (20.0, 1.0, 0.0, 0.0); //MC=M1
//--------Debug---
glGetFloatv(GL_MODELVIEW_MATRIX, M);
glGetFloatv(GL_PROJECTION_MATRIX, V);
//---------Debug---
glPushMatrix ();
//向x移動-0.75, 向y移動0.5
glTranslatef (-0.75, 0.5, 0.0); //MC=M1*M2
//--------Debug---
glGetFloatv(GL_MODELVIEW_MATRIX, M);
glGetFloatv(GL_PROJECTION_MATRIX, V);
//---------Debug---
//沿著x軸旋轉90度
glRotatef (90.0, 1.0, 0.0, 0.0); //MC=M1*M2*M3
//--------Debug---
glGetFloatv(GL_MODELVIEW_MATRIX, M);
glGetFloatv(GL_PROJECTION_MATRIX, V);
//---------Debug---
glutSolidTorus (0.275, 0.85, 15, 15);
glPopMatrix ();
//--------Debug---
glGetFloatv(GL_MODELVIEW_MATRIX, M);
glGetFloatv(GL_PROJECTION_MATRIX, V);
//---------Debug---
glPushMatrix ();
glTranslatef (-0.75, -0.5, 0.0); //MC=M1*M2
glRotatef (270.0, 1.0, 0.0, 0.0); //MC=M1*M2*M3
glutSolidCone (1.0, 2.0, 15, 15);
glPopMatrix ();
glPushMatrix ();
glTranslatef (0.75, 0.0, -1.0);
glutSolidSphere (1.0, 15, 15);
glPopMatrix ();
glPopMatrix ();
glFlush ();
}
void reshape(int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
if (w <= h)
glOrtho (-2.5, 2.5, -2.5*(GLfloat)h/(GLfloat)w,
2.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
else
glOrtho (-2.5*(GLfloat)w/(GLfloat)h,
2.5*(GLfloat)w/(GLfloat)h, -2.5, 2.5, -10.0, 10.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}
/* Main Loop
* Open window with initial window size, title bar,
* RGBA display mode, and handle input events.
*/
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutCreateWindow (argv[0]);
init ();
glutReshapeFunc (reshape);
glutDisplayFunc(display);
glutKeyboardFunc (keyboard);
glutMainLoop();
return 0;
}