DrawPixels?

Jan 9, 2012 at 1:40 PM

in opengl red book. image.c example:

for (i = 0; i < checkImageHeight; i++) {
      for (j = 0; j < checkImageWidth; j++) {
         c = ((((i&0x8)==0)^((j&0x8))==0))*255;
         checkImage[i][j][0] = (GLubyte) c;
         checkImage[i][j][1] = (GLubyte) c;
         checkImage[i][j][2] = (GLubyte) c;
         checkImage[i][j][3] = (GLubyte) 255;
      }
   }

how i can implement drawpixels?

           OpenGL gl = this.openGLControl1.OpenGL;
            gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
         //  ChangeSize(this.openGLControl1.Width, this.openGLControl1.Height);
            gl.RasterPos(0, 0);
            gl.DrawPixels(checkimagewidth, checkimageheight, OpenGL.GL_RGB, OpenGL.GL_UNSIGNED_BYTE, checkimage);
            gl.Flush();

 

sorry,my poor english

Jan 14, 2012 at 10:43 PM
It seems that DrawPixels is only defined for float[] data, not byte[] data. I think it should at least be defined for uint[], ushort[] and byte[] as well. Also I think the GL type of the data should be inferred, i.e. instead of:
public void DrawPixels(int width, int height, uint format, uint type, float[] pixels)

the signature should be:

public void DrawPixels(int width, int height, uint format, float[] pixels)

and GL_FLOAT should be inferred for the "type".

Also I really need the IntPtr version so I can glDrawPixels with a null "pixels" argument so the pixels come from a pixel buffer object (PBO) instead. That version, of course would have a type parameter since the type can't actually be inferred from IntPtr, especially if it's null!.

Here are the changes I made to OpenGL.cs:

After the following line:

        [DllImport(LIBRARY_OPENGL)] protected static extern void glDrawPixels(int width, int height, uint format, uint type, float[] pixels);

I added:

        [DllImport(LIBRARY_OPENGL)] protected static extern void glDrawPixels(int width, int height, uint format, uint type, uint[] pixels);
        [DllImport(LIBRARY_OPENGL)] protected static extern void glDrawPixels(int width, int height, uint format, uint type, ushort[] pixels);
        [DllImport(LIBRARY_OPENGL)] protected static extern void glDrawPixels(int width, int height, uint format, uint type, byte[] pixels);
        [DllImport(LIBRARY_OPENGL)] protected static extern void glDrawPixels(int width, int height, uint format, uint type, IntPtr pixels);

I removed this definition:

	/// <summary>
	/// Draws a rectangle of pixel data at the current raster position.
	/// </summary>
	/// <param name="width">Width of pixel data.</param>
	/// <param name="height">Height of pixel data.</param>
	/// <param name="format">Format of pixel data.</param>
	/// <param name="type">Type of pixel data.</param>
	/// <param name="pixels">Pixel data buffer.</param>
	public void DrawPixels(int width, int height, uint format, uint type,  float[] pixels)
	{
	    PreGLCall();
	    glDrawPixels(width, height, format, type, pixels);
	    PostGLCall();
	}

and replaced it with these:

 

        /// <summary>
        /// Draws a rectangle of pixel data at the current raster position.
        /// </summary>
        /// <param name="width">Width of pixel data.</param>
        /// <param name="height">Height of pixel data.</param>
        /// <param name="format">Format of pixel data.</param>
        /// <param name="pixels">Pixel data buffer.</param>
        public void DrawPixels(int width, int height, uint format, float[] pixels)
        {
            PreGLCall();
            glDrawPixels(width, height, format, GL_FLOAT, pixels);
            PostGLCall();
        }

        /// <summary>
        /// Draws a rectangle of pixel data at the current raster position.
        /// </summary>
        /// <param name="width">Width of pixel data.</param>
        /// <param name="height">Height of pixel data.</param>
        /// <param name="format">Format of pixel data.</param>
        /// <param name="pixels">Pixel data buffer.</param>
        public void DrawPixels(int width, int height, uint format, uint[] pixels)
        {
            PreGLCall();
            glDrawPixels(width, height, format, GL_UNSIGNED_INT, pixels);
            PostGLCall();
        }

        /// <summary>
        /// Draws a rectangle of pixel data at the current raster position.
        /// </summary>
        /// <param name="width">Width of pixel data.</param>
        /// <param name="height">Height of pixel data.</param>
        /// <param name="format">Format of pixel data.</param>
        /// <param name="pixels">Pixel data buffer.</param>
        public void DrawPixels(int width, int height, uint format, ushort[] pixels)
        {
            PreGLCall();
            glDrawPixels(width, height, format, GL_UNSIGNED_SHORT, pixels);
            PostGLCall();
        }

        /// <summary>
        /// Draws a rectangle of pixel data at the current raster position.
        /// </summary>
        /// <param name="width">Width of pixel data.</param>
        /// <param name="height">Height of pixel data.</param>
        /// <param name="format">Format of pixel data.</param>
        /// <param name="pixels">Pixel data buffer.</param>
        public void DrawPixels(int width, int height, uint format, byte[] pixels)
        {
            PreGLCall();
            glDrawPixels(width, height, format, GL_UNSIGNED_BYTE, pixels);
            PostGLCall();
        }

        /// <summary>
        /// Draws a rectangle of pixel data at the current raster position.
        /// </summary>
        /// <param name="width">Width of pixel data.</param>
        /// <param name="height">Height of pixel data.</param>
        /// <param name="format">Format of pixel data.</param>
        /// <param name="type">The GL data type.</param>
        /// <param name="pixels">Pixel data buffer.</param>
        public void DrawPixels(int width, int height, uint format, uint type, IntPtr pixels)
	{
	    PreGLCall();
	    glDrawPixels(width, height, format, type, pixels);
	    PostGLCall();
	}
Coordinator
Jan 16, 2012 at 3:49 PM

Very sensible. Thanks for sharing this - I will change the main codebase so that we keep this improvement.

http://sharpgl.codeplex.com/workitem/497

Jan 18, 2012 at 3:13 PM

dear mbrca, thanks your help.