Archive for August, 2007

Domain and web hosting - Page 324 OpenGL Super Bible! Finally, the light

Tuesday, August 21st, 2007

Page 324 OpenGL Super Bible! Finally, the light source GL_LIGHT0 is enabled: glEnable(GL_LIGHT0); Listing 9-4 Light and rendering context setup for LITJET // This function does any needed initialization on the rendering // context. Here it sets up and initializes the lighting for // the scene. void SetupRC() { // Light values and coordinates GLfloat ambientLight[] = { 0.3f, 0.3f, 0.3f, 1.0f }; GLfloat diffuseLight[] = { 0.7f, 0.7f, 0.7f, 1.0f }; Glfloat lightPos[] = { -50.f, 50.0f, 100.0f, 1.0f }; glEnable(GL_DEPTH_TEST); // Hidden surface removal glFrontFace(GL_CCW); // Counter clock-wise polygons face out glEnable(GL_CULL_FACE); // Do not calculate inside of jet // Enable lighting glEnable(GL_LIGHTING); // Setup and enable light 0 glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight); glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight); glLightfv(GL_LIGHT0,GL_POSITION,lightPos); glEnable(GL_LIGHT0); // Enable color tracking glEnable(GL_COLOR_MATERIAL); // Set Material properties to follow glColor values glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); // Light blue background glClearColor(0.0f, 0.0f, 1.0f, 1.0f ); } Setting the Material Properties Notice in Listing 9-4 that color tracking is enabled, and the properties to be tracked are the ambient and diffuse reflective properties for the front surface of the polygons. This is just as it was defined in the AMBIENT sample program: // Enable color tracking glEnable(GL_COLOR_MATERIAL); // Set Material properties to follow glColor values glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.

OpenGL Super (Best web design) Bible! Page 323 static const int

Tuesday, August 21st, 2007

OpenGL Super Bible! Page 323 static const int z = 2; // Calculate two vectors from the three points v1[x] = v[0][x] - v[1][x]; v1[y] = v[0][y] - v[1][y]; v1[z] = v[0][z] - v[1][z]; v2[x] = v[1][x] - v[2][x]; v2[y] = v[1][y] - v[2][y]; v2[z] = v[1][z] - v[2][z]; // Take the cross product of the two vectors to get // the normal vector which will be stored in out[] out[x] = v1[y]*v2[z] - v1[z]*v2[y]; out[y] = v1[z]*v2[x] - v1[x]*v2[z]; out[z] = v1[x]*v2[y] - v1[y]*v2[x]; // Normalize the vector (shorten length to one) ReduceToUnit(out); } Setting Up a Source Now that you understand the requirements of setting up your polygons to receive and interact with a light source, it s time to turn on the lights! Listing 9-4 shows the SetupRC() function from the example program LITJET. Part of the setup process for this sample program creates a light source and places it to the upper-left, slightly behind the viewer. The light source GL_LIGHT0 has its ambient and diffuse components set to the intensities specified by the arrays ambientLight[], and diffuseLight[].This results in a moderate white light source. GLfloat ambientLight[] = { 0.3f, 0.3f, 0.3f, 1.0f }; GLfloat diffuseLight[] = { 0.7f, 0.7f, 0.7f, 1.0f }; // Setup and enable light 0 glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight); glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight); The light is positioned by this code: GLfloat lightPos[] = { -50.f, 50.0f, 100.0f, 1.0f }; glLightfv(GL_LIGHT0,GL_POSITION,lightPos); Here lightPos[] contains the position of the light. The last value in this array is 1.0, which specifies that the designated coordinates are the position of the light source. If the last value in the array is 0.0, it indicates that the light is an infinite distance away along the vector specified by this array. We ll touch more on this later.
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.

Page 322 OpenGL Super Bible! Figure 9-14 Two (Web hosting contract)

Monday, August 20th, 2007

Page 322 OpenGL Super Bible! Figure 9-14 Two vectors defined by three points on a plane Figure 9-15 A normal vector as cross product of two vectors Don t worry if you don t know how to take the cross product of two vectors; all you need is the function in Listing 9-3. To use this function, pass it an array containing any three vertices from your polygon (specify in counterclockwise winding order), and an array that will contain the normal vector on return. The constant values x, y, and z are provided for your benefit if you want to see how the function works. Listing 9-3 Function to calculate a normal vector with any three vertices from a polygon // Points p1, p2, & p3 specified in counterclockwise order void calcNormal(float v[3][3], float out[3]) { float v1[3],v2[3]; static const int x = 0; static const int y = 1;
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.

Yahoo web hosting - OpenGL Super Bible! Page 321 // value for

Sunday, August 19th, 2007

OpenGL Super Bible! Page 321 // value for vectors whose length may be calculated too close to zero. if(length == 0.0f) length = 1.0f; // Dividing each element by the length will result in a // unit normal vector. vector[0] /= length; vector[1] /= length; vector[2] /= length; } Finding a Normal Figure 9-13 presents another polygon that is not simply lying in one of the axis planes. The normal vector pointing away from this surface is more difficult to guess, so we need an easy way to calculate the normal for any arbitrary polygon in 3D coordinates. Figure 9-13 A nontrivial normal problem You can easily calculate the normal vector for any polygon consisting of at least three points that lie in a single plane (a flat polygon). Figure 9-14 shows three points, P1, P2, and P3, that you can use to define two vectors: vector V1 from P1 to P2, and vector V2 from P1 to P2. Mathematically, two vectors in three-dimensional space define a plane (your original polygon lies in this plane). If you take the cross product of those two vectors (written mathematically as V1 X V2, the resulting vector is perpendicular to that plane (or normal). Figure 9-15 shows the vector V3 derived by taking the cross product of V1 and V2.
Please visit Domain Name Hosting services for high quality webhost to host and run your jsp applications.

Page 320 OpenGL Super Bible! Polygon Winding: Take

Saturday, August 18th, 2007

Page 320 OpenGL Super Bible! Polygon Winding: Take special note of the order of the vertices in the jet s triangle. If you viewed this triangle being drawn from the direction in which the normal vector points, the corners would appear counterclockwise around the triangle. This is called polygon winding. By default, the front of a polygon is defined as the side from which the vertices appear to be wound in a counterclockwise fashion. Unit Normals As OpenGL does its magic, all surface normals must eventually be converted to unit normals. A unit normal is just a normal vector that has a length of 1. The normal in Figure 9-12 has a length of 9. You can find the length of any normal by squaring each component, adding them together, and taking the square root. Divide each component of the normal by the length and you get a vector pointed in exactly the same direction, but only 1 unit long. In this case, our new normal vector would be specified as (0,1,0). This is called normalization. Thus, for lighting calculations, all normal vectors must be normalized. Talk about jargon! You can tell OpenGL to convert your normals to unit normals automatically, by enabling normalization with glEnable and a parameter of GL_NORMALIZE: glEnable(GL_NORMALIZE); This does, however, have performance penalties. It s far better to calculate your normals ahead of time as unit normals instead of relying on OpenGL to do this for you. Given any normal vector specified by a coordinate triplet that indicates the direction from the origin, you can easily find the equivalent unit normal vector with the function in Listing 9-2. Listing 9-2 A function that reduces any normal vector to a unit normal vector // Reduces a normal vector specified as a set of three coordinates, // to a unit normal vector of length 1. void ReduceToUnit(float vector[3]) { float length; // Calculate the length of the vector length = (float)sqrt((vector[0]*vector[0]) + (vector[1]*vector[1]) + (vector[2]*vector[2])); // Keep the program from blowing up by providing an acceptable
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.

OpenGL Super Bible! Page (Web hosting faq) 319 Another way of

Friday, August 17th, 2007

OpenGL Super Bible! Page 319 Another way of looking at this is, if the vertex were translated to the origin, the point specified by subtracting the two original points would still specify the direction pointing away and at a 90 angle from the surface. Figure 9-12 shows the newly translated normal vector. Figure 9-12 The newly translated normal vector The vector is a directional quantity that tells OpenGL which direction the vertices (or polygon) face. This next code segment shows a normal vector being specified for one of the triangles in the JET example program: glBegin(GL_TRIANGLES); glNormal3f(0.0f, -1.0f, 0.0f); glVertex3f(0.0f, 0.0f, 60.0f); glVertex3f(-15.0f, 0.0f, 30.0f); glVertex3f(15.0f,0.0f,30.0f); glEnd(); The function glNormal3f takes the coordinate triplet that specifies a normal vector pointing in the direction perpendicular to the surface of this triangle. In this example, the normals for all three vertices have the same direction, which is down the negative y axis. This is a very simple example because the triangle is lying flat in the xz plane, and it actually represents a bottom section of the jet. The prospect of specifying a normal for every vertex or polygon in your drawing may seem daunting, especially since very few surfaces will lie cleanly in one of the major planes. Never fear, we will shortly present a reusable function that you can call again and again to calculate your normals for you.
If you are searching for cheap webhost for your web application, please visit MySQL5 Web Hosting services.

Page 318 OpenGL Super Bible! You may already (Web server type)

Thursday, August 16th, 2007

Page 318 OpenGL Super Bible! You may already be asking why we must specify a normal vector for each vertex. Why can t we just specify a single normal for a polygon and use it for each vertex? We can and for our first few examples, we will. However, there are times when you don t want each normal to be exactly perpendicular to the surface of the polygon. You may have noticed that many surfaces are not flat! You can approximate these surfaces with flat, polygonal sections, but you will end up with a jagged or multifaceted surface. Later we ll discuss a technique to produce the illusion of smooth curves with straight lines by tweaking your surface normals (more magic!). But first things first. Specifying a Normal To see how we specify a normal for a vertex, let s take a look at Figure 9-11 a plane floating above the xz plane in 3D space. We ve made this simple to demonstrate the concept. Notice the line through the vertex (1,1,0) that is perpendicular to the plane. If we select any point on this line, say (1,10,0), then the line from the first point (1,1,0) to the second point (1,10,0) is our normal vector. The second point specified actually indicates that the direction from the vertex is up in the y direction. This is also used to indicate the front and back sides of polygons, as the vector travels up and away from the front surface. Figure 9-11 A normal vector traveling perpendicular from the surface You can see that this second point is the number of units in the x, y, and z directions for some point on the normal vector away from the vertex. Rather than specifying two points for each normal vector, we can subtract the vertex from the second point on the normal, yielding a single coordinate triplet that indicates the x, y, and z steps away from the vertex. For our example this would be (1,10,0) - (1,1,0) = (1-1, 10-1, 0) = (0, 9, 0)
We recommend high quality webhost to host and run your jsp application: christian web host services.

OpenGL Super Bible! (Com web hosting) Page 317 Figure 9-9 Light

Wednesday, August 15th, 2007

OpenGL Super Bible! Page 317 Figure 9-9 Light is reflected off objects at specific angles From a programming standpoint, this presents a slight conceptual difficulty. Each polygon is created as a set of vertices, which are nothing more than points. Each vertex is then struck by a ray of light at some angle. How then do you (or OpenGL) calculate the angle between a point and a line (the ray of light)? Of course you can t geometrically find the angle between a single point and a line in 3D space, because there are an infinite number of possibilities. Therefore, you must associate with each vertex some piece of information that denotes a direction upward from the vertex and away from the surface of the primitive. Surface Normals A line from the vertex in this upward direction would then start in some imaginary plane (or your polygon) at a right angle. This line is called a normal vector. That word vector may sound like something the Star Trek crew members toss around, but it just means a line perpendicular to a real or imaginary surface. A vector is a line pointed in some direction, and the word normal is just another way for eggheads to say perpendicular (intersecting at a 90 angle). As if the word perpendicular weren t bad enough! Therefore, a normal vector is a line pointed in a direction that is at a 90 angle to the surface of your polygon. Figure 9-10 presents examples of 2D and 3D normal vectors. Figure 9-10 A 2D and a 3D normal vector
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.

Web hosting unlimited bandwidth - Page 316 OpenGL Super Bible! // Nice light

Tuesday, August 14th, 2007

Page 316 OpenGL Super Bible! // Nice light blue background glClearColor(0.0f, 0.0f, 05.f,1.0f); } Using a Light Source Manipulating the ambient light has its uses, but for most applications attempting to model the real world, one or more specific sources of light must be specified. In addition to their intensities and colors, these sources will have a location and a direction. The placement of these lights can dramatically affect the appearance of your scene. OpenGL supports up to eight independent light sources located anywhere in your scene or out of the viewing volume. You can locate a light source an infinite distance away and make its light rays parallel, or make it a nearby light source radiating outward. You can also specify a spotlight with a specific cone of light radiating from it, as well as manipulate its characteristics. Which Way Is Up? When you specify a light source, you tell OpenGL where it is and in which direction it s shining. Often the light source will be shining in all directions, or it may be directional. Either way, for any object you draw, the rays of light from any source (other than a pure ambient source) will strike the surface of the polygons that make up the object at an angle. Of course, in the case of a directional light, the surface of all polygons may not necessarily be illuminated. To calculate the shading effects across the surface of the polygons, OpenGL must be able to calculate this angle. In Figure 9-9, a polygon (a square) is being struck by a ray of light from some source. The ray makes an angle (A) with the plane as it strikes the surface. The light is then reflected at an angle (B) toward the viewer (or you wouldn t see it). These angles are used in conjunction with the lighting and material properties we have discussed thus far to calculate the apparent color of that location. It happens by design that the locations used by OpenGL are the vertices of the polygon. By calculating the apparent colors for each vertex and then doing smooth shading between them (explained in Chapter 8) , the illusion of lighting is created. Magic!
If you are in need for chaep and reliable webhost to host your website, our recommendation is http web server services.

Free web host - OpenGL Super Bible! Page 315 Figure 9-8 Output

Monday, August 13th, 2007

OpenGL Super Bible! Page 315 Figure 9-8 Output from AMBIENT when the light source is cut in half You can see how we might reduce the ambient light in a scene to produce a dimmer image. This is useful for simulations in which dusk approaches gradually or when a more direct light source is blocked, as when an object is in the shadow of another, larger object. Listing 9-1 Set up for ambient lighting conditions // This function does any needed initialization on the rendering // context. Here it sets up and initializes the lighting for // the scene. void SetupRC() { // Light values // Bright white light GLfloat ambientLight[] = { 1.0f, 1.0f, 1.0f, 1.0f }; out glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glFrontFace(GL_CCW); // Hidden surface removal// Do not calculate inside of jet// Counter clock-wise polygons face // Lighting stuff glEnable(GL_LIGHTING); // Enable lighting // Set light model to use ambient light specified by ambientLight[] glLightModelfv(GL_LIGHT_MODEL_AMBIENT,ambientLight) ; glEnable(GL_COLOR_MATERIAL); // Enable Material color tracking // Front material ambient and diffuse colors track glColor glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
You want to have a cheap webhost for your apache application, then check apache web hosting services.