views:

1248

answers:

2

I'm very new to OpenGL and I'm using managed OpenGLES wrapper written for Windows Mobile phones (OpenGLES Windows Mobile). I have a simple task of drawing a square (got that) and texturing it with an image from the disk, so I basically just want to display a jpg file on the screen. However every time I try to do it, I get a white square displayed, with no texture. I verified that my texture loads correctly (using some of the existing examples, and just replacing texture with mine). So I think my problem is correctly mapping texture to the object. Here's my code (functions and objects removed to make it simple):

        private Bitmap image = new Bitmap(@"\Storage Card\someimage.jpg");
        using (MemoryStream ms = new MemoryStream())
        {
            Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
            myImage = Texture.LoadStream(ms, false);
        }
        gl.ShadeModel(gl.GL_SMOOTH);
        gl.ClearColor(0.0f, 0.0f, 0.0f, 0.5f);
        gl.BlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA);
        gl.Hint(gl.GL_PERSPECTIVE_CORRECTION_HINT, gl.GL_NICEST);
        glLocation.Clear();
        gl.Clear(gl.GL_COLOR_BUFFER_BIT);
        gl.MatrixMode(gl.GL_PROJECTION);
        gl.LoadIdentity();
        gl.Orthof(0, ClientSize.Width, ClientSize.Height, 0, 0, 1);
        gl.Disable(gl.GL_DEPTH_TEST);

        gl.MatrixMode(gl.GL_MODELVIEW);
        gl.LoadIdentity();
        float[] rectangle = new float[] {
            0, 0,
            myImage.Width, 0,
            0,  myImage.Height,
            myImage.Width,  myImage.Height
        };

        float[] texturePosition = new float[] {
            0, 0,
            myImage.Width, 0,
            0,  myImage.Height,
            myImage.Width,  myImage.Height
        };

        //Bind texture
        gl.BindTexture(gl.GL_TEXTURE_2D, myImage.Name);
        gl.TexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR);
        gl.TexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR);
        //move cursor
        glLocation.MoveCursor(Location.X, Location.Y);
        gl.EnableClientState(gl.GL_TEXTURE_COORD_ARRAY);
        gl.EnableClientState(gl.GL_VERTEX_ARRAY);

        //draw square and texture it.
        fixed (float* rectanglePointer = &rectangle[0], positionPointer = &texturePosition[0])
        {
            gl.TexCoordPointer(2, gl.GL_FLOAT, 0, (IntPtr)positionPointer);
            gl.VertexPointer(2, gl.GL_FLOAT, 0, (IntPtr)rectanglePointer);
            gl.DrawArrays(gl.GL_TRIANGLE_STRIP, 0, 4);
        }

        gl.DisableClientState(gl.GL_TEXTURE_COORD_ARRAY);
        gl.DisableClientState(gl.GL_VERTEX_ARRAY);
        egl.SwapBuffers(myDisplay, mySurface);
        gl.Clear(gl.GL_COLOR_BUFFER_BIT);
A: 

Try changing texture position coords from myImage.Width and myImage.Height to 1.0. Texture coords are from 0.0 to 1.0.

+2  A: 

you have not enabled GL_TEXTURE_2D - add this line

gl.glEnable(gl.GL_TEXTURE_2D)
Quakeboy