我创建了一个包含几个对象(房屋,建筑物)的程序。我想移动一个对象(正方形)而不影响其他对象,但是当我做其他对象消失时。
如何在不导致现有对象消失的情况下移动对象?
#include <glut.h>
#include <stdlib.h>
float wall1[4][2] = {{100,200},{100,100},{300,100},{300,200}};
float roof1 [3][2] = {{100,200},{200,250},{300,200}};
float door1 [4][2] = {{175,100},{175,150},{225,150},{225,100}};
float wall2 [4][2] = {{500,600},{500,500},{700,500},{700,600}};
float roof2 [3][2] = {{500,600},{600,650},{700,600}};
float door2 [4][2] = {{575,500},{575,550},{625,550},{625,500}};
float wall3[4][2] = {{100,1000},{100,900},{300,900},{300,1000}};
float roof3 [3][2] = {{100,1000},{200,1050},{300,1000}};
float door3 [4][2] = {{175,900},{175,950},{225,950},{225,900}};
float wall4[4][2] = {{900,200},{900,100},{1100,100},{1100,200}};
float roof4 [3][2] = {{900,200},{1000,250},{1100,200}};
float door4 [4][2] = {{975,100},{975,150},{1025,150},{1025,100}};
float wall5[4][2] = {{200,1300},{200,1200},{400,1200},{400,1300}};
float roof5 [3][2] = {{200,1300},{300,1350},{400,1300}};
float door5 [4][2] = {{275,1200},{275,1250},{325,1250},{325,1200}};
float wall6[4][2] = {{1100,700},{1100,600},{1300,600},{1300,700}};
float roof6 [3][2] = {{1100,700},{1200,750},{1300,700}};
float door6 [4][2] = {{1175,600},{1175,650},{1225,650},{1225,600}};
float build1[4][2]={{1000,1000},{1100,1000},{1100,1400},{1000,1400}};
float build2[4][2]={{1100,1000},{1200,1000},{1200,1300},{1100,1300}};
void myinit()
{
glClearColor(0.0,1.0,0.0,1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,1500,0,1500);
}
void drawhouse()
{
int i;
glColor3f(1,1,0);
glBegin(GL_POLYGON);
for(i=0;i<4;i++)
glVertex2fv(wall1[i]);
glEnd();
glColor3f(10,0,0);
glBegin(GL_POLYGON);
for(i=0;i<3;i++)
glVertex2fv(roof1[i]);
glEnd();
glColor3f(0,0,1);
glBegin(GL_POLYGON);
for(i=0;i<4;i++)
glVertex2fv(door1[i]);
glEnd();
glColor3f(1,1,0);
glBegin(GL_POLYGON);
for(i=0;i<4;i++)
glVertex2fv(wall2[i]);
glEnd();
glColor3f(10,0,0);
glBegin(GL_POLYGON);
for(i=0;i<3;i++)
glVertex2fv(roof2[i]);
glEnd();
glColor3f(0,0,1);
glBegin(GL_POLYGON);
for(i=0;i<4;i++)
glVertex2fv(door2[i]);
glEnd();
glColor3f(1,1,0);
glBegin(GL_POLYGON);
for(i=0;i<4;i++)
glVertex2fv(wall3[i]);
glEnd();
glColor3f(10,0,0);
glBegin(GL_POLYGON);
for(i=0;i<3;i++)
glVertex2fv(roof3[i]);
glEnd();
glColor3f(0,0,1);
glBegin(GL_POLYGON);
for(i=0;i<4;i++)
glVertex2fv(door3[i]);
glEnd();
glColor3f(1,1,0);
glBegin(GL_POLYGON);
for(i=0;i<4;i++)
glVertex2fv(wall4[i]);
glEnd();
glColor3f(10,0,0);
glBegin(GL_POLYGON);
for(i=0;i<3;i++)
glVertex2fv(roof4[i]);
glEnd();
glColor3f(0,0,1);
glBegin(GL_POLYGON);
for(i=0;i<4;i++)
glVertex2fv(door4[i]);
glEnd();
glColor3f(1,1,0);
glBegin(GL_POLYGON);
for(i=0;i<4;i++)
glVertex2fv(wall5[i]);
glEnd();
glColor3f(10,0,0);
glBegin(GL_POLYGON);
for(i=0;i<3;i++)
glVertex2fv(roof5[i]);
glEnd();
glColor3f(0,0,1);
glBegin(GL_POLYGON);
for(i=0;i<4;i++)
glVertex2fv(door5[i]);
glEnd();
glColor3f(1,1,0);
glBegin(GL_POLYGON);
for(i=0;i<4;i++)
glVertex2fv(wall6[i]);
glEnd();
glColor3f(10,0,0);
glBegin(GL_POLYGON);
for(i=0;i<3;i++)
glVertex2fv(roof6[i]);
glEnd();
glColor3f(0,0,1);
glBegin(GL_POLYGON);
for(i=0;i<4;i++)
glVertex2fv(door6[i]);
glEnd();
glutPostRedisplay();
}
void company()
{
int i;
glColor3f(0,0,1);
glBegin(GL_POLYGON);
for(i=0;i<4;i++)
glVertex2fv(build1[i]);
glEnd();
glColor3f(0.5,0,0.5);
glBegin(GL_POLYGON);
for(i=0;i<4;i++)
glVertex2fv(build2[i]);
glEnd();
glutPostRedisplay();
}
void path1(int value)
{
float i;
for(i=0;i<270;i=i+0.1)
{
glColor3f(1.0,0.0,0.0);
glBegin(GL_POLYGON);
glVertex2f(1000.0+i,1000.0-i);
glVertex2f(1050.0+i,1000.0-i);
glVertex2f(1050.0+i,1050.0-i);
glVertex2f(1000.0+i,1050.0-i);
glEnd();
glClear(GL_COLOR_BUFFER_BIT);
glutTimerFunc(200000,drawhouse, 0);
glFlush();
}
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
drawhouse();
company();
glPushMatrix();
path1(0);
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800,800);
glutInitWindowPosition(0,0);
glutCreateWindow("House ");
myinit();
glutDisplayFunc(display);
glutMainLoop();
}
答案 0 :(得分:2)
转到您当地的图书馆并获取一本常用的opengl书籍(例如“Beginning OpenGL Programming”)并仔细阅读前几章。这对您的帮助远远超过您对问题的任何回答。你似乎还没有掌握基本概念。这绝不是一种侮辱,没有人生来就有这种知识。
要归档您想要做的事情,您可能需要特别注意:操纵矩阵堆栈,场景图,对象空间与世界空间对比相机空间和一般转换。
干杯 凯