Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# OpenGL
Salaam
i am trying to draw something(anything at all) with OpenGL in visual studio 2010, project runs without error but nothing gets drawn, nothing happens at all.
 
Here is the code:
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 Tao.OpenGl;
using Tao.Platform.Windows;
 
namespace opengl
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            SimpleOpenGlControl gl = new SimpleOpenGlControl();
            gl.InitializeContexts();
        }
 
     
        private void gl_Paint(object sender,PaintEventArgs e)
        {
            
            Gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
           
            Gl.glBegin(Gl.GL_POLYGON);
            Gl.glColor3f(1.0f, 0.0f, 1.0f);
            Gl.glVertex3f(0.25f, 0.25f, 0.0f);
            Gl.glVertex3f(0.75f, 0.25f, 0.0f);
            Gl.glVertex3f(0.75f, 0.75f, 0.0f);
            Gl.glVertex3f(0.25f, 0.75f, 0.25f);
            Gl.glEnd();
            Gl.glFlush();
        }
    }
}
someone please suggest what am i missing..!
Posted 10-Jul-12 1:34am
Edited 10-Jul-12 4:17am
lukeer15.1K
v2
Comments
Wes Aday at 10-Jul-12 12:17pm
   
Are you sure that it runs without error? I am not even sure that this would compile. Gl in your gl_Paint method is undefined.
Trak4Net at 10-Jul-12 13:48pm
   
I agree, the gl initialized in Form1_Load is within the scope of the form1_load and goes out of scope after form1_load exits. I don't know how gl_Paint would even get called unless that happens from gl.InitializeContexts, but I don't see a handler attached in the form1_load so don't know how that could even happen. As stated by Wes the variable Gl (remember C# is case sensitive) is not declared in your code sample, unless it is a static object in one of the using namespaces.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I think it is how you have your control declared in the constructor. If you added the SimpleOpenGlControl to the form then the constructor code you are using is pointing to a new reference not the one in the controls collection. If I use the code below I get a white background and it looks like a fuscia colored square. As I suspected Gl variable is a static class in the library...
As you see in the form2_load method I am generating a new object of SimpleOpenGlControl, initializing its context and then adding a handler to the paint event and finally adding the control to the forms control collection. I am not familiar with the OpenGL libraries so cannot give you any advice on drawing, but at least this will show you what you expected to see previously (although I am filling with white instead of black in the glClearColor method)...
 

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 Tao.OpenGl;
using Tao.Platform.Windows;
 
namespace Testing
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
 
        private void Form2_Load(object sender, EventArgs e)
        {
            Tao.Platform.Windows.SimpleOpenGlControl gl = new Tao.Platform.Windows.SimpleOpenGlControl();
            gl.InitializeContexts();
            gl.Paint += new PaintEventHandler(gl_Paint);
            this.Controls.Add(gl);
        }
 
        void gl_Paint(object sender, PaintEventArgs e)
        {
            Gl.glClearColor(255.0f, 255.0f, 255.0f, 255.0f);
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
 
            Gl.glBegin(Gl.GL_POLYGON);
            Gl.glColor3f(1.0f, 0.0f, 1.0f);
            Gl.glVertex3f(0.25f, 0.25f, 0.0f);
            Gl.glVertex3f(0.75f, 0.25f, 0.0f);
            Gl.glVertex3f(0.75f, 0.75f, 0.0f);
            Gl.glVertex3f(0.25f, 0.75f, 0.25f);
            Gl.glEnd();
            Gl.glFlush();
 
        }
    }
}
  Permalink  
Comments
suleman115 at 11-Jul-12 3:08am
   
Thanks for help,your code worked for me.I am a begginer,so don't know much of the technicalities.
I only wanna ask why that backcolor (white in your example) doesn't cover the whole form? And would it do so?
SenoritaMarissa at 30-Jul-13 10:37am
   
hI!! I tried the solution code and yet nothing is getting displayed. Although there are no errors
Trak4Net at 11-Jul-12 3:26am
   
As I mentioned I am not familiar with the openGL libraries. The control has size and location properties so you can set it with code or you might even be able to add the controls to the toolbox in visual studios and drag them onto the form. If you can then you should have access to size and location in the properties window, as well as all of the events available.
I just short cutted it and programmatically added the control.
If you add the control from the toolbox then you do not need to create a new instance in the form load, you might however need to call the initialize contexts though. If you need an example I would be glad to add one tomorrow.
suleman115 at 12-Jul-12 9:27am
   
well that's right, control will have all properties but problem is, toolbox doesn't have "SimpleOpenGLControl" and "choose toolbox items" doesn't offer one, I've checked in .NET components, where it should be(and in all other components, just in case).
Trak4Net at 12-Jul-12 13:58pm
   
In the .NET components tab there is a browse button near bottom. Choose that and find where you downloaded the TAO dll's to. Choose the file TAO.Platform.Windows.dll, now you will have the SimpleOpenGLControl in your items to choose from. I tested it, you still need the controlname.InitializeContexts() in the form load. If I were you I would research on the TAO SDK for more information on how it should be implemented.
Also, check out these links (especially the latter of the two if you haven't already)
 
http://stackoverflow.com/questions/2795442/is-the-tao-framework-dead
 
http://www.opentk.com/
suleman115 at 19-Jul-12 11:36am
   
OK..thanks a lot for all this help...!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,382


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 10 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100