Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ OpenGL将纹理应用于细分_C++_Opengl_Textures_Tessellation - Fatal编程技术网

C++ OpenGL将纹理应用于细分

C++ OpenGL将纹理应用于细分,c++,opengl,textures,tessellation,C++,Opengl,Textures,Tessellation,我试着取一个凹多边形,并将图像作为纹理应用于它。多边形可以有多个轮廓,包括内部孔和外部“岛”。它可以是任何形状,但将比图像小,并将适合其内部。它不一定会触及图像的边缘 我已经成功地显示了镶嵌多边形,并对一个简单的正方形进行了纹理处理,但无法使两者协同工作 下面是我加载纹理的方式: GLuint texture; int width, height; BYTE * data; FILE * file; // open texture data fi

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

我已经成功地显示了镶嵌多边形,并对一个简单的正方形进行了纹理处理,但无法使两者协同工作

下面是我加载纹理的方式:

    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]);
以下是细分顶点回调函数:

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();
}
结果是正确绘制的多边形没有应用纹理

// init
glGenTextures( 1, &texture );

// vertex callback
glBindTexture(GL_TEXTURE_2D, 1);
我不认为
glGenTextures()
返回的第一个ID必须是
1

尝试在
glBindTexture()
调用中使用
texture
而不是
1


此外,实际上没有理由启用纹理并为每个顶点重新绑定纹理。只需在调用tesselator之前执行一次即可。

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

  • 捕获BindTexture并在显示列表中启用,或
  • BindTexture并在调用调用调用列表之前启用(TEXTURE_2D)

问题在于细分函数中的glDisable(GL\u TEXTURE\u 2D)调用。删除后,纹理被正确应用。

我将enable和bind调用移到了镶嵌函数中,并将相关代码添加到原始post中。post a。