Home > OpenGL ES > setupView: from Part IV Rewritten
setupView: from Part IV Rewritten
Posted on Saturday, May 2, 2009 by android apps market for tablests
I wrote the Part 4 of the OpenGL ES From the Ground Up series using plain arrays of GLfloats. That is the most common way of doing it that you will see out in the wild as it's the most portable, since it doesn't use any data structures not defined within OpenGL itself.
I took the opportunity to rewrite the setupView: method to use the Vertex3D, Vector3D, and Color3D structs we defined back in Part 1. Neither approach is "better", but I thought it might be interesting to see it done a little different way. When I was first learning OpenGL, I found it easier to think in terms of Vertices, Colors, and Triangles, rather than all the various-length arrays of floats. If you're like I was, then you may find this version easier to read and understand.
In addition to use our custom data structures, I also reduced the amount of the ambient light component and moved the light over to the right. I then used the Vector3DMakeWithStartAndEndPoints() to point the moved light at the icosahedron. By moving the light over and having it strike the object at an angle, the effect is a little more dramatic.
You should feel free to play with the light attributes, add additional lights or icosahedrons, and generally just play around to get a feel for how changes will effect your scene. This stuff is conceptually hard to grok, so don't expect it to come overnight (and if it does, count yourself lucky).
I took the opportunity to rewrite the setupView: method to use the Vertex3D, Vector3D, and Color3D structs we defined back in Part 1. Neither approach is "better", but I thought it might be interesting to see it done a little different way. When I was first learning OpenGL, I found it easier to think in terms of Vertices, Colors, and Triangles, rather than all the various-length arrays of floats. If you're like I was, then you may find this version easier to read and understand.
In addition to use our custom data structures, I also reduced the amount of the ambient light component and moved the light over to the right. I then used the Vector3DMakeWithStartAndEndPoints() to point the moved light at the icosahedron. By moving the light over and having it strike the object at an angle, the effect is a little more dramatic.
-(void)setupView:(GLView*)view
{
const GLfloat zNear = 0.01, zFar = 1000.0, fieldOfView = 45.0;
GLfloat size;
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
size = zNear * tanf(DEGREES_TO_RADIANS(fieldOfView) / 2.0);
CGRect rect = view.bounds;
glFrustumf(-size, size, -size / (rect.size.width / rect.size.height), size /
(rect.size.width / rect.size.height), zNear, zFar);
glViewport(0, 0, rect.size.width, rect.size.height);
glMatrixMode(GL_MODELVIEW);
// Enable lighting
glEnable(GL_LIGHTING);
// Turn the first light on
glEnable(GL_LIGHT0);
// Define the ambient component of the first light
static const Color3D light0Ambient[] = {{0.05, 0.05, 0.05, 1.0}};
glLightfv(GL_LIGHT0, GL_AMBIENT, (const GLfloat *)light0Ambient);
// Define the diffuse component of the first light
static const Color3D light0Diffuse[] = {{0.4, 0.4, 0.4, 1.0}};
glLightfv(GL_LIGHT0, GL_DIFFUSE, (const GLfloat *)light0Diffuse);
// Define the specular component and shininess of the first light
static const Color3D light0Specular[] = {{0.7, 0.7, 0.7, 1.0}};
glLightfv(GL_LIGHT0, GL_SPECULAR, (const GLfloat *)light0Specular);
glLightf(GL_LIGHT0, GL_SHININESS, 0.4);
// Define the position of the first light
// const GLfloat light0Position[] = {10.0, 10.0, 10.0};
static const Vertex3D light0Position[] = {{10.0, 10.0, 10.0}};
glLightfv(GL_LIGHT0, GL_POSITION, (const GLfloat *)light0Position);
// Calculate light vector so it points at the object
static const Vertex3D objectPoint[] = {{0.0, 0.0, -3.0}};
const Vertex3D lightVector = Vector3DMakeWithStartAndEndPoints(light0Position[0], objectPoint[0]);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, (GLfloat *)&lightVector);
// Define a cutoff angle. This defines a 50° field of vision, since the cutoff
// is number of degrees to each side of an imaginary line drawn from the light's
// position along the vector supplied in GL_SPOT_DIRECTION above
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 25.0);
glLoadIdentity();
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}You should feel free to play with the light attributes, add additional lights or icosahedrons, and generally just play around to get a feel for how changes will effect your scene. This stuff is conceptually hard to grok, so don't expect it to come overnight (and if it does, count yourself lucky).
Category Article OpenGL ES
Powered by Blogger.
Blog Archive
-
▼
2009
(496)
-
▼
May
(55)
- Another List for WWDC First-Timers
- Nearby Intersections on GOOG-411
- Nearby Intersections on GOOG-411
- Part 6
- WWDC Session Data Now Available at Attendee Site
- WWDC Session Data is Gone
- WWDC JSON Credit
- Another iCal Script
- WWDC Session Times for iCal Import
- Better Session Script
- WWDC Session Times
- Another Party, RSVP Required
- Multitouch on a MacBook
- Yet Another WWDC Party List
- OpenGL ES From the Ground Up, Part 6: Textures and...
- Mini Picture Sudoku Gadget
- Wolf Shirt Update
- Wolf Shirts
- Concurrent Operations (aka Threading)
- Google Sync Beta - Now for S60
- Google Sync Beta - Now for S60
- WWDC Party List
- Device Detection
- Yahoo Limits Mobile Development to One Platform
- iPhone Intelligence Party
- The Iterative Web App - Gmail for Mobile Gets Labels
- The Iterative Web App - Gmail for Mobile Gets Labels
- Nokia N Series: Highly innovated devices
- Meet the Nokia N97 and its smartphone rivals
- Just for the Record
- Feed Changes
- Dick Move - iwyre.net
- Google Product Search for Android now with Barcode...
- Google Product Search for Android now with Barcode...
- WWDC First Timer's Guide Redux
- OpenGL ES From the Ground Up, Part 5: Living in a ...
- Behind the scenes of the NYC - Google mapping part...
- Behind the scenes of the NYC - Google mapping part...
- No Steve-Note
- Sky Map for Android - A Mobile Planetarium
- Sky Map for Android - A Mobile Planetarium
- GoGadget
- Cheeky Quote Gadget
- YouTube uploads and more now available on Android
- YouTube uploads and more now available on Android
- Procedural Spheres in OpenGL ES
- New BlackBerry Enterprise Server Packages for Goog...
- New BlackBerry Enterprise Server Packages for Goog...
- Use Google Apps seamlessly with corporate BlackBer...
- Use Google Apps seamlessly with corporate BlackBer...
- Do more with Google Latitude!
- Do more with Google Latitude!
- OpenGL ES From the Ground Up, Part 1 Addendum: Alp...
- setupView: from Part IV Rewritten
- OpenGL ES From the Ground Up, Part 4: Let There Be...
-
▼
May
(55)