0

Trying to create a Rectangle and label the corners using A,B,C,D . I am successfully able to draw a rectangle, however unable to label the corners.

Here is the source code so far:

#include <GL/glut.h>
#include <stdlib.h>

/* GLUT callback Handlers */
static void resize(int width, int height)
{
    const float ar = (float) width / (float) height;

    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity() ;
}

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

    glBegin(GL_QUADS);
        glColor3d(1,0,0);
        glVertex3f(-1,-1,-10);
        glColor3d(1,1,0);
        glVertex3f(1,-1,-10);
        glColor3d(1,1,1);
        glVertex3f(1,1,-10);
        glColor3d(0,1,1);
        glVertex3f(-1,1,-10);
    glEnd();
    glutSwapBuffers();
}


static void key(unsigned char key, int x, int y)
{
    switch (key)
    {
        case 27 :
        case 'q':
            exit(0);
            break;
    }
    glutPostRedisplay();
}

static void idle(void)
{
    glutPostRedisplay();
}

/* Program entry point */
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("GLUT quadPoly");

    glutReshapeFunc(resize);
    glutDisplayFunc(display);
    glutKeyboardFunc(key);
    glutIdleFunc(idle);

    glClearColor(0,0,0,0);

    glutMainLoop();

    return EXIT_SUCCESS;
}

How to label the corners in the rectangle?

OBX
  • 6,044
  • 7
  • 33
  • 77

1 Answers1

0

How to label the corners in the rectangle?

The glut liberary function glutStrokeCharacter renders a stroke character. Stroke characters are rendered by drawing line primitives. The maximum top height of a character in a font is 119.05 units; the minimum at the bottom is 33.33 units. The width of a character can be get by glutStrokeWidth. The character is rendered to the origin of the current model view projection matrix.

The following function renders a character with a size h to position x, y, z. The alignment can be set by left and top:

void LabelStroke(char c, float h, bool left, bool top, float x, float y, float z)
{
    float scale = h / ( 119.05f + 33.33f );
    float xo = x;
    float yo = y;
    if ( left )
    {
        int w = glutStrokeWidth(GLUT_STROKE_ROMAN, c );
        xo -= h * (float)w / ( 119.05f + 33.33f );
    }
    if ( top )
        yo -= h * (float)119.05f / ( 119.05f + 33.33f );

    glPushMatrix();
    glTranslatef(xo, yo, z);
    glScalef(scale, scale, scale); 
    glutStrokeCharacter(GLUT_STROKE_ROMAN, c); 
    glPopMatrix();
}  

Use the function somehow like this:

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

    glBegin(GL_QUADS);
        glColor3d(1,0,0);
        glVertex3f(-1,-1,-10);
        glColor3d(1,1,0);
        glVertex3f(1,-1,-10);
        glColor3d(1,1,1);
        glVertex3f(1,1,-10);
        glColor3d(0,1,1);
        glVertex3f(-1,1,-10);
    glEnd();

    float size = 0.5f;
    float offsl = size * 0.7f;
    glColor3d(1.0, 1.0, 1.0);
    LabelStroke( 'A', size, true,  true,  -1.0f, -1.0f, -10.0f );
    LabelStroke( 'B', size, false, true,   1.0f, -1.0f, -10.0f );
    LabelStroke( 'C', size, false, false,  1.0f,  1.0f, -10.0f );
    LabelStroke( 'D', size, true,  false, -1.0f,  1.0f, -10.0f );

    glutSwapBuffers();
}

See the preview:


With the glut library function glutBitmapCharacter, a bitmap character can be rendered. A bitmap character is rendered by drawing pixels (e.g. glDrawPixels). The position of the character can be set and moved by glRasterPos and respectively glBitmap:

void LabelBitmap( char c, bool left, bool top, float x, float y, float z  )
{
    int w = glutBitmapWidth( GLUT_BITMAP_TIMES_ROMAN_24, c );
    glRasterPos3f( x, y, z );
    glBitmap(0, 0, 0, 0, left ? -w : 0, top ? -24 : 0, NULL);
    glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, c); 
}

void display(void)
{
    .....

    glColor3d(1.0, 1.0, 1.0);
    LabelBitmap( 'A', true,  true,  -1.0f, -1.0f, -10.0f );
    LabelBitmap( 'B', false, true,   1.0f, -1.0f, -10.0f );
    LabelBitmap( 'C', false, false,  1.0f,  1.0f, -10.0f );
    LabelBitmap( 'D', true,  false, -1.0f,  1.0f, -10.0f );

    .....
}

Note, the current color (glColor), which is to be used to render the character, has to be set before glRasterPos is called.
The current raster position also includes some associated color data and texture coordinates. When glRasterPos is called, then the current colors and texture coordinates replace the colors and texture coordinates stored in the current raster position’s associated data.

Preview:

Rabbid76
  • 202,892
  • 27
  • 131
  • 174