Jump to content

  • Log In with Google      Sign In   
  • Create Account

OpenGL draws Texture incorrect


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 DJLaitier   Members   -  Reputation: 100

Like
0Likes
Like

Posted 23 August 2011 - 04:46 AM

Hey Community :)

I´m trying to display a Quad with a texture, but it´s displaying just some pixels:

http://imageshack.us/photo/my-images/21/errorkpl.jpg/

Here´s my code:

int x1, x2, y1, y2;

    	public Form1()
    	{

        	x1 = 20;
        	x2 = 100;
        	y1 = 20;
        	y2 = 100;

        	InitializeComponent();
        	this.simpleOpenGlControl1.InitializeContexts();
   		
        	const int XSIZE = 640;
        	const int YSIZE = 480;
        	Gl.glMatrixMode(Gl.GL_PROJECTION);
        	Gl.glLoadIdentity();
        	Gl.glOrtho(0, XSIZE, YSIZE, 0, 0, 1);
        	Gl.glMatrixMode(Gl.GL_MODELVIEW);

        	Gl.glEnable(Gl.GL_TEXTURE_2D);
        	Gl.glDisable(Gl.GL_DEPTH_TEST);
        	Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
        	
        	// Texture

        	Bitmap b = new Bitmap(@"C:\Documents and Settings\MartDani\Desktop\menu_button.bmp");

        	Bitmap bitmap;
        	bitmap = (Bitmap)b.Clone(); // Input bitmap kopieren

        	int m_Width = bitmap.Width;
        	int m_Height = bitmap.Height;
        	int[] handle = new int[1];
        	Gl.glGenTextures(handle.Length, handle);

        	int m_Handle = handle[0];
        	handle = null;

        	Rectangle rect = new Rectangle(0, 0, m_Width, m_Height);
        	System.Drawing.Imaging.BitmapData bdata = bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat);

        	Gl.glBindTexture(Gl.GL_TEXTURE_2D, m_Handle);
        	Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGB, m_Width, m_Height, 0, Gl.GL_RGB, Gl.GL_UNSIGNED_BYTE, bdata.Scan0);
        	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);

        	Gl.glEnable(Gl.GL_TEXTURE_2D);
        	Gl.glTexEnvf(Gl.GL_TEXTURE_ENV, Gl.GL_TEXTURE_ENV_MODE, Gl.GL_DECAL);
        	Gl.glColor3f(1f, 1f, 1f);
        	Gl.glBegin(Gl.GL_QUADS);

        	Gl.glTexCoord2f(x1, y1); Gl.glVertex2f(x1, y1);
        	Gl.glTexCoord2f(x1, y2); Gl.glVertex2f(x1, y2);
        	Gl.glTexCoord2f(x2, y2); Gl.glVertex2f(x2, y2);
        	Gl.glTexCoord2f(x2, y1); Gl.glVertex2f(x2, y1);

        	Gl.glEnd();
        	Gl.glDisable(Gl.GL_TEXTURE_2D);
        	bitmap.UnlockBits(bdata);
        	bitmap.Dispose();
        	//

        	// Point
        	Gl.glBegin(Gl.GL_POINTS);
        	Gl.glVertex2f(5, 5);
        	Gl.glEnd();
        	// /Point

        	// Quad
        	Gl.glEnable(Gl.GL_TEXTURE_2D);
        	
        	Gl.glBegin(Gl.GL_QUADS);
        	Gl.glColor3f(1, 0, 0);
        	Gl.glVertex2f(x1 + 100, y1 + 100); Gl.glVertex2f(x2 + 100, y1 + 100); Gl.glVertex2f(x2 + 100, y2 + 100); Gl.glVertex2f(x1 + 100, y2 + 100);
        	Gl.glEnd();
        	// /Quad

    	}

I hope, anyone Can help me :)

Greetings from Germany

Sponsor:

#2 karwosts   Members   -  Reputation: 840

Like
1Likes
Like

Posted 23 August 2011 - 11:04 AM

Gl.glTexCoord2f(x1, y1); Gl.glVertex2f(x1, y1);
Gl.glTexCoord2f(x1, y2); Gl.glVertex2f(x1, y2);
Gl.glTexCoord2f(x2, y2); Gl.glVertex2f(x2, y2);
Gl.glTexCoord2f(x2, y1); Gl.glVertex2f(x2, y1);


You don't want texcoord to be the same as the vertex coordinates. Textures range from 0 to 1. So by specifying it from 20 to 100 you're squishing and repeating your texture 80 times across the surface.

Try this instead:
                Gl.glTexCoord2f(0, 0); Gl.glVertex2f(x1, y1);
                Gl.glTexCoord2f(0, 1); Gl.glVertex2f(x1, y2);
                Gl.glTexCoord2f(1, 1); Gl.glVertex2f(x2, y2);
                Gl.glTexCoord2f(1, 0); Gl.glVertex2f(x2, y1);

My Projects:
Portfolio Map for Android - Free Visual Portfolio Tracker
Electron Flux for Android - Free Puzzle/Logic Game

#3 DJLaitier   Members   -  Reputation: 100

Like
0Likes
Like

Posted 24 August 2011 - 01:14 AM

Yes, that has done the job :D And i changed the Image-Format, so I choosed a .jpg (no alpha-channel), and now, it´s displaying it very good :D Thanks




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS