1

我正在尝试采用凹多边形并将图像作为纹理应用到它。多边形可以有多个轮廓,包括内部孔和外部“岛”。它可以是任何形状,但会比图像小并适合其中。它不一定会触及图像的边缘。

我已经成功地显示了镶嵌多边形,并纹理了一个简单的正方形,但不能让两者一起工作。

这是我加载纹理的方式:

    GLuint texture;
    int width, height;
    BYTE * data;
    FILE * file;

    // open texture data
    file = fopen( filename, "rb" );
    if ( file == NULL ) return 0;

    // allocate buffer
    width = 256;
    height = 256;
    data = (BYTE *)malloc( width * height * 3 );

    // read texture data
    fread( data, width * height * 3, 1, file );
    fclose( file );

    glGenTextures( 1, &texture );
    glBindTexture( GL_TEXTURE_2D, texture );
    glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap ? GL_REPEAT : GL_CLAMP );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap ? GL_REPEAT : GL_CLAMP );

    gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width,height, GL_RGB, GL_UNSIGNED_BYTE, data );

    free( data );

    return texture;

这是曲面细分功能:

GLuint tessellate1()
{
GLuint id = glGenLists(1);  // create a display list
if(!id) return id;          // failed to create a list, return 0

GLUtesselator *tess = gluNewTess(); // create a tessellator
if(!tess) return 0;  // failed to create tessellation object, return 0

GLdouble quad1[4][3] = { {-1,3,0}, {0,0,0}, {1,3,0}, {0,2,0} };

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, GLTexture::LoadTextureRAW("texture.raw", true));

// register callback functions
gluTessCallback(tess, GLU_TESS_BEGIN, (void (CALLBACK *)())tessBeginCB);
gluTessCallback(tess, GLU_TESS_END, (void (CALLBACK *)())tessEndCB);
gluTessCallback(tess, GLU_TESS_ERROR, (void (CALLBACK *)())tessErrorCB);
gluTessCallback(tess, GLU_TESS_VERTEX, (void (CALLBACK *)())tessVertexCB);

glNewList(id, GL_COMPILE);
glColor3f(1,1,1);
gluTessBeginPolygon(tess, 0);                   // with NULL data
    gluTessBeginContour(tess);
        gluTessVertex(tess, quad1[0], quad1[0]);
        gluTessVertex(tess, quad1[1], quad1[1]);
        gluTessVertex(tess, quad1[2], quad1[2]);
        gluTessVertex(tess, quad1[3], quad1[3]);
    gluTessEndContour(tess);
gluTessEndPolygon(tess);
glEndList();

gluDeleteTess(tess);        // delete after tessellation

glDisable(GL_TEXTURE_2D);

setCamera(0, 0, 5, 0, 0, 0);

return id;      // return handle ID of a display list
}

这是镶嵌顶点回调函数:

// cast back to double type
const GLdouble *ptr = (const GLdouble*)data;

double dImageX = -1, dImageY = -1;

//hardcoded extents of the polygon for the purposes of testing
int minX = 607011, maxX = 616590;
int minY = 4918219, maxY = 4923933;

//get the % coord of the texture for a poly vertex.  Assumes image and poly bounds are the same for the purposes of testing
dImageX = (ptr[0] - minX) / (maxX - minX);
dImageY = (ptr[1] - minY) / (maxY - minY);

glTexCoord2d(dImageX, dImageY);
glVertex2d(ptr[0], ptr[1]);

这是显示回调:

void displayCB()
{
// clear buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

// save the initial ModelView matrix before modifying ModelView matrix
glPushMatrix();

// tramsform camera
glTranslatef(0, 0, cameraDistance);
glRotatef(cameraAngleX, 1, 0, 0);   // pitch
glRotatef(cameraAngleY, 0, 1, 0);   // heading

// draw meshes
glCallList(listId1);  //id of the tessellated poly

// draw info messages
showInfo();

glPopMatrix();

glutSwapBuffers();
}

结果是一个正确绘制的多边形,没有应用纹理。

4

3 回答 3

1
// init
glGenTextures( 1, &texture );

// vertex callback
glBindTexture(GL_TEXTURE_2D, 1);

我不认为返回的第一个 IDglGenTextures()必须是1.

尝试在通话中使用texture而不是。1glBindTexture()

此外,确实没有理由为每个 vertex启用纹理和重新绑定纹理。只需在调用 tesselator 之前执行一次。

于 2012-12-18T19:03:49.703 回答
1

您没有在显示列表中捕获纹理绑定和启用,因此重播时不会考虑它。所以,要么:

  • 在显示列表中捕获 BindTexture 和 Enable,或者
  • 调用 CallList 之前的 BindTexture 和 Enable(TEXTURE_2D)
于 2012-12-19T14:39:15.737 回答
1

问题是曲面细分函数中的 glDisable(GL_TEXTURE_2D) 调用。删除它后,纹理被正确应用。

于 2012-12-19T15:02:44.883 回答