求助:OpenGL关于绘制3D网格线的问题,帮忙看下代码,本人新人
- C/C++ code
#include "stdafx.h"#include <windows.h>#include <glut.h>#include <glaux.h>#pragma comment(lib,"opengl32.lib")#pragma comment(lib,"glaux.lib")LRESULT CALLBACK WindowProc(HWND,UINT,WPARAM,LPARAM);void EnableOpenGL(HWND hwnd,HDC* ,HGLRC*);void DisableOpenGL(HWND,HDC,HGLRC);int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR nCmdLine, int nCmdShow){ WNDCLASSEX wcex; HWND hwnd; HDC hDC; HGLRC hRC; MSG msg; BOOL bQuit=FALSE; float i=0.0f,Angle=0.0f; wcex.cbSize=sizeof(WNDCLASSEX); wcex.cbClsExtra=0; wcex.cbWndExtra=0; wcex.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH); wcex.hCursor=LoadCursor(NULL,IDC_CROSS); wcex.hIcon=LoadIcon(NULL,IDI_INFORMATION); wcex.hIconSm=NULL; wcex.hInstance=hInstance; wcex.lpfnWndProc=WindowProc; wcex.lpszClassName="OpenGL___Xiang"; wcex.lpszMenuName=NULL; wcex.style=CS_HREDRAW|CS_VREDRAW; if (!RegisterClassEx(&wcex)) { MessageBox(NULL,"注册类失败!","Error",MB_ICONERROR); return 0; } hwnd=CreateWindowEx(0, "OpenGL___Xiang","OpenGL Sample",WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT, NULL,NULL,hInstance,0); ShowWindow(hwnd,SW_MAXIMIZE); EnableOpenGL(hwnd,&hDC,&hRC); while (!bQuit) { if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { if (msg.message==WM_QUIT) { bQuit=TRUE; } else { TranslateMessage(&msg); DispatchMessage(&msg); } } else { glClearColor(0.0f,0.0f,0.0f,0.5f); glClearDepth(1.0f); glEnable(GL_SMOOTH); glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST); glPushMatrix(); glTranslatef(0.0f,0.0f,0.0f); glColor3f(0.0f,1.0f,0.0f); for (i=-50;i<=50;i+=1) { glBegin(GL_LINES); glVertex3f( -50,0,i); glVertex3f( 50,0,i); glVertex3f( i,0,-50); glVertex3f( i,0,50); glEnd(); } glPopMatrix(); SwapBuffers(hDC); } } DisableOpenGL(hwnd,hDC,hRC); DestroyWindow(hwnd); return msg.wParam;}LRESULT CALLBACK WindowProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam){ switch (msg) { case WM_CLOSE: PostQuitMessage(0); break; case WM_KEYDOWN: { switch (wparam) { case VK_ESCAPE: PostQuitMessage(0); break; } } break; default: return DefWindowProc(hwnd,msg,wparam,lparam); } return 0;}void EnableOpenGL(HWND hwnd,HDC* hDC,HGLRC* hRC){ PIXELFORMATDESCRIPTOR pfd; int iFormat; *hDC=GetDC(hwnd); ZeroMemory(&pfd,sizeof(pfd)); pfd.nSize=sizeof(pfd); pfd.nVersion=1; pfd.dwFlags=PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER; pfd.iPixelType=PFD_TYPE_RGBA; pfd.cColorBits=24; pfd.cDepthBits=16; pfd.iLayerType=PFD_MAIN_PLANE; //颜色输出单通道? iFormat=ChoosePixelFormat(*hDC,&pfd); SetPixelFormat(*hDC,iFormat,&pfd); *hRC=wglCreateContext(*hDC); wglMakeCurrent(*hDC,*hRC);}void DisableOpenGL(HWND hwnd,HDC hdc,HGLRC hRC){ wglMakeCurrent(NULL,NULL); wglDeleteContext(hRC); ReleaseDC(hwnd,hdc);}[解决办法]
第一种可能:视角不对吧,你是在XOZ平面上绘制的网格,OpenGL默认视角是人眼位置在Z轴上,所以你看到的只能是一条线。
第二种可能:投影不正确,导致只看到某一条线
------解决方案--------------------
看看这篇文章有没有参考价值
http://blog.csdn.net/xianglitian/article/details/6164103
[解决办法]
同意2楼的说法,代码是对的,可能是视角不对