opengl实现用键盘使物体上下移动还有照相机的移动

zxiaolu1 2015-05-13 09:22:35
代码不知道哪里错了,运行结果不对,按键盘没有反应,哪位大神指点一下
// This assignment may cost you some efferts, so I give you some important HINTS, hope that may help you.
// Enjoy the coding and thinking, do pay more attention to the library functions used in OPENGL, such as how they are used? what are the parameters used? and why?

// 实验报告里面多写点感想,即对函数的理解,以及调用的顺序,步骤等。思考为什么是这样子调用的,为什么参数是这样子设置的?。。。等等你觉得值得研究的问题。
#include <stdlib.h>
#include "glut.h"

//定义变换控制变量
//float fTranslate;
float fRotate = 0.0f;
float fScale = 1.0f; // set inital scale value to 1.0f
float fRevolve=0.0f;
bool bPersp = false;
bool bAnim = false;//about旋转
bool bWire = false;
bool tAnim=false;
//定义窗口大小
int wHeight = 0;
int wWidth = 0;

//todo
//hint: some additional parameters may needed here when you operate the teapot

void Draw_Leg()
{
glScalef(1, 1, 3);//缩放
glutSolidCube(1.0);//创建实心立方体
}

void Draw_Scene()
{ //画茶壶
glPushMatrix();
glTranslatef(0, 0, 4+1);//移动
glRotatef(90, 1, 0, 0); //旋转。notice the rotation here, you may have a TRY removing this line to see what it looks like.
//todo; hint: when operate the teapot, you may need to change some parameters
//(0,5,0)
glutSolidTeapot(1);
glPopMatrix();
//画桌面
glPushMatrix();
glTranslatef(0, 0, 3.5);
glScalef(5, 4, 1);
glutSolidCube(1.0);
glPopMatrix();
//画四条腿
glPushMatrix();
glTranslatef(1.5, 1, 1.5);
Draw_Leg();
glPopMatrix();

glPushMatrix();
glTranslatef(-1.5, 1, 1.5);
Draw_Leg();
glPopMatrix();

glPushMatrix();
glTranslatef(1.5, -1, 1.5);
Draw_Leg();
glPopMatrix();

glPushMatrix();
glTranslatef(-1.5, -1,1.5);
Draw_Leg();
glPopMatrix();

}

void updateView(int width, int height)//切换投影方式
{
glViewport(0,0,width,height); // Reset The Current Viewport

glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix

float whRatio = (GLfloat)width/(GLfloat)height;

if (bPersp){
//todo when 'p' operation, hint: use FUNCTION gluPerspective
}
else
glOrtho(-3 ,3, -3, 3,-100,100);

glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
}

void reshape(int width, int height)
{
if (height==0) // Prevent A Divide By Zero By
{
height=1; // Making Height Equal One
}

wHeight = height;
wWidth = width;

updateView(wHeight, wWidth);
}

void idle()
{
glutPostRedisplay();//窗口重新绘制
}
float size=0.1f;
float eye[] = {0, 0, 8};
float center[] = {0, 0, 0};
float move[]={0,0,0};
//todo; hint: you may need another ARRAY when you operate the teapot
//用键盘操控图形
void key(unsigned char k, int x, int y)
{
switch(k)
{
case 27://27是ESC键
case 'q': {exit(0); break; }
case 'p': {bPersp = !bPersp; updateView(wHeight, wWidth);break; }
//旋转&暂停 现况&填充
case ' ': {bAnim = !bAnim; break;}
case 'o': {bWire = !bWire; break;}
//相机移动
case 'a': {//todo, hint: eye[] and center[] are the keys to solve the problems
eye[]={3,0,8};
break;
}
case 'd': {//todo
eye[]={-3,0,8};
break;
}
case 'w': {//todo
eye[]={0,3,8};
//center[]={0,13,0};
break;
}
case 's': {//todo
eye[]={0,-3,8};
//center[]={0,13,0};
break;
}
case 'z': {//todo
eye[]={8,-8,-8};
center[]={0,-8,0};
break;
}
case 'c': {//todo
eye[]={-8,-8,8};
center[]={0,-8,0};
break;
}

//茶壶相关操作
case 'l': {//todo, hint:use the ARRAY that you defined, and notice the teapot can NOT be moved out the range of the table.
//glPushMatrix();
move[0]=move[0]+size;
// glTranslatef(move[0],move[1],move[2]);
//glutSolidTeapot(1);
//glPopMatrix();
break;
}
case 'j': {//todo
//glPushMatrix();
move[0]=move[0]-size;
// glTranslatef(move[0],move[1],move[2]);
//glutSolidTeapot(1);
//glPopMatrix();
break;
}
case 'i': {//todo
//glPushMatrix();
move[2]+=size;
//glTranslatef(move[0],move[1],move[2]);
// glutSolidTeapot(1);
//glPopMatrix();
break;
}
case 'k': {//todo
//glPushMatrix();
move[2]-=size;
// glTranslatef(move[0],move[1],move[2]);
// glutSolidTeapot(1);
// glPopMatrix();
break;
}
case 'e': {//todo 旋转
tAnim=!tAnim;
/*if(tAnim)
{
glPushMatrix();
glRotatef(fRotate, 0, 1.0f, 0);
glutSolidTeapot(1);
glPopMatrix();
fRotate+=0.1f;*/
break;
}
}
}


void redraw()
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); // Reset The Current Modelview Matrix
//视点转换
gluLookAt(eye[0], eye[1], eye[2],
center[0], center[1], center[2],
0, 1, 0); // 场景(0,0,0)的视点中心 (0,5,50),Y轴向上
//线框模式和填充模式转换
if (bWire) {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
else {
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
//光照
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);//启动光照
GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_pos[] = {5,5,5,1};

glLightfv(GL_LIGHT0, GL_POSITION, light_pos);//光源位置,颜色
glLightfv(GL_LIGHT0, GL_AMBIENT, white);//环境光
glEnable(GL_LIGHT0);//启动光源

// glTranslatef(0.0f, 0.0f,-6.0f); // Place the triangle at Center
glRotatef(fRotate, 0, 1.0f, 0); // Rotate around Y axis
glRotatef(-90, 1, 0, 0);
glScalef(0.2, 0.2, 0.2);
Draw_Scene(); // Draw Scene

glRotatef(fRevolve, 0, 1.0f, 0); // Rotate around Y axis
glRotatef(-90, 1, 0, 0);
glScalef(0.2, 0.2, 0.2);
glutSolidTeapot(1);

glTranslatef(move[0],move[1],move[2]);
glutSolidTeapot(1);

if (bAnim) fRotate += 0.5f;
if (tAnim) fRevolve+=0.3f;
//todo; hint: when you want to rotate the teapot, you may like to add another line here =)
glutSwapBuffers();
}

int main (int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(480,480);
int windowHandle = glutCreateWindow("Ex 3");

glutDisplayFunc(redraw);
glutReshapeFunc(reshape);
glutKeyboardFunc(key);
glutIdleFunc(idle);

glutMainLoop();
return 0;
}


...全文
2211 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
wdlove58 2015-11-16
  • 打赏
  • 举报
回复
case 'z': {//todo eye[]={8,-8,-8}; center[]={0,-8,0}; break; } case 'c': {//todo eye[]={-8,-8,8}; center[]={0,-8,0}; break; 你指的移动不了是指按z和c键不能移动吧,是设置的值8太大了,移动到屏幕外去了,所以屏幕什么都没有。我把你设置的8改为2后,运行的可以正常移动。

1,451

社区成员

发帖
与我相关
我的任务
社区描述
多媒体/设计/Flash/Silverlight 开发 图象工具使用
社区管理员
  • 图象工具使用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧