computer graphic OpenGL
Assignment7
In this assignment, you are going to create a cube object with applying transparency effect. To initiate transparency, you need to define the following codes.
glEnable(GL_BLEND); //Enable alphablend blending glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); //Set the blend function
Your cube object needs to be rotated when the user presses ‘+’ or ‘-‘ keyboard button ( ‘+’: counter- clock-wise rotation, ‘-‘ clock-wise rotation.) Also, you need to support two keyboard buttons ‘<’ and ‘>’. ‘<’ is for decreasing the alpha blending value by 0.01 and ‘>’ is for increasing it by 0.01.
The default value for the alpha blending should be 0.6. When the user presses the button ‘>’, the value is continuously increased by 0.01 up to 1.0.
(HINT: use assignment 3-2 and reference Chapter 5 alpha blending)
Figure 1 Rotation examples when the user presses ‘–‘ button.
Figure 2. When the alpha blending is 1.0, you are not able to see lines (left). But, if the value is changed closer to 0.0, you can see background lines (right).
TEMPLATE
#ifdef __APPLE_CC___x000D_ #include <GLUT/glut.h>_x000D_ #else_x000D_ #include <GL/freeglut.h>_x000D_ #endif_x000D_
int angledirection = 0;_x000D_
// Clears the window and draws the tetrahedron. The tetrahedron is easily_x000D_
// specified with a triangle strip, though the specification really isn't very_x000D_
// easy to read._x000D_
void display() {_x000D_
// We rotate 2 degree * angledirection around y so we can see a lot of the left side._x000D_
glRotatef(2.0*angledirection, 0, 1, 0);_x000D_
// clear frame buffer_x000D_
glClear(GL_COLOR_BUFFER_BIT);_x000D_
// Draw a white grid "floor" for the tetrahedron to sit on._x000D_
glColor3f(1.0, 1.0, 1.0);_x000D_
glBegin(GL_LINES);_x000D_
for (GLfloat i = -2.5; i <= 2.5; i += 0.25) {_x000D_
glVertex3f(i, 0, 2.5); glVertex3f(i, 0, -2.5);_x000D_
glVertex3f(2.5, 0, i); glVertex3f(-2.5, 0, i);_x000D_ }_x000D_
glEnd();
// Draw the tetrahedron. It is a four sided figure, so when defining it_x000D_ // with a triangle strip we have to repeat the last two vertices._x000D_ glBegin(GL_TRIANGLE_STRIP);_x000D_
glColor3f(1, 1, 1); glVertex3f(0, 2, 0);_x000D_
glColor3f(1, 0, 0); glVertex3f(-1, 0, 1);_x000D_
glColor3f(0, 1, 0); glVertex3f(1, 0, 1);_x000D_
glColor3f(0, 0, 1); glVertex3f(0, 0, -1);_x000D_
glColor3f(1, 1, 1); glVertex3f(0, 2, 0);_x000D_
glColor3f(1, 0, 0); glVertex3f(-1, 0, 1);_x000D_
glEnd();
glFlush(); }
// Sets up global attributes like clear color and drawing color, enables and_x000D_ // initializes any needed modes (in this case we want backfaces culled), and_x000D_ // sets up the desired projection and modelview matrices. It is cleaner to_x000D_ // define these operations in a function separate from main()._x000D_
void init() {
// Set the current clear color to sky blue and the current drawing color to // white.
glClearColor(0.1, 0.39, 0.88, 1.0);
glColor3f(1.0, 1.0, 1.0);
// Tell the rendering engine not to draw backfaces. Without this code,_x000D_ // all four faces of the tetrahedron would be drawn and it is possible_x000D_ // that faces farther away could be drawn after nearer to the viewer._x000D_ // Since there is only one closed polyhedron in the whole scene,_x000D_
// eliminating the drawing of backfaces gives us the realism we need._x000D_ // THIS DOES NOT WORK IN GENERAL._x000D_ glEnable(GL_CULL_FACE);_x000D_ glCullFace(GL_BACK);_x000D_
// Set the camera lens so that we have a perspective viewing volume whose_x000D_ // horizontal bounds at the near clipping plane are -2..2 and vertical_x000D_ // bounds are -1.5..1.5. The near clipping plane is 1 unit from the camera_x000D_ // and the far clipping plane is 40 units away._x000D_ glMatrixMode(GL_PROJECTION);_x000D_
glLoadIdentity();_x000D_ glFrustum(-2, 2, -1.5, 1.5, 1, 40);_x000D_
// Set up transforms so that the tetrahedron which is defined right at_x000D_ // the origin will be rotated and moved into the view volume._x000D_ // We rotate 45 degrees around x to "drop" the top of the pyramid_x000D_ // down a bit. Then we move the object back 3 units "into the screen"._x000D_ glMatrixMode(GL_MODELVIEW);_x000D_
glLoadIdentity();_x000D_ glTranslatef(0, 0, -3);_x000D_ glRotatef(45, 1, 0, 0);_x000D_
}
void keyboard(unsigned char key, int x, int y)_x000D_
{_x000D_
switch(key)_x000D_
{_x000D_
}_x000D_
glutPostRedisplay();_x000D_
}
// Initializes GLUT, the display mode, and main window; registers callbacks;_x000D_
// does application initialization; enters the main event loop._x000D_
int main(int argc, char** argv) {_x000D_
glutInit(&argc, argv);_x000D_
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);_x000D_
glutInitWindowPosition(80, 80);_x000D_
glutInitWindowSize(800, 600);_x000D_
glutCreateWindow("A Simple Tetrahedron");_x000D_
glutDisplayFunc(display);_x000D_
glutKeyboardFunc(keyboard);_x000D_
init();_x000D_
glutMainLoop();_x000D_
}
"You need a similar assignment done from scratch? Our qualified writers will help you with a guaranteed AI-free & plagiarism-free A+ quality paper, Confidentiality, Timely delivery & Livechat/phone Support.
Discount Code: CIPD30
Click ORDER NOW..


