Realistic ball Rendering

Source: Internet
Author: User

Key functions for realistic ball rendering: glMaterialfv (GL_FRONT, GL_DIFFUSE, mat_shininess); // sets the glLightfv (GL_LIGHT0, GL_POSITION, light_position) of the material reflection index; // glShadeModel (GL_SMOOTH ); // set the shadow model glViewport (0, 0, (GLsizei) w, (GLsizei) h); glOrtho (x1, x2, y1, y2, z1, z2) in the viewpoint design ); // create a parallel visual object [cpp] # include <GL/glut. h>/* initialize material attributes, light source attributes, lighting models, and enable depth buffers. */void init (void) {GLfloat light_position [] = {1.0, 1.0, 1.0, 0.0}; GLfloat mat_di Ffuse [] = {1.0, 1.0, 1.0, 1.0}; glClearColor (0.0, 0.0, 1.0, 0.0); // set the background color to Blue glShadeModel (GL_SMOOTH ); // resolve (GL_FRONT, lag, mat_specular); resolve (GL_FRONT, GL_DIFFUSE, mat_diffuse); glLightfv (GL_LIGHT0, GL_POSITION, light_position); glable (GL_LIGHTING); glable (GL_LIGHT0 ); glable (GL_DEPTH_TEST);}/* call the GLUT function to draw a ball */void display (void) {glClear (GL_COLOR_ B) UFFER_BIT | GL_DEPTH_BUFFER_BIT); glsolidsphere (1.0, 40, 50); // glFlush ();}/* defines the reshape function of GLUT, w and h are the window width and height of the output image respectively */void reshape (int w, int h) {glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); if (w <= h) glOrtho (-1.5, 1.5,-1.5 * (GLfloat) h/(GLfloat) w, 1.5 * (GLfloat) h/(GLfloat) w,-10.0, 10.0); // create a parallel View Body else glOrtho (-1.5 * (GLfloat) w/(GLfloat) h, 1.5 * (GLfloat) w/(GLfloat) h,-1.5, 1.5,-10.0, 10.0 ); glMatrixMode (GL_MODELVIEW); glLoadIdentity ();} int main (int argc, char ** argv) {gluinit (& argc, argv ); // The GLUT environment initializes the gluinitdisplaymode (glu_single | glu_rgb | glu_depth); // The display mode initializes the gluinitwindowsize (300,300); // defines the window size. The value is 100,100. // defines the window position. AteWindow (argv [0]); // display window. The window title is the execution function name init ); // register the OpenGL plot function (a special call method, the same below). The response function of the registered window size change is (); // enter the GLUT message loop and run the program return 0;}: Set [cpp] GLfloat light_position [] = {1.0, 1.0, 1.0, 0.0} In init }; GLfloat mat_diffuse [] = {1.0, 1.0, 1.0, 1.0}; glClearColor (0.0, 0.0, 1.0, 0.0); // set the background color to Blue glShadeModel (GL_S MOOTH); // glMaterialfv (GL_FRONT, GL_AMBIENT, mat_specular); glMaterialfv (GL_FRONT, GL_DIFFUSE, mat_diffuse); changed to [cpp] GLfloat light_position [] = {1.0, 1.0, 1.0, 0.0}; GLfloat mat_diffuse [] = {1.0, 1.0, 1.0, 1.0}; GLfloat mat_shininess [] = {50.0}; glClearColor (0.0, 0.0, 1.0, 0.0 ); // set the background color to Blue glShadeModel (GL_SMOOTH); // glMaterialfv (GL_FRONT, GL_AMBIENT, mat_specular); glMaterialfv (GL_FRONT, GL_SPECULAR, mat_diffuse); glMaterialfv (GL_FRONT, GL_SHININESS, mat_shininess); // set the material reflection index to change the GL_DIFFUSE of the diffuse light to GL_SPECULAR of the mirror light, the reader can try it. --------------------------------------------------------- The above Code is made using glut, that is, openGL utilizing tool. Another way is to use OpenGL AUX to download glaux. lib and glaux. h. The Code is as follows: [cpp] # include <GL/glut. h> # include <gl/GLAUX. H ># pragma comment (lib, "glaux") void myinit (void) {GLfloat mat_specular [] = {1.0, 1.0, 1.0, 1.0 }; GLfloat mat_shininess [] = {50.0}; GLfloat light_position [] = {1.0, 1.0, 1.0, 0.0}; glMaterialfv (GL_FRONT, G Rochelle specular, mat_specular); // set material glMaterialfv (GL_FRONT, GL_SHININESS, mat_shininess); // you can specify glLightfv (GL_LIGHT0, GL_POSITION, light_position) for the material reflection index ); // The Position of the light source: GL_LIGHTING; GL_LIGHT0; glDepthFunc (GL_LEQUAL); GL_DEPTH_TEST;} void display (void) {glClear (callback | callback ); auxsolsphere (1.0); glFlush ();} void myReshape (GLsizei w, GLsiz Ei h) {glViewport (0, 0, w, h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); if (w <= h) glOrtho (-1.5, 1.5, -1.5 * (GLfloat) h/(GLfloat) w, 1.5 * (GLfloat) h/(GLfloat) w,-10.0, 10.0); else glOrtho (-1.5 * (GLfloat) w/(GLfloat) h, 1.5 * (GLfloat) w/(GLfloat) h,-1.5, 1.5,-10.0, 10.0); glMatrixMode (GL_MODELVIEW); glLoadIdentity ();} int main (int argc, char ** argv) {auxInitDisplayMode (AUX_SINGLE | AUX_RGBA | AUX_DEPTH); auxInitPosition (0, 0,500,500); auxInitWindow (LPCWSTR) "Lighting Sphere"); myinit (); auxReshapeFunc (callback) myReshape); auxMainLoop (AUXMAINPROC) display) ;}the effect is as follows: Finally, we can see the result of a set of ultra-complete brightness models. The code is here. Result

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.