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 8724179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:46:28+00:00 2026-06-13T07:46:28+00:00

I’m trying for exercise to do the following: there is an array of points,

  • 0

I’m trying for exercise to do the following: there is an array of points, expressed through couples of GLfloats.The user should click in a point in the window to make another points appear.When a point is added, the window shall be redrawn and the points shall display connected with a line.So the user shall be able to draw lines clicking on some locations of the windows.
In this code whenever there’s a click I push a point in the vector, but the points is not added, so I just see the gray window (APPLE_GRAY defined in header) without the points.

#include <OpenGL/OpenGL.h>
#include <GLUT/GLUT.h>
#include <stdio.h>
#include <stdlib.h>
#include "utility.h"

const int width=500;
const int height=500;

GLfloat* points;
size_t size=100;
int count=0;

void pushPoint(GLfloat x, GLfloat y)
{
    count++;
    if(count>size)
    {
        size+=100;
        points=(GLfloat*)realloc(points,2*size*sizeof(GLfloat));
    }
    points[2*count-2]=x;
    points[2*count-1]=y;
}

void init()
{
    points=(GLfloat*)malloc(2*size*sizeof(GLfloat));
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glLoadIdentity();
    glOrtho(0, width, height, 0, 0, 1);
    glViewport(0, 0, 500, 500);
    glEnable(GL_DEPTH_TEST);
    glMatrixMode(GL_PROJECTION);
}

void display()
{
    glClearColor(APPLE_GRAY);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor4f(RED);
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(2, GL_FLOAT, 0, points);
    glDrawElements(GL_LINE_LOOP,count,GL_FLOAT,points);
    glFlush();
    glDisableClientState(GL_VERTEX_ARRAY);
}

void mouse(int button, int state, int x, int y)
{
    if(state)
    {
        pushPoint(x, y);
        glutPostRedisplay();
    }
}

void motion(int x, int y)
{
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(width,height);
    glutCreateWindow("Test");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    init();
    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-06-13T07:46:29+00:00Added an answer on June 13, 2026 at 7:46 am

    Again, glutInitDisplayMode() needs to be called before glutCreateWindow().

    GL_FLOAT is not a valid value for type in glDrawElements(). Also, points is not an index array.

    Use glDrawArrays():

    #include <GL/glut.h>
    
    const int width=500;
    const int height=500;
    
    GLfloat* points;
    size_t size=100;
    int count=0;
    
    void pushPoint(GLfloat x, GLfloat y)
    {
        count++;
        if(count>size)
        {
            size+=100;
            points=(GLfloat*)realloc(points,2*size*sizeof(GLfloat));
        }
        points[2*count-2]=x;
        points[2*count-1]=y;
    }
    
    void init()
    {
        points=(GLfloat*)malloc(2*size*sizeof(GLfloat));
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, width, height, 0, 0, 1);
        glEnable(GL_DEPTH_TEST);
    }
    
    void display()
    {
        glClearColor(0,0,0,0);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glColor3ub(255,0,0);
        glEnableClientState(GL_VERTEX_ARRAY);
        glVertexPointer(2, GL_FLOAT, 0, points);
        glDrawArrays(GL_LINE_LOOP,0,count);
        glDisableClientState(GL_VERTEX_ARRAY);
        glutSwapBuffers();
    }
    
    void mouse(int button, int state, int x, int y)
    {
        if(state)
        {
            pushPoint(x, y);
            glutPostRedisplay();
        }
    }
    
    int main(int argc, char** argv)
    {
        glutInit(&argc, argv);
        glutInitWindowPosition(100, 100);
        glutInitWindowSize(width,height);
        glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
        glutCreateWindow("Test");
        glutDisplayFunc(display);
        glutMouseFunc(mouse);
        init();
        glutMainLoop();
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I have an array which has BIG numbers and small numbers in it. I

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.