opengl 中投影有两种:透视投影 和 正投影
1. 透视投影 :(视景体为棱台,产生的3d图像更具真实感)
glFrustum(GLdouble left,GLdouble right,GLdouble bottom,GLdouble top,GLdouble near,GLdouble far):
left , right , bottom , top这四个参数为剪裁面的范围,near为近平面的值,far 为远平面的值。(near 和far只能取正值)
gluPerspective(GLdouble fovy,GLdouble aspect,GLdouble near,GLdouble far):
fovy为视角大小,aspect为屏幕的width/height ,near 为近平面的值,far 为远平面的值。(near和far也只能取正)
2. 正投影:(视景体为长方体或正方体,真实感不及透视投影)
glOrtho(GLdouble left,GLdouble right,GLdouble bottom,GLdouble top,GLdouble near,GLdouble far):
left , right , bottom , top这四个参数为剪裁面的范围,near为近平面的值,far 为远平面的值。
实例:glFrustum()
#include<windows.h>
#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>
#include<iostream>
using namespace std;
GLfloat width=600,height=600;
void myInit(void)
{
glClearColor(0.0,0.0,0.0,1.0);
}
void idle(void)
{
glutPostRedisplay();
}
void display()
{
GLfloat a=glutGet(GLUT_ELAPSED_TIME);
a/=20.0;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(0.0,1.0,0.0);
glLoadIdentity();
gluLookAt(0.0,150.0,300.0,0.0,0,0.0,0.0,1.0,0.0);
glPushMatrix();
glRotatef(a,1.0,0.0,0.0);
glRotatef(a,0.0,1.0,0.0);
glutWireSphere(150.0,15,15);
glPopMatrix();
glutSwapBuffers();
}
void changeSize(int w,int h)
{
glViewport(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-20.0,20.0,-20.0,20.0,20.0,550.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH|GLUT_RGB);
glutInitWindowPosition(300,100);
glutInitWindowSize(width,height);
glutCreateWindow("glFrustum test");
myInit();
glutDisplayFunc(display);
glutReshapeFunc(changeSize);
glutIdleFunc(idle);
glutMainLoop();
return 0;
}
实例:gluPerspective()
#include<windows.h>
#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>
#include<iostream>
using namespace std;
GLfloat width=600,height=600;
GLfloat x=180.0,y=180.0,z=180.0;
GLfloat xRotate=0.0f,yRotate=0.0f;
void myInit(void)
{
glClearColor(0.0,0.0,0.0,1.0);
}
void drawAxis()
{
glColor3f(1.0,1.0,1.0);
glPushMatrix();
glBegin(GL_LINES);
glVertex3f(-x,0,0);
glVertex3f(x,0,0);
glVertex3f(0,-y,0);
glVertex3f(0,y,0);
glVertex3f(0,0,-z);
glVertex3f(0,0,z);
glEnd();
glPointSize(5.0);
glBegin(GL_POINTS);
glColor3f(1.0,0.0,0.0);
glVertex3f(x,0,0);
glColor3f(0.0,1.0,0.0);
glVertex3f(0,y,0);
glColor3f(0.0,0.0,1.0);
glVertex3f(0,0,z);
glEnd();
glPopMatrix();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,1.0,0.0);
glLoadIdentity();
gluLookAt(0.0,150.0,300.0,0.0,0,0.0,0.0,1.0,0.0);
glPushMatrix();
glRotated(xRotate,1.0,0.0,0.0);
glRotated(yRotate,0.0,1.0,0.0);
glutWireCube(100);
drawAxis();
glPopMatrix();
glFlush();
}
void specialKey(int key,int x,int y)
{
if(key==GLUT_KEY_UP){
xRotate-=5.0f;
}else if(key==GLUT_KEY_DOWN){
xRotate+=5.0f;
}else if(key==GLUT_KEY_LEFT){
yRotate-=5.0f;
}else if(key==GLUT_KEY_RIGHT){
yRotate+=5.0f;
}
glutPostRedisplay();
}
void changeSize(int w,int h)
{
glViewport(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(80,width/height,0,200.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(300,100);
glutInitWindowSize(width,height);
glutCreateWindow("gluPerspective test");
myInit();
glutDisplayFunc(display);
glutReshapeFunc(changeSize);
glutSpecialFunc(specialKey);
glutMainLoop();
return 0;
}
实例:glOrtho()
#include<windows.h>
#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>
#include<iostream>
using namespace std;
GLfloat width=600,height=600;
void myInit(void)
{
glClearColor(0.0,0.0,0.0,1.0);
}
void idle(void)
{
glutPostRedisplay();
}
void display()
{
GLfloat a=glutGet(GLUT_ELAPSED_TIME);
a/=20.0;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(0.0,1.0,0.0);
glPushMatrix();
glRotatef(a,1.0,0.0,0.0);
glRotatef(a,0.0,1.0,0.0);
glutWireSphere(60,15,15);
glPopMatrix();
glutSwapBuffers();
}
void changeSize(int w,int h)
{
glViewport(0,0,w,h);
GLfloat rate=w/h;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-300*rate,300*rate,-300,300,-300,300);
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowPosition(300,100);
glutInitWindowSize(width,height);
glutCreateWindow("glortho test");
myInit();
glutDisplayFunc(display);
glutReshapeFunc(changeSize);
glutIdleFunc(idle);
glutMainLoop();
return 0;
}