openGL——03、三维透视投影
2018-04-07 本文已影响12人
长青之木
需求
使用三维透视投影实现下面的透视效果:
透视投影.png原理
在三维空间的xy平面上画螺旋线,再用透视投影的方法投影到与xy平面不平行的平面上。
代码
/**
* @date: 2017.06.05
*/
#include <GL/glut.h>
GLfloat winWidth = 600, winHeight = 600;
GLfloat x0 = 100, y0 = 50, z0 = 50;
GLfloat xref = 50, yref = 50, zref = 0.0;
GLfloat Vx = 0.0, Vy = 1.0, Vz = 0.0;
GLfloat xwMin = -40, ywMin =-60, xwMax = 40, ywMax = 60;
GLfloat dnear = 25.0, dfar = 125.0;
void init(void) {
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(x0, y0, z0, xref, yref, zref, Vx, Vy, Vz);
glMatrixMode(GL_PROJECTION);
glFrustum(xwMin, xwMax, ywMin, ywMax, dnear, dfar);
}
void displayFcn(void) {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 1.0, 0.0);
glPolygonMode(GL_FRONT, GL_FILL);
glPolygonMode(GL_BACK, GL_LINE);
glBegin(GL_QUADS);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(100.0, 0.0, 0.0);
glVertex3f(100.0, 100.0, 0.0);
glVertex3f(0.0, 100.0, 0.0);
glEnd();
glFlush();
}
void reshapeFcn(GLint newWidth, GLint newHeight) {
glViewport(0, 0, newWidth, newHeight);
winWidth = newHeight;
winHeight = newHeight;
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(50, 50);
glutInitWindowSize(winWidth, winHeight);
glutCreateWindow("perspectiver View of A Square");
init();
glutDisplayFunc(displayFcn);
glutReshapeFunc(reshapeFcn);
glutMainLoop();
}
githup链接:
https://github.com/lifeSo/OpenGLDemo/blob/master/01.get start/05.perspectiver view of square.c
https://github.com/lifeSo/OpenGLDemo/blob/master/01.get start/06.perspectiver view of spiral line.c