首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络游戏 >

OpenGL有关问题了。实在搞不定了。请帮忙看看

2012-02-04 
OpenGL问题了。。实在搞不定了。。。请帮忙看看刚学Opengl,我写了一个小程序,想法是让小球从盒子里面飘出来,可

OpenGL问题了。。实在搞不定了。。。请帮忙看看
刚学Opengl,我写了一个小程序,想法是让小球从盒子里面飘出来,可是我现在碰到了一个问题,就是我写了一堆以后,我搞不清我当前坐标是什么了。。求大家帮帮忙了。。,帮我改下气球飞出来的位置,谢谢了。。
或者帮我分析下现在坐标是什么样了也可以。
第一次,乱了点。。。见谅。
code:
// OpenBox.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <gl/glut.h>
#include <math.h>
#include <stdlib.h>
#define BALLON 7
static double ANGLE = 0;
static float cb[BALLON][7];//定义BALLON个气球
void ballon(float a,float b,float c);
void colorBox();
void myDisplay();
void myIdle();
void initBallon();

void initBallon();
int main(int argc, char* argv[])
{
initBallon();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(50,100);
glutInitWindowSize(400,300);
glutCreateWindow("An open box^_^");

glutDisplayFunc(myDisplay);
glutIdleFunc(myIdle);

glutMainLoop();

printf("Hello World!\n");
return 0;
}
void myDisplay()
{
glEnable(GL_DEPTH_TEST);
glClearColor(238.0/255, 43.0/255, 249.0/255, 1.0);//背景色设置为粉红色
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(75,1,20,400);
gluLookAt(0, 0, 50, 0, 0, 0, 0, 1.0, 0);

glPushMatrix();

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//glTranslatef(0,0,0);
colorBox();
for(int i=0;i<BALLON;i++)
{
glPushMatrix();
glTranslatef(0.5,0.8,0.8);//就是这里了,我已经搞不清坐标系是什么样的了。。。
glTranslatef(cb[i][3],cb[i][5],cb[i][4]);
glScalef(cb[i][6],cb[i][6],cb[i][6]);
glRotated(90, 1, 0, 0);
ballon(cb[i][0],cb[i][1], cb[i][2]);
glPopMatrix();
}


glPopMatrix();


glFlush();
glutSwapBuffers();

}
void myIdle()
{
if (ANGLE > 90)
{
/* ANGLE =0;
for(int i=0;i<10;i++)
{
glDeleteLists(i,1);
cb[i][0]=1.0f*rand()/RAND_MAX;
cb[i][1]=1.0f*rand()/RAND_MAX;
cb[i][2]=1.0f*rand()/RAND_MAX;
cb[i][3]=0.1f+0.3f*rand()/RAND_MAX;
cb[i][4]=-0.30f;
cb[i][5]=-0.1f-0.3f*rand()/RAND_MAX;
cb[i][6]=0.4f;
}
*/
}
else
{
ANGLE+=0.3;
for(int i=0;i<BALLON;i++)
{
cb[i][3]=cb[i][3]+0.05f*(rand()-rand())/RAND_MAX;
cb[i][4]=cb[i][4]+0.1f*rand()/RAND_MAX;
cb[i][5]=cb[i][5]+0.05f*(rand()-rand())/RAND_MAX;
cb[i][6]=cb[i][6]+0.02f;
}

}
myDisplay();
}
void ballon(float a,float b,float c)
{
  glShadeModel(GL_SMOOTH);
  glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);

glColor3f(a,b,c);
glScalef(0.1f,0.05f,0.1f);
//glRotated(-bh*4,0.0,1.0,0.0);
float rx=1.0f;
float ange1=0.0f;
float ange2=0.0f;

  glShadeModel(GL_SMOOTH);
  glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);

glEnable(GL_NORMALIZE);
glPushMatrix();
for(float i=0;i<20;i++)
{
glBegin(GL_QUAD_STRIP);
for(float j=0;j<363;j++)
{
rx=(cosf(3.1416f/2.0f-3.1416f/20*(i)));
glNormal3f(rx*cosf(ange1),
sinf(3.1416f/2.0f-3.1416f/20*i)-0.01f*i*i,
-rx*sinf(ange1));
glVertex3f(rx*cosf(ange1),
sinf(3.1416f/2.0f-3.1416f/20*i)-0.01f*i*i,
-rx*sinf(ange1));
rx=(cosf(3.1416f/2.0f-3.1416f/20*(i+1)));
glNormal3f(rx*cosf(ange1),
sinf(3.1416f/2.0f-3.1416f/20*(i+1))-0.01f*(i+1)*(i+1),
-rx*sinf(ange1));
glVertex3f(rx*cosf(ange1),
sinf(3.1416f/2.0f-3.1416f/20*(i+1))-0.01f*(i+1)*(i+1),
-rx*sinf(ange1));
ange1=3.1416f/180.0f*j;
}
glEnd();
};
// glLineStipple(1,0xFFFF);
glLineWidth (0.5f);
glColor3f(1.0f,1.0f,0.0f);


glBegin(GL_LINES);
glVertex3f(0.0f,-5.0f,0.0f);
glVertex3f(0.0f,-5.5f,0.1f);
glVertex3f(0.0f,-5.5f,0.1f);
glVertex3f(0.05f,-6.5f,0.0f);
glVertex3f(0.05f,-6.5f,0.0f);
glVertex3f(0.0f,-8.5f,0.0f);
glEnd();
// glLineStipple(1,0xFFFF);
glLineWidth (1.0f);

glPopMatrix();
}
void colorBox()
{
//glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

//ballon(0.8, 0.5, 0.2);
glTranslatef(0,-20,0.0f);
glRotated(-80, 1.0, 0, 0);//沿X轴逆向旋转90°
glRotated(-225, 0, 0, 1);//沿z轴旋转30°
glScalef(15,15,15);

glPushMatrix();
glBegin(GL_QUADS); //画背面
glColor3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,0.0f);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(1.0f,0.0f,0.0f);
glColor3f(1.0f,1.0f,0.0f);
glVertex3f(1.0f,1.0f,0.0f);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);
glEnd();
glPopMatrix();

glPushMatrix();
glRotated(ANGLE, 0, -1, 0);
glRotated(-90,0.0,1.0,0.0);

glBegin(GL_QUADS);//画左侧
glColor3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,0.0f);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(1.0f,0.0f,0.0f);
glColor3f(0.0f,1.0f,1.0f);
glVertex3f(1.0f,1.0f,0.0f);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);
glEnd();
glPopMatrix();

glPushMatrix();
glTranslatef(1.0, 0.0, 0.0);
glRotated(-90,0.0,1.0,0.0);
glRotated(-ANGLE, 0, -1, 0);
glBegin(GL_QUADS);//画右侧
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,0.0f);
glColor3f(1.0f,0.0f,1.0f);
glVertex3f(1.0f,0.0f,0.0f);
glColor3f(1.0f,1.0f,1.0f);
glVertex3f(1.0f,1.0f,0.0f);
glColor3f(1.0f,1.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);
glEnd();
glPopMatrix();

glPushMatrix();
glTranslatef(0.0, 1.0, 0.0);
glRotated(90, 1.0, 0,0.0);
glRotated(-ANGLE, 1, 0, 0);
glBegin(GL_QUADS);//画上面
glColor3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,0.0f);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(1.0f,0.0f,0.0f);
glColor3f(1.0f,0.0f,1.0f);
glVertex3f(1.0f,1.0f,0.0f);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(0.0f,1.0f,0.0f);
glEnd();
glPopMatrix();

glPushMatrix();
//glTranslatef(0.0, -1.0, 0.0);
glRotated(90, 1.0, 0,0.0);
glRotated(ANGLE, 1, 0, 0);
glBegin(GL_QUADS);//画下面
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(0.0f,0.0f,0.0f);
glColor3f(1.0f,1.0f,0.0f);
glVertex3f(1.0f,0.0f,0.0f);
glColor3f(1.0f,1.0f,1.0f);
glVertex3f(1.0f,1.0f,0.0f);
glColor3f(0.0f,1.0f,1.0f);
glVertex3f(0.0f,1.0f,0.0f);
glEnd();
glPopMatrix();

glPushMatrix();
glRotated(ANGLE, 1, 0, 0);
glTranslatef(0.0, 0.0, 1.0);
glRotated(ANGLE, 1, 0, 0);
glBegin(GL_QUADS);//画正对面
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(0.0f,0.0f,0.0f);
glColor3f(1.0f,0.0f,1.0f);
glVertex3f(1.0f,0.0f,0.0f);
glColor3f(1.0f,1.0f,1.0f);
glVertex3f(1.0f,1.0f,0.0f);
glColor3f(0.0f,1.0f,1.0f);
glVertex3f(0.0f,1.0f,0.0f);
glEnd();
glPopMatrix();

//glPopMatrix();
};
void initBallon()
{
for(int i=0;i<BALLON;i++)
{
cb[i][0]=1.0f*rand()/RAND_MAX;
cb[i][1]=1.0f*rand()/RAND_MAX;
cb[i][2]=1.0f*rand()/RAND_MAX;
cb[i][3]=15*(0.1f+0.3f*rand()/RAND_MAX);
cb[i][4]=15*(-0.30f);
cb[i][5]=15*(-0.1f-0.3f*rand()/RAND_MAX);
cb[i][6]=10*(0.4f);
}
}

[解决办法]
不管你写了多少代码

一但你使用了glMatrixMode(GL_MODELVIEW);那么这个坐标最后计算出来的就是在相机坐标系中的坐标
使用了glMatrixMode(GL_PROJECTION);那么这个坐标就是在投影变换后的正规化的坐标系 
 建议楼主看看我写的那个教学,专门讲openGL坐标变换的

就是置顶的那个帖子
------解决方案--------------------


你的注释

C/C++ code
);//就是这里了,我已经搞不清坐标系是什么样的了。。。 

热点排行