MFC对话框显示不出来立方体的贴图!(OpenGL)

LeoSame 2015-04-13 04:53:45
我用VC++制作一个对话框,想在一个对话框上显示一个旋转的立方体贴图,但是为什么只有立方体,而没有贴图呢?

我的贴图是这样的:

下面是我的这个对话框的类的.cpp代码:
// locks.cpp : implementation file
//
#include "stdafx.h"
#include "user.h"
#include "locks.h"
#include "OpenGL1.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// Clocks dialog


Clocks::Clocks(CWnd* pParent /*=NULL*/)
: CDialog(Clocks::IDD, pParent)
{
//{{AFX_DATA_INIT(Clocks)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}


void Clocks::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(Clocks)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(Clocks, CDialog)
//{{AFX_MSG_MAP(Clocks)
ON_WM_SIZE()
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// Clocks message handlers

void Clocks::OnSize(UINT nType, int cx, int cy)
{
copengl.Reshap(cx,cy);
CDialog::OnSize(nType, cx, cy);

}

void Clocks::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
copengl.Render();
CDialog::OnTimer(nIDEvent);
}

BOOL Clocks::OnInitDialog()
{
CDialog::OnInitDialog();

/************************************************************************/
/* 传递给SetupPixelFormat的参数是要绘图的对象的DC,可以是窗口中的某一个控件
这里使用的是窗口的DC,即在窗口中绘图,而不是在窗口的某个控件中绘图*/
/************************************************************************/
copengl.SetupPixelFormat(::GetDC(m_hWnd));
//得到绘图区域对应的长方形
CRect rect;
GetClientRect(&rect);

//设置绘图区域的宽和高
copengl.Init(rect.right,rect.bottom);
SetTimer(1,10,0);
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}

下面是我定义的OpenGL类的cpp代码:
// OpenGL1.cpp: implementation of the OpenGL class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "user.h"
#include "OpenGL1.h"
#pragma comment(lib,"glaux.lib")
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
int a=0;
GLuint g_texture = 0;

COpenGL::COpenGL(void)
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); //设置显示模式为双缓冲,RGEBA
}

COpenGL::~COpenGL(void)
{
wglMakeCurrent(hDC,NULL);
wglDeleteContext(hRC);
}

bool COpenGL::SetupPixelFormat(HDC hDC0){
int nPixelFormat; // 象素点格式
hDC=hDC0;
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, // 忽略累加位
32, // 32 位深度缓存
0, // 无模板缓存
0, // 无辅助缓存
PFD_MAIN_PLANE, // 主层
0, // 保留
0, 0, 0 // 忽略层,可见性和损毁掩模
};
if (!(nPixelFormat = ChoosePixelFormat(hDC, &pfd)))
{
MessageBox(NULL,_T("can not find proper mode"),_T("Error"),MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
SetPixelFormat(hDC,nPixelFormat,&pfd);
hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC);
return TRUE;
}
void COpenGL::Reshap(int width,int height)
{
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective
( 60.0f,
(GLfloat)width/(GLfloat)height,
0.1f,
100.0f
);
//gluLookAt(10,5,10,0,0,0,0,1,0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void COpenGL::Render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // 清楚颜色数据和深度数据(清屏)
//glColor3f(1.0, 0.0, 0.0);
glLoadIdentity();
glTranslatef(0.0, 0.0, -5.0);
glScalef(2,2,2);

glBindTexture(GL_TEXTURE_2D, g_texture); //使用贴图纹理
glPushMatrix();
//glRotatef(a++,0,1,0);

glBindTexture(GL_TEXTURE_2D, g_texture); //使用贴图纹理
glBegin(GL_QUADS); //顶面
glNormal3f(0.0f,1.0f,0.0f);
glVertex3f(0.5f,0.5f,0.5f);
glVertex3f(0.5f,0.5f,-0.5f);
glVertex3f(-0.5f,0.5f,-0.5f);
glVertex3f(-0.5f,0.5f,0.5f);
glEnd();

glBindTexture(GL_TEXTURE_2D, g_texture); //使用贴图纹理
glBegin(GL_QUADS); //底面
glNormal3f(0.0f,-1.0f,0.0f);
glVertex3f(0.5f,-0.5f,0.5f);
glVertex3f(-0.5f,-0.5f,0.5f);
glVertex3f(-0.5f,-0.5f,-0.5f);
glVertex3f(0.5f,-0.5f,-0.5f);
glEnd();

glBindTexture(GL_TEXTURE_2D, g_texture); //使用贴图纹理
glBegin(GL_QUADS); //前面
glNormal3f(0.0f,0.0f,1.0f);
glVertex3f(0.5f,0.5f,0.5f);
glVertex3f(-0.5f,0.5f,0.5f);
glVertex3f(-0.5f,-0.5f,0.5f);
glVertex3f(0.5f,-0.5f,0.5f);
glEnd();

glBindTexture(GL_TEXTURE_2D, g_texture); //使用贴图纹理
glBindTexture(GL_TEXTURE_2D, g_texture); //使用贴图纹理
glBegin(GL_QUADS); //背面
glNormal3f(0.0f,0.0f,-1.0f);
glVertex3f(0.5f,0.5f,-0.5f);
glVertex3f(0.5f,-0.5f,-0.5f);
glVertex3f(-0.5f,-0.5f,-0.5f);
glVertex3f(-0.5f,0.5f,-0.5f);
glEnd();

glBindTexture(GL_TEXTURE_2D, g_texture); //使用贴图纹理
glBegin(GL_QUADS); //左面
glNormal3f(-1.0f,0.0f,0.0f);
glVertex3f(-0.5f,0.5f,0.5f);
glVertex3f(-0.5f,0.5f,-0.5f);
glVertex3f(-0.5f,-0.5f,-0.5f);
glVertex3f(-0.5f,-0.5f,0.5f);
glEnd();

glBindTexture(GL_TEXTURE_2D, g_texture); //使用贴图纹理

glBegin(GL_QUADS); //右面
glNormal3f(1.0f,0.0f,0.0f);
glVertex3f(0.5f,0.5f,0.5f);
glVertex3f(0.5f,-0.5f,0.5f);
glVertex3f(0.5f,-0.5f,-0.5f);
glVertex3f(0.5f,0.5f,-0.5f);
glEnd();

glPopMatrix();
glFlush();
SwapBuffers(hDC);

//glutSwapBuffers(); //交换缓冲区。显示图形

}

int COpenGL::LoadGLTextures(GLuint &unTexture, const char *chFileName)
{
AUX_RGBImageRec *TextureImage; //保存贴图数据的指针
TextureImage = auxDIBImageLoad("data/tu.bmp"); //载入贴图数据
glGenTextures(1, &unTexture); // 创建一个纹理,unTexture

glBindTexture(GL_TEXTURE_2D, unTexture); //绑定纹理,然后对该纹理区添加纹理数据

//设置纹理的信息,
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage->sizeX, TextureImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //设置滤波为线性滤波
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //线性滤波

if (TextureImage) //释放资源
{
if (TextureImage->data)
{
free(TextureImage->data);
}
free(TextureImage);
}
return 1;
}

void COpenGL::Init(int width, int height)
{
GLfloat mat_specular[]={1.0,1.0,1.0,1.0};
GLfloat mat_shininess[]={500.0};
GLfloat light_position[]={1.0,1.0,1.0,0.0};
GLfloat white_light[]={1.0,1.0,1.0,1.0};
GLfloat lmodel_ambient[]={0.1,0.1,0.1,1.0};
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
glLightfv(GL_LIGHT0,GL_DIFFUSE,white_light);
glLightfv(GL_LIGHT0,GL_SPECULAR,white_light);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,lmodel_ambient);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);

glCullFace(GL_BACK); //背面裁剪(背面不可见)
glEnable(GL_CULL_FACE); //启用裁剪
glEnable(GL_TEXTURE_2D);
LoadGLTextures(g_texture, "Data/tu.bmp"); //载入纹理贴图

//glViewport(0, 0, 200, 200);
glMatrixMode(GL_PROJECTION);
gluPerspective(60, (GLfloat)width/(GLfloat)height, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

求正解!
...全文
295 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaozi199103 2015-06-03
  • 打赏
  • 举报
回复
你好,请问你这个问题是怎么解决的?我也遇到的相同的问题
LeoSame 2015-04-19
  • 打赏
  • 举报
回复
我弄好了,谢谢各位
LeoSame 2015-04-14
  • 打赏
  • 举报
回复
谁能给我一个不让我那么辛苦的答案?我已经一天没有离开电脑前了。。。求救
赵4老师 2015-04-14
  • 打赏
  • 举报
回复
搜网络教程“学OpenGL编3D游戏”。

15,979

社区成员

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

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