Issues with the color comand in OpenGL

71 Views Asked by At
#include <GL/glut.h>
#include <stdio.h>

const int posicao=28.0;/*esta constante sera usada para fixar a posicao do inicio do texto para a funcao de escrita*/

float VERTICAL=30.0, HORIZ=30.0;

int seletor=1;

char escolha_de_magias[]={"?1-Copia de Criatura: Faz uma replica perfeita de qualquer"/* criatura contra a qual voce esteja lutando. A replica tera os mesmos poderes do orignal, mas estara sob seu controle@2-Percepcao Extra-Sensorial: Sintoniza ondas psiquicas. Isso podera ajuda-lo a ler a mente de uma criatura ou descobrir o que esta por tras de uma porta trancada. Mas este encanto pode dar informacoes equivokdas, se houver + de a font psikica perto de uma outra.@3-Fogo: Tds as criaturas tm medo d fogo, e ste enknto da o podr d fazr aparecr fogo o sua vontad. Vc pdera causr a pkena xplosao n chao q keimara pr varios os ou criar a brreira d fogo p mantr criaturas a dstancia.@4-Ouro dos Tolos: Trnsforma pdra comum em a plha do q parece sr ouro. Mas eh apnas a ilusao - embora + confiavl q o Enknto d ilusao abaixo - e a plha d ouro logo voltara a sr pdra.@5-Ilusao: Ste e  enknto podroso, mas q n e mto confiavl. Prmit criar a ilusao convncnte (pr exemplo, q o chao sta cobrto d krvao em brasa) p enganr a criatura. O enknto fikra imediatamnt sm efeito s acontcr qlqr coisa q dsfaca a ilusao (pr exemplo, vc convnc a criatura q s trnsformou em a serpnt e entao imediatamnt atnge sua kbca c  golp d spada!). e efcient sbre td c criaturas intlignts.@6-Levitacao: Vc pde lancr ste enknto sbre objtos, advrsarios ou ate sbre si msm. Fara cm q tdo q estja sob sua influencia flutue livremnt no ar, sob o seu cntrole.@7-Escudo: Cria um scudo invsivl à sua frent q o protgera d objtos fsicos, cm flechas, spadas ou criaturas. O scudo n tm efeito cntra a magia e, evdntmnte, s nd fora dle pod tokr em vc, vc tbm n podra tokr em nd fora dle.@8-Sorte: Este enknto e spcial pq pod sr lancado a qlqr momnto durante a sua avntura, a n sr durant a batalha. a vz lancado, recuperara o seu indce d SORTE em metad d seu indce d SORTE Inicial, arredondado p baixo. Ste enknto nunk lvara o seu indce d SORTE a  nmro suprior a seu nivl Inicial.@9-Habilidade: Restabelecera o seu indce d HABILIDADE. Funciona exatamnt cm o enknto d Sort.@10-Energia: Recuperara o seu indce d Energia, aumentando-o em metad d seu vlr Inicial. Funciona cmo o enknto d Sort@11-Forca: Ste enknto tm o efeito d aumentr mto a sua frca, e e mto utl qnd s luta cntra criaturas forts. Porm, dv sr utlizado c cautla, ja q e dfcil controlr a sua prpria frca qnd ela aumnta d+@12-Fraqueza: N tm efeito cntra tds as criatras, ms qnd tm, a criatra s trna fragl e mto mnos prigosa em a btalha"*/};

void escritoura (char *p)/* FUNCAO QUE ESCREVE NA TELA. Esta pronta mas sujeita a mudanças*/
{
    int contcarac=0, position=posicao, selecao=0;
    float cor[4]={ 0.0, 0.0, 0.0, 0.0};
    p++;
    glRasterPos2f(-28.0, position);
    while(*p)
    {
        if(*p=='1' || *p=='2' || *p=='3' || *p=='4' || *p=='5' || *p=='6' || *p=='7' || *p=='8' || *p=='9' || *p=='0')/* testa se eu estou olhando para um algarismo*/
        {
          /*if(*(p+1)!='1' && *(p+1)!='2' && *(p+1)!='3' && *(p+1)!='4' && *(p+1)!='5' && *(p+1)!='6' && *(p+1)!='7' && *(p+1)!='8' && *(p+1)!='9' && *(p+1)!='0')/*testa se o proximo nao e um algarismo*/
          {
            glColor3d(1.0, 0.0, 0.0);
          }
        }   
        glutBitmapCharacter(GLUT_BITMAP_8_BY_13, *p);
        p++;
        contcarac++;
    }
    return;
}

void ABACAXI()
{
    glClearColor(1.0,1.0,1.0,0.0);
    glClear(GL_COLOR_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(HORIZ-60.0, HORIZ, VERTICAL-60.0, VERTICAL,1,100);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.0, 0.0, -5.0);

    glColor3d(0.0, 1.0, 0.0);

    glBegin(GL_LINE_STRIP);
        glVertex3d(-30.0, -30.0, 0.0);
        glVertex3d(-30.0, 30.0, 0.0);
        glVertex3d(30.0, 30.0, 0.0);
        glVertex3d(30.0, -30.0, 0.0);
    glEnd();

    escritoura(escolha_de_magias);

    glFlush();
}

void teclado(int tecla, int x, int y)
{
    switch(tecla)
    {
        case GLUT_KEY_UP:
        VERTICAL++;
        break;

        case GLUT_KEY_DOWN:
        VERTICAL--;
        break;

        case GLUT_KEY_RIGHT:
        HORIZ++;
        break;

        case GLUT_KEY_LEFT:
        HORIZ--;
        break;
    }
    glutPostRedisplay();
}

int main(int argc, char **argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(1000,700);
    glutCreateWindow("Alo!");
    glutDisplayFunc(ABACAXI);
    glutSpecialFunc(teclado);
    glutPostRedisplay();
    glutMainLoop();
} 

The code ahead is supposed to draw the text in escolha_de_magias in red, but keeps drawing it in green.
I made a test with glGetFloatv(GET_CURRENT_COLOR, cor) function right before the first calling of glutBitmapCharacter() and it returned that the color of drawing is set to red.

I don't know whats happening

1

There are 1 best solutions below

4
On BEST ANSWER

The color which is use for drawing characters by glutBitmapCharacter is set by glRasterPos. glRasterPos takes the current color set by glColor and associates it.

You have to se the the red color before glRasterPos2f, to draw a red text.

glColor3d(1.0, 0.0, 0.0);
glRasterPos2f(-28.0, position);

Your text appears in green, because the last color set before the call of glRasterPos2f, was the color for the GL_LINE_STRIP primitive.

If you want to draw a text with different colors, then you have to do multiple calls to glRasterPos at every point when the color of the text changes.


The current raster position can be get by glGetFloatv using the parameter GL_CURRENT_RASTER_POSITION:

float pos[4];
glGetFloatv( GL_CURRENT_RASTER_POSITION, pos );

Sadly you may not get the position which you have set by glRasterPos, because the coordinates which are passed to glRasterPos are transformed by the current model view matrix and projection matrix, but when you read back the position, then you will get the transformed coordinates and the coordinates are window coordinates rather than normalized device coordinates.


If you want to change the associated color, but to keep the raster position, then you have to read the raster current position glGetFloatv(GL_CURRENT_RASTER_POSITION, ...).
This will return window coordinates.
So you have to set an orthographic projection, which transforms from window coordinates to normalized device coordinates, before glRasterPos can be called again to associate a new color. the model view matrix has to be the identity matrix.

Change the function escritoura somehow as follows, to solve your issue:

void escritoura (char *p)
{
    int contcarac=0, position=posicao, selecao=0;
    p++; 
    glRasterPos2f(-28.0f, position);
    while(*p)
    {
        if(*p=='1' || *p=='2' || *p=='3' || *p=='4' || *p=='5' || *p=='6' || *p=='7' || *p=='8' || *p=='9' || *p=='0')
        {
            // get the current raster position in window coordinates
            float pos[4];
            glGetFloatv( GL_CURRENT_RASTER_POSITION, pos );    

            // push model view matrix and set identity matrix
            glMatrixMode(GL_MODELVIEW);
            glPushMatrix();
            glLoadIdentity();

            // push projection matrix and set transformation form window coordinates to NDC
            glMatrixMode(GL_PROJECTION);
            glPushMatrix();
            glLoadIdentity();
            glOrtho( 0, 1000, 0, 700, -1, 1 ); // (1000, 700) is the window size!

            // change current color
            glColor3d( 1.0, 0.0, 0.0 );

            // associate new color, but keep position
            glRasterPos4fv( pos );

            // restore (pop) model view and projection matrix
            glMatrixMode(GL_PROJECTION);
            glPopMatrix();
            glMatrixMode(GL_MODELVIEW);
            glPopMatrix();
        }
        glutBitmapCharacter(GLUT_BITMAP_8_BY_13, *p);
        p++; contcarac++;
    }
    return;
}