quida
quida

Reputation: 109

Draw pixel in C

I'm trying to draw triangle in C using opengl but without standard opengl functions for drawing triangles and lines. (I use ubuntu.)

I used Bresenham line drawing algoritm. I suppose, that the code of this algorithm itself is fine, because it draws tringle but it look like on following picture. enter image description here

And I need to make it look like this

enter image description here

In the line drawing algorithm I draw the line pixel by pixel using following function:

void setPixel(int x, int y) {
    glBegin(GL_POINTS);
    glVertex2i(x,y);
    glEnd();
    glFlush();
}

Could you help me with the following? : Why aren't the lines smooth? Why is there a space between pixels? Why aren't the lines thinner?

Upvotes: 4

Views: 3737

Answers (3)

Andon M. Coleman
Andon M. Coleman

Reputation: 43319

First of all, do not use point primitives to represent pixels. If you have anti-aliasing enabled (GL_MULTISAMPLE or the older GL_POINT_SMOOTH), then they are not actually rectangular in nature.

Since you are using an older version of OpenGL here, you still have direct access to raster functions. That is, you can skip primitive assembly and draw relative to glRasterPos2i (...). glWindowPos2i (...) will even skip coordinate transform and let you draw directly in window coordinates without messing with projection/modelview matrices (assuming you have an OpenGL 1.4+ implementation).

Here is how you could accomplish the same thing as in your sample code:

void setPixel(int x, int y) {
  const GLubyte color [] = { 255, 255, 255, 255 };

  glWindowPos2i (x, y);
  glDrawPixels  (1, 1, GL_RGBA, GL_UNSIGNED_BYTE, color);

  glFlush();
}

If you don't have access to glWindowPos2i, then use an orthographic projection matrix equal to your window's dimensions and an identity modelview matrix and replace glWindowPos2i (...) with glRasterPos2i (...).

Neither solution will be high-performance, but at least you will not have to worry about things like point size, depth tests, etc.

Upvotes: 0

Sam
Sam

Reputation: 7858

  • As stated by @Nemanja Boric, points aren't simple pixels. One does usually draw such squares using glBegin(GL_QUADS), since the point size is limited.

  • If this is not a line drawing exercise, use glBegin(GL_LINE_LOOP);

    • glEnable(GL_LINE_SMOOTH); for smooth lines

    • glLineWidth(); to adjust the line width. However, with GL_LINE_SMOOTH enabled, the line looks thinner because of antialiasing.

Upvotes: 1

Nemanja Boric
Nemanja Boric

Reputation: 22157

You may want to change point size with

glPointSize(1.0f); // or similar value

before glBegin call.

Upvotes: 2

Related Questions