written 8.3 years ago by | • modified 8.3 years ago |
Mumbai university > Comp > SEM 4 > Computer Graphics
Marks: 5M
Year: Dec 2015, May 2015
written 8.3 years ago by | • modified 8.3 years ago |
Mumbai university > Comp > SEM 4 > Computer Graphics
Marks: 5M
Year: Dec 2015, May 2015
written 8.3 years ago by |
2.Rendering Curves in OpenGL
OpenGL does not directly support rendering any curves other that lines and polylines. However, you can sample a curve and draw it as a line strip, e.g.,:
Float x, y;
glBegin(GL_LINE_STRIP);
For(int t = 0 ; t <= 1 ; t + = .01)
computeCurve( t, & x, & y);
glVertex2f(x, y);
}
glEnd();
You can adjust the step-size to determine how many line segments to draw. Adding line segments will increase the accuracy of the curve, but slow down the rendering.
The GLU does have some specialized libraries to assist with generating and rendering curves. For example, the following code renders a disk with a hole in its center, centered about the z-axis.
GLUquadric q = gluNewQuadric();
gluDisk(q, innerRadius, outerRadius, sliceCount, 1);
gluDeleteQuadric(q);
3.Shading in OpenGL
OpenGL only directly supports Gouraud shading or flat shading.
Gouraud is enabled by default, computing vertex colors, and interpolating colors across triangle faces.
Flat shading can be enabled with glShadeModel(GL FLAT).
This renders an entire face with the color of a single vertex, giving a faceted appearance.
Note that the meshappears smooth, although the coarseness of the geometry is visible at the silhouettes of the mesh
With pixel shaders on programmable graphics hardware, it is possible to achieve Phong shading by using a small program to compute the illumination at each pixel with interpolated normals.
It is even possible to use a normal map to assign arbitrary normals within faces, with a pixel shader using these normals to compute the illumination.
4.Visibility in OpenGL
OpenGL directly supports depth buffering, but it is often used in addition to other visibility techniques in interactive applications.
To use depth buffering in OpenGL with GLUT, the OpenGL context must be initialized with memory allocated for a depth buffer, with a command such as glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
Next, depth writing and testing must be enabled in OpenGL:
glEnable(GL_DEPTH_TEST);
OpenGL will automatically write pseudo-depth values to the depth buffer when a primitive is rendered as long as the depth test is enabled.
The glDepthMask function can be used to disable depth writes, so depth testing will occur without writing to the depth buffer when rendering a primitive.
When clearing the display to render a new frame, the depth buffer should also be cleared:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
5.Lighting in OpenGL
OpenGL provides a slightly modified version of Phong lighting. Lighting and any specific lights to use must be enabled to see its effects:
glEnable(GL_LIGHTING); // enable Phong lighting
glEnable(GL_LIGHT0); // enable the first light source
glEnable(GL_LIGHT1); // enable the second light source
Lights can be directional (infinitely far away) or positional.
Positional lights can be either point lights or spotlights.
Directional light have the w component set to 0 and positional lights have w set to 1.