View Full Version : Problema Openglut
#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_FILTER,GL_NEAREST); // ( NEW )
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,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_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,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_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,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
DanieleC88
31-03-2005, 18:58
Da una occhiata veloce, direi che il codice è a posto. Puoi mandare l'output di un debugger? Io ho fatto un programmino simile sotto Linux (SDL+OpenGL) e va alla perfezione. Magari un'altra volta ti mando un po' di codice.
#include <GL/openglut.h>
#include <gl/gl.h>
#include <gl/glaux.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;
const char * msg = "\tPress L in order to enable/disable ligthning \
\tPress B in order to enable/disable blending \
\tPress F5 in order to use no filtering \
\tPress F6 in order to use linear filtering \
\tPress F7 in order to use mipmapped filtering(all dimension accepted and higthest quality)";
GLuint texture[1], 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 };
AUX_RGBImageRec *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 NULL; // 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
return auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer
}
}
int LoadGLTextures(const 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(1, &texture[0]);
// Create MipMapped Texture
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); // ( NEW )
gluBuild2DMipmaps(GL_TEXTURE_2D, 1, 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);
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[0]); // 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_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_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 )
{
free(texture);
return -1; // If Texture Didn't Load Return FALSE ( NEW )
}
glutMainLoop();
return 0;
}
//:~
AnonimoVeneziano
31-03-2005, 20:14
Non ho capito se adesso hai risolto o no ...
Comunque se riesci ad evitare l'utilizzo di GLaux è meglio , oltre ad essere codice non portabile è anche vecchio .
Potresti usare questo per caricare le BMP :
http://chaoslizard.sourceforge.net/glbmp/
Ciao
#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 :'(
AnonimoVeneziano
31-03-2005, 23:09
Lucas , il tuo codice ha qualche problema .
prima di tutto le textures non si dichiarano come variabili ma come arrays.
Quindi :
GLuint texture[1];
e non
GLuint texture;
L'errore a riguardo della dichiarazione della texture porta a dover modificare altre linee, tra le quali :
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);
che devono diventare :
glGenTextures(1, texture);
glBindTexture(GL_TEXTURE_2D, texture[0]);
//set up texture filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//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);
(io preferisco mettere glTexImage2D dopo glTexParameteri, quindi te l'ho messo sotto)
Inoltre non abiliti il DEPTH test all' inizializzazione creando un casino quando il blending è disabilitato. E non solo, non abiliti la finestra al DEPTH testing usando GLUT_DEPTH con glutInitDisplayMode() lasciando quindi il DEPTH test sempre disabilitato!! Già che ci sei abilita pure l'alpha channel con GLUT_ALPHA .
Un altra cosa che non fai è specificare i vettori Normali con glNormal3f() per ogni faccia del cubo creando così casini con il lighting quando questo viene abilitato .
Un altra cosa , luce in inglese si scrive "light", non "ligth" :p (questo però non l'ho corretto)
Quindi il codice finale io lo trasformerei così (il programma è stato modificato un po' per permettemi di compilarlo sotto linux con GCC, tra le modifiche la sostituzione delle variabili "bool" con "int") Il programma si lancia con "nomeprogramma filetexture.bmp" ovviamente la texture deve essere di dimensioni standard per opengl , ossia con altezza e larghezza o 64 o 128 o 256:
#include <GL/openglut.h>
#include <GL/gl.h>
#include <stdio.h>
#include <stdlib.h>
#include "glbmp.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[1] = { 0}, tx_count = 0;
int blend = 0, ligth = 0;
// 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=0;
// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
if (glbmp_LoadBitmap(Filename, 0, &bitmap))
{
Status=1;
//generate and bind the OpenGL texture
glGenTextures(1, texture);
glBindTexture(GL_TEXTURE_2D, texture[0]);
//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[0]); // Select Our Texture
glBegin(GL_QUADS); glNormal3f(0.0f, 0.0f, 1.0f);
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
glNormal3f(0.0f, 0.0f, -1.0f);
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
glNormal3f(0.0f,1.0f, 0.0f);
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
glNormal3f(0.0f, -1.0f, 0.0f);
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
glNormal3f(1.0f, 0.0f, 0.0f);
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
glNormal3f(-1.0f, 0.0f, 0.0f);
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;
}
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH| GLUT_ALPHA);
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(argv[1])) // Jump To Texture Loading Routine ( NEW )
{
return -1; // If Texture Didn't Load Return FALSE ( NEW )
}
glutMainLoop();
return 0;
}
//:~
Ciao
vBulletin® v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.