1

首先,我们(我和我的朋友)正在开展一个计算机图形项目,我们应该在该项目中模拟粒子(在我们的例子中,小立方体 (mesh3d)),这些粒子将在空间环境中相互交互。也就是说,数百个粒子使用物理定律爆炸并相互吸引。

我们的实施基于我们学校提供的框架,因此我们必须“适应”它。

在创建立方体 (mesh3d) 时,我们提供了包含纹理的材质。

Mesh3D* 
SolarViewer::
createCube()
{
// initialize Mesh3D
Mesh3D *cube = new Mesh3D();
MeshMaterial* mat = new MeshMaterial; //we create the material for the cube
mat->m_diffuseTexture.create("particle.tga"); //we create the diffuse texture of the material to be particle.tga

// setup uniform cube with side length 0.5 and center of cube being (0,0,0)
std::vector< Vector3 > cubeVertices;
std::vector< Vector3 > cubeNormals;
std::vector< Vector3 > cubeColors;
std::vector< unsigned int > cubeIndices;
float d = 1.0;


// front
cubeVertices.push_back(Vector3(-d,-d, d));
cubeVertices.push_back(Vector3( d,-d, d));
cubeVertices.push_back(Vector3( d, d, d));
cubeVertices.push_back(Vector3(-d, d, d));
for(int k = 0; k < 4; k++) cubeNormals.push_back(Vector3(0,0,1));
for(int k = 0; k < 4; k++) cubeColors.push_back(Vector3(0.8,0.3,0.3));
cubeIndices.push_back(0);
cubeIndices.push_back(1);
cubeIndices.push_back(2);
cubeIndices.push_back(0);
cubeIndices.push_back(2);
cubeIndices.push_back(3);


// right
cubeVertices.push_back(Vector3( d,-d,-d));
cubeVertices.push_back(Vector3( d,-d, d));
cubeVertices.push_back(Vector3( d, d, d));
cubeVertices.push_back(Vector3( d, d,-d));
for(int k = 0; k < 4; k++) cubeNormals.push_back(Vector3(1,0,0));
for(int k = 0; k < 4; k++) cubeColors.push_back(Vector3(0.3,0.8,0.3));
cubeIndices.push_back(4);
cubeIndices.push_back(5);
cubeIndices.push_back(6);
cubeIndices.push_back(4);
cubeIndices.push_back(6);
cubeIndices.push_back(7);


// back
cubeVertices.push_back(Vector3( d,-d,-d));
cubeVertices.push_back(Vector3(-d,-d,-d));
cubeVertices.push_back(Vector3(-d, d,-d));
cubeVertices.push_back(Vector3( d, d,-d));
for(int k = 0; k < 4; k++) cubeNormals.push_back(Vector3(0,0,-1));
for(int k = 0; k < 4; k++) cubeColors.push_back(Vector3(0.3,0.3,0.8));
cubeIndices.push_back(8);
cubeIndices.push_back(9);
cubeIndices.push_back(10);
cubeIndices.push_back(8);
cubeIndices.push_back(10);
cubeIndices.push_back(11);


// left
cubeVertices.push_back(Vector3(-d,-d, d));
cubeVertices.push_back(Vector3(-d,-d,-d));
cubeVertices.push_back(Vector3(-d, d,-d));
cubeVertices.push_back(Vector3(-d, d, d));
for(int k = 0; k < 4; k++) cubeNormals.push_back(Vector3(-1,0,0));
for(int k = 0; k < 4; k++) cubeColors.push_back(Vector3(0.8,0.8,0.3));
cubeIndices.push_back(12);
cubeIndices.push_back(13);
cubeIndices.push_back(14);
cubeIndices.push_back(12);
cubeIndices.push_back(14);
cubeIndices.push_back(15);


// top
cubeVertices.push_back(Vector3(-d, d,-d));
cubeVertices.push_back(Vector3( d, d,-d));
cubeVertices.push_back(Vector3( d, d, d));
cubeVertices.push_back(Vector3(-d, d, d));
for(int k = 0; k < 4; k++) cubeNormals.push_back(Vector3(0,1,0));
for(int k = 0; k < 4; k++) cubeColors.push_back(Vector3(0.8,0.3,0.8));
cubeIndices.push_back(16);
cubeIndices.push_back(17);
cubeIndices.push_back(18);
cubeIndices.push_back(16);
cubeIndices.push_back(18);
cubeIndices.push_back(19);


// bottom
cubeVertices.push_back(Vector3( d,-d,-d));
cubeVertices.push_back(Vector3(-d,-d,-d));
cubeVertices.push_back(Vector3(-d,-d, d));
cubeVertices.push_back(Vector3( d,-d, d));
for(int k = 0; k < 4; k++) cubeNormals.push_back(Vector3(0,-1,0));
for(int k = 0; k < 4; k++) cubeColors.push_back(Vector3(0.3,0.8,0.8));
cubeIndices.push_back(20);
cubeIndices.push_back(21);
cubeIndices.push_back(22);
cubeIndices.push_back(20);
cubeIndices.push_back(22);
cubeIndices.push_back(23);


cube->setIndices(cubeIndices, mat); //we set the cubeIndices and the material for the cube
cube->setVertexPositions(cubeVertices);
cube->setVertexNormals(cubeNormals);
cube->setVertexColors(cubeColors);

    return cube;
}

然后我们在绘制场景方法中遍历所有立方体以绘制它们

for (std::vector<Mesh3D*>::iterator mIt = m_meshes.begin(); mIt != m_meshes.end(); ++mIt)
                {
                    Mesh3D* cube =  *mIt;           
                    m_meshShaderTexture.setMatrix4x4Uniform("modelworld", cube->getTransformation());
                   cube->getMaterial(0).m_diffuseTexture.bind();
                   m_meshShaderTexture.setIntUniform("texture", cube->getMaterial(0).m_diffuseTexture.getLayer());
                   draw_object(m_meshShaderTexture, cube);// method called to draw the cube

                }

我们在用于绘制 mesh3d 对象的方法“drawObject”中调用了这条线:

    void SolarViewer::draw_object(Shader& sh, Mesh3D *mesh)
{

sh.setMatrix4x4Uniform("modelworld", mesh->getTransformation() );

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glVertexPointer( 3, GL_DOUBLE, 0, mesh->getVertexPointer() );
glNormalPointer( GL_DOUBLE, 0, mesh->getNormalPointer() );
glTexCoordPointer( 2, GL_DOUBLE, 0, mesh->getUvTextureCoordPointer() );


for(unsigned int i = 0; i < mesh->getNumberOfParts(); i++)
{   
    glDrawElements( GL_TRIANGLES, mesh->getNumberOfFaces()*3, GL_UNSIGNED_INT, mesh->getVertexIndicesPointer() );

}

glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);

}

但最后,在尝试编译时,以下行给了我们一个 BAD_ACCESS :(

glDrawElements( GL_TRIANGLES, mesh->getNumberOfFaces()*3, GL_UNSIGNED_INT, mesh->getVertexIndicesPointer() );

我们做错了什么?绑定以下纹理(.tga)是否足以产生发光效果?: http://i166.photobucket.com/albums/u83/j1m68/star.jpg

4

0 回答 0