OpenGL的问题,为什么这个程序窗口变化后老是出现图像残缺甚至完全消失?

boodweb 2001-12-21 10:05:55
为什么这个程序窗口变化后老是出现图像残缺甚至完全消失?

// OpenGL.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "gl\gl.h"
#include "gl\glu.h"
#include "stdlib.h"
#include "resource.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text

//OpenGL Variables
HDC hDC;
HGLRC hRC;

// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

//OpenGL Functions
BOOL InitOpenGL(HWND hWnd);
void SetSize(int nWidth,int nHight);
void DrawScene();

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;

// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_OPENGL, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_OPENGL);

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return msg.wParam;
}



//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_OPENGL);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_OPENGL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

return RegisterClassEx(&wcex);
}

//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

hInst = hInstance; // Store instance handle in our global variable

hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

return TRUE;
}

//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
RECT rc;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

switch (message)
{
case WM_CREATE:
InitOpenGL(hWnd);
GetClientRect(hWnd,&rc);
SetSize(rc.right,rc.bottom);
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
DrawScene();
EndPaint(hWnd, &ps);
break;
case WM_SIZE:
GetClientRect(hWnd,&rc);
SetSize(rc.right,rc.bottom);
InvalidateRect(hWnd,NULL,FALSE);
break;
case WM_DESTROY:
wglDeleteContext(hRC);
ReleaseDC(hWnd,hDC);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

BOOL InitOpenGL(HWND hWnd)
{
int PixelFormat;
RECT rc;
PIXELFORMATDESCRIPTOR pfd={
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW,
PFD_TYPE_RGBA,
16,
0,0,0,0,0,0,
0,
0,
0,
0,0,0,0,
16,
0,
0,
PFD_MAIN_PLANE,
0,
0,0,0
};
hDC=GetDC(hWnd);
PixelFormat=ChoosePixelFormat(hDC,&pfd);
if(PixelFormat==0)
{
MessageBox(NULL,"Choose Pixel Format Failed!","ERR",0);
return FALSE;
}
if(SetPixelFormat(hDC,PixelFormat,&pfd)==0)
{
MessageBox(NULL,"Set Pixel Format Failed!","ERR",0);
return FALSE;
}
hRC=wglCreateContext(hDC);
wglMakeCurrent(hDC,hRC);
GetClientRect(hWnd,&rc);
glClearColor(0.0,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
gluPerspective(45.0,(float)(rc.right/rc.bottom),1.0,100.0);
return TRUE;
}
void DrawScene()
{
GLUquadricObj *obj;

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslated(0,0,-5);
glColor3f(1.0,0.0,0.0);

obj=gluNewQuadric();
gluQuadricDrawStyle(obj,GLU_FILL);
gluSphere(obj,1.5,24,24);
glFlush();
}

void SetSize(int nWidth,int nHight)
{
GLdouble aspect;
if(nHight==0)
aspect = (GLdouble)nWidth;
else
aspect = (GLdouble)nWidth/(GLdouble)nHight;

glViewport(0,0,nWidth,nHight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,aspect,1,100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glDrawBuffer(GL_BACK);

glEnable(GL_DEPTH_TEST);

}
...全文
147 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
tangt 2001-12-22
  • 打赏
  • 举报
回复
怪不得没人答,这么长的代码,吓也吓跑了,
从你提的现象看,应该是在窗口resize后场景的定义没有及时刷新,
再就是注意在使用双缓冲时,注意你调了哪个到前台.
代码我没读,抱歉,你好像还在用很早的版本,快用新的吧,过两天相信1.3就有了,

8,325

社区成员

发帖
与我相关
我的任务
社区描述
游戏开发相关内容讨论专区
社区管理员
  • 游戏开发
  • 呆呆敲代码的小Y
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧