C# glRotate without GLUT

Started by
7 comments, last by Mobby6 16 years, 10 months ago
I am trying to get the normal rotating image on the screen. Here is my following code. I am doing this in C#, which leads to my problem of finding any help on the web for this. It seems most are using C++. Thanks in advance. I left out any rotation attempts I had in there since nothing was working. Thanks


    public class glDraw
    {
        public Form1 mainWin = null;
        public System.Windows.Forms.Panel panel = null;

        public bool m_bAttemptedInitialization = false;
        public IntPtr m_IntPtr_HWND = IntPtr.Zero;
        public IntPtr m_IntPtr_HDC = IntPtr.Zero;
        public IntPtr m_IntPtr_HGLRC = IntPtr.Zero;

        double TxtScale = 0.05;
        public int eyeDistance = 100;
        double scaledTxtHeight = 0.0;
        public int angle = 1;
        public glDraw()
        {
        }


        public void OneTimeInitialize(IntPtr IntPtr_HWND)
        {
            m_IntPtr_HWND = IntPtr_HWND;
            glInitializer.Initializer.InitOpenGL("xyz", m_IntPtr_HWND, ref m_IntPtr_HDC, ref m_IntPtr_HGLRC);
            scaledTxtHeight = glInitializer.Initializer.fontMetrics[(uint)'A'].gmfBlackBoxY * TxtScale;
        }

        public void pnlResize()
        {
            if (!m_bAttemptedInitialization)
            {
                m_bAttemptedInitialization = true;
                OneTimeInitialize(panel.Handle);
            }
            // Set Viewport dimensions
            GL.glViewport(0, 0, panel.Width, panel.Height);
            glSetupRC();
            panel.Invalidate(false);
        }

        public void pnlPaint(System.Windows.Forms.PaintEventArgs e)
        {
            if (!m_bAttemptedInitialization)
            {
                m_bAttemptedInitialization = true;
                OneTimeInitialize(panel.Handle);
                glSetupRC();
            }
            if (m_IntPtr_HGLRC != IntPtr.Zero)
            {
                glRenderScene();
                WGL.wglSwapBuffers(m_IntPtr_HDC);
            }
        }

        private void glSetupRC()
        {
            GL.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
            GL.glColor3d(0.0, 1.0, 0.0);

            GL.glMatrixMode(GL.GL_PROJECTION);
            GL.glLoadIdentity();
            GLU.gluPerspective(45, (panel.Width*1.0)/panel.Height, 10, 500);
            GL.glMatrixMode(GL.GL_MODELVIEW);

            GL.glFrontFace(GL.GL_CCW);
            GL.glEnable(GL.GL_DEPTH_TEST);
            GL.glEnable(GL.GL_COLOR_MATERIAL); 
        }
        private void DrawTriangle()
        {
            GL.glBegin(GL.GL_TRIANGLES);
            GL.glColor3d(.8, .6, .0);
            GL.glVertex3d(-4.0, 8.0, 4.0);
            GL.glVertex3d(2.0, 15.0, 0.0);
            GL.glVertex3d(8.0, 8.0, 4.0);

            GL.glColor3d(.6, .8, .0);
            GL.glVertex3d(-4.0, 8.0, 4.0);
            GL.glVertex3d(2.0, 15.0, 0.0);
            GL.glVertex3d(-4.0, 8.0, -4.0);

            GL.glColor3d(0.6, 0.8, .0);
            GL.glVertex3d(-4.0, 8.0, -4.0);
            GL.glVertex3d(2.0, 15.0, 0.0);
            GL.glVertex3d(8.0, 8.0, -4.0);


            GL.glColor3d(.6, .8, .0);
            GL.glVertex3d(8.0, 8.0, -4.0);
            GL.glVertex3d(2.0, 15.0, 0.0);
            GL.glVertex3d(8.0, 8.0, 4.0);
            GL.glEnd();
        }
        private void glRenderScene()
        {
            GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
            GL.glLoadIdentity();	

            GLU.gluLookAt(90, 25, eyeDistance, 0, 0, 0, 0, 1, 0);

    //        GL.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE);

              DrawTriangle();
        }

    }

And then some code on my actual form, I think this is everything I need to post



namespace Blah
{
    public partial class Form1 : Form
    {
        glDraw plot;

        public Form1()
        {
            InitializeComponent();
            pnlGL.BackColor = Color.Transparent;
            plot = new glDraw();
            plot.mainWin = this;
            plot.panel = pnlGL;
        }

    }
}

Advertisement
What exactly isn't working?
Everything in that code works, what I cant get to work is to make that cube constantly spin. Just like the famous 'Spinning cube'.

I left all the code out since none of it was working, I am pretty much asking what code to add to make that cube rotate around itself. Wether it be a boolean statement, if statement, etc.

Thanks
simply try the following source code right infront of your DrawTriangle() call. angle has to be declared somewhere else. try to initialize it with 0.0f, so in the first render call the render matrices are not modified by the call. if the first image shows your scene and in the second image the cube is gone than something is wrong with your initial matrix setup. That case check your projection and model matrices.

GL.glRotatef(angle, 0.3f, 0.2f, 0.1f);
angle += 0.1f;
if(angle >= 360.0f)
angle = 0.0f;

greetz.

[Edited by - Achilleos on June 6, 2007 10:41:45 AM]
I just tried adding that right before my DrawTriangle() in my render scene with no luck. I changed angle to be a float also.

I also tried adding that in my actual drawTriangle class before I begin drawing the triangle.

Any ideas, it still draws the triangle, just nothing happens, no rotation.

        private void glRenderScene()        {            GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);            GL.glLoadIdentity();	            GLU.gluLookAt(90, 25, eyeDistance, 0, 0, 0, 0, 1, 0);    //        GL.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE);            GL.glRotatef(angle, 0.3f, 0.2f, 0.1f);            angle += 0.1f;            if (angle >= 360.0f)                angle = 0.0f;            DrawTriangle();            GL.glFlush();        }
maybe you do some context switching after the first rendering and the drawing calls at a later time are send to a context without opengl. try to draw the triangle only every 30. frame but leave everything else as it is. The triangle should be flickering. if not there is something wrong with the context management.

i cannot imagine why the rotation call is not altering the values of your model matrix. you could also try to dynamically change the values of your glLookAt call to see if the image is really redrawing every frame (alters the matrix values too).
Any ideas on how to go about this, some coding examples. I am familiar with coding, just C# and OpenGL are very new to me, and I am trying to get down the basics.

Thanks
So i guess i was right?

If it is render context problem, you have to take a look at the context management reference of the device / render context system you are using.
For example you could be using SDL or GLUT which are platform independent. Or maybe you are using a windows api with WGL.

I have only write a context sharing manager for a multiple render context mfc (windows) application. But its not hard to implement it by yourself. Just take a look at the reference page of the system you are using and search for "context".

Greetz.
umm try a quick one and set the matrix stack to modelview before you perform the rotation.

This topic is closed to new replies.

Advertisement