b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

opengl_glflush_OpenGL中glPushMatrix和glPopMatrix的原理(2)

电脑杂谈  发布时间:2016-06-03 20:04:35  来源:网络整理

//(右乘:变换矩阵置于栈顶矩阵的右面。转载时gqb666增加。
glpushmatrix(); //将当前变换矩阵(单位阵)压入堆栈
glTranslatef( 0.0, 0.0, -5.0 ); // transformation 1
glpushmatrix(); //将平移变换后的的矩阵作为当前变换矩阵压入堆栈,
glRotated( (GLdouble)spin, 1.0, 0.0, 0.0 );
glLightfv( GL_LIGHT0, GL_POSITION, position );
glTranslated( 0.0, 0.0, 1.5 );
glDisable( GL_LIGHTING );
glColor3f( 0.0, 1.0, 0.0 );
glutWireCube( 0.1 );//绿色的下框,代表光源位置
glEnable( GL_LIGHTING );
glPopMatrix(); //消除绘制绿色WireCube时对当前变换矩阵的影响

glutSolidSphere( 0.5, 40, 40 );//以被光照的物体
glPopMatrix(); // Pop the old matrix without the transformations. //返回到单位矩阵
glFlush();
}

void reshape( int w, int h )
{
glViewport( 0, 0, (GLsizei)w, (GLsizei)h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 40.0, (GLfloat)w/(GLfloat)h, 1.0, 20.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}

void mouse( int button, int state, int x, int y )
{
switch ( button )
{
case GLUT_LEFT_BUTTON:
if ( state == GLUT_DOWN )
{
spin = ( spin 30 ) % 360;
glutPostRedisplay();
}
break;
default:
break;
}
}

int main( int argc, char ** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH );
glutInitWindowPosition( 100, 100 );
glutInitWindowSize( 500, 500 );
glutCreateWindow( argv[0] );
init();
glutDisplayFunc( display );
glutReshapeFunc( reshape );
glutMouseFunc( mouse );
glutMainLoop();
return 0;
}

例2 机械手臂的旋转

以下样例中的机械手臂是由两个简单的长方体根据一定的继承关系构成的。glpushmatrix和glPopMatrix之间的变换相对前一次是独立的

#include "windows.h"
#include <GL/gl.h>

#include <GL/glu.h>

#include <GL/glaux.h>

void myinit(void);

void drawPlane(void);

void CALLBACK elbowAdd (void);

void CALLBACK elbowSubtract (void);

void CALLBACK shoulderAdd (void);

void CALLBACK shoulderSubtract (void);

void CALLBACK display(void);

void CALLBACK myReshape(GLsizei w, GLsizei h);


static int shoulder = 0, elbow = 0;

void CALLBACK elbowAdd (void)

{
elbow = (elbow 5) % 360;

}

void CALLBACK elbowSubtract (void)

{
elbow = (elbow - 5) % 360;

}

void CALLBACK shoulderAdd (void)

{
shoulder = (shoulder 5) % 360;

}

void CALLBACK shoulderSubtract (void)

{
shoulder = (shoulder - 5) % 360;

}

void CALLBACK display(void)

{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 1.0, 1.0);

glPushMatrix(); // 将此句凝视掉后能够发现上一次的变换结果对当前变换有影响,加上了glpushmatrix的目的是让各次变换相互独立。

glTranslatef (-0.5, 0.0, 0.0); // 将旋转后的Wirebox向左移动0.5个单位
glRotatef ((GLfloat) shoulder, 0.0, 0.0, 1.0); //看到shoulder是相对于0的绝对角度,不是基于上一次位置的相对角度。
glTranslatef (1.0, 0.0, 0.0); //Step 1将WireBox向右移动一个单位
// void auxWireBox(GLdouble width,GLdouble height,GLdouble depth)

auxWireBox(2.0, 0.2, 0.5); //这个WireBox以x=1为中心,width=2从该中心開始向两边各延伸1个单位。

glTranslatef (1.0, 0.0, 0.0);
glRotatef ((GLfloat) elbow, 0.0, 0.0, 1.0);
glTranslatef (0.8, 0.0, 0.0);
auxWireBox(1.6, 0.2, 0.5);

glPopMatrix(); // 能够看出glpushmatrix和glPopMatrix之间的变换效果被消除。清除上一次对modelview矩阵的改动。
glFlush();

}

void myinit (void)

{
glShadeModel (GL_FLAT);

}

void CALLBACK myReshape(GLsizei w, GLsizei h)

{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef (0.0, 0.0, -5.0); /*觉得是viewing transform 不好理解,因此时是物体不动,世界坐标系向z轴正方向移动5个单位,睛位于世界坐标系的原点; 此处理解为对模型的变换更加直观既将物体向负z轴移动5个单位。*/
}

void main(void)


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/shenmilingyu/article-7565-2.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...