Jump to content
  • Advertisement
Sign in to follow this  
renderkid

OpenGL Custom math data structure accessing same memory block

This topic is 966 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone,

 

I have been having problems with my GLXMATRIX data, when I was sending it to GLSL shader.
So I decided to do test with a simple triangle and to see if there is a problem with my math or data structure.
 
I created several variables:
  • GLXMATRIX projection
  • GLXMATRIX translate

Then I initialized them as Identity matrices and I uploaded perspective data to projection matrix and scaling data to translation matrix:

  • GLXMatrixPerspectiveCM(&projection, 45.0f, 800.0f/600.0f, 0.1f, 100.0f);
  • GLXMatrixScaling(&translate, 0.5f, 0.5f, 0.5f);

Perspective collumn major function looks like this.

GLXMATRIX* GLXMatrixPerspectiveCM(GLXMATRIX *pOut,float fov,float aspect, float zn,float zf) {
#ifdef TYW_DEBUG
	if (!pOut)return nullptr;
#endif
	float q = 1.0f / tan(0.5f*fov * (PI / 180.0f));
	float A = q / aspect;
	float B = (zn + zf) / (zn - zf);
	float C = (2.0f * zn * zf) / (zn - zf);

	pOut[0][0] = A;
	pOut[1][1] = q;
	pOut[2][2] = B;
	pOut[2][3] = C;
	pOut[3][2] = -1;
	pOut[3][3] = 0;
	return pOut;
}

Then I sended those matrices to shader.

 //SetUniformMatrix4fv(std::string progIndx, std::string unfIndx, int count, bool transpose, const float* value);

  • renderProgManager.SetUniformMatrix4fv("text.glsl", "objectPosition", 1, false, translate);
  • renderProgManager.SetUniformMatrix4fv("text.glsl", "orthoMatrix", 1, false, projection);

But instead of seing triangle properly I got this.

[sharedmedia=core:attachments:29805]
 
I could not get what was wrong. I thought the problem will be with math but it couldn't be becaue I used the same math code from OpenGL superbible. So I decided to test if my perspective matrix is wrong or is something else fucks up.This time I didn't send projection matrix and sended only translation matrix. I thought that the triangle will display at least normaly. But guess what, it wasn't. It was displaying the same, even if my translation matrix was equals to identity. The I figured out that somewhy it access same memory block when I'm modifying it.
 
So I decided to create GLXMATRIX pointer to see if I was right.
  • GLXMATRIX* projection
  • GLXMATRIX* translate

I created memory space for them.

  • projection = new GLXMATRIX;
  • translate = new GLXMATRIX;
Then I did all the same things that I did before. Just needed to put in some places " * " operator that I could send data to shader. Luckily, this time the triangle looked as it was supposed to look.
[sharedmedia=core:attachments:29806]
 
The thing is that I can't understand why it was like this and maybe any of you have an answer?
The data structure is basicly the same as it is defined in D3DX math library.
 
#ifndef GLMATRIX_DEFINED
typedef struct _GLMATRIX {
	union {
		struct {
			float _11, _21, _31, _41;
			float _12, _22, _32, _42;
			float _13, _23, _33, _43;
			float _14, _24, _34, _44;
		};
		float m[4][4];
	};
}GLMATRIX;
#define GLMATRIX_DEFINED
#endif

typedef struct GLXMATRIX : public GLMATRIX {
public:
	GLXMATRIX() {}
	GLXMATRIX(const float*);
	GLXMATRIX(GLXMATRIX &);
	GLXMATRIX(
		float m11, float m21, float m31, float m41,
		float m12, float m22, float m32, float m42,
		float m13, float m23, float m33, float m43,
		float m14, float m24, float m34, float m44);

	//acces grants
	float& operator () (unsigned int row, unsigned int col);
	float  operator () (unsigned int row, unsigned int col) const;

	//casting operators
	operator float* ();
	operator const float* () const;

	//assignement operators
	GLXMATRIX& operator *= (const  GLXMATRIX&);
	GLXMATRIX& operator += (const  GLXMATRIX&);
	GLXMATRIX& operator -= (const  GLXMATRIX&);
	GLXMATRIX& operator *= (float);
	GLXMATRIX& operator /= (float);

	//unary operators
	GLXMATRIX operator + () const;
	GLXMATRIX operator - () const;

	//binary operators
	GLXMATRIX operator * (const GLXMATRIX&) const;
	GLXMATRIX operator + (const GLXMATRIX&) const;
	GLXMATRIX operator - (const GLXMATRIX&) const;
	GLXMATRIX operator * (float) const;
	GLXMATRIX operator / (float) const;

	friend GLXMATRIX operator * (float, const GLXMATRIX&);

	bool operator == (const GLXMATRIX&) const;
	bool operator != (const GLXMATRIX&) const;
}GLXMATRIX, *LPGLXMATRIX;

//casting operators
inline GLXMATRIX::operator float* ()
{
	return (float*)&_11;
}

inline GLXMATRIX::operator const float* () const
{
	return (const float*)&_11;
}

 

 

Share this post


Link to post
Share on other sites
Advertisement

When I wrote this article I noticed that the way I access array in Perspective function was totally wrong and this explains why it was like this. Strangely I was not getting any errors.

So the fixed math function looks like this

GLXMATRIX* GLXMatrixPerspectiveCM(GLXMATRIX *pOut,	float fov, float aspect, float zn,	float zf) {
#ifdef TYW_DEBUG
	if (!pOut)return nullptr;
#endif
	float q = 1.0f / tan(0.5f*fov * (PI / 180.0f));
	float A = q / aspect;
	float B = (zn + zf) / (zn - zf);
	float C = (2.0f * zn * zf) / (zn - zf);

	pOut->m[0][0] = A;
	pOut->m[1][1] = q;
	pOut->m[2][2] = B;
	pOut->m[2][3] = C;
	pOut->m[3][2] = -1;
	pOut->m[3][3] = 1;
	return pOut;
}

However in OpenGL supperbible at the array location [3][3] the data is setted to zero but in here I setted to one because the triangle was not showing to me. Do you know why it is like this?

Edited by renderkid

Share this post


Link to post
Share on other sites

I managed to solve the problem with perspective matrix. Jus needed to position triangle on z = -1 axis. However I found out that I have to use row major and not collumn major even though my multiplication in shader looks like this

  • gl_Position = perspective * translate * rotation * object;

As the main problem of this thread was solved I moved to new thread which has the problem with rotation-> http://www.gamedev.net/topic/673538-rotation-matrix-stretches-geometry/

Edited by renderkid

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!