OpenGL 3d 入门

                                            OpenGL    3d 入门

 

摘录了计算机图形学书上的一个例子,嘿嘿,比想象的简单啊;

稍微修改下,算是3D入门吧;

#include "glut.h"

void axis(double length)
{
	glPushMatrix();
	glBegin(GL_LINES);
	glVertex3d(0.0, 0.0, 0.0);
	glVertex3d(0.0, 0.0, length);
	glEnd();
	glTranslated(0.0, 0.0, length-0.2) ;
	glutWireCone(0.04, 0.3, 8, 8) ;
	glPopMatrix();
}

void myDisplay(void)
{
	glClear(GL_COLOR_BUFFER_BIT) ;
	glMatrixMode(GL_PROJECTION) ;
	glLoadIdentity();
	glOrtho(-2.0*64/48.0, 2.0*64/48.0, -2.0, 2.0, 0.1, 100) ;
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(1.3,1.6,2.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) ;

	// draw x-y-z coordinates
	axis(0.5);  // z
	glPushMatrix();
	glRotated(90.0,0, 1.0, 0);
	axis(0.5); //x
	glPopMatrix();
	glPushMatrix();
	glRotated(-90.0, 1.0, 0.0, 0.0);
	axis(0.5); //y
	glPopMatrix();
	
	//draw cube
	glPushMatrix();
	glTranslated(0.5,0.5,0.5);
	glutWireCube(1.0);
	glPopMatrix();
	
	//draw teapot
	glPushMatrix();
	glTranslated(1.0,1.0,0.0);
	glutWireTeapot(0.25);
	glPopMatrix();

	//draw Sphere
	glPushMatrix();
	glTranslated(1.0,0.0,0.0);
	glutWireSphere(0.25,12,8);
	glPopMatrix();

	//draw Torus
	glPushMatrix();
	glTranslated(0.0,1.0,0.0);
	glRotated(-90, 1.0, 0.0, 0.0);
	glutWireTorus(0.1,0.25,12,12);
	glPopMatrix();
	
	//draw Dodecahedron
	glPushMatrix();
	glTranslated(0.0,0.0,1.0);
	glScaled(0.577*0.25,0.577*0.25,0.577*0.25);
	glutWireDodecahedron();
	glPopMatrix();

	//draw cone
	glPushMatrix();
	glTranslated(1.0,1.0,1.0);
	glRotated(-90,1.0, 0.0, 0.0);
	glutWireCone(0.15,0.3,8,8);
	glPopMatrix();

	//draw cube
	glPushMatrix();
	glTranslated(1.0,0.0,1.0);
	glutWireCube(0.3);
	glPopMatrix();
	
	//draw cyliner
	glPushMatrix();
	glTranslated(0.0, 1.0, 1.0);
	GLUquadricObj * quadricObj = gluNewQuadric();
	gluQuadricDrawStyle(quadricObj,GLU_LINE);
	gluCylinder(quadricObj,0.2, 0.2, 0.3, 6, 6);
	glPopMatrix();

	glFlush();
}

void myInit(void)
{			
	glClearColor(0.0, 0.0, 0.0, 0.0);     
    glColor3f(0.0f, 1.0f, 0.0f);            
	 
	glMatrixMode(GL_PROJECTION); 
	glLoadIdentity(); 	 
}

int main(int argc,char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowSize(640,480);
	glViewport(0,0, 640, 480) ;
	glutInitWindowPosition(100,150);
	glutCreateWindow("My3D");
	glutDisplayFunc(myDisplay);
	myInit( );
	glutMainLoop( );
}


上图:

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值