【OpenGL】画立方体

时间:2023-03-08 18:34:44
【OpenGL】画立方体

编写一个程序,该程序运行时可以用鼠标的一个按键调整立方体的方向,用另一个按键平移立方体,用第三个按键缩放立方体。

这是题目,我的程序不一定完全按照这个来。初学OpenGL,对那一堆坐标系表示十分混乱,慢慢看吧,有点头绪了。

(一)

 #include <gl/glut.h>
#include <math.h>
#define GL_PI 3.1415f GLfloat xRot = -35.0f;
GLfloat yRot = 15.0f;
GLfloat xMov = 0.0f;
GLfloat winW = 0.0f;
GLfloat winH = 0.0f;
GLfloat zoom = 1.0f; void RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT); glPushMatrix();
glLoadIdentity();
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f); // 底面
glBegin(GL_LINE_LOOP);
glVertex3f(0.0f+xMov, 0.0f, 0.0f);
glVertex3f(1.0f+xMov, 0.0f, 0.0f);
glVertex3f(1.0f+xMov, 0.0f, 1.0f);
glVertex3f(0.0f+xMov, 0.0f, 1.0f);
glEnd(); // 顶面
glBegin(GL_LINE_LOOP);
glVertex3f(0.0f+xMov, 1.0f, 1.0f);
glVertex3f(0.0f+xMov, 1.0f, 0.0f);
glVertex3f(1.0f+xMov, 1.0f, 0.0f);
glVertex3f(1.0f+xMov, 1.0f, 1.0f);
glEnd(); // 背面
glBegin(GL_LINE_LOOP);
glVertex3f(0.0f+xMov, 0.0f, 0.0f);
glVertex3f(1.0f+xMov, 0.0f, 0.0f);
glVertex3f(1.0f+xMov, 1.0f, 0.0f);
glVertex3f(0.0f+xMov, 1.0f, 0.0f);
glEnd(); // 前面
glBegin(GL_LINE_LOOP);
glVertex3f(1.0f+xMov, 0.0f, 1.0f);
glVertex3f(0.0f+xMov, 0.0f, 1.0f);
glVertex3f(0.0f+xMov, 1.0f, 1.0f);
glVertex3f(1.0f+xMov, 1.0f, 1.0f);
glEnd(); // 右面
glBegin(GL_LINE_LOOP);
glVertex3f(1.0f+xMov, 0.0f, 0.0f);
glVertex3f(1.0f+xMov, 0.0f, 1.0f);
glVertex3f(1.0f+xMov, 1.0f, 1.0f);
glVertex3f(1.0f+xMov, 1.0f, 0.0f);
glEnd(); // 左面
glBegin(GL_LINE_LOOP);
glVertex3f(0.0f+xMov, 0.0f, 0.0f);
glVertex3f(0.0f+xMov, 0.0f, 1.0f);
glVertex3f(0.0f+xMov, 1.0f, 1.0f);
glVertex3f(0.0f+xMov, 1.0f, 0.0f);
glEnd(); glPopMatrix();
glFlush();
}
void OnReshape(int w, int h)
{
GLfloat aspectRatio = (GLfloat)w/(GLfloat)h;
winW = w;
winH = h; glViewport(,,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
{
glOrtho(-3.0f*zoom, 3.0f*zoom, -3.0f*zoom/aspectRatio, 3.0f*zoom/aspectRatio, -10.0f*zoom, 10.0f*zoom);
}
else{
glOrtho(-3.0f*zoom*aspectRatio, 3.0f*zoom*aspectRatio, -3.0f*zoom, 3.0f*zoom, -10.0f*zoom, 10.0f*zoom);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void OnMouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
xRot += ;
yRot += ;
glutPostRedisplay();
}
else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
{
xMov -= 0.1;
glutPostRedisplay();
}
else if (button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)
{
zoom += 0.1;
OnReshape(winW,winH);
glutPostRedisplay();
}
}
void OnKeyUpDown(int key, int x, int y)
{
if (key == GLUT_KEY_UP){
zoom -= 0.1;
}
else if (key == GLUT_KEY_DOWN){
zoom += 0.1;
}
OnReshape(winW,winH);
glutPostRedisplay();
} void SetupRC()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f);
} void main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutCreateWindow("Cube");
glutDisplayFunc(RenderScene);
glutReshapeFunc(OnReshape);
glutMouseFunc(OnMouse);
glutSpecialFunc(OnKeyUpDown); SetupRC(); glutMainLoop();
}

这个程序略长,显得有点笨。手工实现了平移和放大缩小的功能,值得记录一下。

(二)

 #include <windows.h>
#include<gl/glut.h>
#include <math.h>
#include<stdio.h> GLfloat angle=0.0f;
GLfloat translate_x=0.0f;
GLfloat zoom=1.0f; void myDisplay()
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0f, 1.0f, 1.0f);
glPushMatrix();
{
glMatrixMode(GL_MODELVIEW); //切换到模型视图矩阵
glLoadIdentity();
glRotatef(angle,1.0f,0.0f,0.0f);
glRotatef(angle, 0.0f, 1.0f, 0.0f);
glTranslatef(translate_x,0.0f,0.0f);
glScalef (zoom, zoom, zoom);
glutWireCube(1.5); //画立方体
}
glPopMatrix();
glFlush();
} void reshape(int w,int h)
{
glViewport (, , (GLsizei) w, (GLsizei) h); //调整视口位置和大小
glMatrixMode (GL_PROJECTION);//切换到投影矩阵
glLoadIdentity();//加载单位阵至投影矩阵
if (w <= h){
glOrtho(-3.0f, 3.0f, -3.0f/(w/h), 3.0f/(w/h), -3.0f, 3.0f);
}
else{
glOrtho(-3.0f*w/h, 3.0f*w/h, -3.0f, 3.0f, -3.0f, 3.0f);
} glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
} void OnMouse(int button, int state, int x, int y)
{
//鼠标左键
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
angle += 10.0f;
glutPostRedisplay();
}
//鼠标右键
else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
{
translate_x+=0.1f;
glutPostRedisplay();
}
//鼠标滚轮
else if (state == GLUT_UP && button == GLUT_WHEEL_DOWN)
{
if(zoom>)
zoom-=0.1f;
glutPostRedisplay();
}
else if(state == GLUT_UP && button == GLUT_WHEEL_UP)
{
zoom+=0.1f;
glutPostRedisplay();
}
} int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(,);
glutInitWindowSize(,);
glutCreateWindow("cube"); glutDisplayFunc(myDisplay);
glutReshapeFunc(reshape);
glutMouseFunc( OnMouse );
glutMainLoop();
return ;
}

采用了OpenGL的一些库函数实现,投影还是采用平行投影,表示对透视投影函数gluPerspective以及视点变化函数gluLookAt不是很清楚。

(三)

 #include <windows.h>
#include<gl/glut.h>
#include <math.h>
#include<stdio.h> GLfloat angle=0.0f;
GLfloat translate_x=0.0f;
GLfloat zoom=1.0f; void myDisplay()
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0f, 1.0f, 1.0f);
glPushMatrix(); glMatrixMode(GL_MODELVIEW); //切换到模型视图矩阵
glLoadIdentity();
gluLookAt (0.0f, 0.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); //设置相机参数
glRotatef(angle, 0.0f, 1.0f, 0.0f);
glTranslatef(translate_x,0.0f,0.0f);
glScalef (zoom, zoom, zoom);
glutWireCube(0.5); //画立方体 glPopMatrix();
glFlush();
} void reshape(int w,int h)
{
glViewport (, , (GLsizei) w, (GLsizei) h); //调整视口位置和大小
glMatrixMode (GL_PROJECTION);//切换到投影矩阵
glLoadIdentity();//加载单位阵至投影矩阵
gluPerspective(,w/h,,);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
} void OnMouse(int button, int state, int x, int y)
{
//鼠标左键
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
angle += 10.0f;
glutPostRedisplay();
}
//鼠标右键
else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
{
//xMov -= 0.1;
translate_x+=0.1f;
glutPostRedisplay();
}
//鼠标滚轮
else if (state == GLUT_UP && button == GLUT_WHEEL_DOWN)
{
if(zoom>)
zoom-=0.1f;
glutPostRedisplay();
}
else if(state == GLUT_UP && button == GLUT_WHEEL_UP)
{
zoom+=0.1f;
glutPostRedisplay();
}
} int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(,);
glutInitWindowSize(,);
glutCreateWindow("cube"); glutDisplayFunc(myDisplay);
glutReshapeFunc(reshape);
glutMouseFunc( OnMouse );
glutMainLoop();
return ;
}

用的是透视投影。我的理解是gluPerspective()和gluLookAt()要配合使用,二者中的相机位置要一致。