.obj ファイルを解析し、.obj ファイルで定義されたモデルを OpenGL でレンダリングする C++ プログラムを作成しようとしています。これまでのところ、このコードで行うことになっているのは、.obj ファイルを開き、すべての頂点をベクトルに入れることです (.obj ファイルの頂点は、"v" で始まる行で定義されています)。
私の完全なコードは次のとおりです。
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct vec3{
float x;
float y;
float z;
};
void loadOBJ(const char * Path){
vector<vec3> Vertices;
FILE * OBJFile;
vec3 temp = vec3();
fopen_s(&OBJFile, Path, "r");
char lineHeader[128];
//set to true when there are no more lines in the OBJ file
bool ended = false;
while(!ended){
fscanf_s(OBJFile, "%s", lineHeader);
if(strcmp(lineHeader,"v") == 0){
fscanf_s(OBJFile, "%f %f %f\n", &temp.x, &temp.y, &temp.z);
printf("Point: %f %f %f\n", temp.x, temp.y, temp.z);
Vertices.push_back(temp);
}else if(lineHeader != NULL){
fscanf_s(OBJFile, "\n");
}
else{
ended = true;
}
}
}
int main(){
loadOBJ("example.obj");
cin.get();
return 0;
}
問題はラインで発生します
fscanf_s(OBJFile, "%s", lineHeader);
この行をコメントアウトすると、最初のチャンスの例外が発生しなくなります。文字列の代わりに char を使用すると、初回例外も発生しません。