1. 程式人生 > >OpenGL實現攝像機漫遊/三次貝塞爾曲線

OpenGL實現攝像機漫遊/三次貝塞爾曲線

通過openglAPI實現攝像機漫遊,以及觀察生成的貝塞爾曲面

#include "stdafx.h"
#include <GL/glut.h>
#include <stdlib.h>
#include<iostream>
using namespace std;
void init(void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0); //背景黑色
}
float posz;
float posx;
float posy;
float speed;
float centerx;
float centery;
float centerz;
float
cita = 0; #define PI 3.1415926 GLfloat ctrlPoints[3][3][3] = { { { -4.0f, 0.0f, 4.0f }, { -2.0f, 4.0f, 4.0f }, { 4.0f, 0.0f, 4.0f } }, { { -4.0f, 0.0f, 0.0f }, { -2.0f, 4.0f, 0.0f }, { 4.0f, 0.0f, 0.0f } }, { { -4.0f, 0.0f, -4.0f }, { -2.0f, 4.0f, -4.0f }, { 4.0f, 0.0f, -4.0f } } }; void drawgrid() { glMatrixMode(GL_MODELVIEW); glPushMatrix(); //glLoadIdentity();
glBegin(GL_LINES); for (int i = 0; i < 50; i+=1) { glColor3f(1.0, 1.0, 1.0); glVertex3f(-25+i,0,50); //定點座標範圍 glVertex3f(-25+i,0,-50); } for (int i = 0; i < 50; i += 1) { glColor3f(1.0, 1.0, 1.0); glVertex3f(-50, 0, -25 + i); //定點座標範圍 glVertex3f(50
+ i, 0, -25 + i); } glEnd(); glPopMatrix(); } void DrawPoints() { glPointSize(2.5f); glBegin(GL_POINTS); for (int i = 0; i < 3; ++i)for (int j = 0; j < 3; j++) { glVertex3fv(ctrlPoints[i][j]); } glEnd(); } void display(void) { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 1.0, 1.0); //畫筆白色 glLoadIdentity(); gluLookAt(posx, posy,posz, centerx, centery, centerz, 0.0, 1.0, 0.0); drawgrid(); glPushMatrix(); glTranslatef(0, 0, 10); glutWireTeapot(2); glPopMatrix(); glPushMatrix(); glTranslatef(5, 0, 10); glutWireTeapot(2); glPopMatrix(); glPushMatrix(); glShadeModel(GL_SMOOTH ); glPolygonMode(GL_FRONT, GL_FILL); glBegin(GL_TRIANGLES); glColor3f(1,0,0); glVertex3f(2, 0, 0); glNormal3f(0, 0, 1); glColor3f(0, 1, 0); glVertex3f(0, 2, 0); glNormal3f(0, 0, 1); glColor3f(0, 0, 1); glVertex3f(0, 0, 0); glNormal3f(0, 0, 1); glEnd(); glPopMatrix(); glEnable(GL_AUTO_NORMAL); glColor3f(1, 0, 0); glMap2f(GL_MAP2_VERTEX_3, 0, 10, 3, 3, 0, 10, 9, 3, &ctrlPoints[0][0][0]); glEnable(GL_MAP2_VERTEX_3); glMapGrid2f(10,0,10.0f,10,0,10); glEvalMesh2(GL_LINE, 0, 10, 0, 10); DrawPoints(); glutSwapBuffers(); cout << posx << " "<<posy<<" "<<posz<<endl; cout << centerx << " " << centery << " " << centerz << endl; cout << cita << endl; cout << endl; } void keyboard(unsigned char key,int x,int y) { cout << key << " "<<x<<" "<<y<<endl; int r = 5; switch (key) { case 'w': posx -= speed*sin(cita/180.0f*PI); posz += speed*cos(cita / 180.0f*PI); centerx = posx -r* sin(cita / 180.0f*PI); centerz = posz + r* cos(cita / 180.0f*PI); break; case 'v': posx -= r*sin(cita / 180.0f*PI); posz += r*cos(cita / 180.0f*PI); //centerx = posx - r* sin(cita / 180.0f*PI); //centerz = posz + r* cos(cita / 180.0f*PI); break; case's': posx += speed*sin(cita / 180.0f*PI); posz -= speed*cos(cita / 180.0f*PI); centerx = posx -r*sin(cita / 180.0f*PI); centerz = posz + r* cos(cita / 180.0f*PI); break; case'a': posx += speed*cos(cita / 180.0f*PI); posz += speed*sin(cita / 180.0f*PI); centerx = posx - r* sin(cita / 180.0f*PI); centerz = posz + r*cos(cita / 180.0f*PI); break; case'd': posx -= speed*cos(cita / 180.0f*PI); posz -= speed*sin(cita / 180.0f*PI); centerx = posx -r*sin(cita / 180.0f*PI); centerz = posz + r* cos(cita / 180.0f*PI); break; //case 'i': // centery += speed; // break; //case'k': // centery-= speed; // break; case'j': cita--; if (cita == 360) cita = 0; centerx = posx -r*sin(cita / 180.0f*PI); centerz = posz + r* cos(cita / 180.0f*PI); break; case'l': cita++; if (cita == -360) cita = 0; centerx = posx - r* sin(cita / 180.0f*PI); centerz = posz + r*cos(cita / 180.0f*PI); break; } glutPostRedisplay(); } void reshape(int w, int h) { glViewport(0, 0, (GLsizei)w, (GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); } int main(int argc, char** argv) { centerx = 5; centery = 0; centerz = 10; posx = posy = posz = 0; posy = 1; posz = 20; speed = 0.5; glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(500, 500); glutInitWindowPosition(100, 100); glutCreateWindow(argv[0]); init(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutMainLoop(); return 0; }

結果圖
這裡寫圖片描述