Opengl轮廓字体与纹理冲突

时间:2011-06-12 17:53:12

标签: opengl fonts textures conflict outline

我想在游戏区附近渲染轮廓字体,但这两个部分是冲突的。如果我只渲染轮廓字体,那没关系。当我渲染游戏区域时,立方体的纹理也有缺陷,光线也会改变。当我只渲染游戏区域时,没关系。这是一些图片: http://img.obrazok.com/Untitled.usdh.png

大纲字体部分:

 //Display-lists
GLuint fontBase;
GLYPHMETRICSFLOAT gmf[256]; 
//********************************
//3D Font
//********************************
GLvoid BuildFont(GLvoid)                                // Build Our Bitmap Font
{
    HFONT   font;                                       // Windows Font ID

    fontBase = glGenLists(256);                             // Storage For 256 Characters

    font = CreateFont(  -12,                            // Height Of Font
                        0,                              // Width Of Font
                        0,                              // Angle Of Escapement
                        0,                              // Orientation Angle
                        FW_BOLD,                        // Font Weight
                        FALSE,                          // Italic
                        FALSE,                          // Underline
                        FALSE,                          // Strikeout
                        ANSI_CHARSET,                   // Character Set Identifier
                        OUT_TT_PRECIS,                  // Output Precision
                        CLIP_DEFAULT_PRECIS,            // Clipping Precision
                        ANTIALIASED_QUALITY,            // Output Quality
                        FF_DONTCARE|DEFAULT_PITCH,      // Family And Pitch
                        "Comic Sans MS");               // Font Name

    HDC hDC = GetDC(GetActiveWindow());
    SelectObject(hDC, font);                            // Selects The Font We Created

    wglUseFontOutlines( hDC,                            // Select The Current DC
                        0,                              // Starting Character
                        255,                            // Number Of Display Lists To Build
                        fontBase,                           // Starting Display Lists
                        0.0f,                           // Deviation From The True Outlines
                        0.2f,                           // Font Thickness In The Z Direction
                        WGL_FONT_POLYGONS,              // Use Polygons, Not Lines
                        gmf);                           // Address Of Buffer To Recieve Data
}

GLvoid KillFont(GLvoid)                                 // Delete The Font
{
  glDeleteLists(fontBase, 256);                             // Delete All 256 Characters
}

void printString(const char *string, float x, float y, float z)                 // Custom GL "Print" Routine
{
    float       length=0;                               // Used To Find The Length Of The Text

    for (unsigned int loop=0;loop<(strlen(string));loop++)  // Loop To Find Text Length
    {
        length+=gmf[string[loop]].gmfCellIncX;          // Increase Length By Each Characters Width
    }

    glPushMatrix();
    glRotatef(90,0.0,0.0,1.0);
    glRotatef(90,1.0,0.0,0.0);
    glTranslatef(x,y,z);


    glPushAttrib(GL_LIST_BIT);                          // Pushes The Display List Bits
    glListBase(fontBase);                                   // Sets The Base Character to 0
    glCallLists(strlen(string), GL_UNSIGNED_BYTE, string);  // Draws The Display List Text
    glPopAttrib();                                      // Pops The Display List Bits
    glPopMatrix();

}

渲染游戏区域:

//Display-lists
GLuint ls_mapWalls;


void renderWalls() {
    for (int x = 0; x < mapSize; x++) {
        for (int y = 0; y < mapSize; y++) {
            glPushMatrix();
            glTranslatef(x*2.0f,y*2.0f,0.0f);
            if (map[x][y] == 'X') {
                renderWall();
            }
            glPopMatrix();
        }
    }
}

void renderWall() {
    glPushMatrix();
    renderTexturedCube(1); //1 or 2 
    glPopMatrix();
}

void renderTexturedCube(int color) {
    glEnable(GL_TEXTURE_2D);
    switch (color) {
    case 1 :
        //normal wall
        setMaterialColor(1.0f, 1.0f, 1.0f, 0.2f);
        break;
    case 2 :
        //red wall
        setMaterialColor(1.0f, 0.0f, 0.0f, 1.0f);
        break;
    }

    glBindTexture(GL_TEXTURE_2D, texid[0]);
    glBegin(GL_QUADS);
    // Front Face
    glNormal3f(0.0,0.0,1.0); 
    glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
    glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
    glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
    glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
    // Back Face
    glNormal3f(0.0,0.0,-1.0); 
    glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
    glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
    glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
    glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
    // Top Face
    glNormal3f(0.0,1.0,0.0);
    glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
    glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
    glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
    glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
    // Bottom Face
    glNormal3f(0.0,-1.0,0.0); 
    glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
    glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
    glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
    glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
    // Right face
    glNormal3f(1.0,0.0,0.0);
    glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
    glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
    glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
    glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
    // Left Face
    glNormal3f(-1.0,0.0,0.0); 
    glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
    glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
    glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
    glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
    glEnd();

    setMaterialColor(1.0f, 1.0f, 1.0f, 1.0f);
    glDisable(GL_TEXTURE_2D);
}

渲染部分:

void render(void)   
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();       

    GLdouble ex = vzd*cos(fi)*cos(xi);
    GLdouble ey = vzd*sin(fi)*cos(xi);
    GLdouble ez = vzd*sin(xi);
    gluLookAt( ex, ey, ez, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f );


    printString("Dyna Blaster Beta", 10, 10, 10, 1.0, 0.0, 0.0);
    glCallList(ls_mapWalls);

    glutSwapBuffers();
}

初始部分:

bool init(void)
{
    //setup OpenGL
    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);
    glClearColor (0.0, 0.0, 0.0, 0.0);
    glEnable(GL_CULL_FACE);

    //files load    
    loadMap();
    loadTextures();

    //next init
    ls_mapWalls = glGenLists(1);
    glNewList(ls_mapWalls, GL_COMPILE);
    renderWalls();
    glEndList();    

    glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 2.0);
    GLfloat light_ambient[] = { 0.1, 0.1, 0.1, 1.0 };
    GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
    GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
    GLfloat light_position[] = { 10.0, 10.0, 10.0, 0.0 };
    glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);

    BuildFont();
    fi = 0.385f; xi = 0.515f; vzd = 69.2f;                            
    return true;
}

如果您有疑问或想要查看其他部分代码,请询问。谢谢。

1 个答案:

答案 0 :(得分:2)

MSDN处wglUseFontOutlines的文档说明了这一点:

With WGL_FONT_POLYGONS, the created display lists call glFrontFace( GL_CW )
or glFrontFace( GL_CCW ); thus the current front-face value might be altered.

OpenGL侮辱GL_CCW;所以很有可能,如果你用

重新设置它
glFrontFace( GL_CCW );

绘制文字后,您的游戏区域将正确渲染。