Original Post
Hello! I'm writing a model loader for my own 3D model format, and before rendering I'm loading all the vertex data and so on into vectors. I've found an example in the source code accompanying Evan Pipho's "Focus on 3D models" on how to use std::vector's of 2 dimensions: And then: And finally, after loading the data like this... ...it is addressed like this... ...and accessed like this:
I tried to implement those ideas myself like this... ...but upon compiling I got this:
[source lang=cpp]
struct Vector3
{
float m_fVec[3];
Vector3(float * fVec)
{
memcpy(m_fVec, fVec, sizeof(float[3]));
}
};
[source lang=cpp]
vector<Vector3> m_vVertices;
Vector3 * m_pVerts;
[source lang=cpp]
//get the line
fgets(cLine, 256, fp);
//get the vertex coords
sscanf(cLine, " %f %f %f", &fTemp[0], &fTemp[1], &fTemp[2]);
//add to the vertex array
m_vVertices.push_back(fTemp);
[source lang=cpp]
m_pVerts = &m_vVertices[0];
[source lang=cpp]
glVertex3fv(m_pVerts[vertexIndex].m_fVec);
I tried to implement those ideas myself like this...
[source lang=cpp]
#include <cstdlib>
#include <cstring>
#include <vector>
#include <iostream>
#include <fstream>
#include <windows.h>
#include <GL/GL.h>
int main(int argc, char *argv[]){
struct vertVector{
GLfloat co[3];
vertVector(GLfloat *inCo){
memcpy(co, inCo, sizeof(GLfloat[3]));
}
};
unsigned int hMagic, fMagic;
char hIdent[5]=" ", fIdent[5]=" ";
unsigned int vCount, fCount;
std::ifstream inFile;
inFile.open("house.opsm");
if(inFile){
{
inFile.read((char*)&hMagic,
std::streamsize(sizeof(hMagic)));
inFile.read((char*)&hIdent,
std::streamsize(sizeof(char[4])));
inFile.read((char*)&vCount,
std::streamsize(sizeof(vCount)));
inFile.read((char*)&fCount,
std::streamsize(sizeof(fCount)));
}
printf("Header:\nMagic number: %i\tIdent: %s\nVertices: %i\tFaces: %i\nIdent correct: %i\n",
hMagic, hIdent, vCount, fCount,
!strcmp(hIdent, "OPSM"));
std::vector<vertVector> verts;
vertVector *vertsPtr;
GLfloat tempVert[3];
{
for(unsigned int i=0;i<vCount;i++){
inFile.read((char*)&tempVert,
std::streamsize(sizeof(GLfloat[3])));
verts.push_back(tempVert);
}
}
vertsPtr=&verts[0];
for(unsigned int i=0;i<vCount;i++)
printf("I: %i\t X: %f\tY: %f\tZ: %f\n",
i, vertsPtr.co[0], vertsPtr.co[1], vertsPtr.co[2]);
}
else{
printf("File not found!\n");
}
inFile.close();
return 0;
}
Compiler: Default compiler
Building Makefile: "G:\Documents and Settings\Dim-Or Jagot\Mina dokument\programmering\Projects\opModelLoader\Makefile.win"
Executing make...
make.exe -f "G:\Documents and Settings\Dim-Or Jagot\Mina dokument\programmering\Projects\opModelLoader\Makefile.win" all
g++.exe -c main.cpp -o main.o
main.cpp: In function `int main(int, char**)':
main.cpp:38: error: `main(int, char**)::vertVector' uses local type `main(int, char**)::vertVector'
main.cpp:38: error: trying to instantiate `template<class _Alloc> class std::allocator'
main.cpp:38: error: template argument 2 is invalid
main.cpp:38: error: invalid type in declaration before ';' token
main.cpp:48: error: `push_back' has not been declared
main.cpp:48: error: request for member of non-aggregate type before '(' token
main.cpp:51: error: invalid types `int[int]' for array subscript
make.exe: *** [main.o] Error 1
Execution terminated
What am I doing wrong? I might mention that Pipho declared the structs and the vectors in a header to a wrapper class, but I can't see that that should make any difference. I'm using MinGW32 right now, and compiling Evan Pipho's code whith g++ did not pose any problem, so I can't understand why my code's not working. Have a nice day! Stefanos Carlström