Probleme mit OpenGL auf TPanel



  • Hi,
    Ich bin gerade dabei OpenGL zu lernen und wollte das ganze auf einem Panel zeichnen. Ich habe es geschafft auf das Canvas des Panels zu zeichnen, leider funktioniert das hinzufügen von Licht aber nicht (richtig). Immer wenn das Licht ein ist, werden trotz glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE); und glEnable( GL_COLOR_MATERIAL); alle Farben grau gezeichnet.
    Ich hoffe jemand kann mir helfen. Der Großteil des Codes is aus diversen NeHe Tutorials zusammengesetzt. Wenn ich den selben Code (initGl(),drawGL()) in einem eigenen Fenster ausführe wird die Szene richtig angezeigt.

    //---------------------------------------------------------------------------
    
    #include <vcl.h>
    #include <windows.h>
    #include <gl\gl.h>      // Header file for the OpenGL32 library
    #include <gl\glu.h>     // Header file for the GLu32 library
    #include <gl\glaux.h>   // Header file for the GLaux library
    #pragma hdrstop
    
    #include "main_panel.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm2 *Form2;
    
    HGLRC hrc;
    HDC handle;
    
    float LightPos[] = { 0.0f, 5.0f,4.0f, 1.0f};	// Light position
    
    GLUquadricObj *q;				// Quadratic for drawing a sphere
    
    //---------------------------------------------------------------------------
    __fastcall TForm2::TForm2(TComponent* Owner)
    	: TForm(Owner)
    {
    }
    
    //---------------------------------------------------------------------------
    bool __fastcall initPanel(HDC h)
    {
    	static	PIXELFORMATDESCRIPTOR pfd =
    	{
    		sizeof(PIXELFORMATDESCRIPTOR),          // Size of this pixel format descriptor
    		1,					// Version number
    		PFD_DRAW_TO_WINDOW |			// Format must support window
    		PFD_SUPPORT_OPENGL |			// Format must support OpenGL
    		PFD_DOUBLEBUFFER,			// Must support double buffering
    		PFD_TYPE_RGBA,				// Request an RGBA format
    		24,					// Select our 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,				// Accumulation bits ignored
    		32,					// 32Bit Z-Buffer (Depth buffer)
    		1,					// Use stencil buffer ( * Important * )
    		0,					// No auxiliary buffer
    		PFD_MAIN_PLANE,				// Main drawing layer
    		0,					// Reserved
    		0, 0, 0					// Layer masks ignored
    	};
    
    	int nPixelFormat;
    	if((nPixelFormat = ChoosePixelFormat(h, &pfd))==NULL)
    	{
    		Application->MessageBoxA("could not determine pixel-format","ERROR",MB_ICONERROR);
    		return false;
    	}
    	if(!SetPixelFormat(h, nPixelFormat, &pfd))
    	{
    		Application->MessageBoxA("could not set pixel-format","ERROR",MB_ICONERROR);
    		return false;
    	}
    
    	if((hrc = wglCreateContext(h))==NULL)
    	{
    		Application->MessageBoxA("could not create rendering context","ERROR",MB_ICONERROR);
    		return false;
    	}
    
    	return true;
    }
    //---------------------------------------------------------------------------
    
    void __fastcall initGL(void)
    {
     	float LightAmb[] = { 0.2f, 0.2f, 0.2f, 1.0f};	// Ambient light values
    	float LightDif[] = { 0.6f, 0.6f, 0.6f, 1.0f};	// Diffuse light values
    	float LightSpc[] = {-0.2f, -0.2f, -0.2f, 1.0f};	// Specular light values
    
    	float MatAmb[] = {0.4f, 0.4f, 0.4f, 1.0f};	// Material - ambient values
    	float MatDif[] = {0.2f, 0.6f, 0.9f, 1.0f};	// Material - diffuse values
    	float MatSpc[] = {0.0f, 0.0f, 0.0f, 1.0f};	// Material - specular values
    	float MatShn[] = {0.0f};			// Material - shininess
    
    	glShadeModel(GL_SMOOTH);                // Enable smooth shading
    	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);   // Black background
    	glClearDepth(1.0f);                     // Depth buffer setup
    	glClearStencil(0);
    	glEnable(GL_DEPTH_TEST);                // Enables depth testing
    	glDepthFunc(GL_LEQUAL);                 // The type of depth testing to do
    	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);      // Really nice perspective calculations
    
    	glLightfv(GL_LIGHT1, GL_POSITION, LightPos);		// Set light1 position
    	glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmb);		// Set light1 ambience
    	glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDif);		// Set light1 diffuse
    	glLightfv(GL_LIGHT1, GL_SPECULAR, LightSpc);		// Set light1 specular
    	glEnable(GL_LIGHT1);					// Enable light1
    	glEnable(GL_LIGHTING);					// Enable lighting
    
    	glMaterialfv(GL_FRONT, GL_AMBIENT, MatAmb);		// Set material ambience
    	glMaterialfv(GL_FRONT, GL_DIFFUSE, MatDif);		// Set material diffuse
    	glMaterialfv(GL_FRONT, GL_SPECULAR, MatSpc);		// Set material specular
    	glMaterialfv(GL_FRONT, GL_SHININESS, MatShn);		// Set material shininess
    
    	glCullFace(GL_BACK);					// Set culling face to back face
    	glEnable(GL_CULL_FACE);					// Enable culling
    	glClearColor(0.4f, 0.4f, 0.0f, 1.0f);			// Set clear color
    
    	glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
    	glEnable( GL_COLOR_MATERIAL);
    
    	q = gluNewQuadric();					// Initialize quadratic
    	gluQuadricNormals(q, GL_SMOOTH);			// Enable smooth normal generation
    	gluQuadricTexture(q, GL_FALSE);				// Disable auto texture coords
    }
    
    //---------------------------------------------------------------------------
    void DrawGLRoom()		// Draw the room (Box)
    {
    	glBegin(GL_QUADS);	// Begin drawing quads
    		// Floor
    		glNormal3f(0.0f, 1.0f, 0.0f);		// Normal pointing up
    		glVertex3f(-10.0f,-10.0f,-20.0f);	// Back left
    		glVertex3f(-10.0f,-10.0f, 20.0f);	// Front left
    		glVertex3f( 10.0f,-10.0f, 20.0f);	// Front right
    		glVertex3f( 10.0f,-10.0f,-20.0f);	// Back right
    		// Ceiling
    		glNormal3f(0.0f,-1.0f, 0.0f);		// Normal point down
    		glVertex3f(-10.0f, 10.0f, 20.0f);	// Front left
    		glVertex3f(-10.0f, 10.0f,-20.0f);	// Back left
    		glVertex3f( 10.0f, 10.0f,-20.0f);	// Back right
    		glVertex3f( 10.0f, 10.0f, 20.0f);	// Front right
    		// Front wall
    		glNormal3f(0.0f, 0.0f, 1.0f);		// Normal pointing away from viewer
    		glVertex3f(-10.0f, 10.0f,-20.0f);	// Top left
    		glVertex3f(-10.0f,-10.0f,-20.0f);	// Bottom left
    		glVertex3f( 10.0f,-10.0f,-20.0f);	// Bottom right
    		glVertex3f( 10.0f, 10.0f,-20.0f);	// Top right
    		// Back wall
    		glNormal3f(0.0f, 0.0f,-1.0f);		// Normal pointing towards viewer
    		glVertex3f( 10.0f, 10.0f, 20.0f);	// Top right
    		glVertex3f( 10.0f,-10.0f, 20.0f);	// Bottom right
    		glVertex3f(-10.0f,-10.0f, 20.0f);	// Bottom left
    		glVertex3f(-10.0f, 10.0f, 20.0f);	// Top left
    		// Left wall
    		glNormal3f(1.0f, 0.0f, 0.0f);		// Normal pointing right
    		glVertex3f(-10.0f, 10.0f, 20.0f);	// Top front
    		glVertex3f(-10.0f,-10.0f, 20.0f);	// Bottom front
    		glVertex3f(-10.0f,-10.0f,-20.0f);	// Bottom back
    		glVertex3f(-10.0f, 10.0f,-20.0f);	// Top back
    		// Right wall
    		glNormal3f(-1.0f, 0.0f, 0.0f);		// Normal pointing left
    		glVertex3f( 10.0f, 10.0f,-20.0f);	// Top back
    		glVertex3f( 10.0f,-10.0f,-20.0f);	// Bottom back
    		glVertex3f( 10.0f,-10.0f, 20.0f);	// Bottom front
    		glVertex3f( 10.0f, 10.0f, 20.0f);	// Top front
    	glEnd();        	// Done drawing quads
    }
    
    //---------------------------------------------------------------------------
    void __fastcall drawGL(void)
    {
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    	glMatrixMode(GL_MODELVIEW);
    	glLoadIdentity();
    
    	glTranslatef(0.0f, 0.0f, -20.0f);
    	glLightfv(GL_LIGHT1, GL_POSITION, LightPos);
    	glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
    	DrawGLRoom();
    
    	glLoadIdentity();
    	glTranslatef(0.0f, 0.0f, -20.0f);
    	//glRotatef(45, 1.0, 1.0, 1.0);
    	glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
    
    	glLoadIdentity();
    	glColor4f(0.7f, 0.4f, 0.0f, 1.0f);
    	glTranslatef(0.0f, 0.0f, -20.0f);
    
    	glDisable(GL_LIGHTING);
    	glDepthMask(GL_FALSE);
    	glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
    	glTranslatef(LightPos[0],LightPos[1],LightPos[2]);			// Translate to light's position
    	gluSphere(q, 0.2f, 16, 8);				// Draw a little sphere (Represents light)
    	glEnable(GL_LIGHTING);
    	glDepthMask(GL_TRUE);
    
    	glFlush();
    	SwapBuffers(handle);
    }
    
    //---------------------------------------------------------------------------
     void __fastcall TForm2::IdleLoop(TObject*, bool& done)
    {
    	done = false;
    	wglMakeCurrent(handle, hrc);
    	drawGL();
    	wglMakeCurrent(0, 0);
    	done = true;
    }
    
    //---------------------------------------------------------------------------
    void __fastcall TForm2::FormCreate(TObject *Sender)
    {
    	handle = GetDC(Form2->Panel1->Handle);
    	initPanel(handle);
    	initGL();
    	Application->OnIdle = IdleLoop;
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm2::FormResize(TObject *Sender)
    {
    	int Width = Panel1->Width;
    	int Height = Panel1->Height;
    	GLdouble gldAspect = Width / Height;
    	wglMakeCurrent(handle,hrc);
    
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
    
    	gluPerspective(70.0,           // Field-of-view angle
    				   gldAspect,      // Aspect ratio of viewing volume
    				   1.0,            // Distance to near clipping plane
    				   1000000.0);          // Distance to far clipping plane
    	glViewport(0, 0, Width, Height);
    
    	glMatrixMode(GL_MODELVIEW);
    	glLoadIdentity();
    }
    //---------------------------------------------------------------------------
    

    LG OGL



  • Ich habe die Lösung gefunden.
    Für die Interessierten:
    Anscheinend muss das ganze glLightfv(...) und glMaterialfv(...) Theater statt in der Initialisierung (wie normalerweise) auch in drawGL() stehen um zu funktionieren.

    LG OGL



  • Danke für die Lösung.

    OpenGL auf einem Panel werde ich demnächst auch brauchen.
    Wäre das was für die FAQ?

    Viele Grüße

    Martin



  • Danke für die Lösung.

    OpenGL auf einem Panel werde ich demnächst auch brauchen.
    Wäre das was für die FAQ?

    Viele Grüße

    Martin


Anmelden zum Antworten