以下有段OpenGL代码,但是不能生成预想的纹理效果,请高手帮忙看看

blldw 2006-09-23 02:25:14
#include <gl/glut.h>
#include <gl/glaux.h>
#include <windows.h>
#include <iostream>
#include <cmath>

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

#define PI 3.14159265853

static GLdouble angleY = 0.0;
static GLdouble dRange = 5.0;
static GLdouble angleEle = 0.0;
static GLdouble angleXZ = 0.0;
static bool bAnimate = false;

GLuint texName[6];
GLuint tex;

void LoadTex(char *filename)
{

glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);

AUX_RGBImageRec *pImage = NULL;
pImage = auxDIBImageLoad("setup.bmp");
if (pImage == NULL)
{
std::cout << "error loading bitmap" << std::endl;
return;
}

gluBuild2DMipmaps(GL_TEXTURE_2D, 4, pImage->sizeX, pImage->sizeY,
GL_RGB, GL_UNSIGNED_BYTE, pImage->data);
free(pImage->data);
free(pImage);
}

void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);

LoadTex("1.bmp");
/*
glGenTextures(6, texName);

LoadTex("2.bmp", texName + 1);
LoadTex("3.bmp", texName + 2);
LoadTex("4.bmp", texName + 3);
LoadTex("5.bmp", texName + 4);
LoadTex("6.bmp", texName + 5);
*/
}

void idle()
{
static DWORD dwLastTime = GetTickCount();
DWORD dwCur = 0;

do {
dwCur = GetTickCount();
} while(dwCur - dwLastTime < (1000 / 30));
dwLastTime = dwCur;

angleY += 1.0;
glutPostRedisplay();
}

void reshape(GLsizei width, GLsizei height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)width/(double)height, 0.0, 10.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 'w':
dRange += 1.0;
break;

case 's':
dRange -= 1.0;
break;

case 'a':
angleXZ -= 1.0;
break;

case 'd':
angleXZ += 1.0;
break;

case 'q':
angleEle += 1.0;
break;

case 'r':
angleEle -= 1.0;
break;

case ' ':
bAnimate = !bAnimate;
break;

default:
std::cout << key << std::endl;
break;
}
if (bAnimate)
glutIdleFunc(idle);
else
glutIdleFunc(NULL);

glutPostRedisplay();
}

void display()
{
glLoadIdentity();
gluLookAt(dRange*cosf(angleXZ*PI/180.0),
dRange*sinf(angleEle*PI/180.0),
dRange*sinf(angleXZ*PI/180.0),
0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glPushMatrix();
// draw a cubic
glRotatef(angleY, 0.0f, 1.0f, 0.0f);

glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_QUADS);
// right side
//glBindTexture(GL_TEXTURE_2D, texName[0]);
glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f, 1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f);
glEnd();
/*
glBegin(GL_QUADS);
// back side
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
// left side
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
// front side
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
// top side
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
// bottom side
glColor3f(0.0f, 1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glEnd();
*/
//glutWireCube(1.0);
glPopMatrix();
glFlush();
glutSwapBuffers();
glDisable(GL_TEXTURE_2D);
}

int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
init();
glutCreateWindow(argv[0]);
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
//glutIdleFunc(idle);
glutMainLoop();

return 0;
}

注:setup.bmp文件在工程目录下
...全文
183 点赞 收藏 4
写回复
4 条回复
Silent_Spring 2006年10月01日
1.没通过编译?
---LoadTex声明不正确: void LoadTex(char* filename)
2.6个面都是一个纹理?
---你需要在绘制每个面时重新指定纹理,用BindTexture.

我暂时发现这些

blog.csdn.net/silent_spring
回复 点赞
rongrongshen 2006年09月26日
setup.bmp 图片大小是否符合要求?
回复 点赞
blldw 2006年09月26日
这个与图片的大小关系大吗?
我在另一个程序里用的也是这张setup.bmp图片,没有问题。
回复 点赞
blldw 2006年09月25日
up!
回复 点赞
发动态
发帖子
机器视觉
创建于2007-08-27

2581

社区成员

1.4w+

社区内容

图形图像/机器视觉
社区公告
暂无公告