Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: waving flag not waving

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2010
    Posts
    19

    waving flag not waving

    hello , i adapted the code from nehe`s lesson 11 , the waving flag. i started a new clean project and i figured the first step is to make the actual flag without texture . so i did that but the problem is it s not waving ...

    the variables
    Code :
    float points[45][45][3];    // The Array For The Points On The Grid Of Our "Wave"
    int wiggle_count = 0;		// Counter Used To Control How Fast Flag Waves
     
    GLfloat	xrot;				// X Rotation ( NEW )
    GLfloat	yrot;				// Y Rotation ( NEW )
    GLfloat	zrot;				// Z Rotation ( NEW )
    GLfloat hold;				// Temporarily Holds A Floating Point Value

    the init function
    Code :
    void init(void) 
    {
       glClearColor (0.0, 0.0, 0.0, 0.0);   
       glEnable(GL_DEPTH_TEST);
       glShadeModel (GL_SMOOTH);
       glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do
     
     
       for(int x=0; x<45; x++)
    	{
    		for(int y=0; y<45; y++)
    		{
    			points[x][y][0]=float((x/5.0f)-4.5f);
    			points[x][y][1]=float((y/5.0f)-4.5f);
    			points[x][y][2]=float(sin((((x/5.0f)*40.0f)/360.0f)*3.141592654*2.0f));
    		}
    	}
     
    }

    the flag is an animated, sine-wave picture. composed of a grid that has 45 points by 45 points, which in turn makes 44 quads x 44 quads. the var wiggle_count is used to keep track of how fast the texture waves.

    the display function
    Code :
    void display(void)
    {
    	int x, y;
    	float float_x, float_y, float_xb, float_yb;
     
    	glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    	glLoadIdentity ();
     
    	glTranslatef(0.0f,0.0f,-12.0f);
     
    	glRotatef(xrot,1.0f,0.0f,0.0f);
    	glRotatef(yrot,0.0f,1.0f,0.0f);  
    	glRotatef(zrot,0.0f,0.0f,1.0f);
     
    	glBegin(GL_QUADS);
    	for( x = 0; x < 44; x++ )
    	{
    		for( y = 0; y < 44; y++ )
    		{
    			float_x = float(x)/44.0f;
    			float_y = float(y)/44.0f;
    			float_xb = float(x+1)/44.0f;
    			float_yb = float(y+1)/44.0f;
     
    			glVertex3f( points[x][y][0], points[x][y][1], points[x][y][2] );
     
    			glVertex3f( points[x][y+1][0], points[x][y+1][1], points[x][y+1][2] );
     
    		        glVertex3f( points[x+1][y+1][0], points[x+1][y+1][1], points[x+1][y+1][2] );
     
    			glVertex3f( points[x+1][y][0], points[x+1][y][1], points[x+1][y][2] );
    		}
    	}
    	glEnd();
     
    	if( wiggle_count == 2 )
    	{
    		for( y = 0; y < 45; y++ )
    		{
    			hold=points[0][y][2];
    			for( x = 0; x < 44; x++)
    			{
    				points[x][y][2] = points[x+1][y][2];
    			}
    			points[44][y][2]=hold;
    		}
    		wiggle_count = 0;
    	}
     
    	wiggle_count++;
     
    	xrot+=0.3f;
    	yrot+=0.2f;
    	zrot+=0.4f;
     
    	Sleep(5);
    	glutSwapBuffers();
     
    }

    my reshape function
    Code :
    void reshape (int w, int h)
    {
       glViewport (0, 0, (GLsizei) w, (GLsizei) h); 
       glMatrixMode (GL_PROJECTION);
       glLoadIdentity ();
     
       gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
     
       glMatrixMode (GL_MODELVIEW);
       glLoadIdentity();
     
       glutPostRedisplay();
    }

    and main function
    Code :
    int main(int argc, char** argv)
    {
       glutInit(&amp;argc, argv);
       glutInitDisplayMode (GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
       glutInitWindowSize (500, 500); 
       glutInitWindowPosition (100, 100);
       glutCreateWindow (argv[0]);
       init ();
       glutDisplayFunc(display); 
       glutReshapeFunc(reshape);
       glutKeyboardFunc(keyboard);
     
       glutMainLoop();
       return 0;
    }

    i added a file with what it display , it s just a static flag.

    here s nehes code , if you would please have a look , or just tell me if something is missing from mine

    http://nehe.gamedev.net/data/lessons/vc/lesson11.zip

  2. #2
    Junior Member Newbie
    Join Date
    Dec 2010
    Posts
    19

    Re: waving flag not waving

    or is the whole effect of waving coming from the texture ?
    (maybe this is a stupid question... )

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2010
    Posts
    19

    Re: waving flag not waving

    really....no one ???

  4. #4
    Member Regular Contributor
    Join Date
    Dec 2009
    Posts
    251

    Re: waving flag not waving

    Don't you need a glutTimerFunc or similar that triggers a periodic call to display ?

  5. #5
    Junior Member Newbie
    Join Date
    Dec 2010
    Posts
    19

    Re: waving flag not waving

    can you please exemplify how should i do this ?

  6. #6
    Member Regular Contributor
    Join Date
    Dec 2009
    Posts
    251

    Re: waving flag not waving

    You have to define a timer function that triggers the redisplay:

    Code :
    void timer(int unused) {
      glutPostRedisplay();
      glutTimerFunc(30,timer,0);
    }

    and also add a call to glutTimerFunc to your main() before the glutMainLoop() to start the animation:
    Code :
      glutTimerFunc(30,timer,0);

  7. #7
    Junior Member Newbie
    Join Date
    Dec 2010
    Posts
    19

    Re: waving flag not waving

    thank you very much , mbentrup !!!

    it s waving now

    next i ll try to apply the texture , but the problem is that in nehe`s tutorial`s code it has a bmp loader that depends to glaux library , which i want my project to independent off. i ll try to use another loader.

  8. #8
    Junior Member Newbie
    Join Date
    Dec 2010
    Posts
    19

    Re: waving flag not waving

    i succeded applying a skull texture to the flag , useing a .tga loader .

    this is the texture function i use :

    Code :
    GLuint	texture;
    Texture	treeTexture;
     
     
    bool LoadTreeTextures()
    {
    	// call the tga file loader. 
    	if (LoadTGA(&amp;treeTexture, "flag_texture.tga"))	
    	{		
     
    		glGenTextures(1, &amp;treeTexture.texID);
    		glBindTexture(GL_TEXTURE_2D, treeTexture.texID);
    		glTexImage2D(GL_TEXTURE_2D, 0, treeTexture.bpp / 8, treeTexture.width, treeTexture.height, 0, treeTexture.type, GL_UNSIGNED_BYTE, treeTexture.imageData);
     
    		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    		glEnable(GL_TEXTURE_2D);
    		if (treeTexture.imageData)		
    		{
    		free(treeTexture.imageData);					// Free The Texture Image Memory ( CHANGE )
    		}
    		return true;	
    	}													// Return The Status
    	else return false;
    }

    and the display function :
    Code :
    void display(void)
    {
     
    	int x, y;
    	float float_x, float_y, float_xb, float_yb;
     
    	glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    	glLoadIdentity ();
     
    //	glScalef ( -1,-1 ,-1 );
    	glTranslatef(0.0f,0.0f,-12.0f);
     
     
    	glBegin(GL_QUADS);
    	for( x = 0; x < 44; x++ )
    	{
    		for( y = 0; y < 44; y++ )
    		{
    			float_x = float(x)/44.0f;
    			float_y = float(y)/44.0f;
    			float_xb = float(x+1)/44.0f;
    			float_yb = float(y+1)/44.0f;
     
    			glTexCoord2f( float_x, float_y);
    			glVertex3f( points[x][y][0], points[x][y][1], points[x][y][2] );
     
    			glTexCoord2f( float_x, float_yb );
    			glVertex3f( points[x][y+1][0], points[x][y+1][1], points[x][y+1][2] );
     
    		    glTexCoord2f( float_xb, float_yb );
    			glVertex3f( points[x+1][y+1][0], points[x+1][y+1][1], points[x+1][y+1][2] );
     
    			glTexCoord2f( float_xb, float_y );
    			glVertex3f( points[x+1][y][0], points[x+1][y][1], points[x+1][y][2] );
    		}
    	}
    	glEnd();
     
    	if( wiggle_count == 2 )
    	{
    		for( y = 0; y < 45; y++ )
    		{
    			hold=points[0][y][2];
    			for( x = 0; x < 44; x++)
    			{
    				points[x][y][2] = points[x+1][y][2];
    			}
    			points[44][y][2]=hold;
    		}
    		wiggle_count = 0;
    	}
     
    	wiggle_count++;
     
    	Sleep(5);
    	glutSwapBuffers();
     
     
    }

    and here s how it looks :X

  9. #9
    Junior Member Newbie
    Join Date
    Dec 2010
    Posts
    19

    Problem integrating the flag in another project...

    the next step is my new problem now...

    i have a project where i imported some models that i made in 3ds max , in wich you can move with mouse + WASD , added lightning , a GLUI interface

    i wanted to add the flag on top of another model , a castle.

    the flag appears , waves , but with no texture on ... i looked a lot , but no luck solving it .
    i just copied the code from the previous clean project , in which the flag was textured (as you can see in my prevoius post) .

    i ll try to add as little code as i can ... some functions i ll only write the definition.

    Code :
    //my first function is the texture function that i wrote in the prevoius post
    bool LoadTreeTextures(){
    .....}
     
    //the timer function that mbentrup suggested , thanks again
    void timer(int unused){
    ......}
     
    void myGlutIdle( void ){
    ......}
     
    //the reshape function
    void reshape (int w, int h)
     
    {
       int unused;
     
       glViewport (0, 0, (GLsizei) w, (GLsizei) h); 
       glutSetCursor(GLUT_CURSOR_NONE); //Hide cursor
       glutWarpPointer(WINDOW_WIDTH/2, WINDOW_HEIGHT/2); //Bring cursor to center
       glMatrixMode (GL_PROJECTION);  //next lines of code will affect the projection matrix
       glLoadIdentity();
     
       gluPerspective (45.0, (GLfloat)w / (GLfloat)h, 1.0, 1000.0); 
     
      timer(unused);
     
      glutPostRedisplay();
    }
     
    //function that draws 3ds max object .obj
    void drawmodel(void){
    ...}
     
    //camera function for mouse rotation
    void camera (void) {
    ....}
     
    //my display function :
    void display(void)
    {
        int x, y;
        float float_x, float_y, float_xb, float_yb;
     
        glClearColor (1.0,1.0,1.0,1.0);                     //clear the screen to black
        glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
     
     
        glMatrixMode (GL_MODELVIEW);  
       glLoadIdentity(); 
            camera();										   
    glPushMatrix();					
      glScalef(300,300,300);
      drawmodel2();
    glPopMatrix();
     
    	//FLAG//
      glTranslatef(0.0f,-90.0f,60.0f);		
     
      glBindTexture(GL_TEXTURE_2D, texture);
     
    	glBegin(GL_QUADS);
    	for( x = 0; x < 44; x++ )
    	{
    		for( y = 0; y < 44; y++ )
    		{
    			float_x = float(x)/44.0f;
    			float_y = float(y)/44.0f;
    			float_xb = float(x+1)/44.0f;
    			float_yb = float(y+1)/44.0f;
     
    			glTexCoord2f( float_x, float_y);
    			glVertex3f( points[x][y][0], points[x][y][1], points[x][y][2] );
     
    			glTexCoord2f( float_x, float_yb );
    			glVertex3f( points[x][y+1][0], points[x][y+1][1], points[x][y+1][2] );
     
    			glTexCoord2f( float_xb, float_yb );
    			glVertex3f( points[x+1][y+1][0], points[x+1][y+1][1], points[x+1][y+1][2] );
     
    			glTexCoord2f( float_xb, float_y );
    			glVertex3f( points[x+1][y][0], points[x+1][y][1], points[x+1][y][2] );
    		}
    	}
     
    	glEnd();
     
    	if( wiggle_count == 2 )
    	{
    		for( y = 0; y < 45; y++ )
    		{
    			hold=points[0][y][2];
    			for( x = 0; x < 44; x++)
    			{
    				points[x][y][2] = points[x+1][y][2];
    			}
    			points[44][y][2]=hold;
    		}
    		wiggle_count = 0;
    	}
     
    	wiggle_count++;
     
    glutSwapBuffers();
    }
     
     
    my init function:
    int initGL(GLvoid)										
    {
     
    	if (!LoadTreeTextures())								// Jump To Texture Loading Routine ( NEW )
    	{
    		return FALSE;									// If Texture Didn't Load Return FALSE
    	}
     
       glEnable(GL_TEXTURE_2D);
     
       glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);
       glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
       glLightfv(GL_LIGHT0,GL_SPECULAR,specularLight);
       glLightfv(GL_LIGHT0,GL_POSITION,lightDir);
     
     
       glMaterialfv(GL_FRONT, GL_SPECULAR, specularReflection);
       glMateriali(GL_FRONT,GL_SHININESS,128);
     
       glEnable(GL_LIGHT0);                         	              // activate light0
       glEnable(GL_LIGHTING);                       	              // enable lighting
       glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight); 	      // set light model
       glEnable(GL_COLOR_MATERIAL);                 	              // activate material
       glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
       glEnable(GL_NORMALIZE);										  // normalize normal vectors
     
       glShadeModel(GL_SMOOTH);						        // Enables Smooth Shading
     
       glEnable(GL_DEPTH_TEST);						        // Enables Depth Testing
       glDepthFunc(GL_LEQUAL);						        // The Type Of Depth Test To Do
       glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);   // Really Nice Perspective Calculations
     
     
      // glEnable (GL_FOG); //enable the fog
       glFogf(GL_FOG_START, 1.0f);				// Fog Start Depth
       glFogf(GL_FOG_END, 100.0f);				// Fog End Depth
       glFogi (GL_FOG_MODE, GL_EXP2); //set the fog mode to GL_EXP2
       glFogfv (GL_FOG_COLOR, fogColor); //set the fog color to our color chosen above
       glFogf (GL_FOG_DENSITY, density); //set the density to the value above
       glHint (GL_FOG_HINT, GL_NICEST); // set the fog to look the nicest, may slow down on older cards
     
       glPolygonMode( GL_BACK|GL_FRONT , GL_FILL );
       for(int x=0; x<45; x++)
    	{
    		for(int y=0; y<45; y++)
    		{
    			points[x][y][0]=float((x/5.0f)-4.5f);
    			points[x][y][1]=float((y/5.0f)-4.5f);
    			points[x][y][2]=float(sin((((x/5.0f)*40.0f)/360.0f)*3.141592654*2.0f));
    		}
    	}
     
       return TRUE;								            // Initialization Went OK
    }
     
    //hot keys keyboard funnction :
    void (keyboard){
    ...}
     
    void mouseMovement(int x, int y){
    ...}
     
    int main(int argc, char** argv)
    {
     //  glutInit(&amp;argc, argv);
     
       glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
     
       glutInitWindowPosition (200, 200);				   //window position 
       glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);    //window size
     
       main_window = glutCreateWindow( "Castle Project" );
       //glutCreateWindow (argv[0]);
     
       glutDisplayFunc(display); 
     
        glutReshapeFunc(reshape);
     
       initGL ();
     
       glutPassiveMotionFunc(mouseMovement); //check for mouse movement
       glutKeyboardFunc(keyboard);
     
      ....some GLUI code for interface...
     
          glutTimerFunc(40,timer,0);
     
       glutMainLoop();
       return 0;
    }

    can someone see why isn`t my flag textured ?? plsss

    here s picture :


  10. #10
    Intern Newbie
    Join Date
    Jan 2011
    Location
    Lincoln, UK
    Posts
    36

    Re: Problem integrating the flag in another project...

    in your loadTreeTexture function you define the texture as treeTexture.texID

    in your display function, you define the texture as texture.

    I think you are not binding the texture correctly.

    Dale

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •