[.net] need help with c# opengl texture map

Started by
2 comments, last by Rob Loach 17 years, 7 months ago
Hello, I'm looking for some help with what I hope is an easy problem. Basically, I'm trying to get texturing to work on some models in my application, but instead I just get a bunch of white all over the model. Anyway, I think I am loading and/or applying the texture incorrectly. I was hoping that someone could post an example using C# and OpenGL that Creates a texture image to use (in code is fine, it doesn't need to load from disk) Loads that texture into openGL Show that texture on a square. Thanks much. [Edited by - BradSnobar on September 16, 2006 12:07:25 PM]
Advertisement
What are you using to load your texture?
Rob Loach [Website] [Projects] [Contact]
Hey Rob,

I wrote a method (native managed code) to import bitmaps and other images into a datastructure.
I then convert the image into a format that opengl can read.
Like this:
Graphic.Graphic g = Graphic.FileUtilities.LoadImage(imageFilePath);byte[,,] image = this.ConvertBitmapToOpenGLImage(g, g.Width, g.Height);[/source lang="c#"]Currently, I'm trying to debug the problem with some image data that I just create arbirtrarily.  That should knock the loading the data incorrectly problem out of the way for now. So, image is being set according to the algorythm in the code dump below:  I think that I might have a problem in LoadTexturesRoutine or in the RenderRoutine.      private byte[, ,] MakeArbitraryImage()    {      int i, j;      byte[, ,] result = new byte[512, 512, 4];      for (i = 0; i < 512; i++)      {        for (j = 0; j < 512; j++)        {          //basically, just a non white, non black color          result[i, j, 0] = 105;          result[i, j, 1] = 255;          result[i, j, 2] = 105;        }      }      return result;    }    private uint LoadTexturesRoutine(string imageFilePath)    {      if (string.IsNullOrEmpty(imageFilePath))          return uint.MaxValue;      if (!System.IO.File.Exists(imageFilePath))        return uint.MaxValue;      const int LevelOfDetail = 0; // 0 means there is only one level of detail      const int BorderSize = 0;    //No border on textures      //Get the bitmap      //Graphic.Graphic g = Graphic.FileUtilities.LoadImage(imageFilePath);      //byte[,,] image = this.ConvertBitmapToOpenGLImage(g, g.Width, g.Height);      byte[,,] image = this.MakeArbitraryImage();      //store the image as a texture      uint texID;      GL.glPixelStorei(GLenum.GL_UNPACK_ALIGNMENT, 1); //set the byte boundary so we look at every byte individually      GL.glGenTextures(1, out texID);                  //generate space for one texture and give it an ID      GL.glBindTexture(GLenum.GL_TEXTURE_2D, texID);   //bind this id so that glTexImage2d can use it for a reference to store the texture      GL.glTexImage2D(GLenum.GL_TEXTURE_2D, LevelOfDetail, GLenum.GL_RGBA, g.Width, g.Height, BorderSize, GLenum.GL_RGBA, GLenum.GL_UNSIGNED_BYTE, image);      return texID;    }    private void RenderRoutine()    {      float x, y, z;      float a, b;      a = b = 0;      bool isTextureOk = (this.texName != uint.MaxValue);      if (isTextureOk)      {        GL.glEnable(GLenum.GL_TEXTURE_2D);        GL.glTexEnvf(GLenum.GL_TEXTURE_ENV, GLenum.GL_TEXTURE_ENV_MODE, GLenum.GL_DECAL);        GL.glBindTexture(GLenum.GL_TEXTURE_2D, this.texName);      }      else      {        GL.glPolygonMode(GLenum.GL_FRONT_AND_BACK, GLenum.GL_LINE); //GL_LINE for wireframe GL_FILL for filled in mode      }      foreach (Primitive p in PrimitiveList)      {        GL.glBegin(GLenum.GL_POLYGON);        //Todo: figure out how to get the length of each dimension in an array.        int max = p.vertexData.Length / 3;        for (int j = 0; j < max; j++)        {          if (isTextureOk)          {            a = p.texCoordData[j, 0];            b = p.texCoordData[j, 1];          }          x = p.vertexData[j, 0];          y = p.vertexData[j, 1];          z = p.vertexData[j, 2];          if (isTextureOk)          {            //apply texture            GL.glTexCoord2d(a, b);          }          else          {            GL.glColor3ub(255, 255, 255); //white          }          GL.glVertex3f(x, y, z);        }        GL.glEnd();      }      GL.glFlush();      if (isTextureOk)        GL.glDisable(GLenum.GL_TEXTURE_2D);    }[/source lang="c#"]If you don't see a problem, let me know that also, so that I can continue assesing whether the data I have is accurate or not.
You might want to try an external file loader, like Tao.DevIL. If you take a look at BooGame's Texture.cs, you'll notice a Load() function which converts a byte[] to an OpenGL texture.
Rob Loach [Website] [Projects] [Contact]

This topic is closed to new replies.

Advertisement