School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,489 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,810 people online right now. Registration is fast and FREE... Join Now!




microsoft visual studio-opengl

 

microsoft visual studio-opengl, bouncing balls PROGRAM

pavi.r

4 May, 2009 - 06:09 AM
Post #1

New D.I.C Head
*

Joined: 4 May, 2009
Posts: 1

CODE

#include <windows.h>
#include <GL/glut.h>
#include "tgaload.h"
int random(int );


/* Define ball class for animation values */
class BallClass {
public:
    float inc;
    float yposo,vyo,ypos,times,ay;
    float xposo,vxo,xpos,ax;
   float rotate;
    BallClass () {
       rotate=5;
        inc=.02;
        yposo=2,vyo=0,ypos=2,times=0,ay=-9.8;
        xposo=-2,vxo=random(20)+1,xpos=2,ax=0;
   }

};

/* Create an array of type BallClass that may hold 500 balls */
BallClass ball[500];
int ballnum=0;
int totalball=5;
double d=0; // used in rotation

float zoom = -7;

image_t         fire_image;

void init ( void )
{

   tgaGetColorEXT();
    glColorMaterial ( GL_FRONT, GL_AMBIENT_AND_DIFFUSE );

   glEnable      ( GL_TEXTURE_2D );
   glPixelStorei ( GL_UNPACK_ALIGNMENT, 1 );
   glGenTextures ( 1, 0 );

   glBindTexture ( GL_TEXTURE_2D, 0 );
   tgaLoadImage  ( "fire.tga", &fire_image, TGA_FREE );

   glEnable ( GL_CULL_FACE );
    glClearColor ( 0, 0.0, 1.0, 0.0 );
      glTranslatef ( 0.0, 0.0, zoom );

   }

void display(void)
{

    for (ballnum=0;ballnum < totalball; ballnum++) {
        d+=0.4;
        glPushMatrix  ( );
        glBindTexture ( GL_TEXTURE_2D, 0 );
        GLUquadricObj*    q = gluNewQuadric ( );
          gluQuadricDrawStyle ( q, GLU_FILL   );
          gluQuadricNormals   ( q, GLU_SMOOTH );
          gluQuadricTexture   ( q, GL_TRUE    );
       glTranslated ( ball[ballnum].xpos, ball[ballnum].ypos, -11.0 );
       glRotated(ball[ballnum].rotate,1,1,1);
          gluSphere ( q, .3, 20, 20 );
          gluDeleteQuadric ( q );
        glPopMatrix ( );
   }

    glEnable    ( GL_BLEND );
    glPopMatrix ( );
    glutSwapBuffers ( );
    glClear   ( GL_COLOR_BUFFER_BIT );
}

void reshape(int w, int h)
{
    glViewport(0, 0, (GLint) w, (GLint) h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();

    if ( h==0)
        gluPerspective(45, (GLdouble)w, 1.0, 100.0);
    else
        gluPerspective(45, (GLdouble)w/(GLdouble)h,1.0, 100.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
}

void idle_func (void)
{
for (ballnum=0;ballnum < totalball; ballnum++) {
   ball[ballnum].rotate+=((rand()%10));
   ball[ballnum].times+=ball[ballnum].inc;
   ball[ballnum].ypos=ball[ballnum].yposo + (ball[ballnum].vyo*ball[ballnum].times) + (.5*ball[ballnum].ay*
                    (ball[ballnum].times*ball[ballnum].times));
   ball[ballnum].xpos=ball[ballnum].xposo + (ball[ballnum].vxo*ball[ballnum].times)
                    + (.5*ball[ballnum].ax*(ball[ballnum].times*ball[ballnum].times));
   if ((ball[ballnum].ypos < -4.2)) {
       ball[ballnum].vyo=random(15);
       ball[ballnum].times=0;
      ball[ballnum].yposo=ball[ballnum].ypos;
      ball[ballnum].xposo=ball[ballnum].xpos;
      }
   else if ((ball[ballnum].ypos > 4.2))  {
       ball[ballnum].ay=1*ball[ballnum].ay;ball[ballnum].vyo=-1
               *random(10);ball[ballnum].times=0;
      ball[ballnum].yposo=ball[ballnum].ypos;
      ball[ballnum].xposo=ball[ballnum].xpos;
      }
   else if ((ball[ballnum].xpos > 5.5))  {
       ball[ballnum].vxo=-1-random(20);ball[ballnum].times=0;
      ball[ballnum].xposo=ball[ballnum].xpos;ball[ballnum].yposo=ball[ballnum].ypos;
      }
   else if ((ball[ballnum].xpos < -5.5))  {
       ball[ballnum].vxo=random(20);ball[ballnum].times=0;
      ball[ballnum].xposo=ball[ballnum].xpos;ball[ballnum].yposo=ball[ballnum].ypos;
      }

   }


   glutPostRedisplay();
}

/*  Main Loop
*  Open window with initial window size, title bar,
*  RGBA display mode, and handle input events.
*/
int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
   glutInitWindowSize (800, 600);
   glutCreateWindow (argv[0]);
   init();
   glutReshapeFunc (reshape);
   glutDisplayFunc (display);
   glutIdleFunc (idle_func);
   glutMainLoop();
   return 0;
}



WHENEVER I EXECUTE THE ABOVE CODE,I GET 5 ERRORS SAYING RANDOM IDENTIFIER NOT DEFINED. WHAT SHOULD I DO?sad.gif

Mod Edit: Please use code tags when posting your code. Code tags are used like so => code.gif

Thanks,
PsychoCoder smile.gif

User is offlineProfile CardPM
+Quote Post


PsychoCoder

RE: Microsoft Visual Studio-opengl

4 May, 2009 - 06:35 AM
Post #2

Dyslexics Untie!
Group Icon

Joined: 26 Jul, 2007
Posts: 14,714



Thanked: 501 times
Dream Kudos: 11450
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
  • Post the code you're having problems with
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field

User is offlineProfile CardPM
+Quote Post

modi123_1

RE: Microsoft Visual Studio-opengl

4 May, 2009 - 07:42 AM
Post #3

Suiter #2
Group Icon

Joined: 12 Jun, 2008
Posts: 1,760



Thanked: 72 times
Dream Kudos: 150
My Contributions
fyi.. wrong forum.. but your 'random' is not instantiated anywhere.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 04:15AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month