以文本方式查看主题

-  计算机科学论坛  (http://bbs.xml.org.cn/index.asp)
--  『 C/C++编程思想 』  (http://bbs.xml.org.cn/list.asp?boardid=61)
----  各位哥哥姐姐帮帮忙,一个关于OPENGL的问题  (http://bbs.xml.org.cn/dispbbs.asp?boardid=61&rootid=&id=41734)


--  作者:cai555444
--  发布时间:12/28/2006 9:14:00 PM

--  各位哥哥姐姐帮帮忙,一个关于OPENGL的问题
[QUOTE][QUOTE]我有一段编码,程序已经提示没有问题可是出来是空白的,怎么也没办法实现方向的控制请问是为什么??帮帮忙,谢谢大家!!!
// HomeworkView.cpp : implementation of the CHomeworkView class
//

#include "stdafx.h"
#include "Homework.h"
#include "cel.h"
#include "cesen.h"

#include "HomeworkDoc.h"
#include "HomeworkView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CHomeworkView

IMPLEMENT_DYNCREATE(CHomeworkView, CView)

BEGIN_MESSAGE_MAP(CHomeworkView, CView)
 //{{AFX_MSG_MAP(CHomeworkView)
 ON_WM_KEYDOWN()
 ON_WM_SIZE()
 ON_WM_TIMER()
 ON_WM_DESTROY()
 ON_WM_CREATE()
 ON_WM_CLOSE()
 //}}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()

/////////////////////////////////////////////////////////////////////////////
// CHomeworkView construction/destruction

CHomeworkView::CHomeworkView()
{
 // TODO: add construction code here

}

CHomeworkView::~CHomeworkView()
{
}

BOOL CHomeworkView::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);
}

/////////////////////////////////////////////////////////////////////////////
// CHomeworkView drawing

void CHomeworkView::OnDraw(CDC* pDC)
{
 CHomeworkDoc* pDoc = GetDocument();
 ASSERT_VALID(pDoc);
 // TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CHomeworkView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CHomeworkView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CHomeworkView message handlers


void CHomeworkView::OnSize(UINT nType, int cx, int cy)
{
 CView::OnSize(nType, cx, cy);
 
 // TODO: Add your message handler code here
/////////////////////////////////////////////////////////////////
//添加窗口缩放时的图形变换函数
 glViewport(0,0,cx,cy);
/////////////////////////////////////////////////////////////////
 GLdouble aspect_ratio;
 aspect_ratio = (GLdouble)cx/(GLdouble)cy;
 ::glMatrixMode(GL_PROJECTION);
 ::glLoadIdentity();
 gluPerspective(40.0F, aspect_ratio, 1.0F, 10000.0F);
 ::glMatrixMode(GL_MODELVIEW);
 ::glLoadIdentity();
 
}

void CHomeworkView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
/////////////////////////////////////////////////////////////////
//添加定时器响应函数和场景更新函数
 Invalidate(FALSE); 
/////////////////////////////////////////////////////////////////
 
 CView::OnTimer(nIDEvent);
}

void CHomeworkView::OnDestroy()
{
      CView::OnDestroy();
 
 // TODO: Add your message handler code here
  /////////////////////////////////////////////////////////////////
   //删除调色板和渲染上下文、定时器
 ::wglMakeCurrent(0,0);
 ::wglDeleteContext( m_hRC);
 if (m_hPalette)
     DeleteObject(m_hPalette);
 if ( m_pDC )
 {
  delete m_pDC;
 }
 KillTimer(1);  

 
}
/////////////////////////////////////////////////////////////////
 


int CHomeworkView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
 if (CView::OnCreate(lpCreateStruct) == -1)
  return -1;
 
 // TODO: Add your specialized creation code here
//////////////////////////////////////////////////////////////////
//初始化OpenGL和设置定时器
 m_pDC = new CClientDC(this);
 SetTimer(1,20, NULL);
 InitializeOpenGL(m_pDC);
//////////////////////////////////////////////////////////////////
 Init();  
 return 0;
}


void CHomeworkView::OnClose()
{
 // TODO: Add your message handler code here and/or call default
 
 CView::OnClose();
}


//////////////////////////////////////////////////////////
//      初始化openGL场景
//////////////////////////////////////////////////////////

BOOL CHomeworkView::InitializeOpenGL(CDC *pDC)
{
 m_pDC = pDC;
 SetupPixelFormat();
 //生成绘制描述表
 m_hRC = ::wglCreateContext(m_pDC->GetSafeHdc());
 //置当前绘制描述表
 ::wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC);

 return TRUE;
}
//////////////////////////////////////////////////////////
//      设置像素格式
//////////////////////////////////////////////////////////
BOOL CHomeworkView::SetupPixelFormat()
{
PIXELFORMATDESCRIPTOR pfd = {
    sizeof(PIXELFORMATDESCRIPTOR),    // pfd结构的大小
     1,                                // 版本号
     PFD_DRAW_TO_WINDOW |              // 支持在窗口中绘图
     PFD_SUPPORT_OPENGL |              // 支持 OpenGL
     PFD_DOUBLEBUFFER,                 // 双缓存模式
     PFD_TYPE_RGBA,                    // RGBA 颜色模式
     24,                               // 24 位颜色深度
     0, 0, 0, 0, 0, 0,                 // 忽略颜色位
     0,                                // 没有非透明度缓存
     0,                                // 忽略移位位
     0,                                // 无累加缓存
     0, 0, 0, 0,                       // 忽略累加位
     64,                               // 32 位深度缓存     
     0,                                // 无模板缓存
     0,                                // 无辅助缓存
     PFD_MAIN_PLANE,                   // 主层
     0,                                // 保留
     0, 0, 0                           // 忽略层,可见性和损毁掩模
 };  
 int pixelformat;
 pixelformat = ::ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd);//选择像素格式
 ::SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat, &pfd); //设置像素格式
 if(pfd.dwFlags & PFD_NEED_PALETTE)
  SetLogicalPalette(); //设置逻辑调色板
 return TRUE;
}
//////////////////////////////////////////////////////////
//      场景绘制与渲染
/////////////////////////////////////////////////////////

BOOL CHomeworkView::RenderScene()
{
::glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
 ::glMatrixMode(GL_MODELVIEW);
 ::glLoadIdentity();
  

::glRotatef( 7 ,1.0,0.0,0.0);


 ::glTranslatef(0.0,0.0,-0170.0);
   

 ::glPopMatrix();
 //车轮1 左后
 ::glPushMatrix();
  ::glTranslatef(23,-2.0,-43.0); 

  ::glRotatef(rotation ,1.0,0.0,0.0);
  glCallList(Model); 
 ::glPopMatrix();

 //车轮2 右前  //
 ::glPushMatrix();
  ::glTranslatef(-26.0,-2.0,34);
   ::glRotatef( rotation ,1.0,0.0,0.0);

  glCallList(Model);
 ::glPopMatrix();
 //车轮3 左前 //
 ::glPushMatrix();
  ::glTranslatef(24.0,-1.0,35);
         ::glRotatef( rotation ,1.0,0.0,0.0);
  glCallList(Model);
 ::glPopMatrix();
// 车轮4右后
 ::glPushMatrix();
  ::glTranslatef(-26.0,-3.0,-32);
  ::glRotatef( rotation ,1.0,0.0,0.0);//车轮的旋转
  ::glCallList(Model); 
    ::glPopMatrix();

 rotation = (rotation   +5)   % 360; 

//车身
 ::glPushMatrix();
 ::glTranslatef(0.0,0.0,0.0);
     glCallList(Model2); 
  ::glPushMatrix();


 ::glPopMatrix();
 

 ::SwapBuffers(m_pDC->GetSafeHdc());  //交互缓冲区
 return TRUE;
}

void CHomeworkView::Init(GLvoid)
{ ::glShadeModel(GL_SMOOTH);
 ::glClearColor(0.0F, 0.0F, 0.0F, 0.0F);

 ::glClearDepth(1.0F);
  glEnable(GL_DEPTH_TEST);
  glDepthFunc(GL_LESS);
  glEnable(GL_CULL_FACE);
  glCullFace(GL_BACK);
  glFrontFace(GL_CCW);
  glDisable(GL_DITHER);

 GLfloat ambientLight[] = { 0.3f, 0.3f, 0.3f, 1.0f};
 GLfloat diffuseLight[] = { 0.7f, 0.7f, 0.7f, 1.0f};
 GLfloat lightPos[]     = {6000.0f,6000.0f,6000.0f, 1.0f};


 glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
 glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
 glEnable(GL_LIGHTING);
 glEnable(GL_LIGHT0);
 
   glMateriali(GL_FRONT, GL_SHININESS, 128);

 //Model = GL3DS_initialize_DC10();
 Model=GL3DS_initialize_cel();
 Model2=GL3DS_initialize_cesen();
   
  if (Model ==-1)
 {
  printf("Error initializing display list\n");
  exit (-1);
 }

 rotation =0;
}


void CHomeworkView::SetLogicalPalette()
{
 struct
    {
        WORD Version;
        WORD NumberOfEntries;
        PALETTEENTRY aEntries[256];
    } logicalPalette = { 0x300, 256 };

 BYTE reds[] = {0, 36, 72, 109, 145, 182, 218, 255};
 BYTE greens[] = {0, 36, 72, 109, 145, 182, 218, 255};
 BYTE blues[] = {0, 85, 170, 255};

    for (int colorNum=0; colorNum<256; ++colorNum)
    {
        logicalPalette.aEntries[colorNum].peRed =
            reds[colorNum & 0x07];
        logicalPalette.aEntries[colorNum].peGreen =
            greens[(colorNum >> 0x03) & 0x07];
        logicalPalette.aEntries[colorNum].peBlue =
            blues[(colorNum >> 0x06) & 0x03];
        logicalPalette.aEntries[colorNum].peFlags = 0;
    }

    m_hPalette = CreatePalette ((LOGPALETTE*)&logicalPalette);

}
//////////////////////////////////////////////////////////////////

/////////////////////////////
static  shoulder = 90;
GLfloat elbow[4] = {45,45,45,45};

//////////////////////////////////////
void CHomeworkView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
 // TODO: Add your message handler code here and/or call default
 
 CView::OnKeyDown(nChar, nRepCnt, nFlags);
 switch(nChar)
 { 
 case AUX_LEFT:
   shoulder = (shoulder + 5);
 break;
 case AUX_RIGHT:
   shoulder = (shoulder - 5);
 break;
 case AUX_UP:
      shoulder = (shoulder + 5);
 break;
 case AUX_DOWN:
     shoulder = (shoulder - 5);
     break;
 }
}



--  作者:卷积内核
--  发布时间:12/29/2006 10:16:00 AM

--  
static  shoulder = 90;
你把这个改成非static类型看看,应该是这里的问题。
--  作者:一分之千
--  发布时间:12/31/2006 8:54:00 PM

--  
hehe,  u made a mistake,u should set the opengl drawing function in the ondraw function of the homeworkview. In the OnDraw function of the view class cpp u did nothing.
--  作者:qduxiao
--  发布时间:5/8/2007 4:58:00 PM

--  
缺少按下键盘的宏定义,也就是不会产生任何按键消息!
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
78.125ms