Saturday, July 9, 2022

Lesson01 : Creating OpenGL Context, Rendering


This post discusses  implementing the basic operations of creating OpenGL context, rendering and mouse/keyboard is discussed. Followed by an lesson that utilizes it.

Lesson01 Project
This purpose of this Lesson01 project is to create a Window initialized with OpenGL context and fill it with fuchsia color.
As discussed in introduction create tutorial lesson project Lesson01 under Lessons folder.
Add a header file Scene.h as shown below. The class itself is self explanatory. 
The WM_CLOSE  event is  handled in OnCloseWindow function and the window is destroyed and  application is shutdown. 
The Init method calls BaseScene's 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.
       
#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);
	}

};

Add a  main.cpp  as shown below. It 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.

The code and binaries for Lesson01 is found here

No comments:

Post a Comment