【OpenGL】鼠标控制三维物体移动(添加光照、材质)

图形学期末作业
软件用的vc++6.0 鼠标控制小茶壶移动,桌子可以改成点绘制贴图。

#include 
#include 
#include 
#include 
#include 
GLfloat rx=0, ry=0 ,angle=0;

GLint winWidth=800, winHeight=800;

GLint mouseDx,mouseDy;
void leg(){

	glColor3f(0.2,0.2,0.2);
	glScaled(0.05,0.3,0.05);
	glutSolidCube(500);

}
void table(){
	 glPushMatrix();
	 glTranslatef(0,-75,0);
	 glScaled(0.6,0.05,0.8);
	 glutSolidCube(500);
	//glutSolidSphere(100, 30, 30);
	 glPopMatrix();
	 glPushMatrix();
	 glTranslatef(-60,-40,-70);
	 glutSolidSphere(20,30,30);
	 glPopMatrix();
	 glPushMatrix();
	 glTranslatef(100,-150,80);
	 leg();
	 glPopMatrix();
	 glPushMatrix();
	 glTranslatef(-100,-150,80);
	 leg();
	 glPopMatrix();
	 glPushMatrix();
	 glTranslatef(100,-150,-80);
	 leg();
	 glPopMatrix();
	 glPushMatrix();
	 glTranslatef(-100,-150,-80);
	 leg();
	 glPopMatrix();


}


//显示函数

void Display(){

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glRotatef(1, rx, ry, 0);
    glMatrixMode(GL_MODELVIEW);
		//glutWireTeapot(160);
	glutSolidTeapot(80);
	table();
glutSwapBuffers();
//glFlush();
}


void SetupRC(void)

{
glClearColor(1.0f, 1.0f, 1.0f,1.0f);

}

 

void ChangeSize(int w, int h)

{

winWidth = w;

winHeight = h;

// 设置观察视野为窗口大小(用FLASH里面的话来说应该叫设置摄象机视野)

glViewport(0,0,w,h);

// 重置坐标系统,指定设置投影参数

glMatrixMode(GL_PROJECTION);

///调用单位矩阵,去掉以前的投影参数设置

glLoadIdentity();

    //设置投影参数

glOrtho(-w/2,w/2,-h/2,h/2,-w,w);

glMatrixMode( GL_MODELVIEW );

glLoadIdentity();

}

 

 

void init() 

{

	glClearColor(0.8,0.9,0.8,1.0); 
	glShadeModel(GL_SMOOTH); 

	//光照处理 

	GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
	GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
	GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
	GLfloat light_position0[] = { 1.0, 1.0, 1.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_position0);  

	// 材质处理 

	GLfloat mat_ambient[] = { 0.0, 0.2, 1.0, 1.0 };
	GLfloat mat_diffuse[] = { 0.8, 0.5, 0.2, 1.0 }; 
	GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
	GLfloat mat_shininess[] = { 100.0 }; 
	glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
	glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
	glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
	glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); 
	glEnable(GL_LIGHTING);  
	glEnable(GL_LIGHT0); 

	glEnable(GL_DEPTH_TEST); 

} 

 

 

/鼠标点击

void MousePlot(GLint button,GLint action,GLint xMouse,GLint yMouse){

if(button==GLUT_LEFT_BUTTON && action==GLUT_DOWN){
	mouseDx = xMouse;
    mouseDy = yMouse;

}

    if(button==GLUT_LEFT_BUTTON && action==GLUT_UP){
    glutPostRedisplay();
}
}
鼠标移动

void MouseMove(GLint xMouse,GLint yMouse){
//angle+=2;
	if(xMouse>mouseDx && yMouse==mouseDy)
		{rx=0;ry=1;}
	else if(xMouse<mouseDx && yMouse==mouseDy)
		{rx=0;ry=-1;}
	else if(xMouse==mouseDx && yMouse<mouseDy)
		{rx=-1;ry=0;}
	else if(xMouse==mouseDx && yMouse>mouseDy)
		{rx=1;ry=0;}
	else if(xMouse>mouseDx && yMouse<mouseDy)
		{rx=-1;ry=1;} 
	else if(xMouse>mouseDx && yMouse>mouseDy)
		{rx=1;ry=1;}
	else if(xMouse<mouseDx && yMouse<mouseDy)
		{rx=-1;ry=-1;}
	else if(xMouse<mouseDx && yMouse>mouseDy)
		{rx=1;ry=-1;}

	mouseDx = xMouse;
	mouseDy = yMouse;

	glutPostRedisplay();

}

 

// 主函数

int main(int argc, char* argv[])

{

	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(600, 600);
	glutInitWindowPosition(300,160);
	glutCreateWindow("table");
	glutDisplayFunc(Display);
	 //glutPassiveMotionFunc(PassiveMouseMove);
	glutMotionFunc(MouseMove);
	glutMouseFunc(MousePlot);
	glutReshapeFunc(ChangeSize);
	init();
	glutMainLoop();

return 0;

}

效果:
【OpenGL】鼠标控制三维物体移动(添加光照、材质)_第1张图片

你可能感兴趣的:(计算机视觉)