Find a C language OpenGL code

Source: Internet
Author: User

Http://zhidao.baidu.com/question/164682552.html

 

Use OpenGL to make a small ball move in a square space. When it hits the wall, it will rebound. Please do not come to Baidu. Thank you.

Best Answer
// Bounce. C // demonstrates a simple animated rectangle program with glut // OpenGL superbible, 2nd Edition // Richard S. wright Jr. # include "stdafx. H "# include <windows. h> # include <Gl/glut. h> // initial square position and sizeglfloat X1 = 100366f; glfloat Y1 = 150366f; glsizei rsize = 50; // step size in X and Y directions // (number of pixels to move each time) glfloat xstep = 1.0f; glfloat ystep = 1.0f; // ke EP track of Windows changing width and heightglfloat refreshing wwidth; glfloat refreshing wheight; // called to draw scenevoid renderscene (void) {// clear the window with current clearing color glclear (gl_color_buffer_bit ); // set current drawing color to Red // r g B glcolor3f (1.0f, 0.0f, 0.0f); // draw a filled rectangle with current color glrectf (x1, Y1, x1 + rsize, Y1 + rsize); // flush drawing commands gluts Wapbuffers ();} // called by glut library when idle (Window not being // resized or moved) void timerfunction (INT value) {// reverse direction when you reach left or right edge if (x1> 1_wwidth-rsize | X1 <0) xstep =-xstep; // reverse direction when you reach top or bottom edge if (Y1> running wheight-rsize | Y1 <0) ystep =-ystep; // check bounds. this is incase the window is made // smaller Nd the rectangle is outside the new // clipping volume if (x1> drawing wwidth-rsize) x1 = windowWidth-rsize-1; If (Y1> drawing wheight-rsize) Y1 = windowHeight-rsize-1; // actually move the square X1 + = xstep; Y1 + = ystep; // redraw the scene with new coordinates glupostredisplay (); glutimerfunc (33, timerfunction, 1 );} // setup the rendering statevoid setuprc (void) {// set clear color to Blue glclearc Olor (0.0f, 0.0f, 1.0f, 1.0f);} // called by glut library when the window has chanaged sizevoid changesize (glsizei W, glsizei H) {// prevent a divide by zero if (H = 0) H = 1; // set viewport to window dimensions glviewport (0, 0, W, H ); // reset coordinate system glmatrixmode (gl_projection); glloadidentity (); // keep the square, this time, save calculated // width and height for later use I F (W <= h) {running wheight = 2500000f * H/W; running wwidth = 2500000f;} else {running wwidth = 2500000f * W/H; running wheight = 2500000f ;} // set the clipping volume glortho (0.0f, expires wwidth, 0.0f, interval wheight, 1.0f,-1.0f); glmatrixmode (gl_modelview); glloadidentity ();} // main program entry pointvoid main (void) {gluinitdisplaymode (glu_double | glu_rgb); glucreatewindow ("Bounce"); gludisplayfunc (rendersce Ne); glureshapefunc (changesize); glutimerfunc (33, timerfunction, 1); setuprc (); glumainloop ();} // It is square. Change it ~ The program should be compiled by yourself to make progress. Come on!

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.