求助 我这个关于OpenGL基于MFC的画线怎么画不出呢?谢谢

walikumsalam 2011-05-15 04:32:20
#include "stdafx.h"
#include "F.h"

#include "FDoc.h"
#include "FView.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CFView

IMPLEMENT_DYNCREATE(CFView, CView)

BEGIN_MESSAGE_MAP(CFView, CView)
//{{AFX_MSG_MAP(CFView)
ON_WM_CREATE()
ON_WM_CANCELMODE()
ON_WM_DESTROY()
ON_WM_MOUSEMOVE()
ON_COMMAND(ID_BUTTONnurbs, OnBUTTONnurbs)
ON_COMMAND(ID_BUTTONbezier, OnBUTTONbezier)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFView construction/destruction

CFView::CFView()
{
// TODO: add construction code here
my_pDC=NULL;
mybezier=FALSE;
mynurbs=FALSE;
}

CFView::~CFView()
{
}

BOOL CFView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs

return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CFView drawing

void CFView::OnDraw(CDC* pDC)
{
CFDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
static BOOL bBusy=FALSE;
if(bBusy) return;
bBusy=TRUE;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
mydraw();
glFinish();
SwapBuffers(wglGetCurrentDC());
bBusy=FALSE;
}

/////////////////////////////////////////////////////////////////////////////
// CFView printing

BOOL CFView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}

void CFView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}

void CFView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CFView diagnostics

#ifdef _DEBUG
void CFView::AssertValid() const
{
CView::AssertValid();
}

void CFView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}

CFDoc* CFView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CFDoc)));
return (CFDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CFView message handlers

BOOL CFView::mypixelformat()
{
static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL| // support OpenGL
PFD_DOUBLEBUFFER, // double buffered
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
};
int pixelformat;

if ( (pixelformat = ChoosePixelFormat(my_pDC->GetSafeHdc(), &pfd)) == 0 )
{
MessageBox("ChoosePixelFormat failed");
return FALSE;
}

if (SetPixelFormat(my_pDC->GetSafeHdc(), pixelformat, &pfd) == FALSE)
{
MessageBox("SetPixelFormat failed");
return FALSE;
}

return TRUE;
}

void CFView::myfirst()
{
PIXELFORMATDESCRIPTOR pfd;
int n;
HGLRC hrc;

my_pDC = new CClientDC(this);

ASSERT(my_pDC != NULL);

if (!mypixelformat())
return;
n =::GetPixelFormat(my_pDC->GetSafeHdc());
::DescribePixelFormat(my_pDC->GetSafeHdc(), n, sizeof(pfd), &pfd);


hrc = wglCreateContext(my_pDC->GetSafeHdc());
wglMakeCurrent(my_pDC->GetSafeHdc(), hrc);

GetClientRect(&my_oldRect);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

int CFView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;

// TODO: Add your specialized creation code here
myfirst();
return 0;
}

void CFView::OnCancelMode()
{
CView::OnCancelMode();

// TODO: Add your message handler code here

}

void CFView::OnDestroy()
{
CView::OnDestroy();

// TODO: Add your message handler code here
HGLRC hrc;

hrc = ::wglGetCurrentContext();

::wglMakeCurrent(NULL, NULL);

if (hrc)
::wglDeleteContext(hrc);

if (my_pDC)
delete my_pDC;

}
void CFView::OnBUTTONnurbs()
{
// TODO: Add your command handler code here
mynurbs=!mynurbs;
glClearColor(0.0f,0.0f,1.0f,1.0f);
Invalidate(FALSE);
}

void CFView::mydraw()
{
glTranslatef(0.0f,0.0f,-6.0f);
if(mybezier) {Mybezier();};
if(mynurbs){Mynurbs();};
}

void CFView::Mynurbs()
{
glPushMatrix();
glBegin(GL_LINES);
glVertex2f(0.0f,0.0f);
glVertex2f(0.01f,-0.4f);
glEnd();
glPopMatrix();
}
...全文
302 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
郑小则 2013-11-05
  • 打赏
  • 举报
回复
能说一下你怎么搞定的么?
lumingming 2012-07-26
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

倒掉。。您这个视景体完全没有把那根线包含在内,当然看不到了。

您的线的空间实际位置应该是(0,0,-6)(0.01,-0.4,-6)
而您的视景体的x轴范围是是-1~0,您说能看到吗?改成-1~1应该就能看到了
[/Quote]亲,虽然不明白你在说什么,,,但给我了点思路,,搞定。。
jamesclarke 2011-05-17
  • 打赏
  • 举报
回复
倒掉。。您这个视景体完全没有把那根线包含在内,当然看不到了。

您的线的空间实际位置应该是(0,0,-6)(0.01,-0.4,-6)
而您的视景体的x轴范围是是-1~0,您说能看到吗?改成-1~1应该就能看到了
walikumsalam 2011-05-16
  • 打赏
  • 举报
回复
我的ONSize内容是这样的
void CFView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);

// TODO: Add your message handler code here
if(cy>0)
{
my_oldRect.right=cx;
my_oldRect.bottom=cy;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0,0.0,-1.0*cy/cx,1.0*cy/cx,5.0,9.0);
}
RedrawWindow();

}
是哪里错了呢?怎么改呢?
向立天 2011-05-16
  • 打赏
  • 举报
回复
天鹅梦 2011-05-15
  • 打赏
  • 举报
回复
没看到你设置的近远观察平面,或者说视锥大小,你这个不一定是没画出来,是你画出来了但看不到,所以,要设置一下观察矩阵,而且在画出之前,建议压一下栈,画完弹出
jamesclarke 2011-05-15
  • 打赏
  • 举报
回复
我猜测可能有以下几个方面的问题:
1、线条颜色和背景色重合,所以显示不出来。建议给线条赋予不用的颜色;
2、线条在视景体之外绘制了。建议对视景体进行明确设置,不要使用默认的,特别是前后平面的位置,要将线条的位置囊括在内。

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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