#include
#include
#include
#include
#define max 100
GLfloat angle =0.0;
GLfloat theta =0.0,vp=30.0;
GLfloat v[4][4],u[4][4];
GLuint selectBuf[max];
GLuint hit;
typedef int edge[2];
typedef int face[4];
typedef GLfloat point3[3];
int myWin,w,h;
void myinit(void){
glEnable(GL_DEPTH_TEST);
glClearColor(0.6,0.6,0.6,1.0);
point3 direction0={-1.0,-1.0,-1.0};
point3 direction1={1.0,1.0,1.0};
point3 direction2={1.0,1.0,-1.0};
GLfloat light_pos0[]={2.0,2.0,2.0,1.0};
GLfloat light_col0[]={1.0,0.0,0.0,1.0};
GLfloat amb_col0[]={0.3,0.0,0.0,1.0};
GLfloat light_pos1[]={-2.0,-2.0,-2.0,1.0};
GLfloat light_col1[]={0.0,1.0,0.0,1.0};
GLfloat amb_col1[]={0.0,0.3,0.0,1.0};
GLfloat light_pos2[]={-2.0,-2.0,2.0,1.0};
GLfloat light_col2[]={0.0,0.0,1.0,1.0};
GLfloat amb_col2[]={0.0,0.0,10.3,.0};
glLightfv(GL_LIGHT0,GL_POSITION,light_pos0);
glLightfv(GL_LIGHT0,GL_AMBIENT,amb_col0);
glLightfv(GL_LIGHT0,GL_SPECULAR,light_col0);
glLightfv(GL_LIGHT1,GL_POSITION,light_pos1);
glLightfv(GL_LIGHT1,GL_AMBIENT,amb_col1);
glLightfv(GL_LIGHT1,GL_SPECULAR,light_col1);
glLightfv(GL_LIGHT2,GL_POSITION,light_pos2);
glLightfv(GL_LIGHT2,GL_AMBIENT,amb_col2);
glLightfv(GL_LIGHT2,GL_SPECULAR,light_col2);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION,direction0);
glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,50.0);
glLightf(GL_LIGHT0,GL_SPOT_EXPONENT,2.0);
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION,direction1);
glLightf(GL_LIGHT1,GL_SPOT_CUTOFF,50.0);
glLightf(GL_LIGHT1,GL_SPOT_EXPONENT,2.0);
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION,direction2);
glLightf(GL_LIGHT1,GL_SPOT_CUTOFF,50.0);
glLightf(GL_LIGHT1,GL_SPOT_EXPONENT,2.0);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,1.0);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHT2);
}
point3 vertices[8]={
{-1.0,-1.0,-1.0},
{-1.0,-1.0,1.0},
{-1.0,1.0,-1.0},
{-1.0,1.0,1.0},
{1.0,-1.0,-1.0},
{1.0,-1.0,1.0},
{1.0,1.0,-1.0},
{1.0,1.0,1.0}
};
point3 nomals[6]={
{-1.0, 0.0,0.0},
{0.0, 0.0, 1.0},
{0.0, 1.0, 0.0},
{0.0, 0.0, -1.0},
{ 0.0, -1.0,0.0},
{1.0, 0.0, 0.0}
};
edge edges[24]={
{0,1},{1,3},{3,2},{2,0},
{0,4},{1,5},{3,7},{2,6},
{4,5},{5,7},{7,6},{6,4},
{1,0},{3,1},{2,3},{0,2},
{4,0},{5,1},{7,3},{6,2},
{5,4},{7,5},{6,7},{4,6}
};
face cubev[6]={
{0,1,2,3}, {5,9,18,13},
{14,6,10,19}, {7,11,16,15},
{4,8,17,12}, {22,21,20,23}
};
void cube(void){
int face,edge;
GLfloat shininess[]={50.0};
GLfloat white[]={1.0,1.0,1.0,.50};
GLfloat mat_specular[]={0.8,0.8,0.8,1.0};
glMaterialfv(GL_FRONT,GL_AMBIENT,white );
glMaterialfv(GL_FRONT,GL_DIFFUSE,white );
glMaterialfv(GL_FRONT,GL_SHININESS, shininess);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
glBegin(GL_QUADS);
for (face=0;face<6;face++)
{glNormal3fv(nomals[face]);
for (edge=0;edge<4;edge++)
glVertex3fv(vertices[edges[cubev[face][edge]][0]]);}
glEnd();
}
void render(GLenum mode){
if (mode == GL_SELECT){
glLoadName(1);
}
glPushName(3);
glPushMatrix();
glTranslatef(1.0,1.0,-2.0);
glColor3fv(nomals[5]);
cube();
glPopMatrix();
glPopName();
if (mode == GL_SELECT)
glLoadName(2);
glPushName(3);
glPushMatrix();
glTranslatef(-1.0,-2.0,1.0);
glColor3fv(nomals[2]);
cube();
glPopMatrix();
glPopName();
glFlush();
glutSwapBuffers();
}
void display(void){
#define SCALE 10,0
int i,j;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.,1.,1.,300.);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(10.0,10.,10.0, 0.0,0.0,0.0, 0.0,1.0,0.0);
glPushMatrix();
render(GL_RENDER);
glPopMatrix();
glutSwapBuffers();
}
void reshape(int width ,int high){
w=width;h=high;
glutPostRedisplay();
}
void iterationStep(void){
if (angle>600)angle=0.0;
angle+=0.2;
}
void animate(void){
iterationStep();
glutPostRedisplay();
}
void
processHits(GLint hits, GLuint buffer[])
{
GLint i;
GLuint j, names, *ptr;
printf("\nI'm in processHits!\t");
printf("hits = %d\n", hits);
ptr = (GLuint *) buffer;
for (i = 0; i < hits; i++) { /* for each hit */
names = *ptr;
printf(" number of names for hit = %d\n", names);
ptr++;
printf(" z1 is %g;", (float) *ptr/0xffffffff);
ptr++;
printf(" z2 is %g\n", (float) *ptr/0xffffffff);
ptr++;
printf(" the name is ");
for (j = 0; j < names; j++) { /* for each name */
printf("%d ", *ptr);
ptr++;
}
printf("\n");
}
}
GLuint DoSelect(GLint x,GLint y){
int dy,viewport[4];
GLint hits,temp;
dy=glutGet(GLUT_WINDOW_HEIGHT);
glGetIntegerv(GL_VIEWPORT,viewport);
glSelectBuffer(max,selectBuf);
(void )glRenderMode(GL_SELECT);
glInitNames();
glPushName(0);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPickMatrix((GLdouble)x,(GLdouble)(viewport[3]-y),4.0,4.0,viewport);
glClearColor(0.60,0.60,0.60,0.0);
glClear(GL_COLOR_BUFFER_BIT);
gluPerspective(60.0,1.0,1.0,300.0);
glMatrixMode(GL_MODELVIEW);
//这里不可有glLookAt()函数!
render(GL_SELECT);
glPopMatrix();
hits = glRenderMode(GL_RENDER);
processHits(hits, selectBuf);
if (hits<=0){printf("\nI'm exit form here!");return -1;}
return selectBuf[(hits-1)*4+3];
}
void mouse(int button,int state,int mouseX,int mouseY){
if (state==GLUT_DOWN){
printf("\nMouse at %d \t%d\n",mouseX,mouseY);
}
hit=DoSelect((GLint) mouseX,(GLint) mouseY);
printf("\n fouse on\t %d \n",hit);
glutPostRedisplay();
}
int main(int argc,char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB| GLUT_DEPTH);
glutInitWindowSize(500,500);
glutInitWindowPosition(50,50);
myWin= glutCreateWindow("Two Cube for Pikuping!");
myinit();
glutMouseFunc(mouse);
glutReshapeFunc(reshape);
glutIdleFunc(animate);
glutDisplayFunc(display);
glutMainLoop();
}
阅读(465) | 评论(0) | 转发(0) |