I am having issues with my rendering, I am drawing a quad and if it rotates around the x or y axis it get’s clipped by what seems like the far clipping plane.
this is the quad data structure
typedef struct {
float angle_in_rad;
GLshort vertex_count;
GLfloat vertices[12];
GLfloat colors[16];
GLshort indices[6];
GLfloat tex_coords[8];
}r2d_quad;
Here is the variables that I use
r2d_quad* quad;
vec3 in_vec0, in_vec1, in_vec2, in_vec3;
vec3 out_vec0, out_vec1, out_vec2, out_vec3;
mat3 in_mat, out_mat;
float x_size = 200;
float y_size = 200;
float z_size = 1;
float x_pos = 0;
float y_pos = 0;
float z_pos = 10;
float x_rot = 0.01;
float y_rot = 0.0;
float z_rot = 0.0;
vec4 iv0, iv1, iv2, iv3; //iv = in vector [0 - 3]
vec4 ov0, ov1, ov2, ov3; //ov = out vector [0-3]
vec4 tv4;
vec3 tv3;
mat4 imat, omat; //temp matrix to do some work
mat4 model_mat;
vmathM4MakeIdentity(&m_mat);
vmathM4MakeIdentity(&v_mat);
vmathM4MakeIdentity(&p_mat);
vmathM4MakeIdentity(&mvp_mat);
vmathP3MakeFromElems(&eye, 0, 0, 0);
vmathP3MakeFromElems(¢er, 0, 0, -1);
vmathV3MakeFromElems(&up, 0, 1, 0);
vmathM4MakeLookAt(&v_mat, ¢er, &eye, &up);
vec3 trans;
vmathV3MakeFromElems(&trans, (width * 0.5), (height * 0.5), -20.0);
vmathM4MakeTranslation(&v_mat, &trans);
vmathM4MakeOrthographic(&p_mat, 0, width, 0, height, 1.0, 100.0);
after this I create my buffers
pos_loc = get_attrib_location(ce_get_default_shader(), "a_pos");
col_loc = get_attrib_location(ce_get_default_shader(), "a_col");
mvp_matrix_loc = get_uniform_location(ce_get_default_shader(), "u_mvp_mat");
model_mat_loc = get_uniform_location(ce_get_default_shader(), "u_mod_mat");
view_mat_loc = get_uniform_location(ce_get_default_shader(), "u_view_mat");
proj_mat_loc = get_uniform_location(ce_get_default_shader(), "u_proj_mat");
quad = r2d_new_quad();
vmathM4MakeIdentity(&model_mat);
glGenBuffers(1, &vao);
glBindVertexArray(vao);
glEnableVertexAttribArray(0);
glGenBuffers(1, &vert_buff);
glBindBuffer(GL_ARRAY_BUFFER, vert_buff);
glBufferData(GL_ARRAY_BUFFER, sizeof(quad->vertices), quad->vertices,
GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat),
(GLvoid*)0);
glEnableVertexAttribArray(1);
glGenBuffers(1, &col_buff);
glBindBuffer(GL_ARRAY_BUFFER, col_buff);
glBufferData(GL_ARRAY_BUFFER, sizeof(quad->colors), quad->colors,
GL_STATIC_DRAW);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat),
(GLvoid*)0);
glGenBuffers(1, &ind_buff);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ind_buff);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(quad->indices), quad->indices,
GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(quad->indices), quad->indices,
GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
then I start doing some cpu bound quad scaling since I am targeting opengles 2.0 and cannot use the instancing or more fancy features just ye.
// scaling model
vmathV4MakeFromElems(&iv0, quad->vertices[0], quad->vertices[1], //making a vector4 out of each corner of the quad to then do some manipulations.
quad->vertices[2], 1.0);
vmathV4MakeFromElems(&iv1, quad->vertices[3], quad->vertices[4],
quad->vertices[5], 1.0);
vmathV4MakeFromElems(&iv2, quad->vertices[6], quad->vertices[7],
quad->vertices[8], 1.0);
vmathV4MakeFromElems(&iv3, quad->vertices[9], quad->vertices[10],
quad->vertices[11], 1.0);
vmathM4MakeIdentity(&imat);
vmathV3MakeFromElems(&tv3, x_size, y_size, z_size);
vmathM4MakeScale(&imat, &tv3);
vmathM4AppendScale(&model_mat, &model_mat, &tv3);
// translate model
vmathV4MakeFromElems(&iv0, quad->vertices[0], quad->vertices[1],
quad->vertices[2], 1.0);
vmathV4MakeFromElems(&iv1, quad->vertices[3], quad->vertices[4],
quad->vertices[5], 1.0);
vmathV4MakeFromElems(&iv2, quad->vertices[6], quad->vertices[7],
quad->vertices[8], 1.0);
vmathV4MakeFromElems(&iv3, quad->vertices[9], quad->vertices[10],
quad->vertices[11], 1.0);
vmathM4MakeIdentity(&imat);
vmathV3MakeFromElems(&tv3, x_pos, y_pos, z_pos);
vmathM4MakeTranslation(&imat, &tv3);
vmathM4SetTranslation(&model_mat, &tv3);
vmathM4MulV4(&ov0, &model_mat, &iv0);
vmathM4MulV4(&ov1, &model_mat, &iv1);
vmathM4MulV4(&ov2, &model_mat, &iv2);
vmathM4MulV4(&ov3, &model_mat, &iv3);
quad->vertices[0] = ov0.x;
quad->vertices[1] = ov0.y;
quad->vertices[2] = ov0.z;
quad->vertices[3] = ov1.x;
quad->vertices[4] = ov1.y;
quad->vertices[5] = ov1.z;
quad->vertices[6] = ov2.x;
quad->vertices[7] = ov2.y;
quad->vertices[8] = ov2.z;
quad->vertices[9] = ov3.x;
quad->vertices[10] = ov3.y;
quad->vertices[11] = ov3.z;
this is my rendering loop
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(ce_get_default_shader()->shader_program);
glBindVertexArray(vao);
glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, m_mat.data);
// projection * view * model * vertex_pos;
glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, mat4_get_data(&v_mat));
glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, mat4_get_data(&p_mat));
//--------------- update vertex data ---------------------
// negative translate model
vmathV4MakeFromElems(&iv0, quad->vertices[0], quad->vertices[1],
quad->vertices[2], 1.0);
vmathV4MakeFromElems(&iv1, quad->vertices[3], quad->vertices[4],
quad->vertices[5], 1.0);
vmathV4MakeFromElems(&iv2, quad->vertices[6], quad->vertices[7],
quad->vertices[8], 1.0);
vmathV4MakeFromElems(&iv3, quad->vertices[9], quad->vertices[10],
quad->vertices[11], 1.0);
vmathM4MakeIdentity(&imat); //before rotating I have to translate the model back to the center
vmathV3MakeFromElems(&tv3, -x_pos, -y_pos, -z_pos);
vmathM4MakeTranslation(&imat, &tv3);
vmathM4MakeIdentity(&imat);
vmathV3MakeFromElems(&tv3, -x_pos, -y_pos, -z_pos);
vmathM4MakeTranslation(&imat, &tv3);
vmathM4MakeIdentity(&model_mat);
vmathM4SetTranslation(&model_mat, &tv3);
vmathM4MulV4(&ov0, &model_mat, &iv0);
vmathM4MulV4(&ov1, &model_mat, &iv1);
vmathM4MulV4(&ov2, &model_mat, &iv2);
vmathM4MulV4(&ov3, &model_mat, &iv3);
quad->vertices[0] = ov0.x;
quad->vertices[1] = ov0.y;
quad->vertices[2] = ov0.z;
quad->vertices[3] = ov1.x;
quad->vertices[4] = ov1.y;
quad->vertices[5] = ov1.z;
quad->vertices[6] = ov2.x;
quad->vertices[7] = ov2.y;
quad->vertices[8] = ov2.z;
quad->vertices[9] = ov3.x;
quad->vertices[10] = ov3.y;
quad->vertices[11] = ov3.z;
// rotation model
vmathV4MakeFromElems(&iv0, quad->vertices[0], quad->vertices[1],
quad->vertices[2], 1.0);
vmathV4MakeFromElems(&iv1, quad->vertices[3], quad->vertices[4],
quad->vertices[5], 1.0);
vmathV4MakeFromElems(&iv2, quad->vertices[6], quad->vertices[7],
quad->vertices[8], 1.0);
vmathV4MakeFromElems(&iv3, quad->vertices[9], quad->vertices[10],
quad->vertices[11], 1.0);
vmathM4MakeIdentity(&imat);
vmathV3MakeFromElems(&tv3, x_rot, y_rot, z_rot);
vmathM4MakeIdentity(&model_mat);
vmathM4MakeRotationZYX(&model_mat, &tv3);
vmathM4MulV4(&ov0, &model_mat, &iv0);
vmathM4MulV4(&ov1, &model_mat, &iv1);
vmathM4MulV4(&ov2, &model_mat, &iv2);
vmathM4MulV4(&ov3, &model_mat, &iv3);
quad->vertices[0] = ov0.x;
quad->vertices[1] = ov0.y;
quad->vertices[2] = ov0.z;
quad->vertices[3] = ov1.x;
quad->vertices[4] = ov1.y;
quad->vertices[5] = ov1.z;
quad->vertices[6] = ov2.x;
quad->vertices[7] = ov2.y;
quad->vertices[8] = ov2.z;
quad->vertices[9] = ov3.x;
quad->vertices[10] = ov3.y;
quad->vertices[11] = ov3.z;
// translate model back to it's original position
vmathV4MakeFromElems(&iv0, quad->vertices[0], quad->vertices[1],
quad->vertices[2], 1.0);
vmathV4MakeFromElems(&iv1, quad->vertices[3], quad->vertices[4],
quad->vertices[5], 1.0);
vmathV4MakeFromElems(&iv2, quad->vertices[6], quad->vertices[7],
quad->vertices[8], 1.0);
vmathV4MakeFromElems(&iv3, quad->vertices[9], quad->vertices[10],
quad->vertices[11], 1.0);
vmathM4MakeIdentity(&imat); //do some final matrix calculations
vmathV3MakeFromElems(&tv3, x_pos, y_pos, z_pos);
vmathM4MakeTranslation(&imat, &tv3);
vmathM4SetTranslation(&model_mat, &tv3);
vmathM4MulV4(&ov0, &model_mat, &iv0);
vmathM4MulV4(&ov1, &model_mat, &iv1);
vmathM4MulV4(&ov2, &model_mat, &iv2);
vmathM4MulV4(&ov3, &model_mat, &iv3);
quad->vertices[0] = ov0.x;
quad->vertices[1] = ov0.y;
quad->vertices[2] = ov0.z;
quad->vertices[3] = ov1.x;
quad->vertices[4] = ov1.y;
quad->vertices[5] = ov1.z;
quad->vertices[6] = ov2.x;
quad->vertices[7] = ov2.y;
quad->vertices[8] = ov2.z;
quad->vertices[9] = ov3.x;
quad->vertices[10] = ov3.y;
quad->vertices[11] = ov3.z;
/* //--------------- update vertex data --------------------- */
//--------------- pack vertex data into a buffer ---------------------
// eventually pack into 1 large array and batch draw calls.
//--------------- pack vertex data into a buffer ---------------------
glBindBuffer(GL_ARRAY_BUFFER, vert_buff);
glBufferData(GL_ARRAY_BUFFER, sizeof(quad->vertices), quad->vertices,
GL_STREAM_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, col_buff);
glBufferData(GL_ARRAY_BUFFER, sizeof(quad->colors), quad->colors,
GL_STREAM_DRAW);
glDrawElements(GL_TRIANGLES, quad->vertex_count, GL_UNSIGNED_SHORT, 0);
glBindVertexArray(0);
This is the complete code relevant to the rendering.
This is a video of the problem, you’ll see rotating around y, x or xyz at the same time causes clipping.
This is a video rotating around the xyz axis: https://youtu.be/2lTWl8yMOAA
This is a video rotating around the x axis: https://youtu.be/WuloRzXMWJE
This is a video rotating around the y axis: - YouTube
This is a video rotating around the z axis: https://youtu.be/bNIeW4BsQjE
can anyone help me figure out why I am getting clipping like this?