我已经按照教程(我是opengl的新手),由于某种原因三角形没有显示...我会感激任何建议:)。 它编译没有警告,glClearColor如果我改变它,但没有显示三角形。
#include "GphEditorWindow.h"
#include "gl\GL.h"
#include "gl\GLU.h"
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR plCmdLine, int nCmdShow)
{
POINT windowPos = { 100, 100 };
POINT windowSize = { 800, 600 };
if (!editorWindow.Init(hInstance, windowPos, windowSize))
{
MessageBox(NULL, "EditorWindow init failed", "Graphite Editor", MB_OK | MB_ICONERROR);
return ERROR_APP_INIT_FAILURE;
}
else
{
return editorWindow.Run();
}
return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
return editorWindow.WndProc(hWnd, message, wParam, lParam);
}
LRESULT EditorWindow::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_SIZE:
RECT rect;
GetClientRect(hWnd, &rect);
workspaceWth = rect.right - rect.left;
workspaceHgh = rect.bottom - rect.top;
break;
default:
return (DefWindowProc(hWnd, message, wParam, lParam));
}
return 0L;
}
LRESULT EditorWindowGL::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
long result = EditorWindow::WndProc(hWnd, message, wParam, lParam);
switch (message)
{
case WM_CREATE:
if (!InitWGL(hWnd))
{
MessageBox(NULL, "Rendering Context Creation Failed!", "Graphite Editor", MB_OK | MB_ICONERROR);
return ERROR_APP_INIT_FAILURE;
}
SceneSetup(false);
ShowTitleBarInfo(hWnd);
break;
case WM_DESTROY:
DeleteWGL();
break;
case WM_SIZE:
SceneSetup(false);
break;
case WM_PAINT:
DrawScene();
ValidateRect(hWnd, NULL);
break;
}
return result;
}
bool EditorWindow::Init(HINSTANCE editorHandler, POINT editorWindowPos, POINT editorWindowSize)
{
char editorWindowName[] = "Graphite Editor";
WNDCLASSEX wc;
wc.cbSize = sizeof(wc);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = (WNDPROC)::WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = editorHandler;
wc.hIcon = NULL;
wc.hIconSm = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = editorWindowName;
if (RegisterClassExA(&wc) == 0)
{
DWORD blah = GetLastError();
return false;
}
editorWindowHandler = CreateWindow(editorWindowName, editorWindowName, WS_OVERLAPPEDWINDOW, editorWindowPos.x, editorWindowPos.y, editorWindowSize.x, editorWindowSize.y, NULL, NULL, editorHandler, NULL);
if (editorWindowHandler == NULL)
return false;
ShowWindow(editorWindowHandler, SW_SHOW);
UpdateWindow(editorWindowHandler);
return true;
}
WPARAM EditorWindow::Run()
{
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
bool EditorWindowGL::PixelFormatSet(HDC dcHandler) const
{
PIXELFORMATDESCRIPTOR pixelFormatDesc;
ZeroMemory(&pixelFormatDesc, sizeof(pixelFormatDesc));
pixelFormatDesc.nVersion = 1;
pixelFormatDesc.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER;
pixelFormatDesc.iPixelType = PFD_TYPE_RGBA;
pixelFormatDesc.cColorBits = 32;
pixelFormatDesc.cDepthBits = 32;
pixelFormatDesc.iLayerType = PFD_MAIN_PLANE;
int pixelFormat = ChoosePixelFormat(dcHandler, &pixelFormatDesc);
if (pixelFormat == 0)
return false;
if (!SetPixelFormat(dcHandler, pixelFormat, &pixelFormatDesc))
return false;
return true;
}
bool EditorWindowGL::InitWGL(HWND windowHandler)
{
dcHandler = ::GetDC(windowHandler);
if (!PixelFormatSet(dcHandler))
return false;
rcHandler = wglCreateContext(dcHandler);
if (rcHandler == NULL)
return false;
if (!wglMakeCurrent(dcHandler, rcHandler))
return false;
return true;
}
void EditorWindowGL::DeleteWGL()
{
wglMakeCurrent(NULL, NULL);
wglDeleteContext(rcHandler);
::ReleaseDC(editorWindowHandler, dcHandler);
}
void EditorWindowGL::ShowTitleBarInfo(HWND windowHandler)
{
char buffer[256];
GetWindowText(windowHandler, buffer, 256);
const GLubyte* version = glGetString(GL_VERSION);
strcat_s(buffer, " | OpenGL");
strcat_s(buffer, (char*)version);
const GLubyte* vendor = glGetString(GL_VENDOR);
strcat_s(buffer, " | ");
strcat_s(buffer, (char*)vendor);
const GLubyte* gpu = glGetString(GL_RENDERER);
strcat_s(buffer, " | "); strcat_s(buffer, (char*)gpu);
SetWindowText(windowHandler, buffer);
}
void EditorWindowGL::SceneSetup(bool isometric)
{
glViewport(0, 0, workspaceWth, workspaceHgh);
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
float aspect = workspaceHgh / (float)workspaceWth;
if (!isometric)
glFrustum(-1.0f, 1.0f, aspect * -1.0f, aspect * 1.0f, 1.0f, 10.0f);
else
glOrtho(-1.0f, 1.0f, aspect * -1.0f, aspect * 1.0f, 1.0f, 10.0f);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
}
void EditorWindowGL::DrawScene()
{
const float x0 = 1.0f;
const float y0 = 1.0f;
const float z0 = 1.0f;
glClearColor(0.125f, 0.125f, 0.125f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -3.0f);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glBegin(GL_TRIANGLES);
glVertex3f(-x0, -y0, 0.0f);
glVertex3f(x0, -y0, 1.0f);
glVertex3f(x0, y0, 0.0f);
glEnd();
SwapBuffers(dcHandler);
}