Overview
This lab implements testClearColour - the first concrete test scene in the project. Building on the Test Template lab (Lab 16), you will override all three virtual methods, add a Dear ImGui colour picker widget, and register the test in the application menu. When complete, selecting the test from the menu will display an interactive colour picker that changes the window background colour in real time with no recompile needed.
Learning Objectives
By the end of this lab you will be able to:
- Understand how Dear ImGui's immediate-mode model works and why RenderGUI is called every frame
- Use ImGui::ColorEdit4 to bind a float[4] array to an interactive colour picker widget
- Override Update, Render, and RenderGUI in a concrete Tests subclass
- Register a new test scene with TestMenu::RegisterTest in Main.cpp
- Trace the data flow from an ImGui widget interaction through to a live glClearColor call
Concept
Without a way to change parameters at runtime, every tweak to a colour, size, or position requires a recompile. For a complex renderer this quickly becomes slow and disruptive. Dear ImGui solves this by providing an immediate-mode GUI framework that lets you expose any C++ variable as an interactive widget, rebuilt every frame.
In the immediate-mode model, there are no persistent widget objects. Instead, RenderGUI() is called every frame and you call ImGui functions directly inside it. ImGui reads the current value of the bound variable, draws the widget, and if the user interacts with it, writes the new value back before the function returns. The application then reads that updated variable on the very next Render() call - no callbacks, no events, no signal/slot wiring.
ImGui::ColorEdit4 takes a label string and a pointer to a float[4] array holding red, green, blue, and alpha in the range 0.0 to 1.0. In Render(), glClearColor reads from that same array and glClear(GL_COLOR_BUFFER_BIT) applies the colour. The pattern - expose a member variable to ImGui in RenderGUI(), read it in Render() - is how every future test scene in this project will work.
Tasks
Task 1 - Declare the testClearColour class
- Create the file
src/tests/testClearColour.h. - Use
#pragma onceand include"Tests.h". - Inside
namespace test, declare a classtestClearColourthat publicly inherits fromTests. - Declare the constructor and destructor, then override the three virtual methods:
Update(float deltaTime),Render(), andRenderGUI(). - Add a
privatememberfloat m_ClearColour[4]to hold the RGBA values. - Also add the new file to your
Renderer.vcxprojandRenderer.vcxproj.filtersso Visual Studio includes it in the build.
#pragma once
#include "Tests.h"
namespace test
{
class testClearColour : public Tests
{
public:
testClearColour();
~testClearColour();
// TODO: override Update(float deltaTime)
// TODO: override Render()
// TODO: override RenderGUI()
private:
// TODO: declare float m_ClearColour[4]
};
}
Task 2 - Implement testClearColour
- Create the file
src/tests/testClearColour.cpp. - Include
"testClearColour.h","../Renderer.h"(for theGlCallmacro), and"../vendor/imgui/imgui.h". - In the constructor, initialise
m_ClearColourto a default blue using a member initialiser list:m_ClearColour{ 0.2f, 0.3f, 0.8f, 1.0f }. The four values are red, green, blue, and alpha. - Leave the destructor and
Updatebodies empty - there is nothing to clean up or simulate yet. - In
Render(), callglClearColorpassing the four elements ofm_ClearColouras separate arguments. Wrap the call inGlCall(). Then callGlCall(glClear(GL_COLOR_BUFFER_BIT))to apply it. - In
RenderGUI(), callImGui::ColorEdit4("Pick a colour:", m_ClearColour). ImGui reads the current colour and writes back any user change directly into the array. - Build the project and confirm it compiles with no errors before moving on.
#include "testClearColour.h"
#include "../Renderer.h"
#include "../vendor/imgui/imgui.h"
namespace test
{
// TODO: initialise m_ClearColour with a default blue (0.2, 0.3, 0.8, 1.0)
testClearColour::testClearColour() : m_ClearColour{ /* your values here */ }
{
}
testClearColour::~testClearColour()
{
}
void testClearColour::Update(float deltaTime)
{
// Nothing to update - leave empty
}
void testClearColour::Render()
{
// TODO: GlCall(glClearColor( ... four elements of m_ClearColour ... ))
// TODO: GlCall(glClear(GL_COLOR_BUFFER_BIT))
}
void testClearColour::RenderGUI()
{
// TODO: ImGui::ColorEdit4 - expose m_ClearColour as a colour picker
}
}
Task 3 - Register the test in Main.cpp
- Open
src/Main.cppand find the block where other tests are registered withTestMenu->RegisterTest. - Add this include near the top of the file, alongside the other test includes:
#include "tests/testClearColour.h" - Add this line in the test registration block:
TestMenu->RegisterTest<test::testClearColour>("Test Clear Colour"); - Build and run. Select Test Clear Colour from the menu. You should see an ImGui colour picker widget - drag the sliders or click the colour swatch and watch the background update live.
- Click Back to return to the menu and confirm other tests still work correctly.
// Add near the top with the other test includes:
#include "tests/testClearColour.h"
// Add in the registration block alongside the other RegisterTest calls:
TestMenu->RegisterTest<test::testClearColour>("Test Clear Colour");
Solution
Try the tasks above before revealing the solution.