It will be helpful to go through Previous tutorial before reading this one. LINK
Now, we will change the structure of Vertex a little. Position array is now of size 3 to handle x, y as well as z.
Our rotation matrix
Arrays of vertex and indices
And finally drawing
In order to know about rotation matrix, read this LINK
It tells you about matrix used for rotation around x, y and z axis. For rotation around X as well Y (mixed rotation) we just multiply rotation matrix of X and Y axis.
There are 3 sliders with which you can rotate the cube along X, Y and Z axis.
Check out screenshot:
You can download the code HERE
Now, we will change the structure of Vertex a little. Position array is now of size 3 to handle x, y as well as z.
struct Vertex {
float Position[3];
float Color[4];
};
Our rotation matrix
void ApplyRotationX(float degrees)
{
float radians = degrees * 3.14159f / 180.0f;
float s = std::sin(radians);
float c = std::cos(radians);
float zRotation[16] = {
1, 0, 0, 0,
0, c, s, 0,
0, -s, c, 0,
0, 0, 0, 1
};
GLint modelviewUniform = glGetUniformLocation(buildProgram, "Modelview");
glUniformMatrix4fv(modelviewUniform, 1, 0, &zRotation[0]);
}
void ApplyRotationY(float degrees)
{
float radians = degrees * 3.14159f / 180.0f;
float s = std::sin(radians);
float c = std::cos(radians);
float zRotation[16] = {
s, 0, c, 0,
0, 1, 0, 0,
c, 0, -s, 0,
0, 0, 0, 1
};
GLint modelviewUniform = glGetUniformLocation(buildProgram, "Modelview");
glUniformMatrix4fv(modelviewUniform, 1, 0, &zRotation[0]);
}
void ApplyRotationXY(float degrees)
{
float radians = degrees * 3.14159f / 180.0f;
float s = std::sin(radians);
float c = std::cos(radians);
float zRotation[16] = {
s, 0, c, 0,
s*c, c, -s* s, 0,
c*c, -s, -c * s, 0,
0, 0, 0, 1
};
GLint modelviewUniform = glGetUniformLocation(buildProgram, "Modelview");
glUniformMatrix4fv(modelviewUniform, 1, 0, &zRotation[0]);
}
Arrays of vertex and indices
// Define the positions and colors(R,G,B,A) of 8 vertices of square.
const Vertex Vertices[] = {
{{0, 0, 0}, {1, 0, 0, 1}},
{{0, 0.5, 0}, {0, 1, 0, 1}},
{{0.5, 0.5, 0}, {1, 1, 0, 1}},
{{0.5, 0, 0}, {1, 0, 1, 1}},
{{0, 0, 0.5}, {0, 1, 1, 1}},
{{0, 0.5, 0.5}, {0, 0.5, 0, 1}},
{{0.5, 0.5, 0.5}, {0.5, 0.5, 0, 1}},
{{0.5, 0, 0.5}, {0.5, 0, 0.5, 1}},
};
//Define the order of vertices for 12 triangles/
//0,1,2 forms first triangle
//2,3,0 form second triangle.
const GLubyte Indices[] = {
0, 1, 2,
2, 3, 0,
4,5,6,
6,7,4,
0,1,4,
1,4,5,
1,2,6,
1,5,6,
2,3,6,
3,6,7,
0,3,4,
3,4,7,
};
And finally drawing
// Drawing code
[mimContext presentRenderbuffer:GL_RENDERBUFFER];
glClearColor(1.0f, 1.0f, 1.0f, 1);
glClear(GL_COLOR_BUFFER_BIT);
switch (rotateAlong) {
case 1:
ApplyRotationX(degreesX);
break;
case 2:ApplyRotationY(degreesY);
break;
default:ApplyRotation(degreesZ);
break;
}
glEnableVertexAttribArray(_positionSlot);
glEnableVertexAttribArray(_colorSlot);
//Lets give these functions pointer to head of vertex array.
GLsizei stride = sizeof(Vertex);
const GLvoid* pCoords = &Vertices[0].Position[0];
const GLvoid* pColors = &Vertices[0].Color[0];
//Attribute changed from 2 (when we had drawn square) to 3 (dealing with cube x,y and z)
glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, stride, pCoords);
glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE, stride, pColors);
//Draw the 12 triangles and (12*3=)36 indices.
const GLvoid* bodyIndices = &Indices[0];
glDrawElements(GL_TRIANGLES,12* 3, GL_UNSIGNED_BYTE, bodyIndices);
glDisableVertexAttribArray(_positionSlot);
glDisableVertexAttribArray(_colorSlot);
In order to know about rotation matrix, read this LINK
It tells you about matrix used for rotation around x, y and z axis. For rotation around X as well Y (mixed rotation) we just multiply rotation matrix of X and Y axis.
There are 3 sliders with which you can rotate the cube along X, Y and Z axis.
//Solo Rotation along either of X,Y or Z.
switch (rotateAlong) {
case 1:
ApplyRotationX(degreesX);
break;
case 2:ApplyRotationY(degreesY);
break;
default:ApplyRotation(degreesZ);
break;
}
//Comment above lines in switch in code
//Uncomment following lines to see mixed rotation.
//Mixed Rotation along X and Y
ApplyRotationXY(40);
Check out screenshot:
You can download the code HERE
No comments:
Post a Comment