Sign in to follow this  
Revan1985

OpenGL Object Oriented Programmation

Recommended Posts

Revan1985    568
So, i decide to follow the C-Uint Framework Tutorial http://www.c-unit.com/ for dx, and translate it in opengl... now, i've some problems... first of all, there is not a device, so i do the needed rendering without it... i've created the window, but i don't know how change the CGraphics class to implement OpenGL Initializzation, because the OpenGL need HGLRC and HDC, and i don't know where implement them... can i have some help? i'm trying, but i've only errors... here the "flushed" code how the hell i make sopilers? the code is so long ^^ CGraphics.h
#ifndef CGRAPHICS_H
#define CGRAPHICS_H

#include "stdafx.h"

class CGraphics
{
public:
	CGraphics();
	~CGraphics() { Release(); }
	bool Initialize( HWND HWnd, bool windowed );
	void Release();
	bool Reset();

	bool Windowed;

private:
	HWND hWnd;
	HINSTANCE hInstance;
};

#endif


CGraphics.cpp
#include "stdafx.h"
#include "CGraphics.h"

CGraphics::CGraphics()
{
}

void CGraphics::Release()
{
}

bool CGraphics::Initialize( HWND HWnd, bool windowed )
{
	return true;
}

bool CGraphics::Reset()
{
	return true;
}



CFramework.h
#ifndef CFRAMEWORK_H
#define CFRAMEWORK_H

#include "stdafx.h"
#include "CGraphics.h"

class CBaseApp
{
public:
	virtual ~CBaseApp() { }
	virtual void Release() = 0;
	virtual void OnCreateDevice() = 0;
	virtual void OnResetDevice() = 0;
	virtual void OnLostDevice() = 0;
	virtual void OnDestroyDevice() = 0;
	virtual void OnUpdateFrame( float elapsedTime ) = 0;
	virtual void OnRenderFrame( float elapsedTime ) = 0;
	virtual void OnKeyDown( WPARAM wParam ) = 0;

};

class CFramework
{
public:
	CFramework( CBaseApp* pGameApp );
	~CFramework() { Release(); }
	bool Initialize( char* title, HINSTANCE hInstance, int width, int height, bool windowed = true );
	void Run();
	void Release();
	void ToggleFullScreen();
	static LRESULT CALLBACK StaticWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );

private:
	LRESULT CALLBACK WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );
	void OnCreateDevice();
	void OnResetDevice();
	void OnLostDevice();
	void OnDestroyDevice();
	void OnUpdateFrame();
	void OnRenderFrame();

	HWND				m_hWnd;
	HINSTANCE			m_hInstance;
	bool				m_active;
	int					m_windowWidth;
	int					m_windowHeight;
	WINDOWPLACEMENT		m_wp;

	CGraphics*			m_pGraphics;
	CBaseApp*			m_pGameApp;
};

#endif

CFramework.cpp
#include "stdafx.h"
#include "CFramework.h"

CFramework::CFramework( CBaseApp* pGameApp )
{
	this->m_pGameApp = pGameApp;
	this->m_active = true;
	this->m_pGraphics = new CGraphics();
}

void CFramework::Release()
{
	SAFE_RELEASE( this->m_pGraphics );
	OnLostDevice();
	OnDestroyDevice();
}

bool CFramework::Initialize( char *title, HINSTANCE hInstance, int width, int height, bool windowed )
{
	this->m_hInstance = hInstance;
	this->m_windowWidth = width;
	this->m_windowHeight = height;

	WNDCLASSEX wcex;
	wcex.cbSize			= sizeof( WNDCLASSEX );
	wcex.style			= CS_DBLCLKS;
	wcex.lpfnWndProc	= ( WNDPROC ) StaticWndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon( hInstance, MAKEINTRESOURCE( IDI_ICON1 ) );
	wcex.hCursor		= LoadCursor( NULL, IDC_ARROW );
	wcex.hbrBackground	= ( HBRUSH ) GetStockObject( BLACK_BRUSH );
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	= title;
	wcex.hIconSm		= LoadIcon( hInstance, MAKEINTRESOURCE( IDI_ICON1 ) );

	RegisterClassEx( &wcex );

	this->m_hWnd = CreateWindow( title, title, windowed ? WS_OVERLAPPEDWINDOW : WS_EX_TOPMOST, CW_USEDEFAULT,
			0, width, height, NULL, NULL, hInstance, this );

	RECT rect = { 0, 0, width, height };

	AdjustWindowRect( &rect, GetWindowLong( this->m_hWnd, GWL_STYLE ), false );
	SetWindowPos( this->m_hWnd, HWND_TOP, 0, 0, rect.right - rect.left, rect.bottom - rect.top,
		SWP_NOZORDER | SWP_NOMOVE );

	ShowWindow( this->m_hWnd, SW_SHOW );
	UpdateWindow( this->m_hWnd );

	if( !this->m_pGraphics->Initialize( this->m_hWnd, windowed ) ) return false;

	OnCreateDevice();
	OnResetDevice();

	return true;
}

void CFramework::Run()
{
	MSG msg;
	while( 1 )
	{
		if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
		{
			if( msg.message == WM_QUIT )
			{
				break;
			}
			TranslateMessage( &msg );
			DispatchMessage( &msg );
		}
		else
		{
			if( this->m_active )
			{
				OnUpdateFrame();
				OnRenderFrame();
			}
		}
	}
}


void CFramework::OnCreateDevice()
{
	if( this->m_pGameApp != NULL && this->m_pGraphics != NULL )
		this->m_pGameApp->OnCreateDevice();
}

void CFramework::OnResetDevice()
{
	if( this->m_pGameApp != NULL && this->m_pGraphics != NULL )
		this->m_pGameApp->OnResetDevice();
}

void CFramework::OnLostDevice()
{
	if( this->m_pGameApp != NULL )
		this->m_pGameApp->OnLostDevice();
}

void CFramework::OnDestroyDevice()
{
	if( this->m_pGameApp != NULL )
		this->m_pGameApp->OnDestroyDevice();
}

void CFramework::OnUpdateFrame()
{
	if( this->m_pGameApp != NULL && this->m_pGraphics != NULL )
		this->m_pGameApp->OnUpdateFrame( 0.0f );
}

void CFramework::OnRenderFrame()
{
	if( !this->m_active ) return;

	if( this->m_pGameApp != NULL )
		this->m_pGameApp->OnRenderFrame( 0.0f );
}

void CFramework::ToggleFullScreen()
{
	if( this->m_pGraphics == NULL || !this->m_active ) return;

	this->m_pGraphics->Windowed = !this->m_pGraphics->Windowed;

	if( this->m_pGraphics->Windowed )
	{
		SetWindowLongPtr( this->m_hWnd, GWL_STYLE, WS_OVERLAPPEDWINDOW );
	}
	else
	{
		ZeroMemory( &this->m_wp, sizeof( WINDOWPLACEMENT ) );
		this->m_wp.length = sizeof( WINDOWPLACEMENT );

		GetWindowPlacement( this->m_hWnd, &this->m_wp );

		SetWindowLongPtr( this->m_hWnd, GWL_STYLE, WS_EX_TOPMOST );

		ShowWindow( this->m_hWnd, SW_HIDE );
	}
	
	OnLostDevice();
	this->m_pGraphics->Reset();
	OnResetDevice();

	if( this->m_pGraphics->Windowed )
	{
		SetWindowPlacement( this->m_hWnd, &this->m_wp );
	}

	if( !IsWindowVisible( this->m_hWnd ) )
	{
		ShowWindow( this->m_hWnd, SW_SHOW );
	}
}

LRESULT CALLBACK CFramework::StaticWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
	if( msg == WM_CREATE )
	{
		SetWindowLongPtr( hWnd, GWLP_USERDATA, ( LONG )( ( CREATESTRUCT * )lParam )->lpCreateParams );
	}

	CFramework* targetApp = ( CFramework* ) GetWindowLongPtr( hWnd, GWLP_USERDATA );

	if( targetApp )
	{
		return targetApp->StaticWndProc( hWnd, msg, wParam, lParam );
	}
	return DefWindowProc( hWnd, msg, wParam, lParam );
}

LRESULT CALLBACK CFramework::WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
	switch( msg )
	{
	case WM_DESTROY:
		PostQuitMessage( 0 );
		return 0;
	case WM_PAINT:
		if( this->m_pGraphics != NULL )
		{
			OnUpdateFrame();
			OnRenderFrame();
		}
		ValidateRect( hWnd, 0 );
		return 0;
	case WM_SIZE:
		if( wParam == SIZE_MINIMIZED )
		{
			this->m_active = false;
		}
		else
		{
			this->m_active = true;
			this->m_windowWidth = LOWORD( lParam );
			this->m_windowHeight = HIWORD( lParam );

			if( this->m_pGraphics != NULL )
			{
				OnLostDevice();
				this->m_pGraphics->Reset();
				OnResetDevice();
				OnUpdateFrame();
				OnRenderFrame();
			}
		}
		return 0;
	case WM_KEYDOWN:
		if( this->m_pGameApp != NULL )
		{
			this->m_pGameApp->OnKeyDown( wParam );
		}
		return 0;
	}
	return DefWindowProc( hWnd, msg, wParam, lParam );
}


CGameApp.h

#ifndef CGAMEAPP_H
#define CGAMEAPP_H

#include "stdafx.h"
#include "CFramework.h"

class CGameApp : public CBaseApp
{
public:
	CGameApp();
	~CGameApp() { Release(); }
	void SetFramework( CFramework* pFramework );
	bool Initialize();
	virtual void Release();
	virtual void OnCreateDevice();
	virtual void OnResetDevice();
	virtual void OnLostDevice();
	virtual void OnDestroyDevice();
	virtual void OnUpdateFrame( float elapsedTime );
	virtual void OnRenderFrame( float elapsedTime );
	virtual void OnKeyDown( WPARAM wParam );

private:
	CFramework*		m_pFramework;
};

#endif


CGameApp.cpp

#include "stdafx.h"
#include "CGameApp.h"

CGameApp::CGameApp()
{
	this->m_pFramework = NULL;
}

void CGameApp::Release()
{
	SAFE_RELEASE( this->m_pFramework );
}

void CGameApp::SetFramework( CFramework* pFramework )
{
	this->m_pFramework = pFramework;
}

bool CGameApp::Initialize()
{
	return true;
}

void CGameApp::OnCreateDevice()
{
}

void CGameApp::OnResetDevice()
{
}

void CGameApp::OnLostDevice()
{
}

void CGameApp::OnDestroyDevice()
{
}

void CGameApp::OnUpdateFrame( float elapsedTime )
{
}

void CGameApp::OnRenderFrame( float elapsedTime )
{
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
	glLoadIdentity();
}

void CGameApp::OnKeyDown( WPARAM wParam )
{
	switch( wParam )
	{
	case VK_ESCAPE:
		PostQuitMessage( 0 );
		break;
	case VK_F1:
		if( this->m_pFramework != NULL ) this->m_pFramework->ToggleFullScreen();
		break;
	}
}


main.cpp

#include "stdafx.h"
#include "CGameApp.h"
#include "CFramework.h"

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow )
{
	CGameApp*		pApplication	= new CGameApp();
	CFramework*		pFramework		= new CFramework( ( CBaseApp* )pApplication );

	pApplication->SetFramework( pFramework );

	if( !pApplication->Initialize() ) return 0;

	if( !pFramework->Initialize( "OpenGL", hInstance, 640, 480, true ) ) return 0;

	pFramework->Run();

	SAFE_RELEASE( pApplication );

	return 0;
}


how i implement the opengl initialization? thx ^^

Share this post


Link to post
Share on other sites
Gage64    1235
I'm not sure I understand you're question. Why can't you just put the HGLRC and HDC in the CGraphics class along with the window handle, and then write the standard initialization code in Initialize()?

Or are you asking a design question, i.e., where should they go for it to be a good design?

If that's the case, I think they should probably go in a seperate class, along with the hwnd and hinstance. You can call this class Win32GLContext or something and it will handle all the Windows related initialization (and some other things like swapping buffers), while the CGraphics class will handle only OpenGL and won't contain any Windows-related functionality.

Taking this a step further, you can create an abstract GLContext class that will have pure virtual functions like Init() and SwapBuffers(), and Win32GLContext will inherit from it. That way the CGraphics class only has to know about GLContext and so it will be completely OS-independent.

This has the advantage that it makes porting to another OS much easier, and it makes the CGraphics class easier to maintain because it has less to worry about (i.e., it more closely follows the Single-Responsibility Principle).

Share this post


Link to post
Share on other sites
Revan1985    568
my problem was not "i can't use hglrc and hdc in Cgraphics", but was "How i can use Hglrc and hdc in CGraphics?"

sorry, probably was my "unperfect" english who make this confusion :P

Share this post


Link to post
Share on other sites
Ashkan    451
I'm not sure if I understood your question correctly, but in case you're wondering how to initialize OpenGL using this design, here's a solution. Otherwise, you should provide us with more info and describe your problem in more detail.

CGraphics.h

class CGraphics
{
public:
CGraphics();
~CGraphics() { Release(); }
bool Initialize( HWND HWnd, bool windowed );
void Release();
bool Reset();

bool Windowed;

private:
HWND hWnd;
HINSTANCE hInstance;

HDC hDC;
HGLRC hRC;
};



CGraphics.cpp

bool CGraphics::Initialize( HWND HWnd, bool windowed )
{
PIXELFORMATDESCRIPTOR pdf;
ZeroMemory( &pfd, sizeof( PIXELFORMATDESCRIPTOR ) );
pfd.nSize = sizeof( PIXELFORMATDESCRIPTOR );
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 32;
pfd.iLayerType = PFD_MAIN_PLANE;

hDC = GetDC( HWnd );
if ( !hDC ) {
return false;
}

GLuint pixelFormat = ChoosePixelFormat( hDC, &pfd );
if ( !pixelFormat ) {
return false;
}

DescribePixelFormat( hDC, pixelFormat, sizeof( PIXELFORMATDESCRIPTOR ), &pfd );

if ( !SetPixelFormat( hDC, pixelFormat, &pfd) ) {
return false;
}

hRC = wglCreateContext( hDC );
if ( !hRC ) {
return false;
}

if ( !wglMakeCurrent( hDC, hRC ) ) {
return false;
}

// set initial render states
}



Hope that helped

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By Zaphyk
      I am developing my engine using the OpenGL 3.3 compatibility profile. It runs as expected on my NVIDIA card and on my Intel Card however when I tried it on an AMD setup it ran 3 times worse than on the other setups. Could this be a AMD driver thing or is this probably a problem with my OGL code? Could a different code standard create such bad performance?
    • By Kjell Andersson
      I'm trying to get some legacy OpenGL code to run with a shader pipeline,
      The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
      I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
      I've got a version 330 vertex shader to somewhat work:
      #version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
      What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
      Question:
      What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?
       
      Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.
    • By markshaw001
      Hi i am new to this forum  i wanted to ask for help from all of you i want to generate real time terrain using a 32 bit heightmap i am good at c++ and have started learning Opengl as i am very interested in making landscapes in opengl i have looked around the internet for help about this topic but i am not getting the hang of the concepts and what they are doing can some here suggests me some good resources for making terrain engine please for example like tutorials,books etc so that i can understand the whole concept of terrain generation.
       
    • By KarimIO
      Hey guys. I'm trying to get my application to work on my Nvidia GTX 970 desktop. It currently works on my Intel HD 3000 laptop, but on the desktop, every bind textures specifically from framebuffers, I get half a second of lag. This is done 4 times as I have three RGBA textures and one depth 32F buffer. I tried to use debugging software for the first time - RenderDoc only shows SwapBuffers() and no OGL calls, while Nvidia Nsight crashes upon execution, so neither are helpful. Without binding it runs regularly. This does not happen with non-framebuffer binds.
      GLFramebuffer::GLFramebuffer(FramebufferCreateInfo createInfo) { glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); textures = new GLuint[createInfo.numColorTargets]; glGenTextures(createInfo.numColorTargets, textures); GLenum *DrawBuffers = new GLenum[createInfo.numColorTargets]; for (uint32_t i = 0; i < createInfo.numColorTargets; i++) { glBindTexture(GL_TEXTURE_2D, textures[i]); GLint internalFormat; GLenum format; TranslateFormats(createInfo.colorFormats[i], format, internalFormat); // returns GL_RGBA and GL_RGBA glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, createInfo.width, createInfo.height, 0, format, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); DrawBuffers[i] = GL_COLOR_ATTACHMENT0 + i; glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, textures[i], 0); } if (createInfo.depthFormat != FORMAT_DEPTH_NONE) { GLenum depthFormat; switch (createInfo.depthFormat) { case FORMAT_DEPTH_16: depthFormat = GL_DEPTH_COMPONENT16; break; case FORMAT_DEPTH_24: depthFormat = GL_DEPTH_COMPONENT24; break; case FORMAT_DEPTH_32: depthFormat = GL_DEPTH_COMPONENT32; break; case FORMAT_DEPTH_24_STENCIL_8: depthFormat = GL_DEPTH24_STENCIL8; break; case FORMAT_DEPTH_32_STENCIL_8: depthFormat = GL_DEPTH32F_STENCIL8; break; } glGenTextures(1, &depthrenderbuffer); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, createInfo.width, createInfo.height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthrenderbuffer, 0); } if (createInfo.numColorTargets > 0) glDrawBuffers(createInfo.numColorTargets, DrawBuffers); else glDrawBuffer(GL_NONE); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) std::cout << "Framebuffer Incomplete\n"; glBindFramebuffer(GL_FRAMEBUFFER, 0); width = createInfo.width; height = createInfo.height; } // ... // FBO Creation FramebufferCreateInfo gbufferCI; gbufferCI.colorFormats = gbufferCFs.data(); gbufferCI.depthFormat = FORMAT_DEPTH_32; gbufferCI.numColorTargets = gbufferCFs.size(); gbufferCI.width = engine.settings.resolutionX; gbufferCI.height = engine.settings.resolutionY; gbufferCI.renderPass = nullptr; gbuffer = graphicsWrapper->CreateFramebuffer(gbufferCI); // Bind glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo); // Draw here... // Bind to textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, textures[1]); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, textures[2]); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); Here is an extract of my code. I can't think of anything else to include. I've really been butting my head into a wall trying to think of a reason but I can think of none and all my research yields nothing. Thanks in advance!
    • By Adrianensis
      Hi everyone, I've shared my 2D Game Engine source code. It's the result of 4 years working on it (and I still continue improving features ) and I want to share with the community. You can see some videos on youtube and some demo gifs on my twitter account.
      This Engine has been developed as End-of-Degree Project and it is coded in Javascript, WebGL and GLSL. The engine is written from scratch.
      This is not a professional engine but it's for learning purposes, so anyone can review the code an learn basis about graphics, physics or game engine architecture. Source code on this GitHub repository.
      I'm available for a good conversation about Game Engine / Graphics Programming
  • Popular Now