0

I'm using OpenTK, which is OpenGL in C# so any answer in opengl is satisfying for me...

I've got a 3D object, what i want to do is draw some lines (wireframe) and display only them (I know aobut GLPolygonMode - that's not what i want to do)

I've got my own Line - Drawing algorithm and by using it i want to draw wireframe and display it on the screen. After hours of digging the Internet I found out that i can do it by using backbuffer, but don't know excalty how... here's the sample of my drawing code I draw a pixel on a bitmap (that's not it :()

while (x != x2)
{
    if (d >= 0)
    {
        x += xi;
        y += yi;
        d += ai;
    }
    else
    {
        d += bi;
        x += xi;
    }
    bitmap.SetPixel(x, y, col);
}

Is there any way to draw on a buffer and then display it on the screen? Or is there any other way to do that?

Matt Ellen
  • 11,268
  • 4
  • 68
  • 90
johns
  • 183
  • 1
  • 14
  • 2
    OK not so magic but do we have to know/think of those variables `x, x2, d, col, xi, yi, ai, bi` ? – L.B Jan 14 '14 at 21:34
  • I just removed my previous comments because I think your code looks like the Bresenham's line algorithm. Is that what you are trying to do? – wendelbsilva Jan 14 '14 at 21:40
  • I got Bresenham algorithm, It's drawing on a bitmap and it's workng, I pasted a sample because iwanted you to see that i draw it on a bitmap and I want to draw it on a buffer – johns Jan 14 '14 at 21:42
  • 1
    When I want to display a buffer on the screen, I usually put it (the buffer image) in a quad (covering the screen) as a texture and render it. – wendelbsilva Jan 14 '14 at 21:43
  • 2
    @johns: You will be tempted to replace every call to `SetPixel (...)` with `glDrawPixels (...)` as so many people who try to implement Bresenham's algorithm or things like floodfill on Stack Overflow do, but this is terrible. You should continue drawing into some buffer/bitmap in client memory (without any GL commands) and then when you finally finish drawing your line upload it to GL in the form of a texture or a single call to `glDrawPixels (...)`. Otherwise the API overhead is going to be ridiculous. – Andon M. Coleman Jan 14 '14 at 22:48
  • Qhy do you want to implement your own line drawing algorithm? If you want to draw wireframes you probably want them to interact with the depth buffer, but for this to work efficiently you should use the line drawing methods of OpenGL. Everything else is just pure masochism. – datenwolf Jan 14 '14 at 23:57
  • that's the home asigment for my classes. @AndonM.Coleman what you siad worked, but do you know how to maniupluate it now? I mean can I zoom it/ rotate what i draw? I tryied glRotate, but it doeasn't give any effects – johns Jan 15 '14 at 20:14
  • 1
    @johns: Which part of what I said? :) You cannot rotate something you effectively "blit" using `glDrawPixels (...)`. However, if you go the route of uploading this to a texture, you can transform your texture coordinates with `glMatrixMode (GL_TEXTURE)` and `glRotatef (...)` (you will only be transforming the 2D image though). If you actually want to rotate the line and not the image you rasterized, then you will need to do this yourself in your implementation of Bresenham's Line Algorithm (e.g. transform the starting / ending point of your line segment before rasterizing it). – Andon M. Coleman Jan 15 '14 at 20:22
  • @AndonM.Coleman the part about just calling once DrawPixels(). I've made an array of floats, where i keep the bitmap and draw it. So to sum up, if I want to make a wireframe of a 3d object and manipulate it (rotation, zoom etc.) I have to transform my points before starting drawing? Texture won't work you say? – johns Jan 15 '14 at 20:28
  • 1
    You *might* be able to do this (full 3D rotation) with texture projection, but honestly it is not even worth trying to do it that way. What you really want to do is transform the end-points of your line segment prior to passing them through your software-based rasterization algorithm. It boils down to little more than 2 matrix multiplications and if this is for a class, I imagine this is what the professor wants to see. I know if I were teaching an introductory CG course I would expect to see transformations properly applied ***before*** rasterization. – Andon M. Coleman Jan 15 '14 at 20:34

1 Answers1

1

Considering you're new to OpenGL:

The default configuration of OpenGL uses the double buffering. It means each time you use the OpenTK.Graphics.OpenGL.GL.DrawXXX() methods or SetPixel() or others like that, you are drawing in the back buffer. When you're done with the drawing, you call OpenTK.GLControl.SwapBuffers() and it will switch the back buffer as front buffer, and vice versa. Because front buffer is what is displayed on the screen, you will see your drawing.

And of course you yould not forget the call to OpenTK.Graphics.OpenGL.GL.Begin() with the approriate BeginMode, and OpenTK.Graphics.OpenGL.GL.End().

RawBean
  • 442
  • 5
  • 20