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 2 of 2

Thread: plotting a basic 2d graph

  1. #1
    Newbie Newbie
    Join Date
    Jun 2017
    Posts
    1

    Question plotting a basic 2d graph

    I'm really desperate for some help.
    I'm extreamly new to opengl and i'm trying to do the following.
    create a window with a simple 2d graph created from a dynamic amount of points.
    i have a connected list of points structure storing all the y and x values. plus a flag for a warrning i need to print next to a point on the graph that's flagged.
    i'm writing in VS 2017 in "pure" C language.
    i need to graph to look somthing like this and to close on left mouse click:
    Click image for larger version. 

Name:	graphexample.jpg 
Views:	42 
Size:	32.3 KB 
ID:	2388



    here's my code so far:

    Code cpp:
    int main(int argc, char** argv) 
    {
        glutInit(&argc, argv); //init
        glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); //Color mode
        glutInitWindowSize(500, 500); //Window size
        glutInitWindowPosition(0, 0); //Window position (from upper left corner)
        glutCreateWindow("Test"); //Create window with title
        glutDisplayFunc(display); //Present default display 
        glutMainLoop(); //Start loop 
        return 0;
    }
    void drawText(const char *message, float x, float y, char color, char font)
    {
        // Color - must be before Raster.
        switch (color)
        {
        case 'r':
            glColor3f(0.4, 0.0, 0.0); // 3f -> 3 floats, can also be 3d,3i,3s,3b. 
            break;
        case 'g':
            glColor3f(0.0, 0.4, 0.0); // 3f -> 3 floats, can also be 3d,3i,3s,3b. 
            break;
        case 'b':
            glColor3f(0.0, 0.0, 0.4); // 3f -> 3 floats, can also be 3d,3i,3s,3b. 
            break;
        default:
            glColor3f(0.0, 0.0, 0.0); // 3f -> 3 floats, can also be 3d,3i,3s,3b. 
            break;
        }
     
        //Position
        glRasterPos2f(x, y);
     
        //write using bitmap and stroke chars
        if (font == 24)
        {
            while (*message)
            {
                glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, *message++);
            }
        }
            if (font == 10)
            {
                while (*message)
                {
                    glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, *message++);
                }
            }
        }
    void display(void) 
    {
        /* set clear color to black (red,*green,*blue,*alpha) range*[0,1]*/
        glClearColor(0.9, 0.9, 0.9, 0);
        /* clear window */
        glClear(GL_COLOR_BUFFER_BIT);
     
        /* graph square */
        glColor3f(1.0, 1.0, 1.0);
        glBegin(GL_POLYGON);
        glVertex2f(-0.7, -0.7);
        glVertex2f(-0.7, 0.7);
        glVertex2f(0.7, 0.7);
        glVertex2f(0.7, -0.7);
        glEnd();
     
        // Axis
        glLineWidth(4);
        glColor3f(0, 0, 0);
        glBegin(GL_LINES);
        glVertex2f(-0.7, -0.7);
        glVertex2f(-0.7, 0.7);
        glVertex2f(0.7, -0.7);
        glVertex2f(-0.7, -0.7);
        glEnd();
     
        // Text
        drawText("y\0", -0.7, 0.75, 'd', 24);
        drawText("x\0", 0.75, -0.7, 'd', 24);
        drawText(MESSAGE,-0.4,0.8,'g',24);
     
        /* flush GL buffers */
        glFlush();
    }

    i'd really appriciate any help i'm lost.
    Last edited by Dark Photon; 06-13-2017 at 05:32 PM.

  2. #2
    Intern Contributor
    Join Date
    Dec 2016
    Posts
    54
    Where are you lost? What does the program do when it runs?

    Jeff

Posting Permissions

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