Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: Text and paint problem

  1. #1
    Junior Member Newbie
    Join Date
    May 2012
    Posts
    3

    Text and paint problem

    i didn't know if i should 've post it here or somewhere else i', new so sorry if this is in the wrong place.
    My problem is that the code that it is shown below is supposed to show a text in the screen and simply draw on the screen with different colors, but it doesn't when i change the color the only thing that changes is the text color and i cant draw on the screen, so please help someone who understands this better thank you.

    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <GL/glut.h>
    using namespace std;
    void display(void);

    /* mos harro mi fshi
    void polygon(int a, int b, int c , int d);
    void DrawCube();
    */



    int screenHeight=600;
    int screenWidth=800;

    GLint brush=7.0;
    float gColor[3]={1.0,1.0,1.0};


    void Init()
    {
    glClearColor(0.0,0.0,0.0,0.0);glClear(GL_COLOR_BUF FER_BIT);
    gluOrtho2D(0.0,(GLdouble)screenWidth,0.0,(GLdouble )screenHeight);
    glViewport(0,0,screenWidth,screenHeight);
    }


    void keyboard(unsigned char i,int w, int q)
    {
    switch (i)
    {
    case 'z':// me shtypjen e tastit "z" ne tastier brusha vizaton me ngjyre te zeze dhe mund te perdoret si gome
    gColor[0]=0.0;
    gColor[1]=0.0;
    gColor[2]=0.0;
    break;


    case 'w':// me shtypjen e tastit "w" ne tastier brusha vizaton me ngjyre te bardhe
    gColor[0]=1.0;
    gColor[1]=1.0;
    gColor[2]=1.0;
    break;


    case 'r'://me shtypjen e tastit "r" ne tastier brusha vizaton me ngjyre te kuqe
    gColor[0]=0.0;
    gColor[0]=1.0;
    gColor[1]=0.0;
    gColor[2]=0.0;
    break;

    case 'g'://me shtypjen e tastit "g" ne tastier brusha vizaton me ngjyre te gjelbert
    gColor[0]=0.0;
    gColor[1]=1.0;
    gColor[2]=0.0;
    break;


    case 'b'://me shtypjen e tastit "b" ne tastier brusha vizaton me ngjyre te kalter
    gColor[0]=0.0;
    gColor[1]=0.0;
    gColor[2]=1.0;
    break;

    case 'y'://me shtypjen e tastit "y" ne tastier brusha vizaton me ngjyre te verdh
    gColor[0]=1.0;
    gColor[1]=1.0;
    gColor[2]=0.0;
    break;

    case '>'://me shtypjen e tastit "shift" + ">" ne tastier brusha rritet
    brush++;
    break;

    case '<'://me shtypjen e tastit "shift" + "<" ne tastier brusha zvogelohet
    brush--;
    if (brush<1)
    {
    brush=1;
    }
    break;

    case 'c'://me shtypjen e tastit "c" ne tastier ekrani pastrohet
    glClear(GL_COLOR_BUFFER_BIT);
    break;
    case 'q'://me shtypjen e tastit "q" ne tastier mbyllet programi
    exit(0);
    break;
    }
    }

    void printtext(int x, int y, string String)
    {
    //(x,y) is from the bottom left of the window
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    glOrtho(0, screenWidth, 0, screenHeight, -1.0f, 1.0f);
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();
    glPushAttrib(GL_DEPTH_TEST);
    glDisable(GL_DEPTH_TEST);
    glRasterPos2i(x,y);
    for (int i=0; i<String.size(); i++)
    {
    glutBitmapCharacter(GLUT_BITMAP_9_BY_15, String[i]);
    }
    glPopAttrib();
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();
    }


    void mouse_motion(int x, int y)
    {
    glRecti(x,screenHeight-y,x+brush,(screenHeight-y)+brush);
    glFlush();
    }

    void mouse(int button,int state,int x, int y)
    {
    if(button == GLUT_LEFT_BUTTON && state ==GLUT_DOWN)
    {
    glBegin(GL_POINTS);
    glColor3fv(gColor);
    glRecti(x,screenHeight-y,x+brush,(screenHeight-y)+brush);
    glEnd();
    }
    }



    void Display2(void)
    {
    glEnd();
    glColor3fv(gColor);
    glPointSize(brush);
    glFlush();
    }

    int main(int argc, char *argv[])
    {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(screenWidth, screenHeight);
    glutInitWindowPosition(100, 100);

    glutCreateWindow("PaintBrush");

    glutKeyboardFunc(keyboard);
    glutMotionFunc(mouse_motion);
    glutMouseFunc(mouse);

    glutDisplayFunc(Display2);
    glutDisplayFunc(display);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(70, 1, 1, 100);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    gluLookAt(2, 2, 10, 2, 0, 0, 0, 1, 0);
    glutMainLoop();
    return 0;

    }
    void display(void)
    {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glEnable(GL_DEPTH_TEST);

    char string[64],string2[64],string3[64];
    sprintf(string, "colors: r=red, g=green, w=white, y=yellow, b=blue");
    sprintf(string2, "functions: z=erase, c=clear, q=quit ");
    sprintf(string3,"for mouse zoom press shift + '<' '>'");
    printtext(10,30,string);
    printtext(10,10,string2);
    printtext(10,50,string3);
    glutSwapBuffers();

    //
    }

  2. #2
    Super Moderator OpenGL Guru
    Join Date
    Feb 2000
    Location
    Montreal, Canada
    Posts
    4,421
    My problem is that the code that it is shown below is supposed to show a text in the screen and simply draw on the screen with different colors, but it doesn't
    I traced the code. You never call glColor so it uses whatever GL's default is, which is white.

    If I put a call to glColor, it renders all the text in red

    glColor4f(1.0, 0.0, 0.0, 1.0);
    printtext(10,30,string);
    printtext(10,10,string2);
    printtext(10,50,string3);
    glutSwapBuffers();
    ------------------------------
    Sig: http://glhlib.sourceforge.net
    an open source GLU replacement library. Much more modern than GLU.
    float matrix[16], inverse_matrix[16];
    glhLoadIdentityf2(matrix);
    glhTranslatef2(matrix, 0.0, 0.0, 5.0);
    glhRotateAboutXf2(matrix, angleInRadians);
    glhScalef2(matrix, 1.0, 1.0, -1.0);
    glhQuickInvertMatrixf2(matrix, inverse_matrix);
    glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
    glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

  3. #3
    Junior Member Newbie
    Join Date
    May 2012
    Posts
    3
    Quote Originally Posted by V-man View Post
    I traced the code. You never call glColor so it uses whatever GL's default is, which is white.

    If I put a call to glColor, it renders all the text in red

    glColor4f(1.0, 0.0, 0.0, 1.0);
    printtext(10,30,string);
    printtext(10,10,string2);
    printtext(10,50,string3);
    glutSwapBuffers();
    Thank you for the correction but this still doesn't solve the problem, the mouse is supposed to draw when you click it.
    When i separate the codes they both work perfectly but when i put them together they don't

  4. #4
    Super Moderator OpenGL Guru
    Join Date
    Feb 2000
    Location
    Montreal, Canada
    Posts
    4,421
    Quote Originally Posted by valonj View Post
    Thank you for the correction but this still doesn't solve the problem, the mouse is supposed to draw when you click it.
    When i separate the codes they both work perfectly but when i put them together they don't
    You should do all your drawing operations in your display() function. If you do a draw operation in your mouse function, it just renders to the backbuffer and when display() is called, it clears the backbuffer.
    ------------------------------
    Sig: http://glhlib.sourceforge.net
    an open source GLU replacement library. Much more modern than GLU.
    float matrix[16], inverse_matrix[16];
    glhLoadIdentityf2(matrix);
    glhTranslatef2(matrix, 0.0, 0.0, 5.0);
    glhRotateAboutXf2(matrix, angleInRadians);
    glhScalef2(matrix, 1.0, 1.0, -1.0);
    glhQuickInvertMatrixf2(matrix, inverse_matrix);
    glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
    glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •