OpenGL 代码问题!
以下代码 经过一次鼠标点击后图像消失!
Render() 函数应该是有问题的,但我不太清楚问题在哪儿,请指教!
#include <Windows.h>
#include <wchar.h>
#include <gl\GL.h>
#include <iostream>
using namespace std;
#pragma comment(lib,"opengl32.lib")
HGLRC hrc;
HDC hdc;
double p=0.0f;
LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
void SetDCPixelFormat(HDC hdc)
{
int PixelFormatIndex;
PIXELFORMATDESCRIPTOR PixelFormat=
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW|
PFD_SUPPORT_OPENGL|
PFD_DOUBLEBUFFER|
PFD_TYPE_RGBA,
24,
0,0,0,0,0,0,
0,0,
0,0,0,0,0,
32,
0,0,
PFD_MAIN_PLANE,
0,0,0,0
};
PixelFormatIndex =ChoosePixelFormat (hdc,&PixelFormat);
SetPixelFormat (hdc,PixelFormatIndex,&PixelFormat);
}
void OnSize(GLsizei with,GLsizei height)
{
glPushMatrix ();
glMatrixMode(GL_PROJECTION);
glLoadIdentity() ;
glFrustum (1.0,1.0,44.0,44.0,1.0,10.0);
glViewport (0,0,with,height);
glEnable( GL_DEPTH_TEST );
glPointSize(8);
glLineWidth(5);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glEnable(GL_BLEND);
}
void inline Render()
{
glColor3b (233,233,233);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix ();
glMatrixMode (GL_MODELVIEW);
//glPopMatrix();
glLoadIdentity ();
glPushMatrix();
glTranslated (p,0.0f,0.0f);
//glPopMatrix();
//glRotated (11.5f,0.0f,0.1f,0.0f);
//glScaled (01.1,01.1,01.1);
//glHint (GL_LINE_SMOOTH_HINT,GL_NICEST);
glBegin(GL_TRIANGLES);//开始画三角形
//glShadeModel(GL_SHADE_MODEL);//设置为光滑明暗模式
glColor4f(1.0,0.0,0.0,0.1);//设置第一个顶点为红色
glVertex3f(-1.0,0.0,1.0);
//glEdgeFlag (false);
glColor4f(0.0,1.0,0.0,0.1);//设置第二个顶点为绿色
glVertex3f(0.0,1.0,-1.0);//设置第二个顶点的坐标为(0.0,-1.0)
glColor4f(0.0,0.0,1.0,0.1);//设置第三个顶点为蓝色
glVertex3f(1.0,-1.0,-1.0);//设置第三个顶点的坐标为(-0.5,1.0)
glEnd();//三角形结束
glPopMatrix ();
glPopMatrix ();
glFlush();//强制OpenGL函数在有限时间内运行
//std::cout<<"Flush is ok"<<endl;
}
int CreateGLWindow(int x,int y,int with,int height)
{
HWND hWnd;
wchar_t classname[16] =L"GlWindow";
wchar_t title[10] =L" ";
WNDCLASSEX WinClass;
WinClass.cbSize =sizeof(WNDCLASSEX);
WinClass.style= CS_HREDRAW|CS_VREDRAW |CS_DBLCLKS;
WinClass.lpfnWndProc= WinProc;
WinClass.cbClsExtra= 0;
WinClass.cbWndExtra= 0;
WinClass.hInstance= NULL;//hInstance;
WinClass.hIcon= NULL;//LoadIcon(hInstance, MAKEINTRESOURCE(IDI_USHOWGDI));
WinClass.hCursor= LoadCursor(NULL, IDC_ARROW);
WinClass.hbrBackground= (HBRUSH)(COLOR_WINDOW+2);
WinClass.lpszMenuName= NULL;//MAKEINTRESOURCE(IDC_USHOWGDI);
WinClass.lpszClassName= classname;
WinClass.hIconSm= NULL;//LoadIcon(.hInstance, MAKEINTRESOURCE(IDI_SMALL));
RegisterClassEx(&WinClass);
hWnd=CreateWindowExW(WS_EX_OVERLAPPEDWINDOW, classname,title,WS_CLIPCHILDREN |WS_CLIPSIBLINGS,x,y,with, height,NULL,NULL,NULL,NULL);
if(hWnd)
{
ShowWindow(hWnd,SW_SHOW );
UpdateWindow (hWnd);
//SetWindowRgn(hWnd,CreateRectRgn(5,27,with-5,height-5),1);
return 1;
}
return 0;
}
void StartMessageLoop()
{
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
int a ;
switch(message)
{
case WM_CREATE :
hdc=GetDC(hWnd);
SetDCPixelFormat (hdc);
hrc=wglCreateContext (hdc);
wglMakeCurrent (hdc,hrc);
break;
case WM_SIZE :
OnSize (LOWORD(lParam),HIWORD(lParam));
break;
case WM_LBUTTONDOWN:
SendMessage(hWnd,WM_NCLBUTTONDOWN ,HTCAPTION,0);
p+=0.1;
a=GetTickCount();
for(int i=0;i<200;i++)
{
Render();
}
cout<<GetTickCount()-a<<endl;
SwapBuffers (hdc);
ValidateRect (hWnd,NULL);
break;
case WM_PAINT :
Render();
SwapBuffers (hdc);
ValidateRect (hWnd,NULL);
break;
default:
break;
}
return DefWindowProc (hWnd,message,wParam,lParam);
}
int main()
{
CreateGLWindow (100,10,500,500);
StartMessageLoop ();
}
[解决办法]
Render()函数中再清除深度缓存
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);