该程序从织物中读取顶点和索引,并将顶点存储在向量中,将索引存储在数组中。继承人是 quad.txt -
4 2 numVertices numIndices
-1 -1 0 v1 v2 v3
1 -1 0
1 1 0
-1 1 0
1 2 3
1 3 4
然后我将对象渲染到屏幕上——没问题。但是当我尝试计算法线和平面阴影时,我遇到了问题。该对象使用 GL_TRINANGLES 进行渲染。看起来像这样http://imgur.com/wnKWP(没有足够的声誉直接上传......)
就像三角形被单独着色......无论如何这里是代码:
#include "Object.h"
#include <vector>
#include <iostream>
#include <fstream>
#include <cstring>
#include <glm.hpp>
using namespace std;
const int NMAX = 20000;
GLuint indicesArr[NMAX];
vector<glm::vec3> drawFigure; glm::vec3 figure;
vector<glm::vec3>Vertices; glm::vec3 vertex;
vector<glm::vec3>Normals; glm::vec3 normal;
vector<glm::vec3>Temp; glm::vec3 temp;
int numVertices, numIndices;
//slices = number of subdivisions around the axis
Object::Object() :
mIsInitialised(false)
{
}
Object::~Object()
{
if(mIsInitialised)
{
drawFigure.clear();
Vertices.clear();
Normals.clear();
Temp.clear();
}
}
bool Object::loadTxtFile(const string &filename)
{
ifstream in(filename);
in >> numVertices >> numIndices;
while (!in.eof())
{
for (unsigned int i = 1; i <= numVertices; i++)
{
in >> vertex.x >> vertex.y >> vertex.z;
Vertices.push_back(vertex);
}
for (unsigned int i = 1; i <= numIndices * 3; i++)
in >> indicesArr[i];
}
for (unsigned int i = 1; i <= (numIndices * 3); i++)
{
figure.x = Vertices[indicesArr[i] - 1].x;
figure.y = Vertices[indicesArr[i] - 1].y;
figure.z = Vertices[indicesArr[i] - 1].z;
drawFigure.push_back(figure);
}
for (unsigned int i = 0; i < (numIndices * 3); i+=3)
{
temp.x = drawFigure[i].x;
temp.y = drawFigure[i].y;
temp.z = drawFigure[i].z;
Temp.push_back(temp);
temp.x = drawFigure[i + 1].x;
temp.y = drawFigure[i + 1].y;
temp.z = drawFigure[i + 1].z;
Temp.push_back(temp);
temp.x = drawFigure[i + 2].x;
temp.y = drawFigure[i + 2].y;
temp.z = drawFigure[i + 2].z;
Temp.push_back(temp);
normal = glm::normalize(glm::cross(Temp[i + 2] - Temp[i], Temp[i + 1] - Temp[i]));
Normals.push_back(normal);
}
mIsInitialised = true;
return true; //Return true if successfully loaded
}
void Object::draw()
{
if(!mIsInitialised) { return; }
//Tell OpenGL about our vertex and colour data
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, &drawFigure.front());
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, 0, &Normals.front());
//draw the .txt-file
glDrawArrays(GL_TRIANGLES, 0, (numIndices * 3));
//restore the state GL back
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
}