关于opengl一维纹理的问题

basuye1 2012-09-22 03:47:01
照书上打的,但是没有出现一维纹理的效果。

//EXAM805.h

#if !defined _EXAM805_H_
#define _EXAM805_H_

#include <gl/glut.h>

#define IMAGEWIDTH 32


GLubyte Image[ 4 * IMAGEWIDTH ];
GLuint texName;
GLfloat xzero[] = { 1.0f, 0.0f, 0.0f, 0.0f };
GLfloat slanted[] = { 1.0f, 1.0f, 1.0f, 0.0f };
GLfloat *curCoeff;
GLenum curPlane;
GLint curGenMode;

void Initialization( void );
void OnDisplay( void );
void OnReshape( int w, int h );
void OnKey( unsigned char key, int x, int y );
void CreateImage( void );
void SetupLights( void );

#endif


//main.cpp
#include "EXAM805.h"

void main( int argc, char* argv[] )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
glutInitWindowSize( 640, 480 );
glutCreateWindow( "EXAM805" );


glutReshapeFunc( OnReshape );
glutDisplayFunc( OnDisplay );
glutKeyboardFunc( OnKey );

Initialization( );

glutMainLoop( );
}

void Initialization( void )
{
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glEnable( GL_DEPTH_TEST );
glShadeModel( GL_SMOOTH );

CreateImage( );
glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );

glGenTextures( 1, &texName );
glBindTexture( GL_TEXTURE_1D, texName );
glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexImage1D( GL_TEXTURE_ENV, 0, GL_RGBA, IMAGEWIDTH, 0, GL_RGBA, GL_UNSIGNED_BYTE, Image );
glTexEnvf( GL_TEXTURE_1D, GL_TEXTURE_ENV_MODE, GL_MODULATE );

curCoeff = xzero;
curGenMode = GL_OBJECT_LINEAR;
curPlane = GL_OBJECT_PLANE;
glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, curGenMode );
glTexGenfv( GL_S, curPlane, curCoeff );

SetupLights( );

glEnable( GL_TEXTURE_GEN_S );
glEnable( GL_TEXTURE_1D );
glEnable( GL_CULL_FACE );
glEnable( GL_AUTO_NORMAL );
glEnable( GL_NORMALIZE );
glFrontFace( GL_CW );
glCullFace( GL_BACK );
}

void OnDisplay( void )
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

glPushMatrix( );
glRotatef( 30.0f, 0.0f, 0.0f, 1.0f );
glRotatef( 10.0f, 1.0f, 0.0f, 0.0f );

glBindTexture( GL_TEXTURE_1D, texName );

glutSolidTeapot( 2.0f );

glPopMatrix( );

//glFlush( );
glutSwapBuffers( );
}

void OnReshape( int w, int h )
{
glViewport( 0, 0, GLsizei( w ), GLsizei( h ) );
glMatrixMode( GL_PROJECTION );
glLoadIdentity( );

if( w <= h )
glOrtho( -5.0f, 5.0f, -5.0f * GLfloat( h ) / GLfloat ( w ), 5.0f * GLfloat ( h ) / GLfloat( w ), -5.0f, 5.0f );
else
glOrtho( -5.0f * GLfloat( w ) / GLfloat ( h ), 5.0f * ( w ) / GLfloat( h ), -5.0f , 5.0f , -5.0f, 5.0f );

glMatrixMode( GL_MODELVIEW );
glLoadIdentity( );
}

void OnKey( unsigned char key, int x, int y )
{
switch( key )
{
case 'e':
case 'E':
curGenMode = GL_EYE_LINEAR;
curPlane = GL_EYE_PLANE;
glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, curGenMode );
glTexGenfv( GL_S, curPlane, curCoeff );

break;

case 'o':
case 'O':
curGenMode = GL_OBJECT_LINEAR;
curPlane = GL_OBJECT_PLANE;
glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, curGenMode );
glTexGenfv( GL_S, curPlane, curCoeff );

break;

case 's':
case 'S':
curCoeff = slanted;
glTexGenfv( GL_S, curPlane, curCoeff );

break;

case 'x':
case 'X':
curCoeff = xzero;
glTexGenfv( GL_S, curPlane, curCoeff );
}

glutPostRedisplay( );
}

void CreateImage( void )
{
int j;

for( j = 0; j < IMAGEWIDTH; j ++ )
{
Image[4 * j] = GLubyte( ( j <= 4 ) ? 255 : 0 );
Image[4 * j + 1] = GLubyte( ( j > 4 ) ? 255 : 0 );
Image[4 * j + 2] = GLubyte( 0 );
Image[4 * j + 3] = GLubyte( 255 );
}
}


void SetupLights( )
{
GLfloat ambientLight[ ] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat diffuseLight[ ] = { 0.9f, 0.9f, 0.9f, 1.0f };
GLfloat specularLight[ ] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat lightPos[ ] = { 5.0f, 10.0f, 30.0f, 1.0f };

glEnable( GL_LIGHTING );
glLightfv( GL_LIGHT0, GL_AMBIENT, ambientLight );
glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuseLight );
glLightfv( GL_LIGHT0, GL_SPECULAR, specularLight );
glLightfv( GL_LIGHT0, GL_POSITION, lightPos );
glEnable( GL_LIGHT0 );

glEnable( GL_COLOR_MATERIAL );
glColorMaterial( GL_FRONT, GL_AMBIENT_AND_DIFFUSE );
glMaterialfv( GL_FRONT, GL_SPECULAR, specularLight );
glMateriali( GL_FRONT, GL_SHININESS, 100 );
}

求高手解答下,谢谢
...全文
111 点赞 收藏 3
写回复
3 条回复
basuye1 2012年09月25日
问题解决了,感谢二楼把这么长串没注释的代码都看下来了。O(∩_∩)O~
回复 点赞
kxf87988465 2012年09月24日
这么长的代码,抄的眼都花了
回复 点赞
the_venus 2012年09月24日
glTexImage1D( GL_TEXTURE_ENV, 0, GL_RGBA, IMAGEWIDTH, 0, GL_RGBA, GL_UNSIGNED_BYTE, Image );
glTexEnvf( GL_TEXTURE_1D, GL_TEXTURE_ENV_MODE, GL_MODULATE );

这两句错了好不好,改成下面的:
glTexImage1D(GL_TEXTURE_1D,0,GL_RGBA,IMAGEWIDTH,0,GL_RGBA,GL_UNSIGNED_BYTE,Image);
glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);

自己的粗心,没有办法了
回复 点赞
发动态
发帖子
图形处理/算法
创建于2007-09-28

7612

社区成员

5.0w+

社区内容

VC/MFC 图形处理/算法
社区公告
暂无公告