Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7412557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:33:32+00:00 2026-05-29T06:33:32+00:00

I wrote a simple opengl application in C which plots sin(x). This is my

  • 0

I wrote a simple opengl application in C which plots sin(x). This is my current draw function which runs very slow. How do I have to convert this code to make use of the faster ‘vertex array’ mode?

list of variables and functions used:

  • N = total number of points
  • x1 = min(x)
  • x2 = max(x)
  • y1 = min(y)
  • y2 = max(y)
  • func(x) = sin(x)

and here’s the entire code:

/* to compile, do:

 $ gcc -o out simple.c -lglut

*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#include <time.h>

float xmin = -10, xmax = 10, ymin = -5, ymax = 5;
int nPoints = 3000;

/* function to calculate each data point */
float func(float x)
{
    return sin(x);
}

/* plotting function - very slow */
void draw(float (* func)(float x), float x1, float x2, float y1, float y2, int N)
{
    float x, dx = 1.0/N;

    glPushMatrix(); 

    glScalef(1.0 / (x2 - x1), 1.0 / (y2 - y1), 1.0);
    glTranslatef(-x1, -y1, 0.0);
    glColor3f(1.0, 1.0, 1.0);

    glBegin(GL_LINE_STRIP);

    for(x = x1; x < x2; x += dx)
    {
        glVertex2f(x, func(x));
    }

    glEnd();

    glPopMatrix();
};

/* Redrawing func */
void redraw(void)
{

    clock_t start = clock();
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    // -x, +x, -y, +y, number points

    draw(func, xmin, xmax, ymin, ymax, nPoints);

    glutSwapBuffers();
    printf("Time elapsed: %f\n", ((double)clock() - start) / CLOCKS_PER_SEC);
};

/* Idle proc. Redisplays, if called. */
void idle(void)
{
    // shift 'xmin' & 'xmax' by one.
    xmin++; 
    xmax++;
    glutPostRedisplay();
};

/* Key press processing */
void key(unsigned char c, int x, int y)
{
    if(c == 27) exit(0);
};

/* Window reashape */
void reshape(int w, int h)
{
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, 1, 0, 1, -1, 1);
    glMatrixMode(GL_MODELVIEW);
};

/* Main function */
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    glutCreateWindow("Graph plotter");
    glutReshapeWindow(1024, 800);
    glutPostRedisplay(); // This call may or may not be necessary


    /* Register GLUT callbacks. */
    glutDisplayFunc(redraw);
    glutKeyboardFunc(key);
    glutReshapeFunc(reshape);

    glutIdleFunc(idle);

    /* Init the GL state */
    glLineWidth(2.0);

    /* Main loop */
    glutMainLoop();
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:33:33+00:00Added an answer on May 29, 2026 at 6:33 am

    In terms of LodePNG’s C-style vector struct/functions:

    // shared
    vector pts;
    vector_init( &pts, sizeof( float ) );
    
    // whenever x1, x2, or N changes
    vector_cleanup( &pts );
    float x, dx = 1.0/N;
    for(x = x1; x < x2; x += dx)
    {
        vector_resize( &pts, pts.size + 2 );
        *(float*)vector_get( &pts, pts.size-2 ) = x;
        *(float*)vector_get( &pts, pts.size-1 ) = func(x);
    }
    
    // whenever you want to draw
    glPushMatrix();
    
    glScalef(1.0 / (x2 - x1), 1.0 / (y2 - y1), 1.0);
    glTranslatef(-x1, -y1, 0.0);
    glColor3f(1.0, 1.0, 1.0);
    
    glEnableClientState( GL_VERTEX_ARRAY );
    glVertexPointer( 2, GL_FLOAT, 0, (float*)pts.data );
    glDrawArrays( GL_LINE_STRIP, 0, pts.size / 2 );
    glDisableClientState( GL_VERTEX_ARRAY );
    
    glPopMatrix();
    

    EDIT: Complete code:

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <GL/glut.h>
    #include <time.h>
    
    typedef struct vector /*dynamic vector of void* pointers. This one is used only by the deflate compressor*/
    {
      void* data;
      size_t size; /*in groups of bytes depending on type*/
      size_t allocsize; /*in bytes*/
      unsigned typesize; /*sizeof the type you store in data*/
    } vector;
    
    static unsigned vector_resize(vector* p, size_t size) /*returns 1 if success, 0 if failure ==> nothing done*/
    {
      if(size * p->typesize > p->allocsize)
      {
        size_t newsize = size * p->typesize * 2;
        void* data = realloc(p->data, newsize);
        if(data)
        {
          p->allocsize = newsize;
          p->data = data;
          p->size = size;
        }
        else return 0;
      }
      else p->size = size;
      return 1;
    }
    
    static void vector_cleanup(void* p)
    {
      ((vector*)p)->size = ((vector*)p)->allocsize = 0;
      free(((vector*)p)->data);
      ((vector*)p)->data = NULL;
    }
    
    static void vector_init(vector* p, unsigned typesize)
    {
      p->data = NULL;
      p->size = p->allocsize = 0;
      p->typesize = typesize;
    }
    
    static void* vector_get(vector* p, size_t index)
    {
      return &((char*)p->data)[index * p->typesize];
    }
    
    float xmin = -10, xmax = 10, ymin = -5, ymax = 5;
    int nPoints = 3000;
    vector pts;
    
    /* function to calculate each data point */
    float func(float x)
    {
        return sin(x);
    }
    
    void update(float (* func)(float x), float x1, float x2, int N)
    {
        float x, dx = 1.0/N;
        vector_cleanup( &pts );
        for(x = x1; x < x2; x += dx)
        {
            vector_resize( &pts, pts.size + 2 );
            *(float*)vector_get( &pts, pts.size-2 ) = x;
            *(float*)vector_get( &pts, pts.size-1 ) = func(x);
        }
    }
    
    /* plotting function - very slow */
    void draw(float x1, float x2, float y1, float y2)
    {
        glPushMatrix(); 
    
        glScalef(1.0 / (x2 - x1), 1.0 / (y2 - y1), 1.0);
        glTranslatef(-x1, -y1, 0.0);
        glColor3f(1.0, 1.0, 1.0);
    
        if( pts.size > 0 )
        {
            glEnableClientState( GL_VERTEX_ARRAY );
            glVertexPointer( 2, GL_FLOAT, 0, (float*)pts.data );
            glDrawArrays( GL_LINE_STRIP, 0, pts.size / 2 );
            glDisableClientState( GL_VERTEX_ARRAY );
        }
    
        glPopMatrix();
    };
    
    /* Redrawing func */
    void redraw(void)
    {
        clock_t start = clock();
        glClearColor(0, 0, 0, 0);
        glClear(GL_COLOR_BUFFER_BIT);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    
        // -x, +x, -y, +y, number points
        draw(xmin, xmax, ymin, ymax);
    
        glutSwapBuffers();
        printf("Time elapsed: %f\n", ((double)clock() - start) / CLOCKS_PER_SEC);
    };
    
    /* Idle proc. Redisplays, if called. */
    void idle(void)
    {
        // shift 'xmin' & 'xmax' by one.
        xmin++; 
        xmax++;
    
        update(func, xmin, xmax, nPoints);
    
        glutPostRedisplay();
    };
    
    /* Key press processing */
    void key(unsigned char c, int x, int y)
    {
        if(c == 27) exit(0);
    };
    
    /* Window reashape */
    void reshape(int w, int h)
    {
        glViewport(0, 0, w, h);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, 1, 0, 1, -1, 1);
        glMatrixMode(GL_MODELVIEW);
    };
    
    /* Main function */
    int main(int argc, char **argv)
    {
        vector_init( &pts, sizeof( float ) );
    
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
        glutCreateWindow("Graph plotter");
        glutReshapeWindow(1024, 800);
        glutPostRedisplay(); // This call may or may not be necessary
    
    
        /* Register GLUT callbacks. */
        glutDisplayFunc(redraw);
        glutKeyboardFunc(key);
        glutReshapeFunc(reshape);
    
        glutIdleFunc(idle);
    
        /* Init the GL state */
        glLineWidth(2.0);
    
        /* Main loop */
        glutMainLoop();
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is this simple function which I have used with C++ in the past
I wrote simple iOS app which countdown the time til midnight. I have single
I wrote a simple web service in C# using SharpDevelop (which I just got
I wrote a simple javascript image rotator which picks a random image on each
I wrote a simple javascript function to display a progressbar with the help of
I wrote a simple CMS for one of my clients which does specifically what
In OpenGL, one often writes code like this: glPushMatrix(); // modify the current matrix
What I am doing is very simple. I have a simple 2d Maze game
I have to write a simple program that will draw something in a Windows
I wrote simple silverlight web application using the default ASP test page. If there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.