Hi guys,
I'm new in opengl programming and I have a question respectively a problem.
At first same codesnippets:
#ifndef FIBS_H
#define FIBS_H
#include <gl\glaux.h>
class BMP{
private:
GLuint texture[1];
AUX_RGBImageRec *LoadBMP(char *Filename);
public:
BMP(void);
int LoadGLTextures(void);
GLuint getTextur(void);
GLuint* getTexturPointer(void);
~BMP(void);
};
#endif
#include <windows.h> // Header File For Windows
#include <stdio.h> // Header File For Standard Input/Output ( NEW )
#include <gl\glaux.h>
#include "BMP.h"
AUX_RGBImageRec *BMP::LoadBMP(char *Filename){ // Loads A Bitmap Image
FILE *File=NULL; // File Handle
if (!Filename) // Make Sure A Filename Was Given
return NULL; // If Not Return NULL
File=fopen(Filename,"r"); // Check To See If The File Exists
if (File){ // Does The File Exist?
fclose(File); // Close The Handle
return auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer
}
return NULL; // If Load Failed Return NULL
}
BMP::BMP(void){
}
int BMP::LoadGLTextures(void){ // Load Bitmaps And Convert To Textures
int Status=FALSE; // Status Indicator
AUX_RGBImageRec *TextureImage[1]; // Create Storage Space For The Texture
memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL
// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
if (TextureImage[0]=LoadBMP("Data/NeHe.bmp")){
Status=TRUE; // Set The Status To TRUE
glGenTextures(1, &texture[0]); // Create The Texture
// Typical Texture Generation Using Data From The Bitmap
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
}
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; // Return The Status
}
GLuint BMP::getTextur(void){
return BMP::texture[0];
}
GLuint* BMP::getTexturPointer(void){
return BMP::texture;
}
BMP::~BMP(void){
}
#ifndef FIBS_C
#define FIBS_C
#include "BMP.h"
class Aquarium{
private:
BMP* bitmap;
int init(GLvoid);
void drawAquariumPart(float xCoord, float yCoord, float zCoord, float xSize, float ySize, float zSize);
void drawWater(void);
void drawQuad(void);
void drawWasserPflanzen(void);
public:
Aquarium(void);
~Aquarium(void);
void drawAquarium(void);
};
#endif
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <gl\glut.h>
#include "BMP.h"
#include "Aquarium_Objekt.h"
int Aquarium::init(GLvoid){ // All Setup For OpenGL Goes Here
if (!Aquarium::bitmap->LoadGLTextures()) // Jump To Texture Loading Routine ( NEW )
return FALSE; // If Texture Didn't Load Return FALSE
return TRUE; // Initialization Went OK
}
// Methode zum Zeichnen eines Teils des Aquariums
// x-, y- und z-Koordinate, sowie x-, y- und z-Groesse werden uebergeben
void Aquarium::drawAquariumPart(float xCoord, float yCoord, float zCoord, float xSize, float ySize, float zSize){
// Legt die aktuelle Matrix auf den Stack
glPushMatrix();
// Setzt die aktuelle Farbe auf hellblau
glColor4f(0.3, 0.3, 0.5, 0.7);
// Verschiebt den erzeugten Würfel um xCoord, yCoord, zCoord
glTranslatef(xCoord, yCoord, zCoord);
// Streckt den erzeugten Würfel um xSize, ySize, zSize
glScalef(xSize, ySize, zSize);
// Erzeugt einen soliden Würfel, der gestreckt und verschoben wird
glutSolidCube(1);
// Nimmt die aktuelle Matrix vom Stack
glPopMatrix();
} // end drawAquariumPart
// Methode zum Zeichnen des Wassers
void Aquarium::drawWater(void){
// Transparenz aktivieren
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Legt die aktuelle Matrix auf den Stack
glPushMatrix();
// Setzt die aktuelle Farbe auf hellblau
glColor4f(0.15, 0.45, 0.5, 0.2);
// Verschiebt den erzeugten Würfel um xCoord, yCoord, zCoord
glTranslatef(15, 5, -7.6);
// Streckt den erzeugten Würfel um xSize, ySize, zSize
glScalef(24.6, 9.6, 9.8);
// Erzeugt einen soliden Würfel, der gestreckt und verschoben wird
glutSolidCube(1);
// Nimmt die aktuelle Matrix vom Stack
glPopMatrix();
// Transparenz deaktivieren
glDisable(GL_BLEND);
} // end drawWater
void Aquarium::drawQuad(void){
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, Aquarium::bitmap->getTextur());
glPushMatrix();
glTranslatef(15, 5, -10);
glBegin(GL_QUADS);
// Front Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 0.0f); // Bottom Right Of The Texture and Quad
glEnd();
glPopMatrix();
glDisable(GL_TEXTURE_2D); /* disable texture mapping */
}
void Aquarium::drawWasserPflanzen(void){
this->drawQuad();
}
Aquarium::Aquarium(){
Aquarium::bitmap = new BMP();
if(!this->init())
printf("Konnte Textur nicht laden\n");
else
printf("Laden der Textur erfolgreich\n");
}
Aquarium::~Aquarium(){}
void Aquarium::drawAquarium(){
this->drawWasserPflanzen();
this->drawWater();
// Transparenz aktivieren
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Erzeugt vordere Scheibe
drawAquariumPart(15,5,-2.6,25,10,0.2);
// Erzeugt linke Scheibe
drawAquariumPart(2.6,5,-7.5,0.2,10,10);
// Erzeugt rechte Scheibe
drawAquariumPart(27.4,5,-7.5,0.2,10,10);
// Erzeugt hintere Scheibe
drawAquariumPart(15,5,-12.6,25,10,0.2);
// Erzeugt untere Scheibe
drawAquariumPart(15,0.1,-7.5,24.6,0.2,10);
// Transparenz deaktivieren
glDisable(GL_BLEND);
} // end drawAquarium
And this is my display function
void display(void)
{
if (deltaMove)
moveMeFlat(deltaMove);
if (deltaAngle) {
angle += deltaAngle;
orientMe(angle);
}
frame++;
time=glutGet(GLUT_ELAPSED_TIME);
if (time - timebase > 1000) {
printf("FPS:%4.2f\n",frame*1000.0/(time-timebase));
timebase = time;
frame = 0;
}
// Das bisherige Bild wird gelöscht indem das komplette Bild mit der Hintergrundfarbe überdeckt wird.
// Dazu wird das Bild komplett in Hintergrundfarbe gezeichnet und Verdeckungsrechnung initialisiert.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for(int x =0; x<fischanzahl; x++){
fische[x]->moveFisch();
fische[x]->drawFisch();
}
spotlight->drawSpotLight();
tisch->drawTable();
aquarium->drawAquarium();
// ---
// Sorge dafür, dass wirklich alle glBefehle abgearbeitet und nicht gepuffert werden
glFlush();
// Vertausche die Backbuffer und Frontbuffer: das fertig
// gezeichnete Bild soll jetzt angezeigt werden
glutSwapBuffers();
// Sorge dafür, dass das Bild im nächsten Schleifendurchlauf
// der glutMainLoop wieder gezeichnet wird
glutPostRedisplay();
} // end display
Now my problem... i do not get the textur on the quad. I have no idea why :)
Perhaps anybody can help me.