帮忙看看哪有问题

时间:2022-02-19 21:23:44
class COpenGL : public CStatic
{
// Construction
public:
COpenGL();

// Attributes
public:

// Operations
public:

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(COpenGL)
//}}AFX_VIRTUAL

// Implementation
public:
void RenderScene();
void LoadTexture();
static DWORD WINAPI fnThread(LPVOID lpParameter);
void play();
virtual ~COpenGL();

// Generated message map functions
protected:
DWORD dwThread;
HANDLE hThread;
//{{AFX_MSG(COpenGL)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
//}}AFX_MSG

DECLARE_MESSAGE_MAP()
private:
HWND m_hWnd;
GLuint texture[1];
HGLRC hglrc;
GLfloat yrot;
GLfloat xrot;
};

#include "stdafx.h"
#include "opengl.h"
#include "OpenGL1.h"

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

/////////////////////////////////////////////////////////////////////////////
// COpenGL

COpenGL::COpenGL()
{
}

COpenGL::~COpenGL()
{
}


BEGIN_MESSAGE_MAP(COpenGL, CStatic)
//{{AFX_MSG_MAP(COpenGL)
ON_WM_CREATE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COpenGL message handlers

void COpenGL::play()
{
hThread=CreateThread(NULL, 0, fnThread, this, 0, &dwThread);
}

DWORD WINAPI COpenGL::fnThread(LPVOID lpParameter)
{
COpenGL* window=(COpenGL*)lpParameter;

HDC hDC=::GetDC(window->m_hWnd);
RECT rect;
::GetWindowRect(window->m_hWnd, &rect);
  LONG width=rect.right-rect.left;
  LONG height=rect.bottom-rect.top;
 
     PIXELFORMATDESCRIPTOR pfd=
  {
  sizeof(PIXELFORMATDESCRIPTOR),
  1,
  PFD_DRAW_TO_WINDOW |
  PFD_SUPPORT_OPENGL |
  PFD_DOUBLEBUFFER,
  PFD_TYPE_RGBA,
  16,
  0, 0, 0, 0, 0, 0,
  0,
  0,
  0,
  0, 0, 0, 0,
  16,
  0,
  0,
  PFD_MAIN_PLANE,
  0,
  0, 0, 0
  }; 
 
     int pixelformat; 
     pixelformat = ChoosePixelFormat(hDC, &pfd);
     SetPixelFormat(hDC, pixelformat, &pfd);
 
     HGLRC hglrc = wglCreateContext(hDC);
     wglMakeCurrent(hDC, hglrc);
     
  glViewport(0,0,width,height);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(45.0f,(GLfloat)width/(GLfloat)height, 1 ,150.0f);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
 
window->LoadTexture();
 
  glEnable(GL_TEXTURE_2D);
  glShadeModel(GL_SMOOTH);
  glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0f);
  glDepthFunc(GL_LEQUAL);
  glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE);
while(1)
{
window->RenderScene();
Sleep(10);
SwapBuffers(hDC);
}

}

void COpenGL::LoadTexture()
{
AUX_RGBImageRec *TextureImage[1];
memset(TextureImage, 0, sizeof(void *)*1);

if(TextureImage[0]=auxDIBImageLoad("res/amd.bmp"))
{
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY,\
0, GL_RGB, GL_UNSIGNED_INT, TextureImage[0]->data);
}
if(TextureImage[0])
{
free(TextureImage[0]->data);
free(TextureImage[0]);
}
}

void COpenGL::RenderScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,0.0f, -5.0f);

glRotatef(xrot,1.0f,0.0f,0.0f);
glRotatef(yrot,0.0f,1.0f,0.0f);

glBindTexture(GL_TEXTURE_2D, texture[0]);

glBegin(GL_QUADS);
glNormal3f( 0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);

glNormal3f( 0.0f, 0.0f,-1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);

glNormal3f( 0.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);

glNormal3f( 0.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);

glNormal3f( 1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);

glNormal3f(-1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
glEnd();

xrot+=0.02;
yrot+=0.02;
if(xrot>1.0f)
xrot=0.0f;
if(yrot>1.0f)
yrot=0.0f;
}

int COpenGL::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
if (CStatic::OnCreate(lpCreateStruct) == -1)
return -1;
m_hWnd=GetSafeHwnd();
return 0;
}

6 个解决方案

#1


这看得多头晕啊,怎么也说说现象,和你跟踪的结果吧!~

#2


编译有错??还是程序代码执行结果有错呢??

#3


这样怎么帮你看啊,贴点重要点的

#4


肯定是其他地方出了问题,我遇到过的,你仔细查查...

#5


一頭霧水.

#6


在你的另一个帖子里我已经给你了一个完整的例子,
你按照我的提示一步步做就可以了,
实在不行就给我留言,我把我编译好的例子发给你.

#1


这看得多头晕啊,怎么也说说现象,和你跟踪的结果吧!~

#2


编译有错??还是程序代码执行结果有错呢??

#3


这样怎么帮你看啊,贴点重要点的

#4


肯定是其他地方出了问题,我遇到过的,你仔细查查...

#5


一頭霧水.

#6


在你的另一个帖子里我已经给你了一个完整的例子,
你按照我的提示一步步做就可以了,
实在不行就给我留言,我把我编译好的例子发给你.