0
程序成功加載所有內容,代碼完全髒,我接受但我至少試圖畫一些東西。但它似乎出了問題,沒有錯誤,沒有顯示:PglDrawArrays不會繪製任何東西?
PS:投影,加載文件等一切都設置正確。
objloader.h
#pragma once
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>
#include "opengl.h"
#include "terrain.h"
struct VertexCoord
{
float x,y,z,w;
};
struct TextureCoord
{
float u,v;
};
struct NormalCoord
{
float x,y,z;
};
struct ColorComp
{
float r,g,b;
};
struct VBOVertex
{
float x,y,z,w;
float nx,ny,nz;
float u,v;
float r,g,b;
};
class ObjLoader
{
GLuint VBO;
std::vector<VertexCoord> vertexData;
std::vector<NormalCoord> normalData;
std::vector<TextureCoord> textureData;
std::vector<ColorComp> colorData;
int numVerts;
char *modeldata;
public:
ObjLoader(void);
~ObjLoader(void);
void LoadBuffers(const char* fileName);
void render();
};
objloader.cpp
#include "ObjLoader.h"
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
ObjLoader::ObjLoader(void)
{
glGenBuffers(1,&VBO);
numVerts=0;
}
ObjLoader::~ObjLoader(void)
{
}
void ObjLoader::LoadBuffers(const char* fileName) {
ifstream fs(fileName,ios::in);
string line;
vector<string> faceList;
while (getline(fs,line)) {
if (line.substr(0,2) == "v ") {
istringstream s(line.substr(2));
VertexCoord ver;
s >> ver.x >> ver.y >> ver.z;
vertexData.push_back(ver);
}
else if (line.substr(0,3) == "vt ") {
istringstream s(line.substr(3));
TextureCoord ver;
s >> ver.u >> ver.v;
textureData.push_back(ver);
}
else if (line.substr(0,3) == "vn ") {
istringstream s(line.substr(2));
NormalCoord ver;
s >> ver.x >> ver.y >> ver.z;
normalData.push_back(ver);
}
else if (line.substr(0,2) == "f ") {
faceList.push_back(line);
}
else if (line[0] == '#'){ }
else { }
}
modeldata=new char[faceList.size()*3*sizeof(VBOVertex)];
for (int i=0; i < faceList.size(); i++) {
VBOVertex newvert[3];
int v[3],n[3],t[3];
sscanf(faceList[i].c_str(),"f %d/%d/%d %d/%d/%d %d/%d/%d",&v[0],&t[0],&n[0],
&v[1],&t[1],&n[1],
&v[2],&t[2],&n[2]);
for (int j=0;j < 3; j++) {
newvert[j].x=vertexData[v[j]-1].x;
newvert[j].y=vertexData[v[j]-1].y;
newvert[j].z=vertexData[v[j]-1].z;
newvert[j].w=vertexData[v[j]-1].w;
newvert[j].u=textureData[t[j]-1].u;
newvert[j].v=textureData[t[j]-1].v;
newvert[j].nx=normalData[n[j]-1].x;
newvert[j].ny=normalData[n[j]-1].y;
newvert[j].nz=normalData[n[j]-1].z;
}
memcpy(modeldata + (i*3*sizeof(VBOVertex)),newvert,3*sizeof(VBOVertex));
numVerts++;
}
glBindBuffer(GL_ARRAY_BUFFER,VBO);
glBufferData(GL_ARRAY_BUFFER,faceList.size()*3*sizeof(VBOVertex),(char*)modeldata,GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER,0);
}
void ObjLoader::render() {
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, sizeof(VBOVertex), BUFFER_OFFSET(7 * sizeof(float)));
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, sizeof(VBOVertex), BUFFER_OFFSET(4 * sizeof(float)));
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(VBOVertex), BUFFER_OFFSET(0));
glDrawArrays(GL_TRIANGLES,0,numVerts);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}