OpenGL【C++】台灯二

请添加图片描述

#include<gl/glut.h>
#include <math.h>

static GLfloat yrot = -100.0;//-100
static GLfloat zrot = 40.0;//20
static GLint spin = 0.0;
const GLfloat PI = 3.1415926f;
const GLfloat R = 10.0f;
GLUquadricObj* objCylinder = gluNewQuadric();

void init()
{
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glShadeModel(GL_SMOOTH);

	//设置光源属性:环境光、镜面光、散色光
	GLfloat light_ambient[] = { 1, 1, 1, 1.0 };//{0.2, 0.2, 0.2, 1.0};
	GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
	GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
	GLfloat light_diffuse1[] = { 0.5, 0.5, 0.5, 0 };

	glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
	glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);


	GLfloat spot_direction[] = { 0.0, -1.0, 0.0 };
	GLfloat light_position[] = { 13.0f, -3, 0.0 };
	GLfloat light_position1[] = { 0.0f, 20, 0.0, 1.0 };//固定光源


	//移动光源初始化
	glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 40.0);
	glLightfv(GL_LIGHT0, GL_POSITION, light_position);
	glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spot_direction);

	//固定光源初始化 默认光源位置是 (不要删)
	glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient);
	glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse1);

	//glLightfv(GL_LIGHT1, GL_POSITION, light_position1);
	//glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, spot_direction);


	//设置材料属性:镜面光、散色光、环境光
	GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
	GLfloat mat_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
	GLfloat mat_shininess[] = { 100.0 };
	glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
	glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);


	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);//开启移动光源
	glEnable(GL_LIGHT1);//开启固定光源
	glEnable(GL_DEPTH_TEST);
}

void display()
{

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	//画台灯底座
	glPushMatrix();
	glColor3f(0.2, 0.8, 0.2);
	glTranslated(0.0, -20.0, 10.0);
	glScalef(16.0, 1.0, 12);
	glutSolidCube(2.0);
	glPopMatrix();

	//画灯杆
	//*******************注******************:gluCylinder画圆柱,初试状态沿着-Z轴方向
	//灯杆第一段
	glPushMatrix();
	glColor3f(1.0, 1.0, 1.0);
	glRotatef(90.0, 1.0, 0.0, 0.0);
	gluCylinder(objCylinder, 1.0, 1.0, 21.0, 100, 100);// baseRadius,topRadius, height, slices, stacks
	glPopMatrix();

	//灯杆第二段
	glPushMatrix();
	glRotatef(yrot, 0.0f, 1.0f, 0.0f);
	glRotatef(zrot, 0.0f, 0.0f, 1.0f);

	glColor3f(1.0, 1.0, 1.0);
	glTranslatef(4.0, 6.93, 0.0);
	glRotatef(90.0, 1.0, 0.0, 0.0);
	glRotatef(-30.0, 0.0, 1.0, 0.0);
	gluCylinder(objCylinder, 1.0, 1.0, 8.0, 100, 100);// baseRadius,topRadius, height, slices, stacks
	glPopMatrix();

	//灯杆第三段
	glPushMatrix();
	glRotatef(yrot, 0.0f, 1.0f, 0.0f);
	glRotatef(zrot, 0.0f, 0.0f, 1.0f);

	glColor3f(1.0, 1.0, 1.0);
	glTranslatef(4.0, 6.93, 0.0);
	glRotatef(90.0, 1.0, 0.0, 0.0);
	glRotatef(90.0, 0.0, 1.0, 0.0);
	gluCylinder(objCylinder, 1.0, 1.0, 10.0, 100, 100);// baseRadius,topRadius, height, slices, stacks
	glPopMatrix();

	//灯杆第四段	
	glPushMatrix();
	glRotatef(yrot, 0.0f, 1.0f, 0.0f);
	glRotatef(zrot, 0.0f, 0.0f, 1.0f);

	glColor3f(1.0, 1.0, 1.0);
	glTranslatef(13, 6.93, 0.0);
	glRotatef(90.0, 1.0, 0.0, 0.0);
	gluCylinder(objCylinder, 1.0, 1.0, 5.0, 100, 100);// baseRadius,topRadius, height, slices, stacks
	glPopMatrix();

	//灯壳
	//画圆锥的面
	glPushMatrix();
	glRotatef(yrot, 0.0f, 1.0f, 0.0f);
	glRotatef(zrot, 0.0f, 0.0f, 1.0f);

	glColor3f(0.0, 0.0, 1.0);
	glBegin(GL_TRIANGLE_FAN);
	glVertex3f(13.0f, 2.5f, 0.0f);
	for (GLfloat angle = 0; angle <= 2.0 * PI; angle += PI / 10.0f) {
		GLfloat x = 13 + R * sin(angle);
		GLfloat z = R * cos(angle);

		glVertex3f(x, -3.27, z);
	}
	glVertex3f(13.0f, -3.27, 10.0f);
	glEnd();
	glPopMatrix();

	//灯泡
	glPushMatrix();

	glRotatef(yrot, 0.0f, 1.0f, 0.0f);
	glRotatef(zrot, 0.0f, 0.0f, 1.0f);

	//光源

	GLfloat spot_direction[] = { 0.0, -1.0, 0.0 };
	GLfloat light_position[] = { 13.0f, -3, 0.0 };

	glLightfv(GL_LIGHT0, GL_POSITION, light_position);

	glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spot_direction);

	glDisable(GL_LIGHTING);
	glColor3f(1.0, 0.6, 0.2);
	glTranslatef(13.0, -3.4, 0.0);
	glutSolidSphere(3.0, 100, 100);
	glEnable(GL_LIGHTING);

	glPopMatrix();

	glutSwapBuffers();
}

void changesize(int w, int h)
{
	GLfloat nRange = 40.0;

	if (h == 0)
		h = 1;

	glViewport(0.0, 0.0, (GLsizei)w, (GLsizei)h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	//gluPerspective(40.0,)
	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();
	//gluLookAt(0.5, 2.0, 4.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
	gluLookAt(5.0, 5.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}

void SpecialKeys(int key, int x, int y)
{
	if (key == GLUT_KEY_UP && zrot < 50.0)
		zrot += 5.0;

	if (key == GLUT_KEY_DOWN && zrot > -30.0)
		zrot -= 5.0;

	if (key == GLUT_KEY_LEFT)
		yrot -= 5.0f;

	if (key == GLUT_KEY_RIGHT)
		yrot += 5.0f;

	glutPostRedisplay();
}


int main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(800, 600);
	glutInitWindowPosition(200, 20);
	glutCreateWindow("台灯");

	init();
	glutDisplayFunc(display);
	glutReshapeFunc(changesize);
	glutSpecialFunc(SpecialKeys);
	glutMainLoop();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

向日葵xyz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值