我正在尝试使用iostream创建一个3D立方体来读取txt文件(这是一项任务)。我在cube.h中将Cube :: Load声明为静态布尔值,并在cube.cpp中实现了代码。执行此操作后,我尝试在FirstGL.cpp中调用该方法,但遇到3个错误。
我给出的注释告诉我,我可以使用Cube :: Load(“cube.txt”),因为我已经在多维数据集头文件中给出了类型。
FirstGL.cpp
#include "FirstGL.h"
Cube::Load("cube.txt");
Cube.h
#pragma once
#include <Windows.h>
#include <gl/GL.h>
#include <gl/GLU.h>
#include "GL\freeglut.h"
#include "structures.h"
#include <iostream>
#include <fstream>
#include <iosfwd>
#include <ostream>
#include <string>
class Cube
{
private:
static Vertex* indexedVertices[];
static Color* indexedColors[];
static GLushort* indices[];
static int numVertices, numColors, numIndices;
GLfloat _rotation;
static bool Load(char* path);
public:
Cube(void);
~Cube(void);
void Draw();
void Update();
};
Cube.cpp
#include "Cube.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
Vertex* Cube::indexedVertices[] = nullptr; /*{ 1, 1, 1, -1, 1, 1, // v0,v1,
-1, -1, 1, 1, -1, 1, // v2,v3
1, -1, -1, 1, 1, -1, // v4,v5
-1, 1, -1, -1, -1, -1 }; // v6,v7*/
Color* Cube::indexedColors[] = nullptr; /*{ 1, 1, 1, 1, 1, 0, // v0,v1,
1, 0, 0, 1, 0, 1, // v2,v3
0, 0, 1, 0, 1, 1, // v4,v5
0, 1, 0, 0, 0, 0 }; //v6,v7*/
GLushort* Cube::indices[] = nullptr; /*{ 0, 1, 2, 2, 3, 0, // front
0, 3, 4, 4, 5, 0, // right
0, 5, 6, 6, 1, 0, // top
1, 6, 7, 7, 2, 1, // left
7, 4, 3, 3, 2, 7, // bottom
4, 7, 6, 6, 5, 4 }; // back*/
int Cube::numVertices = 0;
int Cube::numColors = 0;
int Cube::numIndices = 0;
Cube::Cube()
{
}
Cube::~Cube()
{
}
void Cube::Draw()
{
glClear(GL_COLOR_BUFFER_BIT);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glPushMatrix();
glTranslatef(0.0f, 0.0f, 0.0f);
glRotatef(_rotation, 1.0f, 0.0f, 0.0f);
glVertexPointer(3, GL_FLOAT, 0, indexedVertices);
glColorPointer(3, GL_FLOAT, 0, indexedColors);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, indices);
glPopMatrix();
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glFlush(); //Flushes scene we just drew to the graphics card
glutSwapBuffers(); // Initialises double Buffering
glPopMatrix();
}
void Cube::Update()
{
//if (rotation >= 360.0f)
//rotation = 0.0f;
_rotation += 0.1f;
}
bool Cube::Load(char* path)
{
ifstream inFile;
inFile.open(path);
if (!inFile.good())
{
cerr << "Can't open texture file " << path << endl;
return false;
}
inFile >> numVertices;
indexedVertices = new Vertex[numVertices];
for (int i = 0; i < numVertices; i++)
{
inFile >> indexedVertices[i];
}
inFile >> numColors;
indexedColors = new Color[numColors];
for (int i = 0; i < numColors; i++)
{
inFile >> indexedColors[i];
}
inFile >> numIndices;
indices = new GLushort[numIndices];
for (int i = 0; i < numIndices; i++)
{
inFile >> indices[i];
}
inFile.close();
return true;
}
FirstGL.cpp只包含错误行。每个构造函数和头文件都已连接(在我必须使用txt文件之前,立方体类工作正常)。对不起,如果我包含太多代码或没有正确缩进,这是我第一次使用这个网站。谢谢。