So I'm currently rendering a triangle in OpenGL 2.0. I can get it to render but there are some strange things afoot that shouldn't be.
First strange behavior:
I'm required to use glFlush. Without it the screen will just show white when I clear to a dark blue. I will see my triangle if the last frame as I close the program. I know I shouldn't have to call glFlush to do that so I'm wondering why the GPU isn't executing the calls without it. I've tested this on multiple computers, and the behavior is the same, so this isn't a GPU specific issue.
Second strange behavior:
I recently bought a new laptop and moved everything over to it. The only changes I made program wise is I removed GLUT from the project, and took out all related dependencies. I also upgraded from VS 2008 to VS 2010. Also the laptop is 64 bit vs the previous 32 bit. With those being the only changes, my SwapBuffers() call will return false every time, BUT it will still render. I can resize my screen and move it and the triangle is still there and is reacting to the changes. I called GetLastError() and it returns 0, so the thread isn't posting any errors, yet it still returns false every frame.
Now I really don't want to go forward with these issues because I'd rather not build on a foundation of mud, so any help would be appreciated.
Here's the various code (its spread across a couple of modules):
//////////////////////////////////////////////////////////////////////////
// WinMain.cpp
// - Used to create the main loop of execution
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// Includes
//////////////////////////////////////////////////////////////////////////
#include <Windows.h>
#include "../Graphics/CDeviceManager.h"
#include "../Game/CGame.h"
//////////////////////////////////////////////////////////////////////////
// Global Variables
//////////////////////////////////////////////////////////////////////////
HWND g_hWnd = NULL; // The copy of the current handle to the window
HINSTANCE g_hInstance = NULL; // The current copy of the instance of this program
int g_nWindowWidth = 1280;
int g_nWindowHeight = 720;
CWindow g_cWindow;
//////////////////////////////////////////////////////////////////////////
// Forward Declarations
//////////////////////////////////////////////////////////////////////////
// MainWndProc
// - The callback function for the message loop windows uses
LRESULT CALLBACK
MainWndProc( HWND handleWindow, // The handle for the window
UINT unMessage, // The message ID for this iteration
WPARAM wParam, // Additional parameters
LPARAM lParam ); // Additional parameters
//////////////////////////////////////////////////////////////////////////
// WinMain
// - The first function called by windows; serves as the main loop for the program
int APIENTRY
WinMain( HINSTANCE hInstance, // The current instance of the program
HINSTANCE hPrevInstance, // The previous instance of the program
LPSTR lpCmdLine, // Any command line arguments
int nCmdCount ) // How many arguments the are
{
// Save our global copies
g_hInstance = hInstance;
// Create a window
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wcex.lpfnWndProc = (WNDPROC)MainWndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = (HCURSOR)LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = NULL;
wcex.lpszMenuName = NULL;
wcex.lpszClassName = TEXT("Thunderclad Engine"); // Should be unique
wcex.hIconSm = NULL;
// Select window styles
UINT unStyle,unStyleX;
unStyleX = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style
unStyle = WS_OVERLAPPEDWINDOW;
// Describe our window placement
WINDOWPLACEMENT wndPlacement;
wndPlacement.length = sizeof(WINDOWPLACEMENT);
wndPlacement.ptMaxPosition.x = 0;
wndPlacement.ptMaxPosition.y = 0;
wndPlacement.ptMinPosition.x = 0;
wndPlacement.ptMinPosition.y = 0;
wndPlacement.rcNormalPosition.bottom = g_nWindowHeight;
wndPlacement.rcNormalPosition.left = 0;
wndPlacement.rcNormalPosition.top = 0;
wndPlacement.rcNormalPosition.right = g_nWindowWidth;
// Create our instance of our main window
g_cWindow.Init( g_hWnd,
wndPlacement,
&wcex,
unStyle,
unStyleX,
g_nWindowWidth,
g_nWindowHeight,
TEXT("Thunderclad Engine"),
g_hInstance );
// Initialize the game here
CGame::GetInstance()->Initialize();
// Start the message loop
MSG msg;
while( CGame::GetInstance()->IsGameActive() )
{
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
if( msg.message == WM_QUIT )
{
break;
}
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else if( CGame::GetInstance()->IsGameActive() )
{
CGame::GetInstance()->Update();
CGame::GetInstance()->Render();
}
}
// Clean up our classes
CGame::GetInstance()->Destroy();
CDeviceManager::GetInstance()->Destroy();
g_cWindow.Destroy();
// return from the program
return msg.wParam;
}
// MainWndProc
// - This will be the message proc for the main window
LRESULT CALLBACK
MainWndProc( HWND handleWindow, // The handle to the window
UINT unMessage, // The message to process
WPARAM wParam, // An additional parameter
LPARAM lParam ) // An additional parameter
{
g_hWnd = handleWindow;
switch( unMessage )
{
case WM_CREATE:
g_cWindow.SetWindowHandle( handleWindow );
g_cWindow.SetHDC( GetDC( handleWindow ) );
CDeviceManager::GetInstance()->Initialize( &g_cWindow );
break;
case WM_ERASEBKGND:
return false;
case WM_SIZE:
// Resize the window
CDeviceManager::GetInstance()->Resize( LOWORD( lParam ),
HIWORD( lParam ) );
return 0;
break;
case WM_CLOSE:
// Free up resources if the red x is pressed
CGame::GetInstance()->Destroy();
CDeviceManager::GetInstance()->Destroy();
g_cWindow.Destroy();
break;
default:
return DefWindowProc( handleWindow,
unMessage,
wParam,
lParam );
}
return 0;
}
//////////////////////////////////////////////////////////////////////////
// CWindow.cpp
// - Defines the functions for our window class
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// Includes
//////////////////////////////////////////////////////////////////////////
#include "CWindow.h"
#include <Windows.h>
#include <tchar.h>
#include <assert.h>
//////////////////////////////////////////////////////////////////////////
// Constructor
//////////////////////////////////////////////////////////////////////////
CWindow::CWindow()
{
m_handleWindow = NULL;
m_handleInstance = NULL;
m_bIsFullScreen = false;
m_wndPlacement;
m_wndClassDescription;
// Zero out our structures
ZeroMemory( &m_wndClassDescription, sizeof( WNDCLASSEX ) );
ZeroMemory( &m_wndPlacement, sizeof( WINDOWPLACEMENT ) );
}
//////////////////////////////////////////////////////////////////////////
// Destructor
//////////////////////////////////////////////////////////////////////////
CWindow::~CWindow()
{
// Free any resources allocated
}
//////////////////////////////////////////////////////////////////////////
// Function Definitions
//////////////////////////////////////////////////////////////////////////
void
CWindow::Init( HWND &out_handleWindow, // The handle to the window
WINDOWPLACEMENT in_wndPlacement, // The class that describes the window's placement
const WNDCLASSEX* in_wndClassDescription, // The class that describes the class's functionality
UINT in_unWindowStyle, // The window style
UINT in_unWindowExStyle, // The windowEx style
int in_nWindowWidth, // The window width
int in_nWindowHeight, // The window height
LPCWSTR in_szProgramName, // The name of the program
HINSTANCE in_handleCurrentInstance ) // The current instance of the program
{
// Store our copies
SetWindowWidth( in_nWindowWidth );
SetWindowHeight( in_nWindowHeight );
IsFullScreen( false );
// Store our copies of relevant info
m_wndClassDescription = *in_wndClassDescription;
m_handleInstance = in_handleCurrentInstance;
m_lpWindowName = in_szProgramName;
m_unWindowStyle = in_unWindowStyle;
m_unWindowStyleEX = in_unWindowExStyle;
// First we must register the class
assert( RegisterClassEx( &m_wndClassDescription ) != 0 );
RECT windowRect; // Grabs Rectangle Upper Left / Lower Right Values
windowRect.left=(long)0; // Set Left Value To 0
windowRect.right=(long)GetWindowWidth(); // Set Right Value To Requested Width
windowRect.top=(long)0; // Set Top Value To 0
windowRect.bottom=(long)GetWindowHeight(); // Set Bottom Value To Requested Height
// Get a pixel perfect window
AdjustWindowRectEx( &windowRect, m_unWindowStyle, FALSE, m_unWindowStyleEX );
// Then we must create the window from the data provided;
SetWindowHandle( CreateWindowEx( m_unWindowStyleEX,
m_wndClassDescription.lpszClassName,
in_szProgramName,
WS_CLIPSIBLINGS | WS_CLIPCHILDREN | in_unWindowStyle,
0,
0,
GetWindowWidth(),
GetWindowHeight(),
NULL,
NULL,
m_handleInstance,
NULL ) );
assert( GetWindowHandle() != NULL );
// Show the window and start updating it
ShowWindow( GetWindowHandle(), SW_SHOW );
UpdateWindow( GetWindowHandle() );
}
// ReCreateWindow
void
CWindow::ReCreateWindow()
{
// Destroy this window and Unregister it
assert( DestroyWindow( m_handleWindow ) != NULL );
WNDCLASSEX temp = m_wndClassDescription;
UnregisterClass( m_wndClassDescription.lpszClassName, m_handleInstance );
// Re register and position it
// First we must register the class
assert( RegisterClassEx( &m_wndClassDescription ) != 0 );
RECT windowRect; // Grabs Rectangle Upper Left / Lower Right Values
windowRect.left=(long)0; // Set Left Value To 0
windowRect.right=(long)GetWindowWidth(); // Set Right Value To Requested Width
windowRect.top=(long)0; // Set Top Value To 0
windowRect.bottom=(long)GetWindowHeight(); // Set Bottom Value To Requested Height
// Get a pixel perfect window
AdjustWindowRectEx( &windowRect, m_unWindowStyle, FALSE, m_unWindowStyleEX);
m_handleWindow = CreateWindowEx( m_unWindowStyleEX,
m_wndClassDescription.lpszClassName,
m_lpWindowName,
WS_CLIPSIBLINGS | WS_CLIPCHILDREN | m_unWindowStyle,
0,
0,
GetWindowWidth(),
GetWindowHeight(),
NULL,
NULL,
m_handleInstance,
NULL );
UINT error = GetLastError();
assert( m_handleWindow != NULL );
// Show the window and start updating it
ShowWindow( m_handleWindow, SW_SHOW );
UpdateWindow( m_handleWindow );
}
// Destroy
// - Destroys this window
void
CWindow::Destroy()
{
if( m_handleWindow != NULL )
{
// Destroys this window and removes it from the message pump
DestroyWindow( m_handleWindow );
m_handleWindow = NULL;
}
UnregisterClass( m_wndClassDescription.lpszClassName, m_handleInstance );
}
// COpenGLDeviceManager.cpp
// - This file will define all methods declared in COpenGLDeviceManager.h
//////////////////////////////////////////////////////////////////////////
// Includes
//////////////////////////////////////////////////////////////////////////
#include "COpenGLDeviceManager.h"
//////////////////////////////////////////////////////////////////////////
// Constructor
//////////////////////////////////////////////////////////////////////////
COpenGLDeviceManager::COpenGLDeviceManager()
{
//////////////////////////////////////////////////////////////////////////
// Platform Specific initialization (Windows)
#if USING_WINDOWS
m_handleWindowHandle = NULL;
m_handleGLRenderContext = NULL;
m_handleDeviceContext = NULL;
ZeroMemory( &m_tPixelFormat, sizeof(PIXELFORMATDESCRIPTOR) );
#endif
//////////////////////////////////////////////////////////////////////////
}
//////////////////////////////////////////////////////////////////////////
// Destructor
//////////////////////////////////////////////////////////////////////////
COpenGLDeviceManager::~COpenGLDeviceManager()
{
m_handleWindowHandle = NULL;
m_handleDeviceContext = NULL;
m_handleGLRenderContext = NULL;
ZeroMemory( &m_tPixelFormat, sizeof( PIXELFORMATDESCRIPTOR ) );
}
//////////////////////////////////////////////////////////////////////////
// TEMP REMOVE THIS CODE
#include <math.h>
void BuildPerspProjMat(float *m, float fov, float aspect,
float znear, float zfar)
{
float xymax = znear * tan(fov);
float ymin = -xymax;
float xmin = -xymax;
float width = xymax - xmin;
float height = xymax - ymin;
float depth = zfar - znear;
float q = -(zfar + znear) / depth;
float qn = -2 * (zfar * znear) / depth;
float w = 2 * znear / width;
w = w / aspect;
float h = 2 * znear / height;
m[0] = w;
m[1] = 0;
m[2] = 0;
m[3] = 0;
m[4] = 0;
m[5] = h;
m[6] = 0;
m[7] = 0;
m[8] = 0;
m[9] = 0;
m[10] = q;
m[11] = -1;
m[12] = 0;
m[13] = 0;
m[14] = qn;
m[15] = 0;
}
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// Function Definitions
//////////////////////////////////////////////////////////////////////////
STATUS
COpenGLDeviceManager::Initialize( CWindow* programWindow )
{
m_bIsFullScreen = programWindow->IsFullScreen();
//////////////////////////////////////////////////////////////////////////
// Platform Specific Initialization (Windows)
#if USING_WINDOWS
// Store the HWND
m_handleWindowHandle = programWindow->GetWindowHandle();
// Do a NULL check if we are in Debug
ASSERT_NULL( m_handleWindowHandle );
// Get the Device Context
m_handleDeviceContext = programWindow->GetHDC();
// Null check the DC
ASSERT_NULL( m_handleDeviceContext );
//////////////////////////////////////////////////////////////////////////
// Set the pixel format
m_tPixelFormat.nSize = sizeof( PIXELFORMATDESCRIPTOR ); // Size of the PFD
m_tPixelFormat.nVersion = 1; // Set the version number
m_tPixelFormat.dwFlags = PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
m_tPixelFormat.iPixelType = PFD_TYPE_RGBA; // Set the color format
m_tPixelFormat.cColorBits = 32; // Set the color depth
m_tPixelFormat.cDepthBits = 24; // Set the depth buffer depth
m_tPixelFormat.iLayerType = PFD_MAIN_PLANE; // Set the layer for the app
// See if there is a pixel format that will match our requests
int nPixelFormat = 0;
ASSERT_FALSE( nPixelFormat = ChoosePixelFormat( m_handleDeviceContext,
&m_tPixelFormat ) );
ASSERT_FALSE( SetPixelFormat( m_handleDeviceContext,
nPixelFormat,
&m_tPixelFormat ) );
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// So far so good... Now we try to create our RenderContext (HRC)
HGLRC tempContext = wglCreateContext( m_handleDeviceContext );
ASSERT_NULL( tempContext );
DWORD pleaseBeIt = GetLastError();
// See if the tempContext will work
ASSERT_FALSE( wglMakeCurrent( m_handleDeviceContext, tempContext ) ); CHECK();
pleaseBeIt = GetLastError();
//GLenum error = glewInit();
//if( error != GLEW_OK )
//{
// fprintf(stderr, "Error: %s\n", glewGetErrorString(error));
//}
//DWORD lastError = GetLastError();
if( false )//wglewIsSupported( "WGL_ARB_create_context" ) == 1 )
{
//lastError = GetLastError();
//// Define our attributes for using OpenGL 3.0 and forward
//int attribs[] = {
// WGL_CONTEXT_MAJOR_VERSION_ARB, 3,//we want a 3.0 context
// WGL_CONTEXT_MINOR_VERSION_ARB, 1,
// //and it shall be forward compatible so that we can only use up to date functionality
// WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
// 0}; //zero indicates the end of the array
// lastError = GetLastError();
// m_handleGLRenderContext = wglCreateContextAttribsARB( m_handleDeviceContext, 0, attribs ); CHECK();
// ASSERT_NULL( m_handleGLRenderContext );
// ASSERT_FALSE( wglMakeCurrent( NULL, NULL ) ); lastError = GetLastError(); // CHECK();
// ASSERT_FALSE( wglDeleteContext( tempContext ) ); // CHECK();
// ASSERT_FALSE( wglMakeCurrent( m_handleDeviceContext, m_handleGLRenderContext ) ); CHECK();
}
else // If we can't use 3.1
{
m_handleGLRenderContext = tempContext;
}
//////////////////////////////////////////////////////////////////////////
TVector3F position;
TMatrixF rotation;
rotation.MakeIdentity();
position.X = position.Y = position.Z = 0.0f;
m_tempCamera.Initialize( m_tempCamera.CreateViewMatrix( position, rotation ),
m_tempCamera.CreateProjectionMatrix( 1280 / 720,
45, 10, 100 ), CCamera::FREE_ROAM );
#endif
return OK;
}
STATUS
COpenGLDeviceManager::Destroy()
{
//////////////////////////////////////////////////////////////////////////
// Platform Specific Destroy
#if USING_WINDOWS
// Check to see if we can safely release and delete our RenderContext
if( m_handleGLRenderContext )
{
ASSERT_FALSE( wglMakeCurrent( m_handleDeviceContext, NULL ) );
ASSERT_FALSE( wglDeleteContext( m_handleGLRenderContext ) );
m_handleGLRenderContext = NULL;
}
// Check and see if we can safely release our DeviceContext
if( m_handleDeviceContext )
{
ASSERT_FALSE( ReleaseDC( m_handleWindowHandle, m_handleDeviceContext ) );
m_handleDeviceContext = NULL;
}
#else
#endif
return OK;
}
STATUS
COpenGLDeviceManager::Resize( i4 nScreenWidth, i4 nScreenHeight )
{
CHECK();
glViewport (0, 0, nScreenWidth, nScreenHeight); CHECK();
glMatrixMode( GL_PROJECTION ); CHECK();
glLoadMatrixf( m_tempCamera.GetProjectionMatrix().MatrixArray ); CHECK();
glMatrixMode( GL_MODELVIEW ); CHECK();
glLoadMatrixf( m_tempCamera.GetViewMatrix().MatrixArray );
return OK;
}
STATUS
COpenGLDeviceManager::Present()
{
//////////////////////////////////////////////////////////////////////////
// Platform Specific Present
#if USING_WINDOWS
CHECK();
glFlush(); CHECK();
DWORD test = GetLastError();
SwapBuffers( m_handleDeviceContext ); CHECK();
// Invalidate the window to draw
RECT clientRect;
GetClientRect( m_handleWindowHandle, &clientRect );
InvalidateRect( m_handleWindowHandle, &clientRect, FALSE );
#else
#endif
//////////////////////////////////////////////////////////////////////////
return OK;
}
STATUS
COpenGLDeviceManager::Clear( i4 unBuffersToClear )
{
//////////////////////////////////////////////////////////////////////////
// Determine which buffers to clear
GLbitfield glBitToClear = 0;
if( COLOR_BUFFER_MASK( unBuffersToClear ) )
{
glBitToClear |= GL_COLOR_BUFFER_BIT;
}
if( DEPTH_BUFFER_MASK( unBuffersToClear ) )
{
glBitToClear |= GL_DEPTH_BUFFER_BIT;
}
if( STENCIL_BUFFER_MASK( unBuffersToClear ) )
{
glBitToClear |= GL_STENCIL_BUFFER_BIT;
}
//////////////////////////////////////////////////////////////////////////
glClearColor(0.0f, 0.2f, 0.4f, 1.0f); CHECK(); // The color to clear the background to
// Finally we can clear the buffers
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
return OK;
}
// COpenGLRenderer.cpp
// - This class will define the functions declared in COpenGLRenderer.h
//////////////////////////////////////////////////////////////////////////
// Includes
//////////////////////////////////////////////////////////////////////////
#include "COpenGLRenderer.h"
#include "Vertices.h"
//////////////////////////////////////////////////////////////////////////
// Constructor
//////////////////////////////////////////////////////////////////////////
COpenGLRenderer::COpenGLRenderer()
{
}
//////////////////////////////////////////////////////////////////////////
// Destructor
//////////////////////////////////////////////////////////////////////////
COpenGLRenderer::~COpenGLRenderer()
{
}
//////////////////////////////////////////////////////////////////////////
// Function Definitions
//////////////////////////////////////////////////////////////////////////
// Initialize
// - This will handle all the initialization needed for the renderer
STATUS
COpenGLRenderer::Initialize()
{
CHECK();
glEnable(GL_DEPTH_TEST); CHECK(); // Enable depth testing
glDepthFunc( GL_LEQUAL ); CHECK(); // Reject anything less than or equal to the current depth
glClearDepth( 1.0f ); CHECK(); // We clear to full depth
glClearStencil( 0 ); CHECK(); // We clear the stencil buffer to false
glCullFace(GL_BACK); CHECK(); // Enable culling
glClearColor(0.0f, 0.2f, 0.4f, 1.0f); CHECK(); // The color to clear the background to
return OK;
}
// Destroy
// - This will free all the memory used by the renderer
STATUS
COpenGLRenderer::Destroy()
{
return OK;
}
// DrawBuffer
// - This will draw a single vertex buffer
void
COpenGLRenderer::DrawVertexBuffer( CVertexBuffer* pBufferToDraw )
{
glLoadIdentity();
glPushMatrix();
glEnableClientState( GL_VERTEX_ARRAY ); CHECK();
glEnableClientState( GL_NORMAL_ARRAY ); CHECK();
glEnableClientState( GL_COLOR_ARRAY ); CHECK();
glVertexPointer(3,
GL_FLOAT,
pBufferToDraw->GetDeclaration()->GetSizeOfVertex(),
pBufferToDraw->GetVertices() ); CHECK();
glNormalPointer( GL_FLOAT,
pBufferToDraw->GetDeclaration()->GetSizeOfVertex(),
static_cast<uchar1*>(pBufferToDraw->GetVertices()) + sizeof( f4 ) * 3 ); CHECK();
glColorPointer( 4,
GL_FLOAT,
pBufferToDraw->GetDeclaration()->GetSizeOfVertex(),
static_cast<uchar1*>(pBufferToDraw->GetVertices()) + sizeof( f4 ) * 6 ); CHECK();
glDrawArrays( GL_TRIANGLES, 0, 3 ); CHECK();
glPopMatrix();
}
The CHECK() macro just checks the glGetError value for non zero, none of these are being hit.