程序從紡織物讀取頂點和索引,並將頂點存儲在向量中,並將索引存儲在數組中。赫雷什是quad.txt -使用glm-library計算法線OpenGL
4 2 numVertices numIndices
-1 -1 0 V1 V2 V3
1 -1 0
-1 1 0
然後我渲染對象到屏幕 - 沒問題。但是當我嘗試計算法線和平坦陰影時,我遇到了問題。該對象用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);
}
嘗試打印出正常值,以查看它們是否有意義 – Grimmy