开发者

Opengl outline font in conflict with textures

I want to render outline font near playing area, but these two parts are in conflict. If I render only outline font, it's ok. When I render also playing area, textures of cubes have defect and light is changed also. When I render only playing area, it's ok. Here are some pictures: http://img.obrazok.com/Untitled.usdh.png

Outline font part:

 //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 B开发者_JAVA百科y 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();

}

Render playing area:

//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);
}

Render part:

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();
}

Init part:

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;
}

If you have some question or want see another part of code, just ask. Thank you.


The documentation for wglUseFontOutlines at MSDN says this:

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 defults to GL_CCW; so chances are, if you set it back with

glFrontFace( GL_CCW );

after drawing the text, your playing area will render correctly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜