Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualRobTheBloke

Posted 12 March 2013 - 05:32 PM

Btw I did change the BufferData to &vertices[0].pos.x but what difference does it make if it has the .pos.x or if it doesn't? Won't it work properly either way?

That line of code is fine. Sponji was just having a funny five minutes.....

 

Right then. Where to begin with this. I'd probably recommend changing your ctor to take a const ref, rather than duplicating the array...  i.e.


Mesh::Mesh (const std::vector<Vertex>& _vertices)
 

If you ever find yourself putting  using namespace std; in a header file, you're doing it wrong. Using namespace must only be used in a CPP file, after all #includes. This does mean you need to use the std:: prefix in all headers, but that's a good habit to get into. You're just heading for another set of hideous problems if you keep doing that.

 

they just become modified O.o


Yeah, hate to break it to you, but stuff doesn't just randomly modify itself. It means you have a bug, and you're modifying the data somewhere.

Right then. Modify your Mesh class to be this:
 

struct Mesh {
public:

  Mesh ();
  Mesh (vector<Vertex>);

  void Render ();

private:

  // these will not be called.
  inline Mesh (const Mesh&);
  inline const Mesh& operator = (const Mesh&);

  GLuint VBO; // Vertex Buffer Object
  std::vector<Vertex> vertices;
};


Hit compile. Has it spat out a bunch of compile errors? Good. Those are the locations of your bug(s).

If you ever find yourself writing a function declaration like this:

 

Mesh CreateMesh()
{
}


Take yourself outside, slap yourself around a bit, and go and read the chapter on C++ pointers, followed by the chapter on C++ references. Learn how to use them, and your problems will be greatly reduced.

That Mesh you're returning. Does it have a destructor? Does that destructor delete the vertex buffer? Is it therefore a good idea to be creating a temporary Mesh, which you're then assigning to another Mesh object elsewhere, which is then deleted when it falls out of scope? (but since you haven't defined a copy ctor or assingment op, the VBO's are being killed off). Have you considered maybe reading that chapter on pointers again? Now is probably a good time to do so!
 

Mesh* CreateMesh()
{
   return new Mesh( vertices );
}

// elsewhere....
Mesh* g_mesh = CreateMesh();

// and when you are absolutely finished with it....
delete g_mesh;

 

Problem solved?

It now all runs in a Application class which also has a list of all the models in my application. I then add a new model and all models get rendered in a for loop in the RenderScene function.



Let me guess....


struct Model
{
public:

private:
   std::vector<Mesh> m_meshes;
};
 
struct App
{
public:

private:
   std::vector<Model> m_models;
};


* facepalm *

#1RobTheBloke

Posted 12 March 2013 - 05:28 PM

Btw I did change the BufferData to &vertices[0].pos.x but what difference does it make if it has the .pos.x or if it doesn't? Won't it work properly either way?

That line of code is fine. Sponji was just having a funny five minutes.....

 

Right then. Where to begin with this. I'd probably recommend changing your ctor to take a const ref, rather than duplicating the array...  i.e.


Mesh::Mesh (const std::vector<Vertex>& _vertices)
 

If you ever find yourself putting  using namespace std; in a header file, you're doing it wrong. Using namespace must only be used in a CPP file, after all #includes. This does mean you need to use the std:: prefix in all headers, but that's a good habit to get into. You're just heading for another set of hideous problems if you keep doing that.


they just become modified O.o


Yeah, hate to break it to you, but stuff doesn't just randomly modify itself. It means you have a bug, and you're modifying the data somewhere.

Right then. Modify your Mesh class to be this:

struct Mesh {
public:

  Mesh ();
  Mesh (vector<Vertex>);

  void Render ();

private:

  // these will not be called.
  inline Mesh (const Mesh&);
  inline const Mesh& operator = (const Mesh&);

  GLuint VBO; // Vertex Buffer Object
  std::vector<Vertex> vertices;
};


Hit compile. Has it spat out a bunch of compile errors? Good. Those are the locations of your bug(s).

If you ever find yourself writing a function declaration like this:


Mesh CreateMesh()
{
}


Take yourself outside, slap yourself around a bit, and go and read the chapter on C++ pointers, followed by the chapter on C++ references. Learn how to use them, and your problems will be greatly reduced.

That Mesh you're returning. Does it have a destructor? Does that destructor delete the vertex buffer? Is it therefore a good idea to be creating a temporary Mesh, which you're then assigning to another Mesh object elsewhere, which is then deleted when it falls out of scope? (but since you haven't defined a copy ctor or assingment op, the VBO's are being killed off). Have you considered maybe reading that chapter on pointers again? Now is probably a good time to do so!

Mesh* CreateMesh()
{
   return new Mesh( vertices );
}

// elsewhere....
Mesh* g_mesh = CreateMesh();

// and when you are absolutely finished with it....
delete g_mesh;


Problem solved?

PARTNERS