C++; Better way to convert Double 4x4 Matrix to OpenGL Float 4x4 Matrix

Hello,

I have the following code snippet that works below. Long story short, I need to convert a 4x4 matrix which is a double value to a float based 4x4 value which OpenGL will accept.


Is there a better way to write what is written below or is this the best and fastest way?

Looking for efficiency and speed.

Thank you.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

/*

pGlobalPosition is a double 4x4 Matrix exported from an SDK

*/

pGlobalPosition

/*

I am trying to put the value in an equivalent matrix
that is acceptable and fast to OpenGL.

*/

GLfloat fGlobalPosition[16];

fGlobalPosition[0] = (float)pGlobalPosition[0][0];
fGlobalPosition[1] = (float)pGlobalPosition[1][0];
fGlobalPosition[2] = (float)pGlobalPosition[2][0];
fGlobalPosition[3] = (float)pGlobalPosition[3][0];
fGlobalPosition[4] = (float)pGlobalPosition[0][1];
fGlobalPosition[5] = (float)pGlobalPosition[1][1];
fGlobalPosition[6] = (float)pGlobalPosition[2][1];
fGlobalPosition[7] = (float)pGlobalPosition[3][1];
fGlobalPosition[8] = (float)pGlobalPosition[0][2];
fGlobalPosition[9] = (float)pGlobalPosition[1][2];
fGlobalPosition[10] = (float)pGlobalPosition[2][2];
fGlobalPosition[11] = (float)pGlobalPosition[3][2];
fGlobalPosition[12] = (float)pGlobalPosition[0][3];
fGlobalPosition[13] = (float)pGlobalPosition[1][3];
fGlobalPosition[14] = (float)pGlobalPosition[2][3];
fGlobalPosition[15] = (float)pGlobalPosition[3][3];
Last edited on
Well I would probably use some sort of loop to make it look nicer but your way in general should work well enough.
1
2
3
4
5
6
7
for(int a = 0; a < 4; a++)
{
	for(int b = 0; b < 4; b++)
	{
		fGlobalPosition[a * 4 + b] = (float)pGlobalPosition[a][b];
	}
}
OK, the loop idea looks good; especially if I were to write it in a function.
Topic archived. No new replies allowed.