Jump to content
  • Advertisement
Sign in to follow this  
NipsMG2

OpenGL SDL.NET/TAO.OpenGL Question

This topic is 3264 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 got sick of issues w/ SDL.NET and Alpha Blending, so I decided to move over to OpenGL. There is some minor 3d stuff I might be doing in my project so I figured it might be a good move. I'm having trouble getting off the ground however. I'm trying to basically load a project and paint a background to start, but I get only a black screen. I believe I understand how to load the textures properly, I might just be screwing up on the drawing code. My code is below. Any help would be appreciated. Thanks
    public class PulseGLTest
    {
        private static string _resourcePath;
        #region Fields
        private GUISystem _gui;
        private Button _button;
        private ToggleButton _toggleButton;
        private Label _label;
        private CheckBox _checkBox;
        private ComboBox _comboBOx;
        private ListBox _listBox;
        private Panel _panel;
        private RadioButton _radioButton;
        private Slider _slider;
        private RadioButton _radioButton2;
        private Dictionary<string, int> _textures;
        //Width of screen
        int width = 1024;
        //Height of screen
        int height = 768;
        // Bits per pixel of screen
        int bpp = 16;
        // Surface to render on
        Surface screen;
        
        #endregion
        
        #region Properties



        /// <summary>
        /// Width of window
        /// </summary>
        protected int Width
        {
            get
            {
                return width;
            }
        }

        /// <summary>
        /// Height of window
        /// </summary>
        protected int Height
        {
            get
            {
                return height;
            }
        }

        /// <summary>
        /// Bits per pixel of surface
        /// </summary>
        protected int BitsPerPixel
        {
            get
            {
                return this.bpp;
            }
        }



        #endregion Fields

        #region constructors
        public PulseGLTest()
        {
            Initialize();
        }
        #endregion

        protected void DrawGLScene()
        {
            // Clear The Screen And The Depth Buffer
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
             Gl.glLoadIdentity();

            //Tell the GUI to draw itself.
            Gl.glEnable(Gl.GL_BLEND);
            Gl.glBindTexture(Gl.GL_TEXTURE_2D, _textures["Background"]);

            Gl.glBegin(Gl.GL_QUADS);
            var tx = 1024 / 2;
            var ty = 768/2;
            Gl.glTexCoord2f(0.0f, 0.0f); Gl.glVertex3i(0, 0, 1);
            Gl.glTexCoord2f(0.0f, 1.0f); Gl.glVertex3i(1024, 0, 1);
            Gl.glTexCoord2f(1.0f, 1.0f); Gl.glVertex3i(1024, 768, 1);
            Gl.glTexCoord2f(1.0f, 0.0f); Gl.glVertex3i(0, 768, 1);
            Gl.glEnd();


            
            

        }
        /// <summary>
        /// Resizes window
        /// </summary>
        /// <param name="distance"></param>
        protected virtual void  Reshape(float distance)
        {
            // Reset The Current Viewport
            Gl.glViewport(0, 0, Width, Height);
            // Select The Projection Matrix
            Gl.glMatrixMode(Gl.GL_PROJECTION);
            // Reset The Projection Matrix
            Gl.glLoadIdentity();
            // Calculate The Aspect Ratio Of The Window
            Glu.gluPerspective(45.0F, (Width / (float)Height), 0.1F, distance);
            Gl.glOrtho(0, Width, 0, Height, -100, 100);
            // Select The Modelview Matrix
            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            // Reset The Modelview Matrix
            Gl.glLoadIdentity();
        }

        /// <summary>
        /// Initializes the OpenGL system
        /// </summary>
        protected virtual void InitGL()
        {
            LoadGLTextures();
            // Enable Texture Mapping ( NEW )
            Gl.glEnable(Gl.GL_TEXTURE_2D);
            // Enable Smooth Shading
            Gl.glShadeModel(Gl.GL_SMOOTH);
            // Black Background
            Gl.glClearColor(0.0F, 0.0F, 0.0F, 0.5F);
            // Depth Buffer Setup
            Gl.glClearDepth(1.0F);
            // Enables Depth Testing
            Gl.glEnable(Gl.GL_DEPTH_TEST);
            // The Type Of Depth Testing To Do
            Gl.glDepthFunc(Gl.GL_LEQUAL);
            // Really Nice Perspective Calculations
            Gl.glHint(Gl.GL_PERSPECTIVE_CORRECTION_HINT, Gl.GL_NICEST);

            //Initialize anything else too including the screen manager or GUI.
            //InitGUI();

           
        }

        private void LoadGLTextures()
        {
            _textures = new Dictionary<string, int>();
            var img = new Surface(Path.Combine(_resourcePath, @"Skins\Default\tocabg.png"));
            _textures.Add("Background", TextureUtils.SDLSurfaceToGLTexture(img));
        }


        #region Lesson Setup
        /// <summary>
        /// Initializes methods common to all NeHe lessons
        /// </summary>
        protected void Initialize()
        {
            // Sets keyboard events
            Events.KeyboardDown += new EventHandler<KeyboardEventArgs>(this.KeyDown);
            Events.KeyboardUp += new EventHandler<KeyboardEventArgs>(this.KeyDown);
            // Sets the ticker to update OpenGL Context
            Events.Tick += new EventHandler<TickEventArgs>(this.Tick);
            Events.Quit += new EventHandler<QuitEventArgs>(this.Quit);
            //			// Sets the resize window event
            //			Events.VideoResize += new EventHandler<VideoResizeEventArgs> (this.Resize);
            // Set the Frames per second.
            Events.Fps = 60;
            // Sets Window icon and title
            this.WindowAttributes();
            // Creates SDL.NET Surface to hold an OpenGL scene
            screen = Video.SetVideoMode(width, height, true, true);
        }

        /// <summary>
        /// Sets Window icon and caption
        /// </summary>
        protected void WindowAttributes()
        {
            Video.WindowIcon();
            Video.WindowCaption = "Pulse";
                
        }

        

      

        #endregion Lesson Setup

       

        #region Event Handlers
        private void KeyDown(object sender, KeyboardEventArgs e)
        {
            if(e.Down)
            {
                switch (e.Key)
                {
                    case Key.Escape:
                        // Will stop the app loop
                        Events.QuitApplication();
                        break;
                    case Key.F1:
                        // Toggle fullscreen
                        if ((screen.FullScreen))
                        {
                            screen = Video.SetVideoMode(width, height, true, true, true);
                            this.WindowAttributes();
                        }
                        else
                        {
                            screen = Video.SetVideoMode(width, height, true, true);
                        }
                        Reshape(100f);
                        break;
                    
                }
            }
        }

        private void Tick(object sender, TickEventArgs e)
        {
            DrawGLScene();
            Video.GLSwapBuffers();
        }

        private void Quit(object sender, QuitEventArgs e)
        {
            Events.QuitApplication();
        }
        #endregion		}


        #region Run Loop
        static void Main(string[] args)
        {
            _resourcePath = @"c:\temp\Toca";
            var t = new PulseGLTest();

            t.Reshape(100f);
            t.InitGL();
            Events.Run();
        }
        #endregion
    }
}



SDLSurfaceToOpenGLTexture
  public static int SDLSurfaceToGLTexture(Surface surf)
        {
            int textureId;
            int mode;

            
            Gl.glGenTextures(1, out textureId);
            Gl.glBindTexture(Gl.GL_TEXTURE_2D, textureId);
            mode = surf.BytesPerPixel == 3 ? Gl.GL_RGB : Gl.GL_RGBA;
            var surfData = surf.Bitmap;
            surfData.RotateFlip(RotateFlipType.RotateNoneFlipY);
            var data = surfData.LockBits(new System.Drawing.Rectangle(0, 0, surf.Width, surf.Height),
                                         ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
            if(IsPowerOf2(surf.Width )  && IsPowerOf2(surf.Height))
            {
                Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);
                Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR_MIPMAP_NEAREST);
                Glu.gluBuild2DMipmaps(Gl.GL_TEXTURE_2D, mode, surf.Width, surf.Height, mode, Gl.GL_UNSIGNED_BYTE, data.Scan0);
            }
            else
            {
                Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_NEAREST);
                Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);
                Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, mode, surf.Width, surf.Height, 0, mode,
                                Gl.GL_UNSIGNED_BYTE, data.Scan0);
                

               surfData.UnlockBits(data);
                surfData.Dispose();
            }
            

           return textureId;


        }

        public static bool IsPowerOf2(int value)
        {
            return (value & (value - 1)) == 0;
  
        }


Share this post


Link to post
Share on other sites
Advertisement
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!