1

I tried to convert the code of the question I asked over here.

But when I try to run it, I run into an exception and it points to the mat4(which is an 2-d array of 4x4 floats) header of this(CGLM, which is use for OpenGL math in C as GLM is C++ only library) library.

The C++ code works fine and runs perfectly but why does it crash in C.

Here is the C snippet of the code where 'CGLM' is used(this is the code that crashes):-

glUseProgram(shaderProgram);
mat4 model =
{
    { 1.0f, 0.0f, 0.0f, 0.0f },
    { 0.0f, 1.0f, 0.0f, 0.0f },
    { 0.0f, 0.0f, 1.0f, 0.0f },
    { 0.0f, 0.0f, 0.0f, 1.0f },
};
mat4 projection;
glm_ortho(0.0f, (GLfloat)(WIDTH), (GLfloat)(HEIGHT), 0.0f, -1.0f, 1.0f, projection);

vect2 scale = { 2.0f, 2.0f };
vect2 position = { 50.0f, 0.0f };
vec4 color = { 1.0f, 1.0f, 1.0f, 0.5f };
GLfloat rotation = 0.0f;

glm_translate(model, (vec3){ position[0], position[1], 0.0f });

glm_translate(model, (vec3){ 0.5f * scale[0], 0.5f * scale[1], 0.0f });
glm_rotate(model, rotation, (vec3){ 0.0f, 0.0f, 1.0f }); // Why is this function crashing?
glm_translate(model, (vec3){ -0.5f * scale[0], -0.5f * scale[1], 0.0f });

glm_scale(model, (vec3){ scale[0] * width, scale[1] * height, 1.0f });

glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "projection"), 1, GL_FALSE, (GLfloat *)projection);
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "model"), 1, GL_FALSE, (GLfloat *)model);
glUniform4f(glGetUniformLocation(shaderProgram, "spriteColor"), color[0], color[1], color[2], color[3]);

Here is the C++ snippet of the code where GLM is used:-

glUseProgram(shaderProgram);
glm::mat4 model(1.0f);
glm::mat4 projection = glm::ortho(0.0f, static_cast<GLfloat>(WIDTH), static_cast<GLfloat>(HEIGHT), 0.0f, -1.0f, 1.0f);

glm::vec2 scale = glm::vec2(2.0f, 2.0f);
glm::vec2 position = glm::vec2(50.0f, 0.0f);
glm::vec4 color = glm::vec4(1.0f, 1.0f, 1.0f, 0.5f);
GLfloat rotation = 0.0f;

model = glm::translate(model, glm::vec3(position, 0.0f));

model = glm::translate(model, glm::vec3(0.5f * scale.x, 0.5f * scale.y, 0.0f));
model = glm::rotate(model, rotation, glm::vec3(0.0f, 0.0f, 1.0f));
model = glm::translate(model, glm::vec3(-0.5f * scale.x, -0.5f * scale.y, 0.0f));

model = glm::scale(model, glm::vec3(scale * glm::vec2(width, height), 1.0f));

glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "model"), 1, GL_FALSE, glm::value_ptr(model));
glUniform4f(glGetUniformLocation(shaderProgram, "spriteColor"), color.x, color.y, color.z, color.w);
Ruks
  • 3,886
  • 1
  • 10
  • 22
  • So, what exception do you get, and at which point in your code? – derhass Apr 04 '18 at 14:49
  • 'Exception thrown at 0x010F59C7 in spritetest.exe: 0xC0000005: Access violation reading location 0xFFFFFFFF' is said by the compiler instanly after I run it, pointing over here:- https://github.com/recp/cglm/blob/master/include/cglm/vec4.h#L169 – Ruks Apr 04 '18 at 14:53
  • By the compiler? certainly not. But really, use a debugger and lock at the stack trace – derhass Apr 04 '18 at 14:55
  • @derhass I just added the link to the line which causes the exception. – Ruks Apr 04 '18 at 14:57
  • Why would the `glUniform...` call end up in a cglm vector addition? That doesn't make sense. – derhass Apr 04 '18 at 15:01

1 Answers1

4

See CGLM documentation

According to the getting started

Aligment is Required: vec4 and mat4 requires 16 byte aligment because vec4 and mat4 operations are vectorized by SIMD instructions (SSE/AVX).

So you need to initialize identity matrix like:

mat4 model;
glm_mat4_identity(model);
....
mat4 projection;
glm_mat4_identity(projection);
glm_ortho(0.0f, (GLfloat)(WIDTH), (GLfloat)(HEIGHT), 0.0f, -1.0f, 1.0f, projection);
Victor Gubin
  • 2,782
  • 10
  • 24
  • But why is it still crashing? – Ruks Apr 04 '18 at 15:05
  • Which source line ? – Victor Gubin Apr 04 '18 at 15:06
  • This one right here `model = glm::translate(model, glm::vec3(-0.5f * scale.x, -0.5f * scale.y, 0.0f));` – Ruks Apr 04 '18 at 15:10
  • @Ruks I suppose you have a problem in the `C` code don't you ? glm_scale(model, (vec3){ scale[0] * width, scale[1] * height, 1.0f }); – Victor Gubin Apr 04 '18 at 15:13
  • The same things for vectors, i.e. vec3 init = GLM_VEC3_ZERO; etc. – Victor Gubin Apr 04 '18 at 15:15
  • 2
    'C' don't have any exceptions at all. Exceptions is a C++ feature. It it crashes - glm code, written using assembly to accelerate performance bring to undefined behavior, since data is not properly initialized (aligned). `C` is not a C++ is much closer to assembly, so try to avoid tricks like (vec3) { .... }, this is not a constructor call with std::initialized_list unlike the same in `C++`. – Victor Gubin Apr 04 '18 at 15:19
  • It is with the rotation, not the scale. – Ruks Apr 04 '18 at 15:22
  • 1
    It dosn't really meter, assembly code expecting aligned memory. `(vec3){ 0.0f, 0.0f, 1.0f }` this is an stack object array, and compiler crossing the aliment. Assembly function expecting memory address on array with the exact alignment. Simply don't pass the formal parameter arrays, use variables. – Victor Gubin Apr 04 '18 at 15:34