#include <stdio.h>
#include <iostream>
#include <windows.h>
#include <windowsx.h>
//gl文件必须包含在windows后边,do not know why
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
using namespace std;
int nWidth = 900;
int nHeight = 600;
float zoom =1.0;
float ViewAngle=45.0f;
double fovy = 45.f;
double nearSreen=0.1f;
HWND hwnd = NULL;
HDC hDC = NULL;
HGLRC hRC = NULL;
int init(void);
void render(void);
LRESULT CALLBACK MainWndProc(HWND,UINT,WPARAM,LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
char szClassName[] = "MainWClass";
WNDCLASSEX wndclass;
wndclass.cbSize = sizeof(WNDCLASSEX);
wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wndclass.lpfnWndProc = MainWndProc;/*A pointer to the window procedure*/
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadIcon(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wndclass.lpszMenuName = NULL;/*(LPSTR)IDR_MENU1;windows belonging to this class have no default menu*/
wndclass.lpszClassName = szClassName;/*LPCTSTR*/
wndclass.hIconSm = NULL;
RegisterClassEx(&wndclass);
hwnd = CreateWindowEx(
0,
szClassName,
"My first Window",
WS_OVERLAPPEDWINDOW| WS_VISIBLE,
200,
100,
nWidth,
nHeight,
NULL,
NULL,
hInstance,
NULL);
if (hwnd == NULL){
MessageBox(NULL,"创建窗口出错","error",MB_OK);
return -1;
}
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
if(init() == FALSE)
{
MessageBox(hwnd, "init unsuccessed","ERROR",MB_OK);
return 0;
}
MSG msg;
//6.0可以不初始化MSG;
memset(&msg,0,sizeof(msg));
while( msg.message != WM_QUIT )
{
if (PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
render();
//glutReshapeFunc(reshapeFunction);
glClearColor(0.0f,0.0f,0.4f,1.0f);
}
}
return msg.wParam;
}
LRESULT CALLBACK MainWndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch (message)
{
case WM_CHAR:
{
switch (LOWORD(wParam))
{
case 27: PostQuitMessage(0); break; //27='ESC'
}
return 0;
}
case WM_DESTROY: //必须有不然后台程序不收回
{
PostQuitMessage(0);
return 0;
}
}
return DefWindowProc(hwnd,message,wParam,lParam); //如果返回空,创建窗口失败
}
int init()
{
GLuint PixelFormat;
PIXELFORMATDESCRIPTOR pfd;/*pixel format of a drawing surface*/
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW |PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32 ;
pfd.cDepthBits = 32;//Specifies the depth of the depth (z-axis) buffer.
hDC = GetWindowDC(hwnd);
//设置点的形式否则不显示model
PixelFormat = ChoosePixelFormat( hDC, &pfd );
if (PixelFormat == 0) {
MessageBox(NULL, "ChoosePixelFormat() failed: " "Cannot find a suitable pixel format.", "Error", MB_OK);
return 0;
}
//sets the pixel format of the specified device context to the format specified by the iPixelFormat index
if(SetPixelFormat( hDC, PixelFormat, &pfd) == FALSE)
{
MessageBox(NULL, "ChoosePixelFormat() failed: " "Cannot find a suitable pixel format.", "Error", MB_OK);
return 0;
}
/*The wglCreateContext function creates a new OpenGL rendering context, which is suitable for drawing on the device referenced by hdc.
The rendering context has the same pixel format as the device context.*/
// valid handle to an OpenGL rendering context否则只是s静态model
hRC = wglCreateContext( hDC );
wglMakeCurrent( hDC, hRC );
glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
glEnable( GL_DEPTH_TEST );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
//设定视角,不然model无法正常观看
gluPerspective( fovy, (double)nWidth / nHeight, nearSreen, 1000.0f);
return 1;
//void gluLookAt(GLdouble eyex,GLdouble eyey,GLdouble eyez,GLdouble centerx,GLdouble centery,GLdouble centerz,GLdouble upx,GLdouble upy,GLdouble upz)
//up决定了向上向量类似坐标转换
//gluLookAt(0.f,0.f,0.f, 0.f,0.f,-10.f, 0.f,1.f,0.f);
}
void render(void)
{
glPushMatrix();
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
gluLookAt(2.f,1.f,5.f, 0.f,0.f,-0.f, 0.f,1.f,0.f);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
//glTranslated(0.f, -0.5f, 0.f);
/*glScaled(scale, scale, scale);
glRotated((spinx + mouse_x_add)/nWidth*360, 0.0, 1.0, 0.0);
glRotated((spiny + mouse_y_add)/nHeight*360,1.0,0.0,0.0);*/
//draw coordinate
glBegin(GL_LINES);
glColor3f(1.0,0,0);
glVertex3f(0,0,0);
glVertex3f(100,0,0);
glColor3f(0.0,1.0,0);
glVertex3f(0,0,0);
glVertex3f(0,100,0);
glColor3f(0.0,0,1.00);
glVertex3f(0,0,0);
glVertex3f(0,0,100);
glEnd();
Sleep(10);
glPopMatrix();
//刷新缓存进行绘制
SwapBuffers( hDC );
}