Graphics class and DrawBezier

In the code, below, addition to x, y, z axis I want to draw bezier curve.I don’t know using Graphics class at all.I get this error.’ is never assigned to, and will always have its default value null

Could you help me?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CsGL.OpenGL;
using System.Drawing;

namespace bezier_rotation
    class OpenGLView: OpenGLControl 
        [color:#FF0000]Graphics gr;
        Point p1 = new Point(1, 0);
        Point p2 = new Point(3, 2);
        Point p3 = new Point(4, 5);
        Point p4 = new Point(0, 1);
        Pen pn = new Pen(Brushes.Orange);[/color]        public OpenGLView()
            : base()
        private float angleX = 0;
        private float angleY = 0;
        private float angleZ = 0;
        private static float[/*4*/, /*3*/] controlPoints = {
            {-4.0f, -4.0f, 0.0f},
            {-2.0f,  4.0f, 0.0f},
            { 2.0f, -4.0f, 0.0f},
            { 4.0f,  4.0f, 0.0f}};
        public void setAngleX(float X)
            this.angleX = X;
        public void setAngleY(float Y)
            this.angleY = Y;
        public void setAngleZ(float Z)
            this.angleZ = Z;
        public float getAngleX()
            return this.angleX;
        public float getAngleY()
            return this.angleY;
        public float getAngleZ()
            return this.angleZ;
        public override void glDraw()
            //herhangi bir matrs işlemi yapılmadan önce koordinat sistemini sıfırlar
            //glOrtho(left, right, bottom, top, near far).
             * glOrtho sol ve sag yatay eksendeki maksimum ve minimum degerleri,
             * alt ve ust dikey eksendeki max ve min degerleri , yakin ve uzak 
             * eksenindeki max ve min degerleri gosterir. glOrtho kendi başına kesim
             * hacmini belirlememektedirvar olan kesim hacmini degiştirmektedir .Var 
             * olan kesim hacmi glMatrixMode ile tanımlanan projeksiyon matris i 
             * tanımlamaktadır, glOrtho ise var olan kesim hacmini belirtilen kesim hacmi
             * ile belirtilen matris i carpmaktadır .
            GL.glOrtho(-10.0, 10.0, -10.0, 10.0, -10.0, 10.0);
            GL.glViewport(0, 0, this.Size.Width, this.Size.Height);

            //void glRotatef(GLfloat angle, GLfloat x, GLfloat y, GLfloat z);
            GL.glRotatef(angleX, 1, 0, 0);
            GL.glRotatef(angleY, 0, 1, 0);
            GL.glRotatef(angleZ, 0, 0, 1);
             * public static void glutSolidSphere(
               double radius,
               int slices,
               int stacks
            The radius of the sphere. 
            The number of subdivisions around the Z axis (similar to lines of longitude). 
            The number of subdivisions along the Z axis (similar to lines of latitude). 
            //GLUT.glutSolidSphere(5.0, 32, 32);
            GL.glColor3f(0.0f, 0.0f, 0.0f);
            GL.glMap1f(GL.GL_MAP1_VERTEX_3, 0.0f, 1.0f, 3, 4, GL.controlPoints[0]);
            GL.glMapGrid1f(30, 0.0f, 1.0f);
            GL.glEvalMesh1(GL.GL_LINE, 0, 30);

        protected override void InitGLContext()
            GL.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
            //glDepthFunc fonksiyonu ile derinlik testinde kullanılacak 
            //fonksiyonu belirtiyoruz. Burada parametre olarak geçilen 
            //GL_LEQUAL sembolik sabitinin belirttiği derinlik testi fonksiyonu, 
            //o an gelen z değeri, tamponda saklanan z değerine eşit ise veya ondan
            //daha küçükse başarılı olur. 
            //glHint =perspektif hesaplamaları
        public void enableLight()
            float[] ambientLight = { 0.5f, 0.5f, 0.5f, 1.0f };
            float[] diffuseLight = { 0.9f, 0.9f, 0.9f, 0.0f };
            float[] specularLight = { 0.5f, 0.5f, 0.5f, 1.0f };
            float[] position = { 30.0f, 20.0f, 30.0f, 1.0f };
            float[] mat_shinnes = { 100.0f };
            GL.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, ambientLight);
            GL.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, diffuseLight);
            GL.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, specularLight);
            GL.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, position);
            GL.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_SHININESS, mat_shinnes);
        public void drawRefLines()
            //void glColor3d(GLdouble red, GLdouble green, GLdouble blue); 
            GL.glColor3d(1.0, 0.0, 0.0);
            GL.glVertex3d(+1000.0, 0.0, 0.0);
            GL.glVertex3d(-1000.0, 0.0, 0.0);
            GL.glColor3d(0.0, 1.0, 0.0);
            GL.glVertex3d(0.0, +1000.0, 0.0);
            GL.glVertex3d(0.0, -1000.0, 0.0);
            GL.glColor3d(0.0, 0.0, 1.0);
            GL.glVertex3d(0.0, 0.0, +1000.0);
            GL.glVertex3d(0.0, 0.0, -1000.0);
           [color:#FF6666] gr.DrawBezier(pn, p1, p2, p3, p4);[/color]            


I can only assume your class is based on ‘CsGL’? Not familiar with it myself.

The warning you’re getting is from ‘gr’ not having a public property set accessor (use property accessors in lieu of your C++ style counterparts) and not having been assigned a value privately.

I’ll betcha your base class works some magic for you to create and initialize your GL context during construction, but check any documentation to be sure.

That’s me paltry pittance.

Don’t mix OpenGL and GDI (Graphics is the C# pendant to GDI Device Context).

‘gr’ is just a reference to the actual thing, but since it is not set to, let’s say, the graphic object of your OpenGLView, it still is ‘null’ and would crash your app at runtime, if used.

But even if u set ‘gr’ to the OpenGLView’s graphics, I doubt you would see your bezier curve or at least produce some ugly interferences with the opengl rendering.

Try rendering your bezier curve with opengl drawing functions.

Shinta is correct, this approach is not going to work. What you want to do is create a System.Drawing.Bitmap, initialize System.Drawing.Graphics on it, render your bezier and finally load the contents of the bitmap to an OpenGL texture for display. This won’t be terribly fast (System.Drawing is not hardware accelerated), but the approach works fine.

Additionally, CsGL is many many years out of date and has several known and unknown bugs that will never be fixed. If I may suggest, OpenTK is a modern, supported OpenGL binding for C#.

Edit: OpenTK also supplies a “Bezier” class which you can render directly with OpenGL commands. This approach will be hardware-accelerated, hence much faster.