PDA

View Full Version : Graphics class and DrawBezier



Tugba
01-04-2010, 01:14 PM
Hi,
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.

bezier_rotation.OpenGLView.gr' 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
{
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); 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()
{
GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
GL.glMatrixMode(GL.GL_MODELVIEW);
//herhangi bir matrs işlemi yapılmadan önce koordinat sistemini sıfırlar
GL.glLoadIdentity();
GL.glMatrixMode(GL.GL_PROJECTION);
GL.glLoadIdentity();
//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);

GL.glEnable(GL.GL_COLOR_MATERIAL);
GL.glColorMaterial(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE);
//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);
//enableLight();
drawRefLines();
/*
* public static void glutSolidSphere(
double radius,
int slices,
int stacks
);
Parameters
radius
The radius of the sphere.
slices
The number of subdivisions around the Z axis (similar to lines of longitude).
stacks
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);
GL.glFlush();
*/

}



protected override void InitGLContext()
{
GL.glShadeModel(GL.GL_SMOOTH);
GL.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
GL.glClearDepth(1.0);
GL.glEnable(GL.GL_DEPTH_TEST);
//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.
GL.glDepthFunc(GL.GL_LEQUAL);
//glHint =perspektif hesaplamaları
GL.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
}
/*
public void enableLight()
{
GL.glEnable(GL.GL_LIGHTING);
GL.glEnable(GL.GL_LIGHT0);
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);
GL.glFrontFace(GL.GL_CCW);
GL.glEnable(GL.GL_COLOR_MATERIAL);
GL.glColorMaterial(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE);
}
*/
public void drawRefLines()
{
//void glColor3d(GLdouble red, GLdouble green, GLdouble blue);
GL.glColor3d(1.0, 0.0, 0.0);
GL.glBegin(GL.GL_LINES);
GL.glVertex3d(+1000.0, 0.0, 0.0);
GL.glVertex3d(-1000.0, 0.0, 0.0);
GL.glEnd();
GL.glColor3d(0.0, 1.0, 0.0);
GL.glBegin(GL.GL_LINES);
GL.glVertex3d(0.0, +1000.0, 0.0);
GL.glVertex3d(0.0, -1000.0, 0.0);
GL.glEnd();
GL.glColor3d(0.0, 0.0, 1.0);
GL.glBegin(GL.GL_LINES);
GL.glVertex3d(0.0, 0.0, +1000.0);
GL.glVertex3d(0.0, 0.0, -1000.0);
GL.glEnd();
gr.DrawBezier(pn, p1, p2, p3, p4);

}


}
}__________________

Brolingstanz
01-07-2010, 03:02 AM
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.

Shinta
01-08-2010, 02:18 AM
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.

Stephen A
01-08-2010, 08:55 AM
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 (http://www.opentk.com) 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.