Design question, regarding wrapping up OpenGL stuff

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 6531 Views - Last Post: 21 May 2011 - 04:48 AM Rate Topic: -----

#1 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Design question, regarding wrapping up OpenGL stuff

Posted 19 May 2011 - 01:30 AM

Hi guys, I'm getting right into OpenGL now, and I'm wrapping up all the OpenGL stuff so my code is a lot more abstract, but I'm not sure if I should be storing the x, y and z coordinates explicitly if I store the matrix.

I've implemented my own matrices and so on for sanity, and an Object which holds the VBO id, matrix, the mesh, etc. of that game "object". Here's what my object class looks like, without methods:


class Object
{ 
   public:
      Mesh mesh; // vector of class Vertex
      Mat4 matrix; // 4x4 matrix
      unsigned int shader, vbo, texture;
}



The reason I ask I did some reading and others have suggested storing explicit x, y and z coordinates as well as the matrix. What is everyone's opinion on this? What advantages are there to storing the x, y and z coordinates explicitly?

Please keep in mind that I haven't implemented collision detection as of yet in my engine, so I don't know if that affects my design as it stands. I'm really unexperienced with everything at the moment so there's some stuff I just don't know about yet.

Is This A Good Question/Topic? 0
  • +

Replies To: Design question, regarding wrapping up OpenGL stuff

#2 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

Re: Design question, regarding wrapping up OpenGL stuff

Posted 19 May 2011 - 02:53 AM

You should be working with 3D vectors, these will hold the position, velocity and acceleration of your object. To get the position of your object you could just return a vector made up of the position values of your matrix. You will still need the velocity to use when translating the position of your object.

Also don't you think "Object" is a little too vague for a class name?
Was This Post Helpful? 1
  • +
  • -

#3 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: Design question, regarding wrapping up OpenGL stuff

Posted 19 May 2011 - 03:03 AM

So you're saying that the internal structure is fine, and when I want the position, etc, just grab them from their slots in the matrix as a Vector?

Yes I do agree that Object is definitely too vague. I plan on changing it, but I'm not good with names. What sort of names would you suggest? I'd go with GameObject, but that's just as vague in my opinion.

This post has been edited by Hiram: 19 May 2011 - 03:04 AM

Was This Post Helpful? 0
  • +
  • -

#4 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

Re: Design question, regarding wrapping up OpenGL stuff

Posted 19 May 2011 - 04:19 AM

Quote

So you're saying that the internal structure is fine


Well you don't really have much of a class there for me to give an opinion :)

I think GameObject is a lot better, or GameEntity.

This post has been edited by stayscrisp: 19 May 2011 - 04:20 AM

Was This Post Helpful? 1
  • +
  • -

#5 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: Design question, regarding wrapping up OpenGL stuff

Posted 19 May 2011 - 04:24 AM

GameEntity! Awesome, thanks :D

Well, I thought I'd actually try storing the x, y and z coordinates to make translations and such easier, and it's definitely the way to go in my opinion.

You said that I don't have much of a class there, what else would you typically put in a class like this?

Sorry I'm asking such noobish questions, haha I'm completely out of my element with graphics programming. I really enjoy it though, I definitely aim to improve.
Was This Post Helpful? 0
  • +
  • -

#6 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

Re: Design question, regarding wrapping up OpenGL stuff

Posted 19 May 2011 - 04:33 AM

Well store them as vectors and then write some functions to transform them using the objects matrix.

What I mean by not much of a class is that it is not fleshed out, you don't have any member functions or get or set methods. You will most likely want some functions that return the current matrix and the data from inside the class.
Was This Post Helpful? 1
  • +
  • -

#7 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: Design question, regarding wrapping up OpenGL stuff

Posted 19 May 2011 - 04:51 AM

Oh, I already have a few methods for the class; e.g. getting the model coordinates for the centre of the object, buffering it to a VBO, drawing it, etc. I was just more concerned with how I should be storing x/y/z coordinates and if there's anything else I should definitely have.

The way I do it at the moment, is that my GameEntity class stores x, y and z as floats, and whenever I want to move my object, I modify those values, and construct a new Matrix with them, like this:

obj->matrix = Mat4::translate(x++, y, z);



That example of course results in my object moving along the X axis in a positive direction, which gives me the desired result (i.e. my object moves to position X, Y, Z) however I don't use my Vector class at all for this. Are you saying that I should be using my matrix to manipulate the X, Y and Z coordinates instead?

I'm afraid my maths isn't that strong, so I get confused with the mathematics pretty easily.

Thanks heaps for being so helpful!
Was This Post Helpful? 0
  • +
  • -

#8 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

Re: Design question, regarding wrapping up OpenGL stuff

Posted 19 May 2011 - 05:11 AM

Well you are using a matrix to do any transformations in OpenGL, that's what is happening when using the OpenGL functions glTranslate etc.

Using a vector your code could become
obj->matrix = Mat4::translate(m_pos.x, m_pos.y, m_pos.z);



or if the translate function in your matrix takes a vector instead of 3 floats
obj->matrix = Mat4::translate(m_pos);



I really recommend using vectors as they are essential to 3D applications and will need to be used at some point. Check out this link http://members.tripo...VectorLand.html and think about implementing the mathematics into a vector 3D class, including dot products, cross products and overloading operators to use vectors.

Glad I could help :)
Was This Post Helpful? 0
  • +
  • -

#9 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: Design question, regarding wrapping up OpenGL stuff

Posted 19 May 2011 - 05:18 AM

Oh! Of course, that makes sense now. I was confused because I thought you meant multiply my matrix against the coordinate vector to somehow get a new 4x4 matrix, which obviously wouldn't work :P

I should probably clarify, that I am using vectors a lot elsewhere in my code. I've got 2, 3 and 4 element vectors for various things (normals, texture coordinates, etc).

You're much more helpful than most graphics programmers I've met! I've tried getting help from a few other guys, and they haven't really helped, haha. Even my lecturer/teacher at university isn't the most helpful. Of course, I understand that's because this is definitely an area that's more problem-solving based than most other areas.

This post has been edited by Hiram: 19 May 2011 - 05:19 AM

Was This Post Helpful? 0
  • +
  • -

#10 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Design question, regarding wrapping up OpenGL stuff

Posted 19 May 2011 - 11:09 AM

View PostHiram, on 19 May 2011 - 09:30 AM, said:

Hi guys, I'm getting right into OpenGL now, and I'm wrapping up all the OpenGL stuff so my code is a lot more abstract, but I'm not sure if I should be storing the x, y and z coordinates explicitly if I store the matrix.

I've implemented my own matrices and so on for sanity, and an Object which holds the VBO id, matrix, the mesh, etc. of that game "object". Here's what my object class looks like, without methods:


class Object
{ 
   public:
      Mesh mesh; // vector of class Vertex
      Mat4 matrix; // 4x4 matrix
      unsigned int shader, vbo, texture;
}



The reason I ask I did some reading and others have suggested storing explicit x, y and z coordinates as well as the matrix. What is everyone's opinion on this? What advantages are there to storing the x, y and z coordinates explicitly?

Please keep in mind that I haven't implemented collision detection as of yet in my engine, so I don't know if that affects my design as it stands. I'm really unexperienced with everything at the moment so there's some stuff I just don't know about yet.

A couple of points immediately come to mind:

1. You have not shown anything anywhere near enough for us to give sound advice.
2. OpenGL already has basic types and internal matrix representations. Why are you attempting to redefine these when you will ultimately be using them anyway if using OpenGL?

Don't make life complicated for yourself. :)
Was This Post Helpful? 0
  • +
  • -

#11 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: Design question, regarding wrapping up OpenGL stuff

Posted 19 May 2011 - 02:26 PM

View PostButchDean, on 20 May 2011 - 04:09 AM, said:

A couple of points immediately come to mind:

1. You have not shown anything anywhere near enough for us to give sound advice.
2. OpenGL already has basic types and internal matrix representations. Why are you attempting to redefine these when you will ultimately be using them anyway if using OpenGL?

Don't make life complicated for yourself. :)


This is everything that I actually have the for the class (setters and getters excluded):

class GameEntity
{
	private:
		/**
			Returns the centre point for an axis.

			For centre point of x axis, use 0.
			For centre point of y axis, use 1.
			For centre point of z axis, use 2.

			\param k the axis to get the centre point for.
			\return centre point of axis k.
		*/
		float centre(int k);

		Mesh mesh;
		Mat4 matrix;
		unsigned int shader, vbo, texture;

	public:
		GameEntity(std::string meshFile= "", std::string textureFile = "", Mat4 m = Mat4());
		GameEntity(Mesh mesh, Mat4 m = Mat4());

		/**
			return centre point of cube on the x axis.

			\return x axis centroid.
		*/
		float centreX();

		/**
			return centre point of cube on the y axis.

			\return y axis centroid.
		*/
		float centreY();

		/**
			return centre point of cube on the z axis.

			\return z axis centroid.
		*/
		float centreZ();

		/**
		 * Make openGL calls to draw this object
		 */
		void draw();

		/**
		 * Send the mesh to the VBO and set vbo variable accordingly
		 */
		void buffer();
};


As of yet, I can't think of anything else that I'd have to have in the class.

When you say OpenGL has it's own internal matrix representations, are you talking about the GL_PROJECTION and GL_MODELVIEW states (and their matrices)? If so, I do agree that OpenGL has that functionality, but tracking the modelview matrices for each game entity without your own matrices isn't going to be easy in my opinion. How do you do it, if you don't have your own matrices? Maybe I'm missing something :P

Also, another reason for doing my own matrices, vectors, etc is that the university course I'm doing requires us to. That's not the primary motivator though, because I can't imagine tracking the modelview matrices for 1000+ objects using straight OpenGL :P
Was This Post Helpful? 0
  • +
  • -

#12 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

Re: Design question, regarding wrapping up OpenGL stuff

Posted 19 May 2011 - 02:40 PM

I think creating your own matrix class is a good way to learn and also you can optimise your code a lot more by having objects with their own precalculated matrices rather than continuous calls to glTranslate and glRotate :)
Was This Post Helpful? 0
  • +
  • -

#13 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Design question, regarding wrapping up OpenGL stuff

Posted 19 May 2011 - 03:51 PM

View PostHiram, on 19 May 2011 - 10:26 PM, said:

View PostButchDean, on 20 May 2011 - 04:09 AM, said:

A couple of points immediately come to mind:

1. You have not shown anything anywhere near enough for us to give sound advice.
2. OpenGL already has basic types and internal matrix representations. Why are you attempting to redefine these when you will ultimately be using them anyway if using OpenGL?

Don't make life complicated for yourself. :)


This is everything that I actually have the for the class (setters and getters excluded):

class GameEntity
{
	private:
		/**
			Returns the centre point for an axis.

			For centre point of x axis, use 0.
			For centre point of y axis, use 1.
			For centre point of z axis, use 2.

			\param k the axis to get the centre point for.
			\return centre point of axis k.
		*/
		float centre(int k);

		Mesh mesh;
		Mat4 matrix;
		unsigned int shader, vbo, texture;

	public:
		GameEntity(std::string meshFile= "", std::string textureFile = "", Mat4 m = Mat4());
		GameEntity(Mesh mesh, Mat4 m = Mat4());

		/**
			return centre point of cube on the x axis.

			\return x axis centroid.
		*/
		float centreX();

		/**
			return centre point of cube on the y axis.

			\return y axis centroid.
		*/
		float centreY();

		/**
			return centre point of cube on the z axis.

			\return z axis centroid.
		*/
		float centreZ();

		/**
		 * Make openGL calls to draw this object
		 */
		void draw();

		/**
		 * Send the mesh to the VBO and set vbo variable accordingly
		 */
		void buffer();
};


As of yet, I can't think of anything else that I'd have to have in the class.

When you say OpenGL has it's own internal matrix representations, are you talking about the GL_PROJECTION and GL_MODELVIEW states (and their matrices)? If so, I do agree that OpenGL has that functionality, but tracking the modelview matrices for each game entity without your own matrices isn't going to be easy in my opinion. How do you do it, if you don't have your own matrices? Maybe I'm missing something :P

Also, another reason for doing my own matrices, vectors, etc is that the university course I'm doing requires us to. That's not the primary motivator though, because I can't imagine tracking the modelview matrices for 1000+ objects using straight OpenGL :P

Okay, so the way you are being taught is to use a library with its own matrix functionality by creating your own variables that have been processed by your own matrix manipulations. Fair enough I guess.

I've noticed that you don't like using enumerations and pointers in your code. Do you understand them? The reason why I ask is because for determining the 'center point' you could use an enumeration with values X_AXIS, Y_AXIS, etc. This prevents you from having to handle the exception of someone entering (or having an algorithm that does so) the number 10 for instance as there is no 'tenth axis'.

For the pointers, you are passing everything by value with will bite you badly in the long term. Consider pointers where you can as the class develops.

View Poststayscrisp, on 19 May 2011 - 10:40 PM, said:

I think creating your own matrix class is a good way to learn and also you can optimise your code a lot more by having objects with their own precalculated matrices rather than continuous calls to glTranslate and glRotate :)

You won't beat the optimization already in OpenGL, but I guess it is a good exercise. :)
Was This Post Helpful? 0
  • +
  • -

#14 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: Design question, regarding wrapping up OpenGL stuff

Posted 19 May 2011 - 07:06 PM

View PostButchDean, on 20 May 2011 - 08:51 AM, said:

Okay, so the way you are being taught is to use a library with its own matrix functionality by creating your own variables that have been processed by your own matrix manipulations. Fair enough I guess.

I've noticed that you don't like using enumerations and pointers in your code. Do you understand them? The reason why I ask is because for determining the 'center point' you could use an enumeration with values X_AXIS, Y_AXIS, etc. This prevents you from having to handle the exception of someone entering (or having an algorithm that does so) the number 10 for instance as there is no 'tenth axis'.

For the pointers, you are passing everything by value with will bite you badly in the long term. Consider pointers where you can as the class develops.

You won't beat the optimization already in OpenGL, but I guess it is a good exercise. :)


Thanks for that, I'll add in the enumerations. I generally work on the idea that if another programmer were to put the number 10 in for getting the centre of "dimension 10", it's their fault for doing it wrong :P but that's fair enough, I'm happy to change that. I'm using enumerations for heaps of other stuff, like my move method takes an enum.

I'm also using pointers everywhere else in my code, just not for my mesh or matrix. But I'll definitely change those, thanks!

As for not beating the optimisation already in OpenGL, I'm going to have to disagree. I could implement my matrix multiplication with SSE(2/3), or even look into using OpenCL, or lots of different things, all of which may or may not be present in OpenGL's matrix operations.

Also, I'm given to understanding that as of OpenGL 3, you're forced to manage your own matrices because the matrix operations were deprecated, so wouldn't it make sense to do what I'm doing?
Was This Post Helpful? 0
  • +
  • -

#15 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Design question, regarding wrapping up OpenGL stuff

Posted 19 May 2011 - 11:26 PM

View PostHiram, on 20 May 2011 - 03:06 AM, said:

As for not beating the optimisation already in OpenGL, I'm going to have to disagree. I could implement my matrix multiplication with SSE(2/3), or even look into using OpenCL, or lots of different things, all of which may or may not be present in OpenGL's matrix operations.

So you're claiming that you can beat a graphics library developed for high performance workstations and trusted by countless graphics programmers both in and out of games? Okay.

View PostHiram, on 20 May 2011 - 03:06 AM, said:

Also, I'm given to understanding that as of OpenGL 3, you're forced to manage your own matrices because the matrix operations were deprecated, so wouldn't it make sense to do what I'm doing?

I would have to look into this to find out exactly what is meant. Are you sure that you don't mean that the interfaces are deprecated for improved versions rather than it meaning that you implement your own matrices?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2