Saturday, July 9, 2022

Lesson01: Initializing OpenGL Context

Overview 
This post discusses  implementing the basic operations of creating a hosting window, Initializing it with OpenGL context, rendering and handle mouse/keyboard inputs. 

Details
This purpose of  Lesson01 is to create a Host Window, initialized with OpenGL context and fill it  with fuchsia color.

System  class diagram
The functionality is implemented in the Scene class derived from BaseScene class.

Implementation

Scene

The three methods Init,DrawScene and Cleanup are overridden as below.
The Init method calls BaseScene::Init to create hosting window and OpenGL Context.
The DrawScene method clears the window and paints it with fuchsia color.
The Cleanup method does not do anything.

The BaseCamera::camera is not initialized and stays as nullptr hence no keyboard or mouse inputs are processed other than Escape key.

Finally the WM_CLOSE  event is  handled in OnCloseWindow function and the window is destroyed and  application is shutdown when the window is closed or Escape key is pressed. 

#include "Scene\BaseScene.h"

class Scene:public BaseScene
{
	BEGIN_MSG_MAP(Scene)
		MESSAGE_HANDLER(WM_CLOSE, OnCloseWindow)
		CHAIN_MSG_MAP(BaseScene)
	END_MSG_MAP()

	//shutdown the application when the wondow is closed
	LRESULT OnCloseWindow(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		bHandled = TRUE;
		DestroyWindow();
		PostQuitMessage(0);
		return 0;
	}
	

	//override to create application window and opengl context
	int Init(RECT rect, WCHAR *windowname)
	{
		BaseScene::Init(rect, windowname);
		return 0;
	}

	//no cleanup
	void Cleanup()
	{
	}
	
	//draw
	void DrawScene()
	{
		glClearColor(1, 0, 1, 1);
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glCullFace(GL_FRONT_AND_BACK);
	}

};

Application
Scene class is hosted main.cpp. which creates the scene object and displays it. Message pump is added to process windows messages.

#include "Scene.h"

Scene scene;

int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmd_line, int show)
{
	//create window and opengl context
	scene.Init(RECT{ 100, 100, 780, 500 }, L"Modern OpenGL-Tutorial");

	//draw
	scene.ShowWindow(show);

	//message pump
	MSG msg;
	while (GetMessage(&msg, 0, 0, 0)) 
	{
		TranslateMessage(&msg);
		DispatchMessageA(&msg);
	}

	return 0;
}

Output
The output looks as shown in the top.


No comments:

Post a Comment