版权归原作者所有,感谢原作者的无私奉献。这是本人自己敲出的代码,所以写上了原创标题。但代码的内容并非本人原创。再次感谢这位作者的奉献。代码如何使用:在main函数中修改glutDisplayFunc函数参数就可以调用函数。有时候可能不能显示,这是正常的,那就修改main函数中的“glutInitDisplayMode”函数的参数。如果仍然不能使用那就把main函数中glutIdleFunc(&myIdle2);函数注释掉,应该就可以使用了。在代码不修改的情况下实现的效果如下图:在dos窗口中不停的按‘enter“键就可以看见三棱锥不停的旋转。,如果不按enter键,呵呵,会出现点问题。#include<gl/glut.h>
#include<iostream>
using namespace std;
#include<math.h>
const int n=200;
const GLfloat R =0.5f;
const GLfloat PI = 3.1415926;
void myDisplay(void )
{
int i;
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(0.5f);
glBegin(GL_POLYGON);
for(i=0;i<n;i++)
{
glVertex2f(R*cos(2*PI/n*i),R*sin(2*PI/n*i) );
}
glEnd();
//glRectf(-0.5f,-0.5f,0.5f,0.5f);
//glBegin(GL_POINTS);
//glBegin(GL_LINES);
//glVertex2f(0.0f,0.0f);
//glVertex2f(0.5f,0.5f);
//glEnd();
glFlush();
}
const GLfloat factor = 1.0f;
void myDisplay2()
{
GLfloat x;
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(5.0f);
glBegin(GL_LINES);
glVertex2f(-1.0f,0.0f);
glVertex2f(1.0f,0.0f);
glVertex2f(0.0f,-1.0f);
glVertex2f(0.0f,1.0f);
glEnd();
glFlush();
glBegin(GL_LINE_STRIP);
for(x = -1.0f/factor;x<1.0f/factor; x+= 0.01f);
{
glVertex2f(x*factor,sin(x)*factor);
}
glEnd();
glFlush();
}
void myDisplay3()
{
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(5.0f);
glBegin(GL_POINTS);
glVertex2f(0.0f,0.0f);
glVertex2f(0.5f,0.5f);
glEnd();
glFlush();
}
void myDisplay4()
{
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_LINE_STIPPLE);
glLineStipple(2,0xf0f);
glLineWidth(10.0f);
glBegin(GL_LINES);
glVertex2f(0.0f,0.0f);
glVertex2f(0.5f,0.5f);
glEnd();
glFlush();
}
void myDisplay5()
{
glClear(GL_COLOR_BUFFER_BIT);
glPolygonMode(GL_FRONT,GL_FILL);
glPolygonMode(GL_BACK,GL_LINE);
glFrontFace(GL_CCW);
glBegin(GL_POLYGON);
glVertex2f(-0.5f,-0.5f);
glVertex2f(0.0f,-0.5f);
glVertex2f(0.0f,0.0f);
glVertex2f(-0.5f,0.0f);
glEnd();
glFlush();
}
static int i;
void myDisplay6()
{
glClear(GL_COLOR_BUFFER_BIT);
//glColor3f(0.0f,1.0f,1.0f);
glIndexi(2);
glRectf(-0.5f,-0.5f,0.5f,0.5f);
glFlush();
}
void myDisplay7()
{
glClearColor(1.0f,0.0f,0.0f,0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
cout<<i++<<endl;
}
const GLdouble pi = 3.14;
void myDisplay8()
{
//glShadeModel(GL_FLAT);
glShadeModel(GL_SMOOTH);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLE_FAN);
glColor3f(1.0f,1.0f,1.0f);
glVertex2f(0.0f,0.0f);
for(int i=0;i<=8;++i)
{
glColor3f(i&0x04,i&0x02,i&0x01);
glVertex2f(cos(i*pi/4),sin(i*pi/4) );
}
glEnd();
glFlush();
}
int day = 200;
void myDisplay9()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(75,1,1,400000000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,-200000000,200000000,0,0,0,0,0,1);
glColor3f(1.0f,0.0f,0.0f);
glutSolidSphere(69600000,20,20);
glColor3f(0.0f,0.0f,1.0f);
glRotatef(day/360.0*360,0.0f,0.0f,-1.0f);
glTranslatef(150000000,0.0f,0.0f);
glutSolidSphere(15000000,20,20);
glColor3f(1.0f,1.0f,0.0f);
glRotatef(day/30.0*360 -day/360.0*350.0,0.0f,0.0f,-1.0f);
glTranslatef(380000000,0.0f,0.0f);
glutSolidSphere(4345000,20,20);
//day++;
glFlush();
glutSwapBuffers();
}
void myIdle(void)
{
++day;
if(day>=360)
day = 0;
myDisplay();
}
const int width = 400;
const int height = 400;
static GLfloat angle = 0.0f;
void myDisplay10(void )
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0f,1.0f,1.0f,20.0f);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0.0, 5.0, -10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); ;
GLfloat sunLightPositon[] = {0.0f,0.0f,0.0f,1.0f};
GLfloat sunLightAmbient[] = {0.0f,0.0f,0.0f,1.0f};
GLfloat sunLightDiffuse[] = {1.0f,1.0f,1.0f,1.0f};
GLfloat sunLightSpecular[] = { 1.0f,1.0f,1.0f,1.0f};
glLightfv(GL_LIGHT0,GL_POSITION,sunLightPositon);
glLightfv(GL_LIGHT0,GL_AMBIENT,sunLightAmbient);
glLightfv(GL_LIGHT0,GL_DIFFUSE ,sunLightDiffuse);
glLightfv(GL_LIGHT0,GL_SPECULAR ,sunLightSpecular);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
//定义太阳的材质
{
GLfloat sunMatAmbient[] = {0.0f,0.0f,0.0f,1.0f};
GLfloat sunMatDiffuse[] = {0.0f,0.0f,0.0f,1.0f};
GLfloat sunMatSpecular[] = { 0.0f,0.0f,0.0f,1.0f};
GLfloat sunMatEmission[] = {0.5f,0.0f,0.0f,1.0f};
GLfloat sunMatShininess = 0.0f;
glMaterialfv(GL_FRONT,GL_AMBIENT,sunMatAmbient);
glMaterialfv(GL_FRONT,GL_DIFFUSE,sunMatDiffuse);
glMaterialfv(GL_FRONT,GL_SPECULAR,sunMatSpecular);
glMaterialfv(GL_FRONT,GL_EMISSION,sunMatEmission);
glMaterialf(GL_FRONT,GL_SHININESS,sunMatShininess);
glutSolidSphere(2.0,40,32);
}
//定义地球的材质,并绘制地球
{
GLfloat earthMatAmbient[] = { 0.0f,0.0f,0.5f,1.0f};
GLfloat earthMatDiffuse[] = { 0.0f,0.0f,0.5f,1.0f};
GLfloat earthMatSpecular[] = {0.0f,0.0f,1.0f,1.0f};
GLfloat earthMatEmission[] = {0.0f,0.0f,0.0f,1.0f};
GLfloat earthMatShiniess = 30.f;
glMaterialfv(GL_FRONT,GL_AMBIENT,earthMatAmbient);
glMaterialfv(GL_FRONT,GL_DIFFUSE,earthMatDiffuse);
glMaterialfv(GL_FRONT,GL_SPECULAR,earthMatSpecular);
glMaterialfv(GL_FRONT,GL_EMISSION,earthMatEmission);
glMaterialf(GL_FRONT,GL_SHININESS,earthMatShiniess);
glRotatef(angle,0.0f,-1.0f,0.0f);
glTranslatef(5.0f,0.0f,0.0f);
glutSolidSphere(2.0,40,32);
}
glutSwapBuffers();
glFlush();
}
void myDisplay11()
{
glClearColor(1.0f,0.0f,0.0f,0.0f);
glClear(GL_COLOR_BUFFER_BIT);
const int segments = 100;
const GLfloat pi = 3.14;
glLineWidth(10.0);
glBegin(GL_LINE_LOOP);
for(int i=0;i<segments;i++)
{
GLfloat tmp = 2*pi*i/segments;
glVertex2f(cos(tmp),sin(tmp) );
//glVertex2f(3,4);
}
glEnd();
glFlush();
}
void myDisplay12()
{
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_LINE_STIPPLE);
glLineStipple(2,0xf0f);
glLineWidth(10.0f);
glBegin(GL_LINES);
glVertex2f(0.0f,0.0f);
glVertex2f(0.5f,0.5f);
glEnd();
glFlush();
}
#define ColoredVertex(c,v) do{glColor3fv(c);glVertex3fv(v);} while(0)
//GLfloat angle = 0.0f;
void myDisplay13(void )
{
static int list = 0;
if(list == 0)
{
GLfloat PointA[] = {0.5f,-sqrt(6.0f)/12,-sqrt(3.0f)/6};
GLfloat PointB[] = { -0.5f,-sqrt(6.0f)/12,-sqrt(3.0f)/6};
GLfloat PointC[] = { 0.0f,-sqrt(6.0f)/12,sqrt(3.0f)/3};
GLfloat PointD[] = { 0.0f,sqrt(6.0f)/4,0};
GLfloat ColorR[] = {1,0,0};
GLfloat ColorG[] = {0,1,0};
GLfloat ColorB[] = {0,0,1};
GLfloat ColorY[] = {1,1,0};
list = glGenLists(1);
glNewList(list,GL_COMPILE);
glBegin(GL_TRIANGLES);
//平面ABC
ColoredVertex(ColorR,PointA);
ColoredVertex(ColorG,PointB);
ColoredVertex(ColorB,PointC);
//ACD
ColoredVertex(ColorR,PointA);
ColoredVertex(ColorB,PointC);
ColoredVertex(ColorY,PointD);
//CBD
ColoredVertex(ColorB,PointC);
ColoredVertex(ColorG,PointB);
ColoredVertex(ColorY,PointD);
//BAD
ColoredVertex(ColorG,PointB);
ColoredVertex(ColorR,PointA);
ColoredVertex(ColorY,PointD);
glEnd();
glEndList();
glEnable(GL_DEPTH_TEST);
}//if
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle,1,0.5,0);
glCallList(list);
glPopMatrix();
glutSwapBuffers();
}
void myIdle2(void)
{
++angle;
if(angle>=360)
angle = 0;
myDisplay13();
//cout<<"fuck"<<endl;
int a;
//in>>a;
getchar();
}
int main( int argc ,char *argv[])
{
glutInit(&argc,argv);
//glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
glutInitWindowPosition(100,100);
glutCreateWindow("jiazengshen ");
glutDisplayFunc(&myDisplay13);
glutIdleFunc(&myIdle2);
glutMainLoop();
return 0;
}
9935

被折叠的 条评论
为什么被折叠?



