19,464
社区成员
发帖
与我相关
我的任务
分享// OpenGLView.cpp : implementation of the COpenGLView class
//
#include "stdafx.h"
#include "OpenGL.h"
#include<GL/glut.h>
#include "OpenGLDoc.h"
#include "OpenGLView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// COpenGLView
IMPLEMENT_DYNCREATE(COpenGLView, CView)
BEGIN_MESSAGE_MAP(COpenGLView, CView)
//{{AFX_MSG_MAP(COpenGLView)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_SIZE()
ON_COMMAND(ID_MENUITEM0, OnMenuitem0)
ON_COMMAND(ID_MENUITEM1, OnMenuitem1)
ON_COMMAND(ID_MENUITEM2, OnMenuitem2)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// COpenGLView construction/destruction
COpenGLView::COpenGLView()
{
// TODO: add construction code here
m_hGLContext=NULL;
m_GLPixelIndex=0;
}
COpenGLView::~COpenGLView()
{
}
BOOL COpenGLView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.style|=(WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// COpenGLView drawing
void COpenGLView::OnDraw(CDC* pDC)
{
COpenGLDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
BOOL COpenGLView::SetWindowPixelFormat(HDC hDC)
{
PIXELFORMATDESCRIPTOR pixelDesc;
pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pixelDesc.nVersion = 1;
pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_DRAW_TO_BITMAP |
PFD_SUPPORT_OPENGL |
PFD_SUPPORT_GDI |
PFD_STEREO_DONTCARE;
pixelDesc.iPixelType = PFD_TYPE_RGBA;
pixelDesc.cColorBits = 32;
pixelDesc.cRedBits = 8;
pixelDesc.cRedShift = 16;
pixelDesc.cGreenBits = 8;
pixelDesc.cGreenShift = 8;
pixelDesc.cBlueBits = 8;
pixelDesc.cBlueShift = 0;
pixelDesc.cAlphaBits = 0;
pixelDesc.cAlphaShift = 0;
pixelDesc.cAccumBits = 64;
pixelDesc.cAccumRedBits = 16;
pixelDesc.cAccumGreenBits = 16;
pixelDesc.cAccumBlueBits = 16;
pixelDesc.cAccumAlphaBits = 0;
pixelDesc.cDepthBits = 32;
pixelDesc.cStencilBits = 8;
pixelDesc.cAuxBuffers = 0;
pixelDesc.iLayerType = PFD_MAIN_PLANE;
pixelDesc.bReserved = 0;
pixelDesc.dwLayerMask = 0;
pixelDesc.dwVisibleMask = 0;
pixelDesc.dwDamageMask = 0;
//接着用鼠标右键在COpenGLView中添加保护型的成员变量: int m_GLPixelIndex;
m_GLPixelIndex = ChoosePixelFormat( hDC, &pixelDesc);
if (m_GLPixelIndex==0) // Let's choose a default index.
{
m_GLPixelIndex = 1;
if (DescribePixelFormat(hDC, m_GLPixelIndex,
sizeof(PIXELFORMATDESCRIPTOR), &pixelDesc)==0)
{
return FALSE;
}
}
if (SetPixelFormat( hDC, m_GLPixelIndex, &pixelDesc)==FALSE)
{
return FALSE;
}
return TRUE;
}
//
BOOL COpenGLView::CreateViewGLContext(HDC hDC){
m_hGLContext=wglCreateContext(hDC);
if(m_hGLContext==NULL)
return FALSE;
if(wglMakeCurrent(hDC,m_hGLContext)==FALSE)
return FALSE;
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// COpenGLView printing
BOOL COpenGLView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void COpenGLView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void COpenGLView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// COpenGLView diagnostics
#ifdef _DEBUG
void COpenGLView::AssertValid() const
{
CView::AssertValid();
}
void COpenGLView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
COpenGLDoc* COpenGLView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(COpenGLDoc)));
return (COpenGLDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// COpenGLView message handlers
int COpenGLView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
HWND hWnd = GetSafeHwnd();
HDC hDC = ::GetDC(hWnd);
if (SetWindowPixelFormat (hDC)==FALSE)
return 0;
if (CreateViewGLContext (hDC)==FALSE)
return 0;
return 0;
}
void COpenGLView::OnDestroy()
{
if(wglGetCurrentContext()!=NULL){
wglMakeCurrent(NULL,NULL);
}
if(m_hGLContext!=NULL){
wglDeleteContext(m_hGLContext);
m_hGLContext=NULL;
}
CView::OnDestroy();
// TODO: Add your message handler code here
}
void COpenGLView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
GLsizei width,heigth;
GLdouble aspect;
width=cx;
heigth=cy;
if(cy==0)
aspect=(GLdouble)width;
else
aspect=(GLdouble)width/(GLdouble)heigth;
glViewport(0,0,width,heigth);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,500.0*aspect,0.0,500.0);
//gluPerspective();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// TODO: Add your message handler code here
}void COpenGLView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
GLsizei width,heigth;
GLdouble aspect;
width=cx;
heigth=cy;
if(cy==0)
aspect=(GLdouble)width;
else
aspect=(GLdouble)width/(GLdouble)heigth;
glViewport(0,0,width,heigth);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,500.0*aspect,0.0,500.0);
//gluPerspective();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// TODO: Add your message handler code here
}
void COpenGLView::OnMenuitem2()
{
// TODO: Add your command handler code here
float angle=0.0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glRotatef(angle,1.0,0.0,1.0);
glBegin(GL_TRIANGLES);
glColor3f(0.0,0.0,1.0);
glVertex3f(100.0f,50.0f,0.0f);
glVertex3f(450.0f,400.0f,0.0f);
glVertex3f(150.0f,200.0f,0.0f);
glEnd();
glPopMatrix();
SwapBuffers(wglGetCurrentDC());
angle+=1.02;
glFlush();
}
//DDA直线
void COpenGLView::DDALine(int x1,int y1,int x2,int y2,int width)
{
int i,j;
float dx,dy,k,p;
dy=float(y2-y1);
dx=float(x2-x1);
if(dx!=0)
{
k=dy/dx;
if(k<=1&&k>=-1)
{
p=float(y1);
if(dx>0)
{
for(i=x1;i<=x2;i++)
{
for(j=-width/2;j<(width+1)/2;j++)
SetPixel(i,int(p+0.5)+j);
p=p+k;
}
}
else
{
for(i=x1;i>=x2;i--)
{
for(j=-width/2;j<(width+1)/2;j++)
SetPixel(i,int(p+0.5)+j);
p=p-k;
}
}
}
else
{
p=float(x1);
if(dy>0)
{
for(i=y1;i<=y2;i++)
{
for(j=-width/2;j<(width+1)/2;j++)
SetPixel(int(p+0.5)+j,i);
p=p+1/k;
}
}
else
{
for(i=y1;i>=y2;i--)
{
for(j=-width/2;j<(width+1)/2;j++)
SetPixel(int(p+0.5)+j,i);
p=p-1/k;
}
}
}
}
else
{
p=float(x1);
if(dy>0)
for(i=y1;i<=y2;i++)
for(j=-width/2;j<(width+1)/2;j++)
SetPixel(int(p)+j,i);
else
for(i=y1;i>=y2;i--)
for(j=-width/2;j<(width+1)/2;j++)
SetPixel(int(p)+j,i);
}
}
void COpenGLView::OnMenuitem0()
{
// TODO: Add your command handler code here
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT);
DDALine(11,11,450,450,1);
glFlush();
}
void COpenGLView::put(int x0,int y0,int x,int y){
SetPixel(x0+x,y0+y);
SetPixel(x0+x,y0-y);
SetPixel(x0-x,y0+y);
SetPixel(x0-x,y0-y);
SetPixel(x0+y,y0+x);
SetPixel(x0+y,y0-x);
SetPixel(x0-y,y0+x);
SetPixel(x0-y,y0-x);
}
void COpenGLView::OnMenuitem1()
{
// TODO: Add your command handler code here
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.0,0.0,0.0,0.0);
int x0=250;int y0=250;int r=150;
int x;int y;
float d;
x=0;
y=r;
d=1.25-r;
while(x<=y){
put(x0,y0,x,y);
if(d<0)
d+=x*2.0+3;
else{
d+=2.0*(x-y)+5;
y--;
}
x++;
}
glFlush();
}
void COpenGLView::OnMenuitem2()
{
// TODO: Add your command handler code here
float angle=0.0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glRotatef(angle,1.0,0.0,1.0);
glBegin(GL_TRIANGLES);
glColor3f(0.0,0.0,1.0);
glVertex3f(100.0f,50.0f,0.0f);
glVertex3f(450.0f,400.0f,0.0f);
glVertex3f(150.0f,200.0f,0.0f);
glEnd();
glPopMatrix();
SwapBuffers(wglGetCurrentDC());
angle+=1.02;
glFlush();
}