Schrift ins OpenGL fenster, ohne Listen, geht das?
-
hi leute hab mal ne frage bekomm ich ganz einfachen text ohne listen in eine OpenGL anwendung? wenn ja wie?
-
du kannst alternativ eine Textur benuzten auf der alle Zeichen abgebildet sind:
#ifndef Vertexwahn_Util_Font_h #define Vertexwahn_Util_Font_h #include <windows.h> #include <cstdio> #include <gl/gl.h> #include <iostream> using namespace std; namespace Vertexwahn { namespace Util { class Texture { public: Texture(); Texture(char * filename); void load(char * filename); void bind(); private: Texture(const Texture& src) { cout<<"texture copy"<<endl; } friend bool LoadTGA(Texture *texture, char *filename); GLubyte *imageData; // Image Data (Up To 32 Bits) GLuint bpp; // Image Color Depth In Bits Per Pixel. GLuint width; // Image Width GLuint height; // Image Height GLuint texID; // Texture ID Used To Select A Texture }; bool LoadTGA(Texture *texture, char *filename); class Font { public: Font(char *filename); void load(char *file); void drawChar(char c, int tx); void drawText(char * Text); private: Texture fontTexture; }; } } #endif#include "Font.h" //#include "..\..\..\Vertexwahn\Imaging\DevIL\include\IL\il.h" //#include "..\..\..\Vertexwahn\Imaging\DevIL\include\IL\ilu.h" //#include "..\..\..\Vertexwahn\Imaging\DevIL\include\IL\ilut.h" namespace Vertexwahn { namespace Util { Texture::Texture() { } Texture::Texture(char * filename) { //LoadTGA(this, filename); } void Texture::load(char * filename) { //ilInit(); //iluInit(); //ilutRenderer(ILUT_OPENGL); //this->texID = ilutGLLoadImage(filename); LoadTGA(this, filename); } void Texture::bind() { glBindTexture(GL_TEXTURE_2D, this->texID); } bool LoadTGA(Texture *texture, char *filename) // Loads A TGA File Into Memory { GLubyte TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0}; // Uncompressed TGA Header GLubyte TGAcompare[12]; // Used To Compare TGA Header GLubyte header[6]; // First 6 Useful Bytes From The Header GLuint bytesPerPixel; // Holds Number Of Bytes Per Pixel Used In The TGA File GLuint imageSize; // Used To Store The Image Size When Setting Aside Ram GLuint temp; // Temporary Variable GLuint type=GL_RGBA; // Set The Default GL Mode To RBGA (32 BPP) FILE *file = fopen(filename, "rb"); // Open The TGA File if( file==NULL || // Does File Even Exist? fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeof(TGAcompare) || // Are There 12 Bytes To Read? memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0 || // Does The Header Match What We Want? fread(header,1,sizeof(header),file)!=sizeof(header)) // If So Read Next 6 Header Bytes { if (file == NULL) // Does The File Even Exist? *Added Jim Strong* return FALSE; // Return False else // Otherwise { fclose(file); // If Anything Failed, Close The File return FALSE; // Return False } } texture->width = header[1] * 256 + header[0]; // Determine The TGA Width (highbyte*256+lowbyte) texture->height = header[3] * 256 + header[2]; // Determine The TGA Height (highbyte*256+lowbyte) if( texture->width <=0 || // Is The Width Less Than Or Equal To Zero texture->height <=0 || // Is The Height Less Than Or Equal To Zero (header[4]!=24 && header[4]!=32)) // Is The TGA 24 or 32 Bit? { fclose(file); // If Anything Failed, Close The File return FALSE; // Return False } texture->bpp = header[4]; // Grab The TGA's Bits Per Pixel (24 or 32) bytesPerPixel = texture->bpp/8; // Divide By 8 To Get The Bytes Per Pixel imageSize = texture->width*texture->height*bytesPerPixel; // Calculate The Memory Required For The TGA Data texture->imageData=(GLubyte *)malloc(imageSize); // Reserve Memory To Hold The TGA Data if( texture->imageData==NULL || // Does The Storage Memory Exist? fread(texture->imageData, 1, imageSize, file)!=imageSize) // Does The Image Size Match The Memory Reserved? { if(texture->imageData!=NULL) // Was Image Data Loaded free(texture->imageData); // If So, Release The Image Data fclose(file); // Close The File return FALSE; // Return False } for(GLuint i=0; i<int(imageSize); i+=bytesPerPixel) // Loop Through The Image Data { // Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue) temp=texture->imageData[i]; // Temporarily Store The Value At Image Data 'i' texture->imageData[i] = texture->imageData[i + 2]; // Set The 1st Byte To The Value Of The 3rd Byte texture->imageData[i + 2] = temp; // Set The 3rd Byte To The Value In 'temp' (1st Byte Value) } fclose (file); // Close The File // Build A Texture From The Data glGenTextures(1, &texture[0].texID); // Generate OpenGL texture IDs glBindTexture(GL_TEXTURE_2D, texture[0].texID); // Bind Our Texture glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Linear Filtered glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Linear Filtered if (texture[0].bpp==24) // Was The TGA 24 Bits { type=GL_RGB; // If So Set The 'type' To GL_RGB } glTexImage2D(GL_TEXTURE_2D, 0, type, texture[0].width, texture[0].height, 0, type, GL_UNSIGNED_BYTE, texture[0].imageData); return true; // Texture Building Went Ok, Return True } Font::Font(char *filename) : fontTexture(filename) { } void Font::load(char *file) { fontTexture.load(file); } void Font::drawChar(char c, int tx) { float x = 1*(16.0f/256.0f); float y = 2*(16.0f/256.0f); // A = 65 // Jetzt A = 33 y = 15-(c-32)/16; x = (c-32)%16; y *= (16.0f/256.0f); x *= (16.0f/256.0f); glBegin(GL_QUADS); // Use A Quad For Each Character glTexCoord2f(x, y); glVertex2f(tx+0, 0); glTexCoord2f(x, y+16.0f/256.0f); glVertex2f(tx+0, 16); glTexCoord2f(x+16.0f/256.0f, y+16.0f/256.0f); glVertex2f(tx+16, 16); glTexCoord2f(x+16.0f/256.0f, y); glVertex2f(tx+16, 0); glEnd(); // Done Building Our Quad (Character) } void Font::drawText(char * Text) { fontTexture.bind(); glMatrixMode(GL_PROJECTION); // Select The Projection Matrix glPushMatrix(); // Store The Projection Matrix glLoadIdentity(); // Reset The Projection Matrix glOrtho(0,640,0,480,-1,1); // Set Up An Ortho Screen glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix glPushMatrix(); // Model View Matrix retten glLoadIdentity(); // Z-Buffer aus glEnable(GL_BLEND); // Turn Blending On glDisable(GL_DEPTH_TEST); // draw... //int loop = 65; //float cx=float(loop%16)/256.0f; // X Position Of Current Character //float cy=float(loop/16)/256.0f; // Y Position Of Current Character for(int i = 0; Text[i] != 0; i++) drawChar(Text[i],16*i); glDisable(GL_BLEND); // Turn Blending On glEnable(GL_DEPTH_TEST); // Z-Buffer wieder an glPopMatrix(); // Modleview Matrix wiederherstellen glMatrixMode(GL_PROJECTION); // Select The Projection Matrix glPopMatrix(); // Projektionsmatrix wiederherstellen glMatrixMode(GL_MODELVIEW); // Wieder Modelviewmatrix einstellen } } }
-
ahja ok und einfacher gehts net oder was?
-
was verstehst du nicht?
-
Boah ist das grauenvoller Code! Net mal TGA wird anständig geladen! using namespace in einem Header?! OMFG!
Aber acuh noch GL_QUADS! Jung, das ist doch so dermaßen lam. Benutz doch bitte VBOs wenn du schon so viel falsch machst.

-
Net mal TGA wird anständig geladen!
wo siehst du da einen Fehler?
using namespace in einem Header?!
aso du meinst
using namespace std;ja - das ist wirklich nicht schön
Aber acuh noch GL_QUADS! Jung, das ist doch so dermaßen lam. Benutz doch bitte VBOs wenn du schon so viel falsch machst.
mir ist schon klar das VBOs schneller sind - bin aber erstmal damit zufrieden, das es überhaupt funktioniert
Boah ist das grauenvoller Code!
was soll ich noch anders machen?