Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

malpass

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

This topic is 5722 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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++?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!