Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 8 of 8

Thread: Loading Texture - Taoframework.

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2013
    Posts
    10

    Loading Texture - Taoframework.

    Hey all. I've been searching for hours on how to load an image and apply a texture to my particles with no resolve.
    The following is my code which runs, but nothing appears on the screen. When I remove the texture code, the particles
    do what they're supposed to do, but they're still simple GlPoints...
    Any pointers will be appreciated.

    Code :
    private static int[] index;
            private static int width;
            private static int height;
     
            /// <summary>
            ///  TEXTURE CODE
            /// </summary>
     
     
            [EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]//For codeanalyst
            public static void Texture()
            {
                index = new int[1];
                Gl.glGenTextures(1, index);
                //LoadTexture(path);
            }
     
            [EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]//For codeanalyst
            public static  void LoadTexture(string path)
            {
                Bitmap image;
     
                try
                {
                    image = new Bitmap(path);
     
                    image.RotateFlip(RotateFlipType.RotateNoneFlipY);
                    Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
                    height = image.Height;
                    width = image.Width;
                    BitmapData bitmapdata = image.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
     
                    Gl.glEnable(Gl.GL_TEXTURE_2D);									// Enable 2D Texture Mapping
                   // Gl.glDisable(Gl.GL_DEPTH_TEST);									// Disable Depth Testing
                    Gl.glBlendFunc(Gl.GL_SRC_ALPHA, Gl.GL_ONE);							// Set Blending Mode
                    Gl.glEnable(Gl.GL_BLEND);	
                    Gl.glEnable(Gl.GL_TEXTURE_GEN_S); //enable texture coordinate generation
                    Gl.glEnable(Gl.GL_TEXTURE_GEN_T);
     
                    Gl.glBindTexture(Gl.GL_TEXTURE_2D, index[0]);
                    Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGBA, image.Width, image.Height,
                                    0, Gl.GL_BGRA, Gl.GL_UNSIGNED_BYTE, bitmapdata.Scan0);
                    Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);
                    Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);
                    Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_S, Gl.GL_CLAMP);
                    Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_T, Gl.GL_CLAMP);
                    image.UnlockBits(bitmapdata);
                    image.Dispose();
                }
                catch (ArgumentException)
                {
                    Environment.Exit(0);
                }
            }
     
            public static void Activate()
            {
                Gl.glBindTexture(Gl.GL_TEXTURE_2D, index[0]);
            }
     
            public int GetWidth
            {
                get { return width; }
            }
     
            public int GetHeight
            {
                get { return height; }
            }
     
            /////END OF TEXTURE CODE
     
     
            public static void DisplayRainSystem()
            {
     
                Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
                Texture();
                LoadTexture("particle.bmp");
                Activate();
     
                float spost = 0.0f;											// Starting Texture Coordinate Offset
                float alphainc = 0.9f / 2;								// Fade Speed For Alpha Blending
                float alpha = 0.2f;											// Starting Alpha Value
     
                Gl.glPointSize(2.5f);
     
                int RandomColour = R.Next(4) + 1;
                if (RandomColour == 1) Gl.glColor3f(0.0f, 0.0f, 1.0f);
                if (RandomColour == 2) Gl.glColor3f(0.0f, 0.1f, 1.0f);
                if (RandomColour == 3) Gl.glColor3f(0.0f, 0.2f, 1.0f);
                if (RandomColour == 4) Gl.glColor3f(0.0f, 0.5f, 1.0f);
                if (RandomColour == 5) Gl.glColor3f(0.0f, 0.8f, 1.0f);
     
                Gl.glBegin(Gl.GL_POINTS);
     
                for (int i = 0; i <= RS.RainParticles.Count - 1; i++)
                {
                    RainParticle RP = (RainParticle)RS.RainParticles[i];
                    Gl.glVertex2d(RP.xpos, RP.ypos);
                }
     
                Gl.glFlush();
                Gl.glEnd();    
     
            }

  2. #2
    Junior Member Newbie
    Join Date
    Apr 2013
    Posts
    10
    Might I add, this is in C#/Taoframework, so frustrating.

  3. #3
    Junior Member Newbie
    Join Date
    Apr 2013
    Posts
    10
    bump? anyone?

  4. #4
    Advanced Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Singapore
    Posts
    800
    I suggest u remove these calls
    Code :
    Gl.glBlendFunc(Gl.GL_SRC_ALPHA, Gl.GL_ONE);                           
     // Set Blending Mode 
    Gl.glEnable(Gl.GL_BLEND);     
    Gl.glEnable(Gl.GL_TEXTURE_GEN_S); 
    //enable texture coordinate generation 
    Gl.glEnable(Gl.GL_TEXTURE_GEN_T);
    Are you setting any other OpenGL states?
    Regards,
    Mobeen

  5. #5
    Junior Member Newbie
    Join Date
    Apr 2013
    Posts
    10
    Quote Originally Posted by mobeen View Post
    I suggest u remove these calls
    Code :
    Gl.glBlendFunc(Gl.GL_SRC_ALPHA, Gl.GL_ONE);                           
     // Set Blending Mode 
    Gl.glEnable(Gl.GL_BLEND);     
    Gl.glEnable(Gl.GL_TEXTURE_GEN_S); 
    //enable texture coordinate generation 
    Gl.glEnable(Gl.GL_TEXTURE_GEN_T);
    Are you setting any other OpenGL states?
    I've tried this yet the screen doesn't display anything.
    I've even tried drawing2d pictures instead, still no success.

    Code :
    private static int[] index;
            private static int width;
            private static int height;
     
            /// <summary>
            ///  TEXTURE CODE
            /// </summary>
     
     
            [EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]//For codeanalyst
            public static void Texture()
            {
                index = new int[1];
                Gl.glGenTextures(1, index);
                //LoadTexture(path);
            }
     
            [EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]//For codeanalyst
            public static  void LoadTexture(string path)
            {
                Bitmap image;
     
                try
                {
                    image = new Bitmap(path);
     
                    image.RotateFlip(RotateFlipType.RotateNoneFlipY);
                    Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
                    height = image.Height;
                    width = image.Width;
                    BitmapData bitmapdata = image.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
     
                    Gl.glEnable(Gl.GL_TEXTURE_2D);									// Enable 2D Texture Mapping
                   // Gl.glDisable(Gl.GL_DEPTH_TEST);									// Disable Depth Testing
              //      Gl.glBlendFunc(Gl.GL_SRC_ALPHA, Gl.GL_ONE);							// Set Blending Mode
              //      Gl.glEnable(Gl.GL_BLEND);	
              //      Gl.glEnable(Gl.GL_TEXTURE_GEN_S); //enable texture coordinate generation
              //      Gl.glEnable(Gl.GL_TEXTURE_GEN_T);
     
                    Gl.glBindTexture(Gl.GL_TEXTURE_2D, index[0]);
                    Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGBA, image.Width, image.Height,
                                    0, Gl.GL_BGRA, Gl.GL_UNSIGNED_BYTE, bitmapdata.Scan0);
                    Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);
                    Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);
                    Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_S, Gl.GL_CLAMP);
                    Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_T, Gl.GL_CLAMP);
                    image.UnlockBits(bitmapdata);
                    image.Dispose();
                }
                catch (ArgumentException)
                {
                    Environment.Exit(0);
                }
            }
     
     
            public static byte[] ImageToByte(Image img)
            {
                ImageConverter converter = new ImageConverter();
                return (byte[])converter.ConvertTo(img, typeof(byte[]));
            }
     
     
            public static void Activate()
            {
                Gl.glBindTexture(Gl.GL_TEXTURE_2D, index[0]);
            }
     
            public int GetWidth
            {
                get { return width; }
            }
     
            public int GetHeight
            {
                get { return height; }
            }
     
            /////END OF TEXTURE CODE
     
     
            public static void DisplayRainSystem()
            {
     
                Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
     
     
                float spost = 0.0f;											// Starting Texture Coordinate Offset
                float alphainc = 0.9f / 2;								// Fade Speed For Alpha Blending
                float alpha = 0.2f;											// Starting Alpha Value
     
                Gl.glPointSize(2.5f);
     
                int RandomColour = R.Next(4) + 1;
                if (RandomColour == 1) Gl.glColor3f(0.0f, 0.0f, 1.0f);
                if (RandomColour == 2) Gl.glColor3f(0.0f, 0.1f, 1.0f);
                if (RandomColour == 3) Gl.glColor3f(0.0f, 0.2f, 1.0f);
                if (RandomColour == 4) Gl.glColor3f(0.0f, 0.5f, 1.0f);
                if (RandomColour == 5) Gl.glColor3f(0.0f, 0.8f, 1.0f);
     
             //   Texture();
             //   LoadTexture("particle.bmp");
             //   Activate();
     
                System.Drawing.Image tempImage;
                tempImage = System.Drawing.Image.FromFile("particle.bmp");
     
                byte[] imgbyte = ImageToByte(tempImage);
     
                Gl.glBegin(Gl.GL_POINTS);
     
                for (int i = 0; i <= RS.RainParticles.Count - 1; i++)
                {
                    RainParticle RP = (RainParticle)RS.RainParticles[i];
                   // Gl.glVertex2d(RP.xpos, RP.ypos);
                   // Gl.glTexCoord2d(RP.xpos, RP.ypos);
     
                    Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGB, 512, 512, 0, Gl.GL_BGR_EXT, Gl.GL_UNSIGNED_BYTE, imgbyte);
     
     
                }
     
                Gl.glFlush();
                Gl.glEnd();    
     
            }

  6. #6
    Advanced Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Singapore
    Posts
    800
    Quote Originally Posted by intenseza View Post
    I've even tried drawing2d pictures instead, still no success.
    This is not how u draw pictures. You are contiuously allocating texture for each rain particle.

    What I suggest u do first is render a simple full screen quad with the texture on it.
    Try something like this in ur render function (this is in c++) you would know how to convert to c#.
    Code :
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glClear(GL_COLOR_BUFFERBIT|GL_DEPTH_BUFFER_BIT);
     
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, textureID);
    glBegin(GL_QUADS);
    glTexCoord2f(0,0);glVertex3f(-1,-1);
    glTexCoord2f(1,0);glVertex3f(1,-1);
    glTexCoord2f(1,1);glVertex3f(1,1);
    glTexCoord2f(0,1);glVertex3f(-1,1);
    glEnd();
    glDisable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, 0);
    SwapBuffers();

    Note that i havent tested this code but this should work.
    See if the texture appears on a full screen window using this code.
    Regards,
    Mobeen

  7. #7
    Junior Member Newbie
    Join Date
    Apr 2013
    Posts
    10
    Good news, the quad is textured nicely, but how do I apply the textures to Gl.Points that are nothing more than of GlPoint size 1.5?

    EDIT: Got it! Thank you so much
    Last edited by intenseza; 05-09-2013 at 06:41 AM.

  8. #8
    Advanced Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Singapore
    Posts
    800
    You would have to distribute the texture coordinates between the points based on their position.
    Regards,
    Mobeen

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •