opengl中球体纹理映射问题

manye_lby 2009-03-30 05:11:49
我在MFC中用opengl制作了一个旋转的球体,想把一幅bmp位图作为纹理映射到球体表面,球体需要旋转。如何实现?
仿书上的例子添加了纹理映射的程序,程序没有报错,却无法实现纹理,与之前的一样。请高手为我想想问题出在哪里?
或者给我发一个已有的程序吧!
谢谢了^^
...全文
329 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
david_zhaowl 2011-12-06
  • 打赏
  • 举报
回复
请问楼主的问题解决了吗?我也碰到球体纹理没法正确加载的问题。
「已注销」 2009-07-03
  • 打赏
  • 举报
回复
#include <windows.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>
#include <stdio.h>
#include <io.h>

HGLRC hRC=NULL;
HDC hDC=NULL;
HWND hWnd=NULL;
HINSTANCE hInstance;
RECT rect;
int sw=640;
int sh=480;
bool fullscreen=1;
GLfloat aspect;
GLfloat xrot;
GLfloat yrot;
bool light;
bool blend;
bool lp;
bool fp;
bool bp;
//GLfloat lightAmbient[]={0.5f,0.5f,0.5f,1.0f};
GLfloat whiteLight[]={1.0f,1.0f,1.0f,1.0f};
GLfloat lmLightAmbient[]={0.5f,0.5f,0.5f,1.0f};
GLfloat lightDiffuse[]={1.0f,1.0f,1.0f,1.0f};
GLfloat lightPosition[]={0.0f,0.0f,2.0f,1.0f};
GLuint texture[1];

#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glu32.lib")
#pragma comment(lib,"glaux.lib")

AUX_RGBImageRec *LoadBMP(char *FileName)
{
FILE *File=NULL;
if(!FileName)
return NULL;
File=fopen(FileName,"r");
if(File){
fclose(File);
return auxDIBImageLoad(FileName);
}
return NULL;
}

int LoadGLTextures()
{
bool Status=false;
AUX_RGBImageRec *TextureImage[1];
memset(TextureImage,0,sizeof(void *)*1);
if(TextureImage[0]=LoadBMP("Tex/redmouse.bmp"))
{
Status=true;
glGenTextures(1,&texture[0]);
glBindTexture(GL_TEXTURE_2D,texture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
gluBuild2DMipmaps(GL_TEXTURE_2D,3,TextureImage[0]->sizeX,TextureImage[0]->sizeY,GL_RGB,GL_UNSIGNED_BYTE,TextureImage[0]->data);
}
if(TextureImage[0]){
if(TextureImage[0]->data){
free(TextureImage[0]->data);
}
free(TextureImage[0]);
}
return Status;
}


void SceneInit(int w,int h)
{
LoadGLTextures();
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f,0.0f,0.0f,0.5f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
// glLightfv(GL_LIGHT0,GL_AMBIENT,lightAmbient);
glLightfv(GL_LIGHT0,GL_SPECULAR,whiteLight);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,lmLightAmbient);
glLightfv(GL_LIGHT0,GL_DIFFUSE,lightDiffuse);
glLightfv(GL_LIGHT0,GL_POSITION,lightPosition);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glColor4f(1.0f,1.0f,1.0f,0.5f);
glBlendFunc(GL_SRC_ALPHA,GL_ONE);
}

void SceneResizeViewport(GLsizei w,GLsizei h)
{
if(h==0)
{
h=1;
}

aspect=(GLfloat)w/(GLfloat)h;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(45.0f,aspect,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void SceneShow(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-6.0f);
glPushMatrix();
glRotatef(xrot,1.0f,0.0f,0.0f);
glRotatef(yrot,0.0f,1.0f,0.0f);
glColor3f(1.0f,1.0f,1.0f);
glBindTexture(GL_TEXTURE_2D,texture[0]);
GLUquadricObj* qObj=gluNewQuadric();
gluQuadricTexture(qObj,GL_TRUE);
gluSphere(qObj,1.0,20,20);
glPopMatrix();
xrot+=0.5f;
yrot+=0.3f;
}

void EnableOpenGL()
{
PIXELFORMATDESCRIPTOR pfd;
int iFormat;
hDC=GetDC(hWnd);
ZeroMemory(&pfd,sizeof(pfd));
pfd.nSize=sizeof(pfd);
pfd.nVersion=1;
pfd.dwFlags=PFD_DRAW_TO_WINDOW|
PFD_SUPPORT_OPENGL|
PFD_DOUBLEBUFFER;
pfd.iPixelType=PFD_TYPE_RGBA;
pfd.cColorBits=16;
pfd.cDepthBits=16;
pfd.iLayerType=PFD_MAIN_PLANE;
iFormat=ChoosePixelFormat(hDC,&pfd);
SetPixelFormat(hDC,iFormat,&pfd);
hRC=wglCreateContext(hDC);
wglMakeCurrent(hDC,hRC);
}

void DisableOpenGL()
{
wglMakeCurrent(NULL,NULL);
wglDeleteContext(hRC);
ReleaseDC(hWnd,hDC);
}

bool ChangeResolution(int w,int h, int bitDepth)
{
DEVMODE devMode;
int modeSwith;
int closeMode=0;
EnumDisplaySettings(NULL,closeMode,&devMode);
devMode.dmBitsPerPel=bitDepth;
devMode.dmPelsWidth=w;
devMode.dmPelsHeight=h;
devMode.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
modeSwith=ChangeDisplaySettings(&devMode,CDS_FULLSCREEN);
if(modeSwith==DISP_CHANGE_SUCCESSFUL)
{
return true;
}else{
ChangeDisplaySettings(NULL,0);
return false;
}
}


LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_CREATE:
GetWindowRect(hWnd,&rect);
sw=rect.right-rect.left;
sh=rect.bottom-rect.top;
SceneResizeViewport(sw,sh);
return 0;
case WM_SIZE:
if(!fullscreen){
GetWindowRect(hWnd,&rect);
sw=rect.right-rect.left;
sh=rect.bottom-rect.top;
if(sw>0&&sh>0)
SceneResizeViewport(sw,sh);
}else{
SceneResizeViewport(GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN));
}
return 0;
case WM_CLOSE:
ShowWindow(hWnd,SW_HIDE);
PostQuitMessage(0);
return 0;
case WM_DESTROY:
return 0;
case WM_KEYDOWN:
switch(wParam){
case VK_ESCAPE:
PostMessage(hWnd,WM_CLOSE,wParam,lParam);
break;
case 'B':
if(!bp){
bp=true;
blend=!blend;
if(blend)
{
glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
}else{
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
}
}else{
bp=false;
}
break;
}
return 0;
default:
return DefWindowProc(hWnd,message,wParam,lParam);
}
}
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
static TCHAR szAppName[]=TEXT("HelloWin!");
WNDCLASS wc;
MSG msg;
bool bQuit=false;
if(MessageBox(NULL,"是否选择全屏显示模式?","全屏模式运行?",MB_YESNO|MB_ICONQUESTION)==IDNO)
{
fullscreen=0;
}
wc.style=CS_OWNDC;
wc.lpfnWndProc=(WNDPROC)WndProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInstance;
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName=NULL;
wc.lpszClassName=szAppName;
RegisterClass(&wc);
if(fullscreen){
ChangeResolution(640,480,16);
hWnd=CreateWindow(szAppName,TEXT("Game"),WS_POPUP|WS_CLIPSIBLINGS|WS_VISIBLE,0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN),NULL,NULL,hInstance,NULL);
}else{
hWnd=CreateWindow(szAppName,TEXT("Game"),WS_TILEDWINDOW|WS_VISIBLE,GetSystemMetrics(SM_CXSCREEN)/2-sw/2,GetSystemMetrics(SM_CYSCREEN)/2-sh/2,sw,sh,NULL,NULL,hInstance,NULL);
ChangeDisplaySettings(NULL,0);
}
ShowWindow(hWnd,SW_SHOW);
UpdateWindow(hWnd);

EnableOpenGL();
SceneInit(sw,sh);
if(!fullscreen){
GetWindowRect(hWnd,&rect);
sw=rect.right-rect.left;
sh=rect.bottom-rect.top;
if(sw>0&&sh>0)
SceneResizeViewport(sw,sh);
}else{
SceneResizeViewport(GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN));
}
while(!bQuit){
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
if(msg.message==WM_QUIT)
bQuit=true;
else{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else{
SceneShow();
SwapBuffers(hDC);
}
}
DisableOpenGL();
ShowWindow(hWnd,SW_HIDE);
DestroyWindow(hWnd);
ChangeDisplaySettings(NULL,0);
return msg.wParam;
return 0;
}

zzz822163 2009-03-30
  • 打赏
  • 举报
回复
http://www.owlei.com/DancingWind/Course/Tutorial_23.htm

19,468

社区成员

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

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