Sign in to follow this  
taffy

OpenGL Tao Framework on XP cannot apply texture

Recommended Posts

Hello I took the Spinning Triangle from the Tao website and modified it to be similar to what I was used to doing in another framework that I wish to move away from. Here I want a triangle and when I push the button, it will apply a texture. Well all works except when I press the button, no texture is applied; I have to resize the form before the texture is applied. Can you please give me a pointer on how to get the texture applied when the button is pressed? (I do not want to include their Run method - it is commented out).Below is my code. Thanks, Taffy using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; using Tao.OpenGl; using Tao.DevIl; namespace SpinningTriangle { public class frmMain : System.Windows.Forms.Form { private Tao.Platform.Windows.SimpleOpenGlControl GlControl; private System.Windows.Forms.Button btnTexture; private System.ComponentModel.Container components = null; protected override void OnLoad(EventArgs e) { base.OnLoad (e); InitGL(); } protected override void OnResize(EventArgs e) { base.OnResize (e); ResizeGL(GlControl.Width, GlControl.Height); } /* public void Run() { while (Created) { //Invalidate(true); //DrawGL(); Application.DoEvents(); } } */ public void InitGL() { GlControl.InitializeContexts(); OnResize(null); } public void ResizeGL(int w, int h) { Gl.glViewport( 0, 0, w, h); Gl.glMatrixMode ( Gl.GL_PROJECTION ); Gl.glLoadIdentity(); Glu.gluPerspective(60.0, (double)w / (double)h, 1.0,1000.0); Gl.glMatrixMode ( Gl.GL_MODELVIEW ); Gl.glLoadIdentity(); Invalidate(); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint (e); DrawGL(); } private long lastMs = 0; private float angle = 0f; public void DrawGL() { if (lastMs == 0) lastMs = DateTime.Now.Ticks; long currentMs = DateTime.Now.Ticks; long milliseconds = (currentMs - lastMs) / 10000; lastMs = currentMs; Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT); Gl.glLoadIdentity(); Gl.glTranslatef(0, 0, -6); Gl.glRotatef(angle, 0, 1, 0); if (textureid == -1) { // No Texture, draw normally. Gl.glBegin(Gl.GL_TRIANGLES); { Gl.glColor3f(1.0f, 0.0f, 0.0f); Gl.glVertex3f(0.0f, 1.0f, 0.0f); Gl.glColor3f(0.0f, 1.0f, 0.0f); Gl.glVertex3f(-1.0f, -1.0f, 1.0f); Gl.glColor3f(0.0f, 0.0f, 1.0f); Gl.glVertex3f(1.0f, -1.0f, 1.0f); Gl.glColor3f(1.0f, 0.0f, 0.0f); Gl.glVertex3f(0.0f, 1.0f, 0.0f); Gl.glColor3f(0.0f, 0.0f, 1.0f); Gl.glVertex3f(1.0f, -1.0f, 1.0f); Gl.glColor3f(0.0f, 1.0f, 0.0f); Gl.glVertex3f(1.0f, -1.0f, -1.0f); Gl.glColor3f(1.0f, 0.0f, 0.0f); Gl.glVertex3f(0.0f, 1.0f, 0.0f); Gl.glColor3f(0.0f, 1.0f, 0.0f); Gl.glVertex3f(1.0f, -1.0f, -1.0f); Gl.glColor3f(0.0f, 0.0f, 1.0f); Gl.glVertex3f(-1.0f, -1.0f, -1.0f); Gl.glColor3f(1.0f, 0.0f, 0.0f); Gl.glVertex3f(0.0f, 1.0f, 0.0f); Gl.glColor3f(0.0f, 0.0f, 1.0f); Gl.glVertex3f(-1.0f, -1.0f, -1.0f); Gl.glColor3f(0.0f, 1.0f, 0.0f); Gl.glVertex3f(-1.0f, -1.0f, 1.0f); } Gl.glEnd(); } else { Gl.glColor3f(1f,1f,1f); Gl.glBegin(Gl.GL_TRIANGLES); { Gl.glTexCoord2f(1.0f, 0.0f); Gl.glVertex3f(0.0f, 1.0f, 0.0f); Gl.glTexCoord2f(0.0f, 1.0f); Gl.glVertex3f(-1.0f, -1.0f, 1.0f); Gl.glTexCoord2f(0.0f, 0.0f); Gl.glVertex3f(1.0f, -1.0f, 1.0f); Gl.glTexCoord2f(1.0f, 0.0f); Gl.glVertex3f(0.0f, 1.0f, 0.0f); Gl.glTexCoord2f(0.0f, 0.0f); Gl.glVertex3f(1.0f, -1.0f, 1.0f); Gl.glTexCoord2f(0.0f, 1.0f); Gl.glVertex3f(1.0f, -1.0f, -1.0f); Gl.glTexCoord2f(1.0f, 0.0f); Gl.glVertex3f(0.0f, 1.0f, 0.0f); Gl.glTexCoord2f(0.0f, 1.0f); Gl.glVertex3f(1.0f, -1.0f, -1.0f); Gl.glTexCoord2f(0.0f, 0.0f); Gl.glVertex3f(-1.0f, -1.0f, -1.0f); Gl.glTexCoord2f(1.0f, 0.0f); Gl.glVertex3f(0.0f, 1.0f, 0.0f); Gl.glTexCoord2f(0.0f, 0.0f); Gl.glVertex3f(-1.0f, -1.0f, -1.0f); Gl.glTexCoord2f(0.0f, 1.0f); Gl.glVertex3f(-1.0f, -1.0f, 1.0f); } Gl.glEnd(); } Gl.glFlush(); angle += (float)milliseconds / 5f; } public frmMain() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.GlControl = new Tao.Platform.Windows.SimpleOpenGlControl(); this.btnTexture = new System.Windows.Forms.Button(); this.SuspendLayout(); // // GlControl // this.GlControl.AccumBits = ((System.Byte)(0)); this.GlControl.AutoCheckErrors = false; this.GlControl.AutoFinish = false; this.GlControl.AutoMakeCurrent = true; this.GlControl.AutoSwapBuffers = true; this.GlControl.BackColor = System.Drawing.Color.Black; this.GlControl.ColorBits = ((System.Byte)(32)); this.GlControl.DepthBits = ((System.Byte)(16)); this.GlControl.Dock = System.Windows.Forms.Dockstyle.Fill; this.GlControl.Location = new System.Drawing.Point(0, 0); this.GlControl.Name = "GlControl"; this.GlControl.Size = new System.Drawing.Size(292, 273); this.GlControl.StencilBits = ((System.Byte)(0)); this.GlControl.TabIndex = 0; // // btnTexture // this.btnTexture.Location = new System.Drawing.Point(16, 16); this.btnTexture.Name = "btnTexture"; this.btnTexture.Size = new System.Drawing.Size(88, 23); this.btnTexture.TabIndex = 1; this.btnTexture.Text = "Apply Texture"; this.btnTexture.Click += new System.EventHandler(this.btnTexture_Click); // // frmMain // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.btnTexture); this.Controls.Add(this.GlControl); this.Name = "frmMain"; this.Text = "Spinning Triangle"; this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { //frmMain app = new frmMain(); //app.Show(); //app.Run(); Application.Run(new frmMain()); } int textureid = -1; private void btnTexture_Click(object sender, EventArgs e) { if(textureid == -1) { Il.ilInit(); Ilut.ilutInit(); textureid = Ilut.ilutGLLoadImage("texture.jpg"); } } } }

Share this post


Link to post
Share on other sites
it looks like you're not doing anything to update your form after clicking the button. Try throwing a drawGL at the end of your button click event handler.

Also, shouldn't you bind the texture after loading it? Probably, Devil does it for you, but it's something you need to do if you use multiple textures so you might as well start doing it. It's just a matter of calling Gl.glBindTexture( Gl. GL_TEXTURE2D, textureID) ( I think... I could have the arguments wrong)

Share this post


Link to post
Share on other sites
I did call Invalidate() in the btnTexture click event but to no avail.
Below is the code I have now in the btnTexture click event. What occurs
is that the triangle is drawn. When I press the button btnTexture, the
triange does not get textured. I have to resize the form with my mouse
and only then does the triangle get textured. (I have also tried commenting
out the call to DrawGL() since it is in my OnPaint method - to no avail).

Any help would be greatly appreciated.
Thanks,
Taffy

private void btnTexture_Click(object sender, EventArgs e)
{
if(textureid == -1)
{
Il.ilInit();
Ilut.ilutInit();
textureid = Ilut.ilutGLLoadImage("texture.jpg");
Gl.glBindTexture(Gl.GL_TEXTURE_2D, textureid);
}
DrawGL();
Invalidate();
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this