PDA

Visualizza la versione completa : [C]Problema Openglut


Luc@s
30-03-2005, 12:51
#include <GL/openglut.h>
#include <gl/gl.h>
#include <gl/glaux.h>
#include <stdio.h>
#include <stdlib.h>

#define W 800
#define H 600
#define TITLE "Ogl Base"
#define ESC 27
//#define DEBUG

float red=1.0, blue=1.0, green=1.0, x_angle = 0.0, y_angle = 0.0, z_angle = 0.0;
const char * msg = "\tPress UP/DOWN in order to rotate x axis \
\tPress LEFT/RIGTH in order to rotate y axis \
\tPress PAGE UP/PAGE DOWN in order to rotate z axis \
\tPress O/P in order to enable/disable ligthning";

GLuint texture[3], filter;
// 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 };


AUX_RGBImageRec *LoadBMP(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!");
fclose(fout);
#endif
return NULL; // If Not Return NULL
}
File=fopen(Filename,"r");
if (File) // Does The File Exist?
{
#ifdef DEBUG
FILE * fout = fopen("info.txt", "a");
fprintf(fout,"Texture Loaded!");
fclose(fout);
#endif
fclose(File); // Close The Handle
return auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer
}
}

int LoadGLTextures(char *Filename) // Load Bitmaps And Convert To Textures
{
int Status=FALSE;
AUX_RGBImageRec *TextureImage[1];
// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
if (TextureImage[0]=LoadBMP(Filename))
{
Status=TRUE;
glGenTextures(3, &texture[0]);
// Create Nearest Filtered Texture
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_NEAREST); // ( NEW )
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_NEAREST); // ( NEW )
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

// Create Linear Filtered Texture
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

// Create MipMapped Texture
glBindTexture(GL_TEXTURE_2D, texture[2]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR_MIPMAP_NEAREST); // ( NEW )

gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data); // ( NEW )

}
if (TextureImage[0]) // If Texture Exists
{
if (TextureImage[0]->data) // If Texture Image Exists
{
free(TextureImage[0]->data); // Free The Texture Image Memory
}

free(TextureImage[0]); // Free The Image Structure
}
return Status;
}

void renderScene()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

if (!LoadGLTextures("Data/Conan2.bmp")) // Jump To Texture Loading Routine ( NEW )
{
return; // If Texture Didn't Load Return FALSE ( NEW )
}

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

glColor3f(red,green,blue);

glBindTexture(GL_TEXTURE_2D, texture[filter]); // 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 == 'o')
glEnable(GL_LIGHTING);
if(key == 'p')
glDisable(GL_LIGHTING);

}
/*
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_F5 :
filter = 0; break;
case GLUT_KEY_F6 :
filter = 1; break;
case GLUT_KEY_F7 :
filter = 2; break;

case GLUT_KEY_DOWN :
x_angle++; break;
case GLUT_KEY_UP :
x_angle--; break;

case GLUT_KEY_PAGE_DOWN :
z_angle--; break;
case GLUT_KEY_PAGE_UP :
z_angle++; break;

case GLUT_KEY_RIGHT :
y_angle++; break;
case GLUT_KEY_LEFT :
y_angle--; break;

case GLUT_KEY_F11:
MessageBox(NULL, msg, "Help.....", MB_ICONINFORMATION);
break;
}

}



int main(int argc, char **argv)
{
glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);

glutInitWindowPosition(100,100);
glutInitWindowSize(W,H);

glutCreateWindow(TITLE);

glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
glutIdleFunc(renderScene);
glutKeyboardFunc(processNormalKeys);
glutSpecialFunc(processSpecialKeys);

// enable depth testing
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);
glutMainLoop();
return 0;
}


Questo pgm dopo un 25 secondi mi va in 'Out of memory' e mi sballa tutto.....secondo voi a cosa dovuto??

p.s: la bmp una semplice bmp 256X256

Luc@s
30-03-2005, 18:42
up

Luc@s
31-03-2005, 18:26
:dh:

Loading