What's new

Opengl + gui

givemeachance

New member
Hi again!

So far i've been using devcpp for my applications.
Now , that its time to create my first emulator , i've been thinking about adding gui!

After some research , i picked vc++ express as ide (you can just drag & drop objects on a form!) ...but i stuck!

How can i create an opengl window on a form?!
Is it possible without covering the menu object?

..Or.. is there any better way ? (to avoid using vc express)
 

Doomulation

?????????????????????????
You get a window handle when creating the window. Otherwise, you can use FindWindow API and search for your window title to acquire the window handle.
 
OP
G

givemeachance

New member
Hi again !:bouncy: .

I tried very hard , but now i stuck !
Can i have some technical support please ?!


Well , here's what i've done so far (not sure if its working--since im getting errors)
PHP:
public : System::Void INIT_OPENGL(Panel p_)
			 {
HDC hd= GetDC((HWND)p_.Handle.ToPointer());
GLuint		PixelFormat;	
WNDCLASS	wc;	
	DWORD		dwExStyle;						 
	DWORD		dwStyle;						 

	//Really..do i need those?!
 DEVMODE dmScreenSettings;				 
		memset(&dmScreenSettings,0,sizeof(dmScreenSettings));		 
		dmScreenSettings.dmSize=sizeof(dmScreenSettings);	 
		dmScreenSettings.dmPelsWidth	= 320;		 
		dmScreenSettings.dmPelsHeight	= 240;			 
		dmScreenSettings.dmBitsPerPel	= 32;			 
		dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;

	GLuint		PixelFormat= ChoosePixelFormat(hd,&pfd);
	SetPixelFormat(hd,PixelFormat,&pfd);
glClear(GL_COLOR_BUFFER_BIT );
glLoadIdentity();
glTranslatef(0.0f,0.0f,0.0f);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, 320, 240, 0, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
			 }


And , how im using the function:

PHP:
//Main menu -> init button!
		 panel1 vp = new panel1();
		 INIT_OPENGL(vp);
       // INIT_OPENGL(panel1); <---not working

And here's what errors im getting with the last code snip (main menu->init button) :
Form1.h(168) : warning C4101: 'PixelFormat' : unreferenced local variable
Form1.h(171) : warning C4101: 'dwStyle' : unreferenced local variable
Form1.h(170) : warning C4101: 'dwExStyle' : unreferenced local variable
Form1.h(169) : warning C4101: 'wc' : unreferenced local variable
Form1.h(199) : error C2146: syntax error : missing ';' before identifier 'vp'
Form1.h(199) : error C3288: 'System::Windows::Forms::panel ^' : illegal dereference of a handle type
Form1.h(199) : error C2065: 'vp' : undeclared identifier
Form1.h(199) : error C2061: syntax error : identifier 'panel1'

If i remove this part of the code:
PHP:
//Main menu -> init button!
		 panel1 vp = new panel1();
		 INIT_OPENGL(vp);
       // INIT_OPENGL(panel1); <---not working
its working...but of course without opengl support!
 
Last edited:

synch

New member
If panel1 isn't a derived class from panel that you created, there's an error in that declaration, I mean, it should be Panel, not panel1, but I highly doubt you're creating a new panel and not using the one done in the designer :p
 

Garstyciuks

New member
If panel1 is already an object, then you don't need to do panel1 vp = new panel1();. You should be able to do INIT_OPENGL(panel1);

And you don't need to do that devmode stuff.

Here is my initialization of OpenGL control, but it's not for .NET, it's for MFC.

Code:
void COpenGLCtrl::Init()
{
	glShadeModel(GL_SMOOTH);
	glClearColor(0.0f,0.0f,0.0f,0.0f);

	glClearDepth(1.0f);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);
}

void COpenGLCtrl::Resize(int width, int height)
{
	if (height==0) height = 1;
	glViewport(0,0,width,height);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	gluOrtho2D(0,width,height,0); //I was using 2D

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	m_Width = width; //member variables
	m_Height = height;
}

void COpenGLCtrl::InitGL()
{
	int PixelFormat;

	static PIXELFORMATDESCRIPTOR pfd = {
		sizeof(PIXELFORMATDESCRIPTOR),
		1,
		PFD_DRAW_TO_WINDOW |
		PFD_SUPPORT_OPENGL |
		PFD_DOUBLEBUFFER,
		PFD_TYPE_RGBA,
		16,
		0, 0, 0, 0, 0, 0,
		0,
		0,
		0,
		0, 0, 0, 0,
		16,
		0,
		0,
		PFD_MAIN_PLANE,
		0,
		0, 0, 0
	};

	hDC = GetDC()->m_hDC; //hDC is a member variable
	PixelFormat = ChoosePixelFormat(hDC, &pfd);
	SetPixelFormat(hDC, PixelFormat, &pfd);
	hRC = wglCreateContext(hDC); //hRC is a member variable
	HGLRC hRCPrev = wglGetCurrentContext(); //gets the old context
	wglMakeCurrent(hDC,hRC);
	wglShareLists(hRCPrev,hRC); //shares all the textures and other stuff between two contexts

	RECT rt;
	GetWindowRect(&rt);

	Resize(rt.right-rt.left,rt.bottom-rt.top); //setup the viewport

	pthis->Init(); //I don't remember the reason why I used pthis everywhere in this class. It basically is the same as this pointer.
}

The main function here is InitGL. You can omit that context sharing part, if you don't need it. If you can get your hWnd handle correctly, then you should be able to successfully port my code into .NET. Hope this helps.
 
Last edited:
OP
G

givemeachance

New member
Cool! , Thanks!
Now im not getting errors , its just that...nothing happens :)!


The (edited) code:

RESIZE FUNCTION
PHP:
public : System::Void RESIZEGL(GLint w, GLint h)
		 {
		glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, w, h, 0, -1, 1 );
 glMatrixMode( GL_MODELVIEW ); 
 glLoadIdentity();
		 }


Init gl:
PHP:
public : System::Void INIT_OPENGL( Panel^  m  )
			 {
GLint PixelFormat;

	static PIXELFORMATDESCRIPTOR pfd = {
		sizeof(PIXELFORMATDESCRIPTOR),
		1,
		PFD_DRAW_TO_WINDOW |
		PFD_SUPPORT_OPENGL |
		PFD_DOUBLEBUFFER,
		PFD_TYPE_RGBA,
		16,
		0, 0, 0, 0, 0, 0,
		0,
		0,
		0,
		0, 0, 0, 0,
		16,
		0,
		0,
		PFD_MAIN_PLANE,
		0,
		0, 0, 0
	};
	HWND DC = (HWND)(m->Handle.ToPointer());
	HDC hDC =GetDC(DC);
	PixelFormat = ChoosePixelFormat(hDC, &pfd);
	SetPixelFormat(hDC, PixelFormat, &pfd);
	HGLRC  hRC= wglCreateContext(hDC);  
	HGLRC hRCPrev = wglGetCurrentContext();  
	wglMakeCurrent(hDC,hRC);
	wglShareLists(hRCPrev,hRC); 
	RECT rt;
	GetWindowRect(DC,&rt);
	RESIZEGL(rt.right-rt.left,rt.bottom-rt.top);  
	//SetFocus(DC);
glClearColor( 0, 0, 0, 0 );
glLoadIdentity();
			 }


And how im calling the function :

I click on the init button but nothing happens :
PHP:
private: System::Void initToolStripMenuItem1_Click(System::Object^  sender, System::EventArgs^  e) {
			
 INIT_OPENGL( panel1 );

		 }
 

Doomulation

?????????????????????????
Wait a minute... are you doing panel1 = new panel1? That's obviously wrong for a number of reasons. You must create a new instance of a class/struct, hence panel1 = new Panel. panel1 is the NAME of the variable of the type that you're creating.
This is an important mix up. Remember this one!
 
OP
G

givemeachance

New member
Well , thanks everyone !

I found a nice example on google , and after 3 hours of hard work ...

->>>>>>>>>>>>>>>>>>>>>>>>>
yeais6.png



i made it :)!

Now , its time for the hard part(i'll start tomorrow).
/
 

Top