我有一个程序,绘制一个简单的金字塔形状,我可以用我的箭头键旋转,但我想将这个形状转换为一个简单的飞机而不是旋转我希望它仍然用箭头键移动(飞行) 。我怎么能做到这一点?
这是我到目前为止所拥有的:
#include "shared/gltools.h" // GLTools
#include "shared/math3d.h" // 3D Math Library
// Rotation amounts
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;
// Change viewing volume and viewport. Called when window is resized
void ChangeSize(int w, int h)
{
GLfloat fAspect;
// Prevent a divide by zero
if(h == 0)
h = 1;
// Set Viewport to window dimensions
glViewport(0, 0, w, h);
fAspect = (GLfloat)w/(GLfloat)h;
// Reset coordinate system
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Produce the perspective projection
gluPerspective(35.0f, fAspect, 1.0, 40.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
// This function does any needed initialization on the rendering
// context. Here it sets up and initializes the lighting for
// the scene.
void SetupRC()
{
GLbyte *pBytes;
GLint iWidth, iHeight, iComponents;
GLenum eFormat;
// Light values and coordinates
GLfloat whiteLight[] = { 0.05f, 0.05f, 0.05f, 1.0f };
GLfloat sourceLight[] = { 0.25f, 0.25f, 0.25f, 1.0f };
GLfloat lightPos[] = { -10.f, 5.0f, 5.0f, 1.0f };
glEnable(GL_DEPTH_TEST); // Hidden surface removal
glFrontFace(GL_CCW); // Counter clock-wise polygons face out
glEnable(GL_CULL_FACE); // Do not calculate inside
// Enable lighting
glEnable(GL_LIGHTING);
// Setup and enable light 0
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,whiteLight);
glLightfv(GL_LIGHT0,GL_AMBIENT,sourceLight);
glLightfv(GL_LIGHT0,GL_DIFFUSE,sourceLight);
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
glEnable(GL_LIGHT0);
// Enable color tracking
glEnable(GL_COLOR_MATERIAL);
// Set Material properties to follow glColor values
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
// Black background
glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
// Load texture
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
pBytes = gltLoadTGA("stone.tga", &iWidth, &iHeight, &iComponents, &eFormat);
glTexImage2D(GL_TEXTURE_2D, 0, iComponents, iWidth, iHeight, 0, eFormat, GL_UNSIGNED_BYTE, pBytes);
free(pBytes);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
//try these too
//glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
//glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glEnable(GL_TEXTURE_2D);
}
// Respond to arrow keys
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
xRot-= 5.0f;
if(key == GLUT_KEY_DOWN)
xRot += 5.0f;
if(key == GLUT_KEY_LEFT)
yRot -= 5.0f;
if(key == GLUT_KEY_RIGHT)
yRot += 5.0f;
xRot = (GLfloat)((const int)xRot % 360);
yRot = (GLfloat)((const int)yRot % 360);
// Refresh the Window
glutPostRedisplay();
}
// Called to draw scene
void RenderScene(void)
{
M3DVector3f vNormal;
//defines the corner vertices of the pyramid
M3DVector3f vCorners[5] = {
{ 0.0f, 0.3f, 0.0f }, // Top 0
{ -0.25f, 0.0f, -.25f }, // Back left 1
{ 0.5f, 0.0f, -0.50f }, // Back right 2
{ 0.25f, 0.0f, 0.25f }, // Front right 3
{ -0.99f, 0.0f, 0.99f }}; // Front left 4
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Save the matrix state and do the rotations
glPushMatrix();
// Move object back and do in place rotation
glTranslatef(0.0f, -0.25f, -4.0f);
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f);
// Draw the Pyramid
//try different colours
glColor3f(1.0f, 1.0f, 1.0f);//a white pyramid
// glColor3f(1.0f, 0.0f, 0.0f); // a red pyramid
//glColor3f(1.0,0.7,0.7);//reddish
glBegin(GL_TRIANGLES);
// Bottom section - two triangles
glNormal3f(0.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3fv(vCorners[2]);
glTexCoord2f(0.0f, 0.0f);
glVertex3fv(vCorners[4]);
glTexCoord2f(0.0f, 1.0f);
glVertex3fv(vCorners[1]);
glTexCoord2f(1.0f, 1.0f);
glVertex3fv(vCorners[2]);
glTexCoord2f(1.0f, 0.0f);
glVertex3fv(vCorners[3]);
glTexCoord2f(0.0f, 0.0f);
glVertex3fv(vCorners[4]);
//Faces
// Front Face
m3dFindNormal(vNormal, vCorners[0], vCorners[4], vCorners[3]);
glNormal3fv(vNormal);
glTexCoord2f(0.5f, 1.0f);//notice the 0.5 here
glVertex3fv(vCorners[0]);
glTexCoord2f(0.0f, 0.0f);
glVertex3fv(vCorners[4]);
glTexCoord2f(1.0f, 0.0f);
glVertex3fv(vCorners[3]);
// Left Face
m3dFindNormal(vNormal, vCorners[0], vCorners[1], vCorners[4]);
glNormal3fv(vNormal);
glTexCoord2f(0.5f, 1.0f);
glVertex3fv(vCorners[0]);
glTexCoord2f(0.0f, 0.0f);
glVertex3fv(vCorners[1]);
glTexCoord2f(1.0f, 0.0f);
glVertex3fv(vCorners[4]);
// Back Face
m3dFindNormal(vNormal, vCorners[0], vCorners[2], vCorners[1]);
glNormal3fv(vNormal);
glTexCoord2f(0.5f, 1.0f);
glVertex3fv(vCorners[0]);
glTexCoord2f(0.0f, 0.0f);
glVertex3fv(vCorners[2]);
glTexCoord2f(1.0f, 0.0f);
glVertex3fv(vCorners[1]);
// Right Face
m3dFindNormal(vNormal, vCorners[0], vCorners[3], vCorners[2]);
glNormal3fv(vNormal);
glTexCoord2f(0.5f, 1.0f);
glVertex3fv(vCorners[0]);
glTexCoord2f(0.0f, 0.0f);
glVertex3fv(vCorners[3]);
glTexCoord2f(1.0f, 0.0f);
glVertex3fv(vCorners[2]);
glEnd();
// Restore the matrix state
glPopMatrix();
// Buffer swap
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("Textured Pyramid");
glutReshapeFunc(ChangeSize);
glutSpecialFunc(SpecialKeys);
glutDisplayFunc(RenderScene);
SetupRC();
glutMainLoop();
return 0;
}
答案 0 :(得分:1)
那么,你必须根据顶点及其连接(即三角形)来模拟你的“飞机”或其他任何东西。除非你的大脑能够想象所有的坐标,我担心,你将不得不使用某种软件为你做这个(我假设你也是某种艺术家)。
然后,您所要做的就是为您的建模软件编写用于保存“飞机”的文件格式的加载程序,将其转换为您的坐标系统,将生成的顶点放入渲染器中,您将很高兴。容易,不是吗?
答案 1 :(得分:0)
{0,0,0},{3,3,0},{ - 3,3,0}
有一个非常简单的3D splaceship的顶点列表 - 一个三角形。您可以使用它来创建一个VBO,或者只是手动插入它和glDrawArray()它。 :)
答案 2 :(得分:0)
这是一个非常大的问题......而且非常懒惰。
其他答案涵盖了建模方面......虽然不是很好。对于比三角形更复杂的东西(这是一个很好的起点,正如所建议的,为了让事情有效)我建议使用OBJ加载器。格式很简单,使用文本,可以使用简单的字符串操作加载来提取值。
现在,要使事物移动,您需要动态更新模型的顶点或了解转换和顶点管道。您可以移动相机或在相机周围移动世界 - 任何一种方式都是等效的。 glRotate,glTranslate或glTransform可以提供帮助,就像将矩阵传递到着色器一样。
希望这足以通过一些思考或谷歌搜索来填补相当大的空白。你不可能通过这个来完成这一操作...如果你对几何学(笛卡尔坐标)和一点API知识(glTranslate)有基本的了解,那么它并不太难。