Home > Software engineering >  OpenGL couldn't solve the problems encountered in the process of learning
OpenGL couldn't solve the problems encountered in the process of learning

Time:11-02

# define WIN32_LEAN_AND_MEAN//cut too large Windows library

H # include "Windows."
# include "gl \ gl h"
# include "gl \ glu. H"
//# include "GLaux. H"

The static LPCTSTR lpszAppName="OpenGL App";

Float Angle f=0.0;
HDC g_HDC;
//HBRUSH hBlueBrush hRedBrush;

Void SetupPixelFormat (HDC HDC)
{
Int nPixelFormat;//pixel format variable

The static PIXELFORMATDESCRIPTOR PFD=
{

Sizeof (PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32,
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
16,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0,
};

NPixelFormat=ChoosePixelFormat (hDC, & amp; PFD);

SetPixelFormat (hDC, nPixelFormat, & amp; PFD);

}

//Windows event handler
LRESULT a CALLBACK WndProc (HWND HWND, UINT message, WPARAM WPARAM, LPARAM LPARAM)
{
The static HGLRC hRC.
The static HDC HDC.
//char string []="hello world!" ;
Int width, height;


The switch (the message)
{

Case WM_CREATE message handler:

HDC=GetDC (hWnd);
G_HDC=hDC;
SetupPixelFormat (hDC);

HRC=wglCreateContext (hDC);
WglMakeCurrent (hDC, hRC);

/* MessageBox (NULL, "error5,", "closed error", MB_OK | MB_ICONINFORMATION); */
return 0;
break;

Case WM_CLOSE:
WglMakeCurrent (hDC, NULL);
WglDeleteContext (hRC);

The PostQuitMessage (0);

Error4/* MessageBox (NULL, ", ", "closed error", MB_OK | MB_ICONINFORMATION); */
return 0;
break;


Case WM_SIZE:
Height=HIWORD (lParam);
Width=LOWORD (lParam);

If (height==0)
{height=1; }

GlViewport (0, 0, width, height);
GlMatrixMode (GL_PROJECTION);
GlLoadIdentity ();

GluPerspective (45.0 f (GLfloat) width/height (GLfloat), 1.0 f, 1000.0 f);

GlMatrixMode (GL_MODELVIEW);
GlLoadIdentity ();

Error3/* MessageBox (NULL, ", ", "closed error", MB_OK | MB_ICONINFORMATION); */
return 0;
break;
/*
In case the WM_PAINT:
{
PAINTSTRUCT ps;
HBRUSH hOldBrush;

BeginPaint (hWnd, & amp; Ps);

HOldBrush=SelectObject (ps. HDC, hRedBrush);
A Rectangle (ps. HDC, 100100150150);

SelectObject (ps. HDC, hOldBrush);
EndPaint (hWnd, & amp; Ps);
}

break;
*/
Default:
break;//return DefWindowProc (hWnd, message, wParam, lParam);

}


Return DefWindowProc (hWnd, message, wParam, lParam);
}

Int WINAPI WinMain (HINSTANCE HINSTANCE, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
Modifed WNDCLASSEX wc.
MSG MSG.
The HWND HWND;
BOOL done;

/*
HBlueBrush=CreateSolidBrush (RGB (0,0,255));
HRedBrush=CreateSolidBrush (RGB (0, 255));
*/
Wc. CbSize=sizeof (modifed WNDCLASSEX);
Wc. Style=CS_HREDRAW | CS_VREDRAW;
Wc. LpfnWndProc=DefWindowProc;
Wc. CbClsExtra=0;
Wc. CbWndExtra=0;
The wc. The hInstance=hInstance;
Wc. HIcon=LoadIcon (NULL, IDI_APPLICATION);
Wc. HCursor=LoadCursor (hInstance, IDC_ARROW);
Wc. HbrBackground=NULL;
Wc. LpszClassName=lpszAppName;
Wc. LpszMenuName=NULL;
Wc. HIconSm=LoadIcon (wc hInstance, IDI_WINLOGO);




if(! RegisterClassEx (& amp; Wc))
//return 0;
Error1 {MessageBox (NULL, ", ", "closed error", MB_OK | MB_ICONINFORMATION); return 0; }

The hWnd=CreateWindowEx (NULL,
LpszAppName,
"The OpenGL window Application",
//WS_EX_APPWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_SYSMENU | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
100100,
400400,
NULL,
NULL,
HInstance,
NULL);


if(! HWnd)
{
Error2 MessageBox (NULL, ", ", "closed error", MB_OK | MB_ICONINFORMATION);

return 0;
}

ShowWindow (hWnd, SW_SHOW);
UpdateWindow (hWnd);

The done=FALSE;

while(! Done)
{
PeekMessage (& amp; MSG, hWnd, NULL, NULL, PM_REMOVE);

If (MSG) message==WM_QUIT)
{the done=TRUE; }

The else
{
GlClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GlLoadIdentity ();

Angle Angle of=+ 0.1 f;
If (Angle & gt;=360 f)
Angle f=0.0;
GlTranslatef (0.0 f, f, 0.0-5.0 f);//move back five units
GlRotatef (Angle, 0.0 f, f, 0.0 1.0 f);//turn around the z axis

GlColor3f (0.0 1.0 f, 0.0 f, f);

GlBegin (GL_TRIANGLES);
GlVertex3f (0.0 f, f, 0.0 0.0 f);
GlVertex3f (1.0 f, f, 0.0 0.0 f);
GlVertex3f (1.0 f, f, 1.0 0.0 f);
glEnd();

SwapBuffers (g_HDC);

TranslateMessage (& amp; MSG);
DispatchMessage (& amp; MSG);
}

}


Return MSG. WParam;

}
As above, for all code, can display the window in the process of running, but without any map operations, and change the window size after background is changing, become completely black,,, what is the problem, right away to see mentor, urgent urgent!!!!!!

CodePudding user response:

The building Lord, you graduated now? But what is done? nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related