Textausgabe OpenGL



  • Hallo, habe ein kleines Programm, welches Text ausgeben sollte. Tut es aber nicht. Vielleicht kann ja mal jemand drüber gucken und mir den Fehler sagen.

    #include <iostream>
    #include <stdlib.h>
    #include <windows.h>
    #include <iostream>
    #include <stdlib.h>
    #include <GL/glut.h>
    #include <gl/glaux.h>
    #include <stdio.h>						// Header File For Standard Input/Output	( ADD )
    #include <stdarg.h>	
    
    using namespace std;
    
    int width =400;
    int height =400;
    
    HDC			hDC=NULL;		// Private GDI Device Context
    HGLRC		hRC=NULL;		// Permanent Rendering Context
    HWND		hWnd=NULL;		// Holds Our Window Handle
    HINSTANCE	hInstance;		// Holds The Instance Of The Application
    
    GLuint	base;				// Base Display List For The Font Set
    
    GLvoid BuildFont(GLvoid)								// Build Our Bitmap Font
    {
    	HFONT	font;										// Windows Font ID
    	HFONT	oldfont;									// Used For Good House Keeping
    
    	base = glGenLists(96);								// Storage For 96 Characters
    
    	font = CreateFont(	-24,							// Height Of Font
    						0,								// Width Of Font
    						0,								// Angle Of Escapement
    						0,								// Orientation Angle
    						FW_BOLD,						// Font Weight
    						FALSE,							// Italic
    						FALSE,							// Underline
    						FALSE,							// Strikeout
    						ANSI_CHARSET,					// Character Set Identifier
    						OUT_TT_PRECIS,					// Output Precision
    						CLIP_DEFAULT_PRECIS,			// Clipping Precision
    						ANTIALIASED_QUALITY,			// Output Quality
    						FF_DONTCARE|DEFAULT_PITCH,		// Family And Pitch
    						"Courier New");					// Font Name
    
    	oldfont = (HFONT)SelectObject(hDC, font);           // Selects The Font We Want
    	wglUseFontBitmaps(hDC, 32, 96, base);				// Builds 96 Characters Starting At Character 32
    	SelectObject(hDC, oldfont);							// Selects The Font We Want
    	DeleteObject(font);									// Delete The Font
    }
    
    //Function to pdint text - in display aufgerufen
    GLvoid glPrint(const char *fmt, ...)					// Custom GL "Print" Routine
    {
    	char		text[256];								// Holds Our String
    	va_list		ap;										// Pointer To List Of Arguments
    
    	if (fmt == NULL)									// If There's No Text
    		return;											// Do Nothing
    
    	va_start(ap, fmt);									// Parses The String For Variables
    	    vsprintf(text, fmt, ap);						// And Converts Symbols To Actual Numbers
    	va_end(ap);											// Results Are Stored In Text
    
    	glPushAttrib(GL_LIST_BIT);							// Pushes The Display List Bits
    	glListBase(base - 32);								// Sets The Base Character to 32
    	glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);	// Draws The Display List Text
    	glPopAttrib();										// Pops The Display List Bits
    }
    
    //Reshape if window size changes     
    void reshape(int reshapeWidth, int reshapeHeight)     
    {
        glViewport(0, 0,(GLsizei) reshapeWidth, (GLsizei) reshapeHeight);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0,reshapeWidth,0,reshapeHeight,0,1);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        height = reshapeHeight;     
        width = reshapeWidth;
    }
    
    void initGL()
    {
       glShadeModel(GL_SMOOTH);
       glClearColor (0, 0, 0, 0);	       // color, in which framebuffer is deleted            
       glClearDepth (1.0f);		           // value to delete Z-Buffer			
       glEnable (GL_DEPTH_TEST);           // Z-Buffer test aktivated	   		
       glDepthFunc (GL_LEQUAL);			   // Z-Buffer function (everything behind another poly is covered)		
       glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	 //perspective computation of texturen
       BuildFont();
    }
    
    //display function
    void display (void)
    {   
       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer
       glLoadIdentity();					 
       glColor3f(0.0,1.0,1.0);
       glTranslatef(0.0f,0.0f,-1.0f);	
       glRasterPos2f(0,0);  // Position The Text On The Screen
       glPrint("Hello");	// Print GL Text To The Screen
       glFlush();    
    }
    
    int main(int argc, char *argv[])
    {
       glutInit(&argc, argv);
       glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);    
       glutInitWindowSize(400, 400);   
       glutCreateWindow("MeinText");              
       initGL();
       glutReshapeFunc(reshape);
       glutDisplayFunc(display);	    
       glutMainLoop();
      return 0;   
    }
    


  • kannst es kompilieren?



  • Ja das klappt, kann es auch ausführen, aber dabei erscheint nur ein schwarzes Fenster.



  • Warum gibst Du für die Höhe der Schriftart minus 24 an? Und warum für die Breite null?



  • hab im mom sogar prob das zu kompilieren ...

    welche glut version benutzt du?

    und warum includest du 2mal iostream??



  • class_Volrath schrieb:

    hab im mom sogar prob das zu kompilieren ...

    welche glut version benutzt du?

    und warum includest du 2mal iostream??

    ^

    Das riecht doch nach Copy&Paste 😃
    BTW würde ich nicht Nehe's Textfunktion mit wglUseFontBitmaps verwenden, weil das sehr langsam und fehleranfällig ist (Texturen, Z-Buffer - Um mal zwei Schlagworte ich den Raum zu werfen).



  • Ja, aber das muss doch auch so funktionieren.
    <iostream> war n versehn.
    bei 0 werden standard werte verwendet, das minus is für character höhe und nicht cell höhe.
    Er geht auch durch alle Funktionen, nur die schrift sieht man nich 😞
    keine idee?



  • Hallo.

    Habe bis zum Abwinken auch alles probiert, damit mein Text überhaupt erscheint.
    Es scheint so, daß die wgl-Dinger mit ATL aus irgendwelchen Gründen auch immer nicht funzen können ... 😞

    Also funktioniert weder mit wglUseFontBitmaps() noch mit wglUseFontOutlines().

    Ich hoffe jedoch, ich irre mich, weil auch ich das brauche...

    Kann jemand bitte helfen ?

    // MyControl.h : Declaration of the CMyControl
    #pragma once
    #include "resource.h"       // main symbols
    #include <atlctl.h>
    #include "MyProject.h"
    
    // CMyControl
    class ATL_NO_VTABLE CMyControl : 
      public CComObjectRootEx<CComSingleThreadModel>,
      public IDispatchImpl<IMyControl, &IID_IMyControl, &LIBID_MyProjectLib, /*wMajor =*/ 1, /*wMinor =*/ 0>,
      public IPersistStreamInitImpl<CMyControl>,
      public IOleControlImpl<CMyControl>,
      public IOleObjectImpl<CMyControl>,
      public IOleInPlaceActiveObjectImpl<CMyControl>,
      public IViewObjectExImpl<CMyControl>,
      public IOleInPlaceObjectWindowlessImpl<CMyControl>,
      public ISupportErrorInfo,
      public IPersistStorageImpl<CMyControl>,
      public ISpecifyPropertyPagesImpl<CMyControl>,
      public IQuickActivateImpl<CMyControl>,
      public IDataObjectImpl<CMyControl>,
      public IProvideClassInfo2Impl<&CLSID_MyControl, NULL, &LIBID_MyProjectLib>,
      public CComCoClass<CMyControl, &CLSID_MyControl>,
      public CComControl<CMyControl>
    {
    public:
    
      CMyControl():
    	  m_uiListBase(0)
      {
    	  m_bWindowOnly = TRUE;
      }
    
      DECLARE_OLEMISC_STATUS(OLEMISC_RECOMPOSEONRESIZE | 
      OLEMISC_CANTLINKINSIDE | 
        OLEMISC_INSIDEOUT | 
        OLEMISC_ACTIVATEWHENVISIBLE | 
        OLEMISC_SETCLIENTSITEFIRST
        )
    
        DECLARE_REGISTRY_RESOURCEID(IDR_MYCONTROL)
    
      BEGIN_COM_MAP(CMyControl)
        COM_INTERFACE_ENTRY(IMyControl)
        COM_INTERFACE_ENTRY(IDispatch)
        COM_INTERFACE_ENTRY(IViewObjectEx)
        COM_INTERFACE_ENTRY(IViewObject2)
        COM_INTERFACE_ENTRY(IViewObject)
        COM_INTERFACE_ENTRY(IOleInPlaceObjectWindowless)
        COM_INTERFACE_ENTRY(IOleInPlaceObject)
        COM_INTERFACE_ENTRY2(IOleWindow, IOleInPlaceObjectWindowless)
        COM_INTERFACE_ENTRY(IOleInPlaceActiveObject)
        COM_INTERFACE_ENTRY(IOleControl)
        COM_INTERFACE_ENTRY(IOleObject)
        COM_INTERFACE_ENTRY(IPersistStreamInit)
        COM_INTERFACE_ENTRY2(IPersist, IPersistStreamInit)
        COM_INTERFACE_ENTRY(ISupportErrorInfo)
        COM_INTERFACE_ENTRY(ISpecifyPropertyPages)
        COM_INTERFACE_ENTRY(IQuickActivate)
        COM_INTERFACE_ENTRY(IPersistStorage)
        COM_INTERFACE_ENTRY(IDataObject)
        COM_INTERFACE_ENTRY(IProvideClassInfo)
        COM_INTERFACE_ENTRY(IProvideClassInfo2)
      END_COM_MAP()
    
      BEGIN_PROP_MAP(CMyControl)
        PROP_DATA_ENTRY("_cx", m_sizeExtent.cx, VT_UI4)
        PROP_DATA_ENTRY("_cy", m_sizeExtent.cy, VT_UI4)
        // Example entries
        // PROP_ENTRY("Property Description", dispid, clsid)
        // PROP_PAGE(CLSID_StockColorPage)
      END_PROP_MAP()
    
      BEGIN_MSG_MAP(CMyControl)
        MESSAGE_HANDLER(WM_CREATE, OnCreate)
        MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
        MESSAGE_HANDLER(WM_SIZE, OnSize)
        CHAIN_MSG_MAP(CComControl<CMyControl>)
        DEFAULT_REFLECTION_HANDLER()
      END_MSG_MAP()
      // Handler prototypes:
      //  LRESULT MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
      //  LRESULT CommandHandler(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
      //  LRESULT NotifyHandler(int idCtrl, LPNMHDR pnmh, BOOL& bHandled);
    
      // ISupportsErrorInfo
      STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid)
      {
        static const IID* arr[] = 
        {
          &IID_IMyControl,
        };
    
        for (int i=0; i<sizeof(arr)/sizeof(arr[0]); i++)
        {
          if (InlineIsEqualGUID(*arr[i], riid))
            return S_OK;
        }
        return S_FALSE;
      }
    
      // IViewObjectEx
      DECLARE_VIEW_STATUS(VIEWSTATUS_SOLIDBKGND | VIEWSTATUS_OPAQUE)
    
      // IMyControl
    public:
      HRESULT OnDraw(ATL_DRAWINFO& di)
      {
        HDC hdc = di.hdcDraw;
        RECT& rc = *(RECT*)di.prcBounds;
    
        wglMakeCurrent(hdc, m_hRC);
    
        glClearColor(.6f, .6f, .6f, 10.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    #if 0
        glPushMatrix();
    
        // compute dimensions of a quarter of the control
        SIZE qtrSize = { (rc.right - rc.left) / 2, (rc.bottom - rc.top) / 2 };
        glBegin(GL_QUADS);
          glColor3ub(255, 0, 0);
          glVertex3s(-qtrSize.cx, -qtrSize.cy, 0);
    
          glColor3ub(0, 255, 0);
          glVertex3s(qtrSize.cx, -qtrSize.cy, 0);
    
          glColor3ub(0, 0, 255);
          glVertex3s(qtrSize.cx, qtrSize.cy, 0);
    
          glColor3ub(255, 255, 255);
          glVertex3s(-qtrSize.cx, qtrSize.cy, 0);
        glEnd();
    
        glPopMatrix();
    #endif
    
    	glColor3f(0.0, 1.0, 1.0);
    
    	glPointSize(6.0);	
    
    	glBegin(GL_POINTS);
    		glVertex3f(0.0, 0.0, 0.0);
    	glEnd();
    
    	glColor3f(1.0, 0.0, 0.0);
    
    	glListBase(m_uiListBase);
    
    	// Es funktioniert nicht:	
    	//PrintGLText("Test123456");
    
    	// Unten funktionieren auch nicht ... :
    	glCallLists (24, GL_UNSIGNED_BYTE, "Hello Win32 OpenGL World"); 
    	DWORD dw = GetLastError();
    
    	for (int n=32; n < 255; n++)
    		glCallList(n);
    
    	glPointSize(2.0);	
    	glBegin(GL_POINTS);
    		glVertex3f(2.0, 0.0, 0.0);
    	glEnd();
    
        glFinish();
        SwapBuffers(wglGetCurrentDC());
    
        return S_OK;
      }
    
      DECLARE_PROTECT_FINAL_CONSTRUCT()
    
      HRESULT FinalConstruct()
      {
        return S_OK;
      }
    
      void FinalRelease() 
      {
      }
    
      // Set OpenGL pixel format for given DC
      BOOL SetupPixelFormat(HDC hdc)
      {
        static PIXELFORMATDESCRIPTOR pfd =
        {
          sizeof(PIXELFORMATDESCRIPTOR),  // size of this pfd
            1,                            // version number
            PFD_DRAW_TO_WINDOW |          // support window
            PFD_SUPPORT_OPENGL |          // support OpenGL
            PFD_DOUBLEBUFFER,             // double buffered
            PFD_TYPE_RGBA,                // RGBA type
            24,                           // 24-bit color depth
            0, 0, 0, 0, 0, 0,             // color bits ignored
            0,                            // no alpha buffer
            0,                            // shift bit ignored
            0,                            // no accumulation buffer
            0, 0, 0, 0,                   // accum bits ignored
            32,                           // 32-bit z-buffer
            0,                            // no stencil buffer
            0,                            // no auxiliary buffer
            PFD_MAIN_PLANE,               // main layer
            0,                            // reserved
            0, 0, 0                       // layer masks ignored
        };
        int pixelformat;
    
        if ((pixelformat = ChoosePixelFormat(hdc, &pfd)) == 0)
        {
          ATLASSERT(FALSE);
          return FALSE;
        }
    
        if (SetPixelFormat(hdc, pixelformat, &pfd) == FALSE)
        {
          ATLASSERT(FALSE);
          return FALSE;
        }
    
        return TRUE;
      }
    
      // Create rendering context given device context and control bounds
      void CreateContext(HDC hdc, RECT& rc)
      {
        PIXELFORMATDESCRIPTOR pfd;
        if (!SetupPixelFormat(hdc))
          return;
    
        int n = GetPixelFormat(hdc);
        DescribePixelFormat(hdc, n, sizeof(pfd), &pfd);
        m_hRC = wglCreateContext(hdc);
        wglMakeCurrent(hdc, m_hRC);
    
        // OpenGL stuff starts here
        int width = rc.right - rc.left;
        int height = rc.bottom - rc.top;
    
        glViewport(0, 0, width, height);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        //gluOrtho2D(-width/2, width/2, -height/2, height/2);
    
    	float flMax = 3.0f;
    	glOrtho(-flMax, flMax, -flMax, flMax, -flMax, flMax);
    
        glMatrixMode(GL_MODELVIEW);
      }
    
      LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
      {
        HDC hdc = GetDC();
        RECT rc;
        GetClientRect(&rc);
        CreateContext(hdc, rc);
    
    	/******************/
    	/* Added by hdWin */
    	/******************/
    	HDC			hDC(NULL);
    	hDC = GetDC();
    
      	HFONT		hFont (NULL), hOldFont (NULL);						// Windows Font ID
    
    	m_uiListBase = glGenLists(256);						// Storage For 256 Characters
    
    	hFont = CreateFont(	-12,							// Height Of Font
    						0,								// Width Of Font
    						0,								// Angle Of Escapement
    						0,								// Orientation Angle
    						FW_BOLD,						// Font Weight
    						FALSE,							// Italic
    						FALSE,							// Underline
    						FALSE,							// Strikeout
    						ANSI_CHARSET,					// Character Set Identifier
    						OUT_TT_PRECIS,					// Output Precision
    						CLIP_DEFAULT_PRECIS,			// Clipping Precision
    						ANTIALIASED_QUALITY,			// Output Quality
    						FF_DONTCARE|DEFAULT_PITCH,		// Family And Pitch
    						"Comic Sans MS");				// Font Name
    
    	hOldFont = (HFONT)SelectObject(hDC, hFont);
    
    	ZeroMemory(m_gmf, sizeof(m_gmf));
    
    	// MSDN:
    	// The wglUseFontOutlines function creates a set of display lists !
    //#define FONTOUTLINES
    #ifdef FONTOUTLINES
    	BOOL bo = wglUseFontOutlines(	
    						hDC,							// Select The Current DC
    						0,								// Starting Character
    						256,							// Number Of Display Lists To Build
    						m_uiListBase,					// Starting Display Lists
    						0.0f,							// Deviation From The True Outlines
    						0.2f,							// Font Thickness In The Z Direction
    						WGL_FONT_POLYGONS,				// Use Polygons, Not Lines
    						m_gmf);							// Address Of Buffer To Recieve Data
    #endif
    
    #define FONTBITMAPS
    #ifdef FONTBITMAPS
    	BOOL bo = wglUseFontBitmaps(
    						hDC, 0, 256, m_uiListBase);
    #endif
    
    	glCallLists (24, GL_UNSIGNED_BYTE, "Hello Win32 OpenGL World"); 
    
    	DeleteObject(SelectObject(hDC, hOldFont));
    
    	ReleaseDC(hDC);
    	hDC = NULL;
    
        return 0;
      }
    
      LRESULT OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
      {
      	glDeleteLists(m_uiListBase, 256);								// Delete All 256 Characters
    
        wglMakeCurrent(NULL, NULL);
    
        if (m_hRC)
        {
          wglDeleteContext(m_hRC);
          m_hRC = NULL;
        }
    
        return 0;
      }
    
      LRESULT CMyControl::OnSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
      {
        // when resized, recreate the device context
        wglMakeCurrent(NULL,  NULL);
        if (m_hRC)
        {
          wglDeleteContext(m_hRC);
          m_hRC = NULL;
        }
        HDC hdc = GetDC();
        RECT rc;
        GetClientRect(&rc);
        CreateContext(hdc, rc);
    
        return 0;
      }
    
      void CMyControl::PrintGLText(const char *fmt, ...)					// Custom GL "Print" Routine
    {
    	float		length=0;								// Used To Find The Length Of The Text
    	char		text[256];								// Holds Our String
    	va_list		ap;										// Pointer To List Of Arguments
    
    	if (fmt == NULL)									// If There's No Text
    		return;											// Do Nothing
    
    	va_start(ap, fmt);									// Parses The String For Variables
    	    sprintf(text, fmt, ap);						// And Converts Symbols To Actual Numbers
    	va_end(ap);											// Results Are Stored In Text
    
    	for (unsigned int loop=0;loop<(strlen(text));loop++)	// Loop To Find Text Length
    	{
    		length += m_gmf[text[loop]].gmfCellIncX;			// Increase Length By Each Characters Width
    	}
    
    //	glEnable(GL_CULL_FACE);
    //	glCullFace(GL_FRONT_AND_BACK);
    //		glPushMatrix();
    			//glTranslatef(-length/2,0.0f,0.0f);					// Center Our Text On The Screen
    
    			const GLubyte* glubyte (NULL);
    			glubyte = gluErrorString(glGetError());
    
    			glPushAttrib(GL_LIST_BIT);							// Pushes The Display List Bits
    				glListBase(m_uiListBase);									// Sets The Base Character to 0
    				glCallLists((GLsizei)strlen(text), GL_UNSIGNED_BYTE, text);	// Draws The Display List Text
    				DWORD dw = GetLastError();
    				glubyte = gluErrorString(glGetError());
    			glPopAttrib();										// Pops The Display List Bits
    //		glPopMatrix();
    //	glDisable(GL_CULL_FACE);	
    }
    
      // OpenGL rendering context
      HGLRC m_hRC;
      STDMETHOD(RefreshCtrl)(void);
    
      /******************/
      /* Added by hdWin */
      /******************/
      GLYPHMETRICSFLOAT		m_gmf[256];	// Storage For Information About Our Outline Font Characters
      GLuint				m_uiListBase;
    
    };
    
    OBJECT_ENTRY_AUTO(__uuidof(MyControl), CMyControl)
    

Anmelden zum Antworten