How to properly initialize a model matrix in C? - c

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);

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);

Related

jfreechart bar color difference issue

arrayOfPaint[i] = new GradientPaint(0.0F, 0.0F, Color.green, 0.0F, 0.0F, Color.green);
// arrayOfPaint[i] = new GradientPaint(0.0F, 0.0F, Color.red, 0.0F, 0.0F, Color.red);
}
}
}
/* arrayOfPaint[0] = new GradientPaint(0.0F, 0.0F, Color.red, 0.0F, 0.0F, Color.red);
arrayOfPaint[1] = new GradientPaint(0.0F, 0.0F, Color.green, 0.0F, 0.0F, Color.green);
arrayOfPaint[2] = new GradientPaint(0.0F, 0.0F, Color.blue, 0.0F, 0.0F, Color.blue); */
CategoryDataset paramCategoryDatasetCylinder = localDefaultCategoryDatasetCylinder;
JFreeChart localJFreeChartCylinder = ChartFactory.createBarChart3D("", "Section", "Percentage", paramCategoryDatasetCylinder ,PlotOrientation.HORIZONTAL, true, true, false);
CategoryPlot localCategoryPlotCylinder = (CategoryPlot)localJFreeChartCylinder.getPlot();
/* if(compCode.equalsIgnoreCase("0094000") && domainName.equalsIgnoreCase("L and T")){
CategoryAxis yAxis = (CategoryAxis)localCategoryPlotCylinder.getDomainAxis();
yAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
} */
localCategoryPlotCylinder.setBackgroundPaint(Color.white);
localCategoryPlotCylinder.setRangeGridlinesVisible(true);
localCategoryPlotCylinder.setRangeGridlinePaint(Color.BLACK);
localJFreeChartCylinder.setBackgroundPaint(new GradientPaint(0.0F, 0.0F, Color.white, 350.0F, 0.0F, Color.white, true));
I have set GREEN color for both of the bars but I am getting difference in color. I am unable to figure out why it is happpening. Please help in this regard

Can't Get Spinning Cube To Render In OpenGL

I'm trying to get a spinning 3D cube but I can't get it to render. There's no error, but it's just a black screen. Also, there's no checking whether the Fragment shader is working but I can assure you it is I just don't have it included here.
Here's what my code looks like:
#include <glad/glad.h>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#include <cglm/cglm.h>
#include <cglm/mat4.h>
#include <cglm/vec3.h>
#include <stdio.h>
#include <stdlib.h>
float vertices[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f};
GLuint elements[] = {
0, 1, 2,
2, 3, 0};
static const char *vertex_shader_text =
"#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"layout (location = 1) in vec2 aTexCoord;\n"
"out vec2 TexCoord;\n"
"uniform mat4 model;\n"
"uniform mat4 view;\n"
"uniform mat4 projection;\n"
"void main()\n"
"{\n"
" gl_Position = projection * view * model * vec4(aPos, 1.0f);\n"
" TexCoord = vec2(aTexCoord.x, aTexCoord.y);\n"
"}";
static const char *fragment_shader_text =
"#version 330 core\n"
"varying vec3 color;\n"
"void main()\n"
"{\n"
" gl_FragColor = vec4(color, 1.0);\n"
"}\n";
static void error_callback(int error, const char *description) {
fprintf(stderr, "Error: %s\n", description);
}
static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
int main(void) {
GLFWwindow *window;
GLuint vbo, vertex_shader, fragment_shader, program;
GLint mvp_location, vpos_location, vcol_location;
glfwSetErrorCallback(error_callback);
if (!glfwInit())
exit(EXIT_FAILURE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
if (!window) {
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwSetKeyCallback(window, key_callback);
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
fprintf(stderr, "Could Not Load GLFW\n");
exit(1);
}
glfwSwapInterval(1);
unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)0);
glEnableVertexAttribArray(0);
// texture coord attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
glCompileShader(vertex_shader);
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
glCompileShader(fragment_shader);
GLint status;
glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &status);
if(!status){
char buffer[512];
glGetShaderInfoLog(vertex_shader, 512, NULL, buffer);
puts(buffer);
}
program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);
while (!glfwWindowShouldClose(window)) {
float ratio;
int width, height;
glfwGetFramebufferSize(window, &width, &height);
ratio = width / (float)height;
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
mat4 model = {{1.f, 1.f, 1.f, 1.f}, {1.f, 1.f, 1.f, 1.f}, {1.f, 1.f, 1.f, 1.f}, {1.f, 1.f, 1.f, 1.f}};
mat4 view = {{1.f, 1.f, 1.f, 1.f}, {1.f, 1.f, 1.f, 1.f}, {1.f, 1.f, 1.f, 1.f}, {1.f, 1.f, 1.f, 1.f}};
mat4 projection = {{1.f, 1.f, 1.f, 1.f}, {1.f, 1.f, 1.f, 1.f}, {1.f, 1.f, 1.f, 1.f}, {1.f, 1.f, 1.f, 1.f}};
vec3 axis = {0.5f, 1.0f, 0.0f};
vec3 dist = {0.0f, 0.0f, -3.0f};
glm_rotate(model, glfwGetTime(), axis);
glm_translate(view, dist);
glm_perspective(0.79, width / height, 0.1f, 100.0f, projection);
unsigned int modelLoc = glGetUniformLocation(program, "model");
unsigned int viewLoc = glGetUniformLocation(program, "view");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, (const GLfloat *)model);
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, (const GLfloat *)view);
unsigned int projectionLoc = glGetUniformLocation(program, "projection");
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}
You have to initialize the matrices with the Identity matrix:
mat4 model = {
{1.f, 0.f, 0.f, 0.f},
{0.f, 1.f, 0.f, 0.f},
{0.f, 0.f, 1.f, 0.f},
{0.f, 0.f, 0.f, 1.f}};
Also do so for view and projection.
The data types for the arguments of glm_rotate and glm_perspective must be float:
glm_rotate(model, (float)glfwGetTime(), axis);
glm_translate(view, dist);
glm_perspective((float)0.79, (float)width / (float)height, 0.1f, 100.0f, projection);
You also forgot to create the Index buffer:
glBindVertexArray(VAO);
// [...]
unsigned int IBO;
glGenBuffers(1, &IBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);
to install the program:
glUseProgram(program);
to set the projection matrix uniform.
unsigned int projectionLoc = glGetUniformLocation(program, "projection");
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, (const GLfloat *)projection);
Finally you must change the fragment shader. For example:
#version 330 core
in vec2 TexCoord;
void main()
{
gl_FragColor = vec4(TexCoord.xy, 0.5, 1.0);
};

Vulkan - Debugging A Camera Matrix Problem

The problem I'm having is that I'm exporting a scene from Blender and my scene is rotated 90 about the Y axis. It's clearly a matrix issue and I'm having problems visualizing the solution. Blender is configured to export with 'up' = (0, 1, 0) and 'forward' = (0, 0, 1).
I'm using row major matrices as indicated by the following.
mat4x4 cons_mat4x4_perspective(float fovy, float aspect_ratio, float near, float far)
{
mat4x4 result = cons_mat4x4_zero();
float half_tan_fovy = tan(fovy / 2.0f); /* in radians */
result.as_rows[0] = cons_vec4(1.0f / (aspect_ratio * half_tan_fovy), 0.0f, 0.0f, 0.0f);
result.as_rows[1] = cons_vec4(0.0f, 1.0f / half_tan_fovy, 0.0f, 0.0f);
result.as_rows[2] = cons_vec4(0.0f, 0.0f, -(far + near) / (far - near), -1.0f);
result.as_rows[3] = cons_vec4(0.0f, 0.0f, -(2.0f * far * near) / (far - near), 0.0f);
return result;
}
// Vulkan clip matrix
clip = cons_mat4x4_floats(1.0f, 0.0f, 0.0f, 0.0f,
0.0f,-1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.5f, 0.0f,
0.0f, 0.0f, 0.5f, 1.0f);
mat4x4 cons_mat4x4_lookat(vec3 eye, vec3 center, vec3 up)
{
mat4x4 result = cons_mat4x4_identity();
vec3 f = normalize(vec3_sub(center, eye));
vec3 s = normalize(cross(f, up));
vec3 u = cross(s, f);
result.as_rows[0] = cons_vec4(s.x, u.x, -f.x, 0.0f);
result.as_rows[1] = cons_vec4(s.y, u.y, -f.y, 0.0f);
result.as_rows[2] = cons_vec4(s.z, u.z, -f.z, 0.0f);
result.as_rows[3] = cons_vec4(-vec3_dot(s, eye),
vec3_dot(u, eye),
vec3_dot(f, eye),
1.0f);
return result;
}
My model matrix is the identity matrix and I'm creating a lookat matrix with the following parameters:
vec3 _camera = cons_vec3(0.0f, 5.0f, 0.0f);
vec3 _at = cons_vec3(0.0f, 0.0f, 0.0f);
cons_mat4x4_lookat(_camera, _at, cons_vec3(0.0f, 1.0f, 0.0f));

How to pass a GLfloat array through function in C++?

I'm trying to pass a GLfloat type array through a function but the new variable only take the 1st value of the array (0.5f in this case), what am i doing wrong?
My Code:
void main()
{
// some code
GLfloat vertices[] = {
// Positions // Colors // Texture Coords
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // Top Right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // Bottom Right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // Bottom Left
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // Top Left
};
passingArray(vertices);
}
void passingArray(GLfloat anotherVertices[])
{
// this anotherVertices only take 1st value of the vertices array
}

C Initializing Matrix

I have the following matrix;
Vertex axisVertices[] =
{
{ { x_0, y_0, 0.0f, 1.0f }, { 1.0f, 1.0f, 1.0f, 1.0f } }, // origin
{ { x_Max, y_0, 0.0f, 1.0f }, { 0.0f, 1.0f, 0.0f, 1.0f } }, // eixo y
{ { x_0, y_Max, 0.0f, 1.0f }, { 0.0f, 0.0f, 1.0f, 1.0f } } // eixo x
};
#define x_0 0.0f
#define y_0 0.0f
#define x_Max 1.5f
#define y_Max 1.7f
I need to be able to initialize the matrix like this. The only possible way of doing this in C is by defining either an enum, or using #define because initializing like this can't be used with a constant.
The problem is, I need to be able to change the values x_Max and y_Max throughout the code, so #define wont work in this case. How can I accomplish this?
You can pass parameters to #defines to create macros
For example you can do
#define INIT(x_0, y_0, x_Max, y_Max) \
{ { { x_0, y_0, 0.0f, 1.0f }, { 1.0f, 1.0f, 1.0f, 1.0f } }, \
{ { x_Max, y_0, 0.0f, 1.0f }, { 0.0f, 1.0f, 0.0f, 1.0f } }, \
{ { x_0, y_Max, 0.0f, 1.0f }, { 0.0f, 0.0f, 1.0f, 1.0f } } }
Which you can use like this
Vertex axisVertices[] = INIT(0.0f, 0.0f, 1.5f, 1.7f);
Then all occurrences of x_0 will be replaces by 0.0f. Just like passing parameters to functions.

Resources