在OpenGL中定位一个对象数组
Positioning an array of objects in OpenGL
编辑:
我仍然没有得到正确的位置,所有的球都被画在原点,我想我可能把东西放在错误的地方…
Ball.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();
结果是在原点绘制数组,所有的球都在同一个地方!
这样做:
// 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
}
这可能需要一点点的调整,但这有意义/回答你的问题吗?
相关文章:
- 有没有办法使用该类的构造函数初始化另一个类的私有部分内的对象数组?
- 如何将一个字节数组正确地恢复为C 中的对象
- 指针阵列在一个对象数组中函数
- 处理另一个类中类动态分配的对象数组
- 如何创建一个动态数组,该数组可以保存从C++中派生的同一基类的所有不同对象
- C++:创建一个对象数组
- 函数,返回一个对象数组
- 如何在C++中构建一个对象数组,这些对象都继承自一个主类
- 为什么我可以分配一个对象数组,但不能将它们放在向量中
- 我有一个对象数组,我想发出一个包含这些对象中的一个的信号。但信号不能是指针
- 创建一个对象数组,没有匹配的构造函数初始化错误
- 如何分配一个对象数组的索引?(c++)
- 如何在它自己的类中声明一个对象数组?
- 为什么当我创建一个对象数组时调用默认构造函数,但当我将obj创建为简单变量时却出错
- 删除一个对象数组,该数组被上推为基指针
- 用常量变量声明一个对象数组
- 在C++中,我想从一个函数返回一个对象数组,并在另一个函数中使用它
- c++从类方法返回一个对象数组
- 在OpenGL中定位一个对象数组
- 在Open GL Glut中绘制一个对象数组