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

  • Home
  • SEARCH
  • 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 7189349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:17:00+00:00 2026-05-28T19:17:00+00:00

I am using OpenGL/GLUT to implement Bresenham’s line drawing algorithm and having some issues

  • 0

I am using OpenGL/GLUT to implement Bresenham’s line drawing algorithm and having some issues with seemingly arbitrary artifacts showing up. Here is an example:

This should be one line

Here is some code that I think may be relevant. I didn’t include the code that populates the vertex buffer because I’m 99% sure it’s right and have rewritten it. The problem came up one I started using the GLUT mouse callbacks.

 void Line::draw()
 {
     // Bind program and buffer
     glUseProgram(program);
     glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);

     // Get position attribute location
     GLuint vertexPosLoc = glGetAttribLocation(
                               program,
                               "position");

     // Enable attribute
     glEnableVertexAttribArray(vertexPosLoc);

     // Associate vertex position with attribute
     glVertexAttribPointer(vertexPosLoc, 2, GL_FLOAT, GL_FALSE, 0, 0);

     glDrawArrays(GL_POINTS, 0, vertexDataSize);

     // Reset the program
     glDisableVertexAttribArray(vertexPosLoc);
     glBindBuffer(GL_ARRAY_BUFFER, 0);
     glUseProgram(0);
 }



 void display()
 {
     // Clear the color buffer and the depth buffer
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

     vector<Line*>::iterator it;
     for(it = lines.begin(); it < lines.end(); it++)
     {
         (*it)->draw();
     }

     // Draw the temporary line
     if(line)
     {
         line->draw();
     }

     // Swap buffers
     glutSwapBuffers();
 }

void mouseClick(int button, int state, int x, int y)
{
    int viewVals[4];
    glGetIntegerv(GL_VIEWPORT, viewVals);
    y = viewVals[3] - y;
    if(button != GLUT_LEFT_BUTTON)
    {   
        return;
    }   
    if(state == GLUT_DOWN)
    {   
        x1 = x;
        y1 = y;
    }   
    else
    {   
        lines.push_back(line);
        line = NULL;
    }   

    glutPostRedisplay();
}

void mouseMotion(int x, int y)
{
    int viewVals[4];
    glGetIntegerv(GL_VIEWPORT, viewVals);
    y = viewVals[3] - y;

    // Delete the previous line
    delete line;

    // Create a new line
    line = new Line(x1,y1,x,y);
    line->setProgram(program);

    glutPostRedisplay();
}

The idea is that you click a point and the line goes from that point to the point you release. Before I added that functionality along with the glutPostRedisplay() calls, the line drawing seemed to work fine.

In the above picture, the line that was intended to be drawn was the one on the left. It worked, but other artifacts appeared. They aren’t in the vertex buffer either, I’ve checked.

Any ideas where they are coming from?

  • 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-28T19:17:02+00:00Added an answer on May 28, 2026 at 7:17 pm

    The third parameter to glDrawArrays() should be the number of points. Are you perhaps passing the number of floats?

    (This would cause you to draw twice as many points as you intend, since each vertex in the buffer has two float values. The extra points would have junk values.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some trouble implementing looking up and down using the mouse in openGl,
Using OpenGL /GLUT how would I detect if two keys, say 'a' and 'j'
I've been programming opengl using glut as my window handler, lately i've been thinking
What are the basic steps to compile an OpenGL application using GLUT (OpenGL Utility
I'm making a Win32 application with OpenGL in the main window (not using GLUT).
I'm using OpenGL and drawing polygons in a 2D view. How could I blur
I am implementing a simple board game (Breakthrough) using OpenGL (plus GLUT and GLUI).
I'm using opengl, using the GLUT and GLEW libraries to create a plugin for
I'm trying to make a simple textured rectangular in OpenGL using SOIL and GLUT.
I'm interested in making the mouse cursor invisible while using opengl + glut, how

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.