Discussione: Problema Openglut
View Single Post
Old 31-03-2005, 21:38   #7
Luc@s
Senior Member
 
L'Avatar di Luc@s
 
Iscritto dal: Apr 2002
Cittā: Vigevano(PV)
Messaggi: 2124
Codice:
 
#include <GL/openglut.h>
#include <gl/gl.h>
#include "glbmp.h"
#include "Conf.h"

#define W 800
#define H 600

#define BLANDING_DEPTH 0.5 // 50%

#define TITLE "Ogl Base"
#define ESC 27
//#define DEBUG

float red=1.0, blue=1.0, green=1.0, alpha = BLANDING_DEPTH, x_angle = 0.0, y_angle = 0.0, z_angle = 0.0; 

GLuint texture = 0, tx_count = 0;	
bool blend = false, ligth = false;

// ligth
GLfloat LightAmbient[]=  { 0.5f, 0.5f, 0.5f, 1.0f }; 	
GLfloat LightDiffuse[]=  { 1.0f, 1.0f, 1.0f, 1.0f };	
GLfloat LightPosition[]= { 1.0f, 0.0f, 2.0f, 1.0f };

glbmp_t bitmap;

void LoadBMP(const char *Filename)					// Loads A Bitmap Image
{
	FILE *File=NULL;			
	if (!Filename)								// Make Sure A Filename Was Given
	{
    #ifdef DEBUG  
    FILE * fout = fopen("error.txt", "a");
    fprintf(fout,"Texture Not Exist!\n");
    fclose(fout);
    #endif 
		return;							// If Not Return NULL
	}
	File=fopen(Filename,"r");
	if (File)								// Does The File Exist?
	{
    #ifdef DEBUG 
    tx_count++;
    FILE * fout = fopen("info.txt", "a");
    fprintf(fout,"Texture Loaded n %i!\n", tx_count);
    fclose(fout);
    #endif
		fclose(File);							// Close The Handle
		glbmp_LoadBitmap(Filename, 0, &bitmap);				// Load The Bitmap
	}
}

int LoadGLTextures(const char *Filename)								// Load Bitmaps And Convert To Textures
{
 int Status=FALSE;
 // Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
 if (glbmp_LoadBitmap(Filename, 0, &bitmap))
 {
		Status=TRUE;
   //generate and bind the OpenGL texture
   glGenTextures(1, &texture);
   glBindTexture(GL_TEXTURE_2D, texture);

   //copy data from bitmap into texture
   glTexImage2D(GL_TEXTURE_2D, 0, 3, bitmap.width, bitmap.height,
                0, GL_RGB, GL_UNSIGNED_BYTE, bitmap.rgb_data);

   //set up texture filtering
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

   //free the bitmap
   glbmp_FreeBitmap(&bitmap);
 }
	return Status;
}

void renderScene() 
{
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  
  glPushMatrix(); // save previus object
  
  glRotatef(x_angle,1.0,0.0,0.0); // Rotate On The X Axis By x_angle
  glRotatef(y_angle,0.0,1.0,0.0); // Rotate On The Y Axis By y_angle
  glRotatef(z_angle,0.0,0.0,1.0); // Rotate On The Z Axis By z_angle
  
	glColor4f(red,green,blue, alpha);
	glBlendFunc(GL_SRC_ALPHA,GL_ONE);
	
	glBindTexture(GL_TEXTURE_2D, texture);				// Select Our Texture

	glBegin(GL_QUADS);								
    glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);	// Bottom Left Of The Texture and Quad
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);	// Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);	// Top Left Of The Texture and Quad
		// Back Face
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);	// Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);	// Top Left Of The Texture and Quad
		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);	// Bottom Left Of The Texture and Quad
		// Top Face
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);	// Top Left Of The Texture and Quad
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);	// Bottom Left Of The Texture and Quad
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);	// Top Right Of The Texture and Quad
		// Bottom Face
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);	// Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);	// Top Left Of The Texture and Quad
		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);	// Bottom Left Of The Texture and Quad
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);	// Bottom Right Of The Texture and Quad
		// Right face
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);	// Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);	// Top Left Of The Texture and Quad
		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);	// Bottom Left Of The Texture and Quad
		// Left Face
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);	// Bottom Left Of The Texture and Quad
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);	// Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);	// Top Left Of The Texture and Quad
	glEnd();
	
  glPopMatrix();
	
	glutSwapBuffers();


}

void changeSize(int w, int h) 
{
 if(h == 0)
		h = 1;

	float ratio = 1.0* w / h;
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	
	// Set the viewport to be the entire window
	glViewport(0, 0, w, h);

	// Set the correct perspective.
	gluPerspective(45,ratio,1,1000);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(0.0,0.0,5.0, 
		      0.0,0.0,-1.0,
			  0.0f,1.0f,0.0f);
}


void processNormalKeys(unsigned char key, int x, int y) 
{

     if(key == 'q' || key == ESC)
     {
            #ifdef DEBUG 
            FILE * fout = fopen("info.txt", "a");
            fprintf(fout,"Application Exit with 0 result\n");
            fclose(fout);
            #endif 
            exit(0);   
     }
     if(key == 'l' || key == 'L')
     {
            ligth = !ligth;
            if(ligth)
            {
             glEnable(GL_LIGHTING);
            }
            else
            {
             glDisable(GL_LIGHTING);
            }        
     }	
     if(key == 'b' || key == 'B')
     {   
		     blend = !blend;				// Toggle blend TRUE / FALSE	
		     if(blend)				// Is blend TRUE?
		     {
			    glEnable(GL_BLEND);		// Turn Blending On
			    glDisable(GL_DEPTH_TEST);	// Turn Depth Testing Off
		     }
		     else					// Otherwise
		     {
			    glDisable(GL_BLEND);		// Turn Blending Off
			    glEnable(GL_DEPTH_TEST);	// Turn Depth Testing On
         }
     }

}
/*
GLUT_KEY_F1		        F1 function key
GLUT_KEY_F2		        F2 function key
GLUT_KEY_F3		        F3 function key
GLUT_KEY_F4		        F4 function key
GLUT_KEY_F5		        F5 function key
GLUT_KEY_F6		        F6 function key
GLUT_KEY_F7		        F7 function key
GLUT_KEY_F8		        F8 function key
GLUT_KEY_F9		        F9 function key
GLUT_KEY_F10		      F10 function key
GLUT_KEY_F11		      F11 function key
GLUT_KEY_F12		      F12 function key
GLUT_KEY_LEFT		      Left function key
GLUT_KEY_RIGHT	      Up function key
GLUT_KEY_UP		        Right function key
GLUT_KEY_DOWN		      Down function key
GLUT_KEY_PAGE_UP      Page Up function key
GLUT_KEY_PAGE_DOWN	  Page Down function key
GLUT_KEY_HOME		      Home function key
GLUT_KEY_END		      End function key
GLUT_KEY_INSERT		    Insert function key
*/
void processSpecialKeys(int key, int x, int y) 
{
 switch(key) 
 {
		case GLUT_KEY_F1 : 
				red   = 1.0; 
				green = 0.0; 
				blue  = 0.0; break;
		case GLUT_KEY_F2 : 
				red   = 0.0; 
				green = 1.0; 
				blue  = 0.0; break;
		case GLUT_KEY_F3 : 
				red   = 0.0; 
				green = 0.0; 
				blue  = 1.0; break;
		case GLUT_KEY_F4 : 
				red   = 1.0; 
				green = 1.0; 
				blue  = 1.0; break;
          			
		case GLUT_KEY_DOWN : 
        x_angle--;  break;
		case GLUT_KEY_UP : 
        x_angle++;  break;

		case GLUT_KEY_PAGE_UP : 
        z_angle++;  break;        
		case GLUT_KEY_PAGE_DOWN : 
        z_angle--;  break;
        
		case GLUT_KEY_RIGHT : 
        y_angle++;  break;
		case GLUT_KEY_LEFT : 
        y_angle--;  break;
        
    case GLUT_KEY_F11:
         MessageBox(NULL, "Press L in order to enable/disable ligthning \nPress B  in order to enable/disable blending", "Help.....", MB_ICONINFORMATION);
                    break;
	}

}



int main(int argc, char **argv)
{
	glutInit(&argc, argv);
	
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
	
	glutInitWindowPosition(100,100);
	glutInitWindowSize(W,H);
	
	glutCreateWindow(TITLE);
	
	glutDisplayFunc(renderScene);
	glutReshapeFunc(changeSize);
	glutIdleFunc(renderScene);
	glutKeyboardFunc(processNormalKeys);
	glutSpecialFunc(processSpecialKeys);	

	//glEnable(GL_DEPTH_TEST);
	glEnable(GL_TEXTURE_2D);						// Enable Texture Mapping ( NEW )
	glShadeModel(GL_SMOOTH);						// Enable Smooth Shading
	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);					// Black Background
	glClearDepth(1.0f);
	glDepthFunc(GL_LEQUAL);							// The Type Of Depth Testing To Do
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	// Setup Light
	glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
  glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
  glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);
  glEnable(GL_LIGHT1);
  
 	if (!LoadGLTextures(TEXTURE))							// Jump To Texture Loading Routine ( NEW )
	{ 
		return -1;							// If Texture Didn't Load Return FALSE ( NEW )
	}
	
	glutMainLoop();
	return 0;
}
//:~
Con la tua lib....mi si apre e si chiude subito senza mostrare nulla :'(
__________________
Gnu/Linux User
Luc@s č offline   Rispondi citando il messaggio o parte di esso