Matreix Class not working

Hi I'm new to OpenGL(idk if I have already 4 months of learning it it's called new), and I have passed successfully my matrix to the shader, my matrix was an temporary GLfloat.
SO I started my class by building an old in the book trick, of overloading operators
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#pragma once
#include <GL\glew.h>

class Matrix
{
public:
	Matrix(GLfloat* arrays);
	~Matrix();
	class Proxy
	{
	public:
		Proxy(GLfloat* arr);
		GLfloat operator[](int index);
	private:
		GLfloat* arr;
	};
	GLfloat** array_;
	Proxy operator[](int index);
};

//cpp file

#include "..\headers\Matrix.h"


Matrix::Matrix(GLfloat* arrays)
{
	GLfloat* matrixtemp = arrays;

	array_ = new GLfloat*[4];

	for (int i = 0; i < 4; i++)
		array_[i] = new GLfloat[4];

	for (int i = 0; i < 16; i++)
	{
		array_[i % 4][i / 4] = matrixtemp[i];
	}
}


Matrix::~Matrix()
{
}

Matrix::Proxy::Proxy(GLfloat * arr):
	arr(arr)
{
}

GLfloat Matrix::Proxy::operator[](int index)
{
	return arr[index];
}

Matrix::Proxy Matrix::operator[](int index)
{
	return Proxy(array_[index]);
}

I have debugged if it works, and it does. But when I have to introduce it to my Shader function(s->passUniformMatrix4(&m[0][0], "myMatrix");//this is defined like void Shader::passUniformMatrix4(const GLfloat * value, const char* name) )

and I get 2 errors
1
2
Error	C2102	'&' requires l-value
Error (active)	E0158	expression must be an lvalue or a function designator


I repeat: I have debugged the function and it inits very well, it does return the proper value, but I get this error when used in this context. If it wasn't initialized it would compile but crash my program, that's what I believe)

I think you don't need to know OpenGL to solve my problem.
I think you don't need to know OpenGL to solve my problem.

I know neither OpenGL nor how to solve your problem, but there’s something I’d like to understand better in your code, if I may ask:
(s->passUniformMatrix4(&m[0][0], "myMatrix");//this is defined like void Shader::passUniformMatrix4(const GLfloat * value, const char* name)

It seems the first argument of “passUniformMatrix4” should be a pointer.
So why are you passing the address of a bidimensional array?
And why do you specify all dimensions to be 0?
Because it needs to be pointing out to the first element of the array, but I think this is the problem, I don't have any solution yet...
For pointers initialized like this:
1
2
3
array_ = new GLfloat*[4];
for (int i = 0; i < 4; i++)
	array_[i] = new GLfloat[4];


Beware that &array_[0][0] cannot be treated as a pointer to the first element of an array of 16 elements, because the structure is not contiguous -- it looks more like this:
            ARRAY            ARRAY[3]
+---+---+---+---+   +---+---+---+---+
|   |   |   |   |---|   |   |   |   |
+-|-+-|-+-|-+---+   +---+---+---+---+
  |   |   |      ARRAY[2]
  |   | +-|-+---+---+---+
  |   | |   |   |   |   |
  |   | +---+---+---+---+
  |   |      ARRAY[1]
  | +-|-+---+---+---+
  | |   |   |   |   |
  | +---+---+---+---+
  |      ARRAY[0]
+-|-+---+---+---+
|   |   |   |   |
+---+---+---+---+


Further, the use of new in a loop is highly prone to leaking memory. Just use a std::array of 16 elements.

Unfortunately, I'm short on time right now but perhaps I can offer some more help with your proxy class in a while.
Last edited on
Because it needs to be pointing out to the first element of the array

I got your point, but I have further questions :-) if you don't mind

1) How do you define ‘m’? Is it an instance of Matrix?

2) Why did you delete your question on stackoverflow?
https://stackoverflow.com/questions/45514423/my-matrix-class-is-not-passing-right-an-array

3) Is what you posted your real code or a simplified example?
I mean, if you know the dimension of your ‘array_’ from the beginning, why don’t you simply write:
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
#include <iostream>
#include <limits>

using GLfloat = double;

class Matrix {
public:
    Matrix(GLfloat* arrays);
    GLfloat array_[4][4];
};

Matrix::Matrix(GLfloat* arrays)
{
    for (int i = 0; i < 16; i++) {
        array_[i % 4][i / 4] = arrays[i];
    }
}

void waitForEnter();

int main()
{
    GLfloat mydoubles[] { 1.0, 2.0, 3.0, 4.0 };
    Matrix mymatrix(mydoubles);
    waitForEnter();
    return 0;
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

1)
1
2
3
	GLfloat temp[16];
	glGetFloatv(GL_MODELVIEW_MATRIX, temp);
	Matrix m(temp);

2)I deleted it because it would get very many down sides and reduce my rep, getting me away some privileges.
3)Yes, I know, but I find matrices very useful, and in the future reuse the code, with unknown sizes of rows and columns
Last edited on
Solved! All I had to do was to make operator to return a reference to array
Topic archived. No new replies allowed.