Jump to content
  • Advertisement
Sign in to follow this  
walruz

OpenGL Draw image using openGL in .NET application

This topic is 1061 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am learning OpenGL. I have very little experience.

What I trying to achieve: I am trying to do draw image from bmp-file on top of my .NET windows form using openGL.

Here is code sample:
 

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Configuration;
    using System.Drawing.Imaging;
    
    namespace WindowsTestCs
    {
        public partial class MainForm : Form
        {
            private Graphics m_graphics;
            private IntPtr m_hDC;
            private IntPtr m_hRC;
    
            private UInt32 m_texture;
    
            #region Properties
    
            protected override CreateParams CreateParams
            {
                get
                {
                    CreateParams cp = base.CreateParams;
    
                    unchecked
                    {
                        cp.Style = (int)(User32.WS_VISIBLE | User32.WS_POPUP);
                    }
    
                    return cp;
                }
            }
    
            #endregion
    
            public MainForm()
            {
                InitializeComponent();
    
                long _long = User32.GetWindowLong(Handle, User32.GWL_EXSTYLE);
                _long = _long ^ User32.WS_EX_LAYERED;
    
                User32.SetWindowLong(Handle, User32.GWL_EXSTYLE, Convert.ToInt32(_long));
                User32.SetLayeredWindowAttributes(Handle, 0, 0, User32.LWA_COLORKEY);
    
                this.m_graphics = Graphics.FromHwnd(Handle);
                this.m_hDC = this.m_graphics.GetHdc();
                this.m_hRC = CreateHGLRC(this.m_hDC);
    
                OpenGL32.wglMakeCurrent(this.m_hDC, this.m_hRC);
    
                OpenGL32.glEnable(OpenGL32.GL_ALPHA_TEST);
                OpenGL32.glEnable(OpenGL32.GL_DEPTH_TEST);
                OpenGL32.glEnable(OpenGL32.GL_COLOR_MATERIAL);
    
                OpenGL32.glEnable(OpenGL32.GL_LIGHTING);
                OpenGL32.glEnable(OpenGL32.GL_LIGHT0);
    
                OpenGL32.glEnable(OpenGL32.GL_BLEND);
                OpenGL32.glBlendFunc(OpenGL32.GL_SRC_ALPHA, OpenGL32.GL_ONE_MINUS_SRC_ALPHA);
                OpenGL32.glClearColor(0, 0, 0, 0);
    
                OpenGL32.glViewport(0, 0, Width, Height);
                OpenGL32.glMatrixMode(OpenGL32.GL_PROJECTION);
                OpenGL32.glLoadIdentity();
    
                OpenGL32.glMatrixMode(OpenGL32.GL_MODELVIEW);
                OpenGL32.glLoadIdentity();
    
                // Attempt tp load texture
                Bitmap contour = global::WindowsTestCs.Properties.Resources.RMP;
                BitmapData data = contour.LockBits(new Rectangle(0, 0, contour.Width, contour.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
    
                OpenGL32.glGenTextures(1, out m_texture);
    
                OpenGL32.glEnable(OpenGL32.GL_TEXTURE_2D);
                OpenGL32.glBindTexture(OpenGL32.GL_TEXTURE_2D, m_texture);
                OpenGL32.glTexImage2D(OpenGL32.GL_TEXTURE_2D, 0, OpenGL32.GL_RGBA, Width, Height, 0, OpenGL32.GL_LUMINANCE, OpenGL32.GL_UNSIGNED_BYTE, data.Scan0);
                OpenGL32.glPixelStorei(OpenGL32.GL_UNPACK_SWAP_BYTES, OpenGL32.GL_FALSE);
                OpenGL32.glPixelStorei(OpenGL32.GL_UNPACK_LSB_FIRST, OpenGL32.GL_TRUE);
                OpenGL32.glPixelStorei(OpenGL32.GL_UNPACK_ROW_LENGTH, 0);
                OpenGL32.glPixelStorei(OpenGL32.GL_UNPACK_SKIP_PIXELS, 0);
                OpenGL32.glPixelStorei(OpenGL32.GL_UNPACK_SKIP_ROWS, 0);
                OpenGL32.glPixelStorei(OpenGL32.GL_UNPACK_ALIGNMENT, 1);
    
                OpenGL32.glTexParameteri(OpenGL32.GL_TEXTURE_2D, OpenGL32.GL_TEXTURE_MIN_FILTER, OpenGL32.GL_LINEAR);
                OpenGL32.glTexParameteri(OpenGL32.GL_TEXTURE_2D, OpenGL32.GL_TEXTURE_MAG_FILTER, OpenGL32.GL_LINEAR);
    
                contour.UnlockBits(data);
            }
    
            protected override void OnFormClosing(FormClosingEventArgs e)
            {
                base.OnFormClosing(e);
    
                OpenGL32.wglMakeCurrent(IntPtr.Zero, IntPtr.Zero);
                OpenGL32.wglDeleteContext(m_hRC);
    
                m_graphics.ReleaseHdc();
                m_graphics.Dispose();
            }
    
            private IntPtr CreateHGLRC(IntPtr hDC)
            {
                IntPtr hRC = IntPtr.Zero;
    
                UInt32 dwFlags = OpenGL32.PFD_SUPPORT_OPENGL | OpenGL32.PFD_DRAW_TO_WINDOW;
    
                PIXELFORMATDESCRIPTOR pfd = new PIXELFORMATDESCRIPTOR();
                pfd.nSize = (ushort)Marshal.SizeOf(pfd);
                pfd.nVersion = 1;
                pfd.dwFlags = dwFlags;
                pfd.iPixelType = OpenGL32.PFD_TYPE_RGBA;
                pfd.cColorBits = 24;
                pfd.cDepthBits = 32;
                pfd.iLayerType = OpenGL32.PFD_MAIN_PLANE;
    
                int PixelFormat = GDI32.ChoosePixelFormat(hDC, ref pfd);
                if (PixelFormat != 0)
                {
                    bool bResult = GDI32.SetPixelFormat(hDC, PixelFormat, ref pfd);
                    if (bResult)
                        hRC = OpenGL32.wglCreateContext(hDC);
                }
    
                return hRC;
            }
    
            private bool renderSC()
            {
                OpenGL32.glClear(OpenGL32.GL_COLOR_BUFFER_BIT | OpenGL32.GL_DEPTH_BUFFER_BIT);
    
                // drawing anything (just to be sure that openGL is initialized properly)
                OpenGL32.glPushMatrix();
    
                OpenGL32.glColor3f(0, 1, 1);
                OpenGL32.glBegin(OpenGL32.GL_TRIANGLES);                // Drawing Using Triangles
                OpenGL32.glColor3f(1.0f, 0.0f, 0.0f);                   // Set The Color To Red
                OpenGL32.glVertex3f(0.0f, 1.0f, 0.0f);                  // Top
                OpenGL32.glColor3f(0.0f, 1.0f, 0.0f);                   // Set The Color To Green
                OpenGL32.glVertex3f(-1.0f, -1.0f, 0.0f);                // Bottom Left
                OpenGL32.glColor3f(0.0f, 0.0f, 1.0f);                   // Set The Color To Blue
                OpenGL32.glVertex3f(1.0f, -1.0f, 0.0f);                 // Bottom Right
                OpenGL32.glEnd();
    
                OpenGL32.glPopMatrix();
    
                // trying to draw image
                Bitmap contour = global::WindowsTestCs.Properties.Resources.RMP;
    
                OpenGL32.glViewport(0, 0, contour.Width, contour.Height);
    
                OpenGL32.glMatrixMode(OpenGL32.GL_PROJECTION);
                OpenGL32.glPushMatrix();
                OpenGL32.glLoadIdentity();
                OpenGL32.glOrtho(0.0, contour.Width, 0, contour.Height, -1.0, 1.0);
    
                OpenGL32.glMatrixMode(OpenGL32.GL_MODELVIEW);
                OpenGL32.glPushMatrix();
                OpenGL32.glLoadIdentity();
    
                OpenGL32.glEnable(OpenGL32.GL_BLEND);
                OpenGL32.glEnable(OpenGL32.GL_TEXTURE_2D);
                OpenGL32.glBlendFunc(OpenGL32.GL_ZERO, OpenGL32.GL_SRC_COLOR);
    
                OpenGL32.glBindTexture(OpenGL32.GL_TEXTURE_2D, m_texture);
                OpenGL32.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
    
                OpenGL32.glBegin(OpenGL32.GL_QUADS);
    
                OpenGL32.glTexCoord2f(0, 1);
                OpenGL32.glVertex3f(0.0f, 0.0f, 0.0f);
                OpenGL32.glTexCoord2f(0, 0);
                OpenGL32.glVertex3f(0.0f, contour.Height, 0.0f);
                OpenGL32.glTexCoord2f(1, 0);
                OpenGL32.glVertex3f(Width, contour.Height, 0.0f);
                OpenGL32.glTexCoord2f(1, 1);
                OpenGL32.glVertex3f(contour.Width, 0.0f, 0.0f);
    
                OpenGL32.glEnd();
    
                OpenGL32.glDisable(OpenGL32.GL_TEXTURE_2D);
                OpenGL32.glDisable(OpenGL32.GL_BLEND);
    
                OpenGL32.glMatrixMode(OpenGL32.GL_PROJECTION);
                OpenGL32.glPopMatrix();
                OpenGL32.glMatrixMode(OpenGL32.GL_MODELVIEW);
                OpenGL32.glPopMatrix();
    
                OpenGL32.glFlush();
    
                return false;
            }
    
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
    
                renderSC();
    
                GDI32.SwapBuffers(m_hDC);
            }
    
            protected override void WndProc(ref Message m)
            {
                if (User32.WM_ERASEBKGND == m.Msg)
                    m.Result = IntPtr.Zero;
                else
                    base.WndProc(ref m);
            }
        }
    }

Unfortunately, I don't see image on the result form. Nevertheless I do see my openGL drawing.

Could you please help me understand what am I doing wrong?

Share this post


Link to post
Share on other sites
Advertisement

first of all diable alpha test then disable blending then set glClearColor alpha value to 1.0, also change colorBits to 32. and use double buffered window, then you must fix your projection and view matrix, and polygon data itself, somehow i am 99% sure you wont be able to see the thing you want to draw coz viewmatrix is not even looking at that poly.

 

disable gl_lighting

 

and texture loading looks unusual if you really need to use FFP gentextures after enabling texture_2d, also i dont know why you set these pixelstorei things.

 

AND when trying to draw something using ortho (i reckon you want to draw a single quad on screen) gie it pixel coords 1 means 1 pixel not anything else. also z must be between -1 1

 

 

glFlush isnt needed here. 

 

 

after fixing all of these check if drawing code is even executed if yes dont draw the background of the control

 

after seeing something, quit using FFP

 

Edit:

void __fastcall glPush2DDrawing()
{

glPushAttrib(GL_DEPTH_TEST);
glDisable(GL_DEPTH_TEST);


glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();

glOrtho(0, WINDOW_WIDTH, 0, WINDOW_HEIGHT, -1000.0f, 1000.0f); // Change the projection matrix using an orthgraphic projection
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();





}


void __fastcall glPop2DDrawing()
{
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glPopAttrib();
}

its an old code and in c++ but

to setup ortho mode:

glPush2DDrawing();

draw your 2d stuff here

glPop2DDrawing();

Edited by WiredCat

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!