-4
我寫的OpenGL代碼有一個奇怪的錯誤。作爲測試,我創建了一個球體矢量並使用push_back(s1)。我正在向矢量添加多個球體。但是,當我運行該程序時,它只繪製最近被推入矢量的球體。C++ Vectors and OpenGL
#include "Sphere.h";
#include <iostream>;
#include <vector>;
using namespae std;
vector<Sphere> spheres;
Sphere s1 = Sphere(1.0, "One");
Sphere s2 = Sphere(2.0, "Two");
Sphere s3 = Sphere(3.0, "Three");
void init(void) {
spheres.push_back(s1);
spheres.push_back(s2);
spheres.push_back(s3);
for each(Sphere s in spheres) {
cout << s.getName() << "\n";
}
}
// OTHER CODE OMMITED
void display(void) {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 0.0);
glPushMatrix();
for each(Sphere in s) {
s.draw();
}
glPopMatrix();
}
顯然有一個主要的方法在那裏所有的GL的東西都設置,我知道那裏沒有問題。
所以球體有它自己的繪製方法。現在有趣的部分是它在控制檯輸出:
Three
Three
Three
並繼續繪製s3,三次到屏幕。
所以我的問題是:爲什麼它只繪製向量中的最後一個項目三次?我也嘗試使用迭代器和正常循環,但它們都產生相同的結果。
任何人有想法?
編輯
的getName()函數:
string Sphere::getName() {
return name;
}
迭代器向量:
vector<Sphere>::iterator it;
void display() {
for(it = planets.begin(); it != planets.end(); ++it) {
it->draw();
}
}
拉伸代碼在球形:
GLdouble r = 0.0;
GLfloat X = 0.0f;
string name = " ";
Sphere::Sphere(GLdouble ra, GLfloat x, string n)
{
r = ra;
X = pos;
name = n;
}
Sphere::~Sphere(void)
{
}
void Sphere::draw(void)
{
glutSolidSphere(r, 10, 8);
glTranslatef(X, 0.0, 0.0);
}
string Sphere::getName(void)
{
return name;
}
該錯誤似乎出現在您定義的「Sphere」中。 – delnan 2013-03-04 15:07:03
不要在#includes後寫';'。 – Lundin 2013-03-04 15:07:28
我也會感興趣的是看到每個C++代碼(球體中的球體){cout << s.getName()<<「\ n」; }' – 2013-03-04 15:07:41