My First OpenGL c/ C# has simple prob...

Started by
18 comments, last by malpass 20 years, 12 months ago
This is my first attempt at an OpenGL app & its in C# so I had to convert from a c++ tutorial. It loads and (in a sense) works fine, but I think theres a problem with the Z-buffer (I know this because I had this same problem when starting DX, but fixed it by changing the Z-buffer). What the problem is, is that when it loads, it clears the screen fine, but when you resize it goes all messy. But... if you take the window out of focus and back again it clears again fine.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using CsGL.OpenGL;

namespace OpenGLTemplate
{
	public class MainForm : Form
	{
		// OpenGL window
		protected Viewer GLWindow = new Viewer();

		Timer timer;
		int iFPS = 25;

		#region General
		private System.ComponentModel.Container components = null;
		public MainForm(){InitializeComponent();}
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
		#endregion
		#region Windows Form
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.ClientSize = new System.Drawing.Size(300,300);
			this.Text = "OpenGL Template";
			this.SuspendLayout();

			this.Controls.Add(GLWindow);
			GLWindow.Dock = DockStyle.Fill;

			this.FormBorderStyle = FormBorderStyle.Sizable;
			this.ResumeLayout();

			timer = new Timer();
			timer.Tick += new EventHandler(OnTick);
			timer.Interval = 1000/iFPS;
			timer.Start();
		}
		#endregion
		static void Main() {Application.Run(new MainForm());}

		void OnTick(object s,EventArgs ea)
		{
			GLWindow.GLDrawScene();
		}
	}
	public class Viewer : OpenGLControl
	{
		static int cx, cy;
		#region General
		private System.ComponentModel.Container components = null;
		public Viewer(){InitializeComponent();}
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
		#endregion
		#region Windows Form
		private void InitializeComponent()
		{
			// 
			// Form1
			// 
			this.ClientSize = new System.Drawing.Size(300, 300);
			this.Name = "OpenGL Viewer";
			this.Text = "OpenGL";
		}
		#endregion
		protected override void InitGLContext()
		{
			cx = ClientSize.Width;
			cy = ClientSize.Height;

			GL.glMatrixMode(GL.GL_PROJECTION);	// Set Matrix
			GL.glLoadIdentity();				// Reset Matrix
			// Calculate Perspective
			GL.gluPerspective(45.0f,ClientSize.Width/ClientSize.Height,0.1f,100.0f);
			
			GL.glMatrixMode(OpenGL.GL_MODELVIEW);	// Set Matrix
			GL.glLoadIdentity();					// Reset Matrix
			GLInit();								// Init all GL stuff
			// Size GL render window
			GLResizeView(cx,cy);
		}
		bool GLInit()
		{
			GL.glShadeModel(GL.GL_SMOOTH);			// Shade Type
			GL.glClearColor(100.0f,0.0f,0.0f,0.0f);	// Clear Color
			GL.glClearDepth(1.0f);					// Clear Depth
			GL.glEnable(GL.GL_DEPTH_TEST);			// Enable Depth Testing
			GL.glDepthFunc(GL.GL_LEQUAL);			// Type Of Depth Testing

			// Nice perspective calculations
			GL.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT,GL.GL_NICEST);

			// Call DrawScene
			GLDrawScene();

			return true;
		}
		public bool GLDrawScene()
		{
			GL.glClear(GL.GL_COLOR_BUFFER_BIT|GL.GL_DEPTH_BUFFER_BIT);	// Clear Screen & Depth Buffer
			GL.glLoadIdentity();										// Reset Matrix

			return true;
		}
		void GLClose()
		{
			
		}
		void GLResizeView(int width, int height)
		{
			if (height==0) height = 1;

			GL.glViewport(0,0,width,height);
		}
		protected override void OnSizeChanged(EventArgs ea)
		{
			base.OnSizeChanged(ea);
			cx = ClientSize.Width;
			cy = ClientSize.Height;
			GLResizeView(cx,cy);
		}
	}
}

 
Also is this the right way to code OpenGL in C#? If not how? PS. If the code doesnt appear in a code box then could someone also tell me the code for the forum to do that
Advertisement
I don''t know C#, but...

"PS. If the code doesnt appear in a code box then could someone also tell me the code for the forum to do that"

use the [ source ] [ /source ] tags (without the spaces).

btw, 100th post
that s the first time i have seen c# code and know what?

i looks ugly! ill never switch to c# take this for granted
http://www.8ung.at/basiror/theironcross.html
its simpler that c#, I found the error too, just fogot the...


  protected override OpenGLContext CreateContext(){	// Setup Our PixelFormat And Context	ControlGLContext context = new ControlGLContext(this);	DisplayType displayType = new DisplayType(colorDepth, zDepth, stencilDepth, accumDepth);	context.Create(displayType, null);	return context;}  
You say C# looks ugly but it''s not.Just another language for programming.We shoudn''t judge the languages on how they look like but how they behave!

The PAIN is coming...this summer!!!In cinemas everywhere.
I''d say C# looks pretty clean compared to most of the C and C++ code out there, which usually gets pretty hairy.


"Laughter means distance. Where laughter is absent, madness begins. The moment one takes the world with complete seriousness one is potentially insane. The whole art of learning to live means holding fast to laughter; without laughter the world is a torture chamber, a dark place where dark things will happen to us, a horror show filled with bloody deeds of violence."
-- Jens Bjørneboe
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
hm.. i dont see that many differences to c++ or java, so i dont know what is so ugly about it. but if you dont like the font, i''m sure you can change that in your browser settings.
f@dzhttp://festini.device-zero.de
I''m trying to use your code, but I can''t get anything to happen. Is there anything special you are doing? None of the opengl commands work for me, even though they do get called.
well first of all this code above looks very confusing

lots of objects ......

i really hate it when people places every shit into extra functions especially when an ABI like OGL is wrapped into a class dudes that s just UGLY

of course it looks a lot like c++ but i prefer a mixture of C and C++

that s proofed to be very efficient and from what i have heart c# is slower than c++ that might be due to the newer compilers but i really see no reason to switch to c# at all

well there are certainly a few features which can be useful but c++ has been designed for wide spread field of application type and also proved to fit into these fields
OOP is good but to much is bullshit
http://www.8ung.at/basiror/theironcross.html
quote:Original post by Basiror i really hate it when people places every shit into extra functions especially when an ABI like OGL is wrapped into a class dudes that s just UGLY

Uhm, you don''t exactly have a choice if you''re using c# or java.

quote:
that s proofed to be very efficient and from what i have heart c# is slower than c++ that might be due to the newer compilers but i really see no reason to switch to c# at all

Have you ever tried to do windows programming in c++?

This topic is closed to new replies.

Advertisement