視點跟蹤一個節點運動的原理是:把物體矩陣取反。。
#include <stdlib.h>
#include <stdio.h>
#include <gl/glut.h>
#include <math.h>
#include <time.h>
GLfloat angle = 0.0;
GLfloat theta = 0.0, vp = 6;//vp視點的位置
GLfloat cenx, ceny;
const int PI = 3.1415926;
const double N = 200.0;
int myWin;
const int MAX_MAP = 500;
int myMap[MAX_MAP][MAX_MAP];
inline double aToR(double x)
{
return x/180.0 * 3.1415926;
}
void openLight()
{
float light_position[4] = {30, 30, 30, 0};
float light_ambient[4] = {1.0, 1.0, 1.0, 1.0};
float light_diffuse[4] = { 1.0, 1.0, 1.0, 1.0};
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glEnable( GL_LIGHTING );
glEnable( GL_LIGHT0 );
}
void cube()
{
typedef float point3[3];
typedef int edge[2];
typedef int face[4];
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 normals[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 cube[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}
};
//GLdouble myClipPlane[] = {1.0, 1.0, 0.0, -1.0};
//glClipPlane(GL_CLIP_PLANE0, myClipPlane);
///glEnable(GL_DEPTH_TEST);
//glEnable(GL_CLIP_PLANE0);
glBegin(GL_QUADS);
for(int face = 0; face < 6; face ++)
{
glNormal3fv(normals[face]);
for(int edge = 0; edge < 4; edge ++)
{
glVertex3fv(vertices[edges[cube[face][edge]][0]]);
}
}
glEnd();
//glDisable(GL_CLIP_PLANE0);
}
void qumian()
{
glLineWidth(10);
glBegin(GL_LINES);
glVertex3f(40, 0, 0);
glVertex3f(0, 20, 0);
glEnd();
}
void display(void)
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glClearColor(0, 0, 0.5, 1);
openLight();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 0, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0);
glTranslatef(0, 0, -12);
//glTranslatef(0, -5, 0);
glRotatef(-angle, 0.0, 0.0, 1.0);
//glScalef(10, 10, 10);
glPushMatrix();
glRotatef(angle, 0.0, 0.0, 1.0);
//glTranslatef(0, 5, 0);
cube();
glPopMatrix();
glPushMatrix();
glTranslatef(2, 2, -9);
cube();
glPopMatrix();
glPushMatrix();
glTranslatef(2, 3, -2);
cube();
glPopMatrix();
glPushMatrix();
glTranslatef(-1, 3, 2);
cube();
glPopMatrix();
glutSwapBuffers();
}
void reshape(int w, int h)
{
int min = w < h?w:h;
int cenx = w * 0.5;
int ceny = h * 0.5;
glViewport(cenx - min*0.5, ceny - min*0.5, (GLsizei)min, (GLsizei)min);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, 1, 0.1, 300.0);
glutPostRedisplay();
}
void iterationStep(void)
{
angle += 1.0;
cenx = cos(angle / 180 * 3.14159) * vp + vp;
ceny = sin(angle / 180 * 3.14159) * vp;
}
void animate(void)
{
iterationStep();
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("畫球");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(animate);
glutMainLoop();
}
最終效果圖如下所示:
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

