gpt4 book ai didi

c++ - 移动一个对象而不影响其他对象(使用opengl)

转载 作者:行者123 更新时间:2023-11-30 18:59:36 24 4
gpt4 key购买 nike

我创建了一个包含一些对象(房屋、建筑物)的程序。我想移动一个对象(正方形)而不影响其他对象,但是当我移动时其他对象就会消失。

如何移动对象而不导致现有对象消失?

#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();

}

最佳答案

去你本地的图书馆找一本常见的 opengl 书籍(例如“Beginning OpenGL Programming”)并仔细阅读前几章。这对你的帮助远远超过你对你的问题可能得到的任何答案。看来你还没有掌握基本概念。这绝不是一种侮辱,没有人生来就具备这些知识。

要归档您想做的事情,您可能需要特别注意:操作矩阵堆栈、场景图、对象空间与世界空间与相机空间以及一般转换。

干杯凯

关于c++ - 移动一个对象而不影响其他对象(使用opengl),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9825898/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com