【问题标题】:Positioning an array of objects in OpenGL在 OpenGL 中定位对象数组
【发布时间】:2011-04-18 22:22:01
【问题描述】:

编辑:

我仍然没有找到正确的位置,所有的球都在原点被绘制,我想我可能把东西放在了错误的地方......

球.cpp

#include "Ball.h"
#include "Vector2f.h"
#include "Vector3f.h"
#include "Glut/glut.h"
#include "GL/gl.h"
#include "GL/glu.h"


Ball::Ball(void)
{
    Vector3f Temp_position;
    position = Temp_position;
    Vector3f Temp_velocity;
    velocity = Temp_velocity;
}


Ball::~Ball(void)
{
}

void Ball::SetPos(Vector3f New_position)
{
    position = New_position;
}


void Ball::Draw()
{
    glPushMatrix();
    glTranslatef(position.X(), position.Y(), position.Z());
    glColor3d(1, 0, 0);
    glutSolidSphere(0.3, 50, 50);
    glPopMatrix();
}

void Ball::ArrayPosition()
{

Ball *Yellowball = new Ball[8];
Yellowball[0].SetPos(Vector3f (position.X(), position.Y(), position.Z()));
Yellowball[1].SetPos(Vector3f (position.X(), position.Y(), position.Z()));
Yellowball[2].SetPos(Vector3f (position.X(), position.Y(), position.Z()));
Yellowball[3].SetPos(Vector3f (position.X(), position.Y(), position.Z()));
Yellowball[4].SetPos(Vector3f (position.X(), position.Y(), position.Z()));
Yellowball[5].SetPos(Vector3f (position.X(), position.Y(), position.Z()));
Yellowball[6].SetPos(Vector3f (position.X(), position.Y(), position.Z()));
Yellowball[7].SetPos(Vector3f (position.X(), position.Y(), position.Z()));

}

void Ball::DrawYellow()
{

glPushMatrix();
glColor3f(2,1,0);
glutSolidSphere(0.3, 50, 50);
glPopMatrix();
}

void Ball::SetVel(Vector3f New_velocity)
{
    velocity = New_velocity;
}



Vector3f Ball::GetPos()
{
    Vector3f temp;
    temp = position;
    return temp;
}

Vector3f Ball::GetVel()
{
    Vector3f temp;
    temp = velocity;
    return temp;
}

Main.cpp

Ball Redball;
float BALL_RED_START = 0;
float RADIUS_OF_BALL = 0.3;
float BALL_RED_END = 8;

Ball Yellowball; 
float BALL_YELLOW_START = 0;

float BALL_YELLOW_END = 8;

init()

Ball *Yellowball = new Ball[8];

for(int i = BALL_YELLOW_START; i < BALL_YELLOW_START; i++)
{

Yellowball[0].SetPos(Vector3f (1,0,0));
Yellowball[1].SetPos(Vector3f (0,0,0));
Yellowball[2].SetPos(Vector3f (0,0,0));
Yellowball[3].SetPos(Vector3f (0,0,0));
Yellowball[4].SetPos(Vector3f (0,0,0));
Yellowball[5].SetPos(Vector3f (0,0,0));
Yellowball[6].SetPos(Vector3f (0,0,0));
Yellowball[7].SetPos(Vector3f (0,0,0));
}

Draw method:

Ball Yellowball[8];

for (int i = BALL_YELLOW_START; i<BALL_YELLOW_END;i++)
{
glColor3f(2,1,0);

Yellowball[i].DrawYellow();

}
glPopMatrix();

结果是在原点绘制数组所有球都在同一个地方!

【问题讨论】:

  • 您不能将当前 x、y、z 位置(平移)放入数组中的任何原因?
  • 这就是我想要的!我只是不确定如何!
  • 首先,在 Ball 类中添加三行来存储 x、y 和 z int 值。然后,您可以将这些值设置为程序中的某个起始值。在上面的循环中,在 glTranslate 调用中使用这 3 个值(例如 glTranslate( Yellowball[i].x, Yellowball[i].y, Yellowball[i].z ); )
  • x,y,z 应该是私有的并且应该在构造函数中设置,然后应该在 Ball::Draw 方法中进行 glTranslate 调用。如果你至少想要好的设计。请参阅下面的解决方案。

标签: c++ arrays opengl object


【解决方案1】:

做这样的事情:

// first of all, include the x,y position (assuming 2D, since pool) in the Ball object:
class Ball
{
   //...

   private:
      int xpos, ypos;
   //...
};

然后,当您构建球数组时,而不是仅仅制作 8 个球,您将希望在堆上分配内存,以便它在整个游戏中持续存在。这样做:

Ball *YellowBall[8];
YellowBall[0] = new Ball(x0,y0);
YellowBall[1] = new Ball(x1,y1);
YellowBall[2] = new Ball(x2,y2);
YellowBall[3] = new Ball(x3,y3);
// ...

确保当你的游戏结束后,你自己清理干净。

for (int i = 0; i < 8; i++)
   delete YellowBall[i];

然后在 Ball::DrawYellow() 中执行以下操作:

Ball::DrawYellow() 
{
   glColor3f(/*yellow*/); // Set the color to yellow 
   glTranslatef(-x, -y, 0); // Move to the position of the ball
   // Draw the ball
   glTranslatef(x, y, 0); // Move back to the default position
}

从这里开始可能需要稍作调整,但这是否有意义/回答您的问题?

【讨论】:

  • 我认为添加 glPushMatrix/glPopMatrix 对不是坏主意。
  • 是的,这很有道理!我会试一试,看看会发生什么谢谢!
  • 对仍然有一些问题我已经用我所拥有的东西编辑了我的帖子:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-09
  • 2019-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多