Jump to content

  • Log In with Google      Sign In   
  • Create Account

We need your feedback on a survey! Each completed response supports our community and gives you a chance to win a $25 Amazon gift card!


bobbias

Member Since 06 Aug 2002
Offline Last Active Jun 22 2012 05:39 AM

Topics I've Started

Velocity/heading. Cartesian or spherical coordinates?

15 May 2012 - 06:57 AM

I'm working on a game in Java using lwjgl for opengl. I'm working on designing my general "model" representation and I'm stuck trying to decide how I want to represent the heading/velocity of a model. Opengl uses Cartesian coordinates, but I' wondering if there's any reason I should store the model information natively as spherical coordinates... I can't figure out the pros and cons to it.

So I guess what I'm asking is, how does everyone else store the heading/velocity data for your models and why do you store it that way?

(Tao C#) Help with rendering textured TTF fonts in OGL via SDL_TTF

13 January 2012 - 04:21 PM

I'm working with the Tao Framework in C# trying to use SDL's TrueType along with OpenGL to render text on the screen.
I've lifted some code from various tutorials or translated code from c++/CLI to try to do this. I've already rendered a test which worked fine(using the tutorial from http://www.sdltutori...ngl-with-csharp)

Now I'm trying to change my program to render some simple text via SDL's Truetype library creating a texture based on a string. The program compiles fine, but I don't get any sort of rendering.

I've never worked with SDL or OGL before, but I generally prefer to learn on my feet, changing a program as I go and adding to it, rather than testing individual things one by one, which doesn't suit reading tutorials all that well.

I haven't exactly done much programming lately so I'm a bit rusty, but I'm still familiar with C#.

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Tao.Sdl;
using Tao.OpenGl;
using System.Runtime.InteropServices;
using Tao.Platform.Windows; //Is this even needed?
namespace TaoTest
{
	public class Program
	{
		//For attaching a console, which apparently doesn't work, but does no harm either
		[DllImport("kernel32.dll")]
		static extern bool AttachConsole(int dwProcessId);
		private const int ATTACH_PARENT_PROCESS = -1;
		//Member variables
		public static Sdl.SDL_Surface surface = new Sdl.SDL_Surface(); //holds our buffer surface
		private Sdl.SDL_VideoInfo videoinfo;
		private int height = 768;  //default resolution
		private int width = 1366;
		private int bpp = 16; //16 bit graphics
		private int flags = Sdl.SDL_OPENGL | Sdl.SDL_GL_DOUBLEBUFFER | Sdl.SDL_NOFRAME | Sdl.SDL_HWSURFACE;// | Sdl.SDL_FULLSCREEN;
		private int TimeCatcher = 0; //used for timing
		private Game game;
		
		public Program()
		{
			if (Sdl.SDL_Init(Sdl.SDL_INIT_VIDEO) < 0) //This seems to throw an exception from not finding Apple.Foundation.
			{
				Console.WriteLine("Error initializing SDL");
				MessageBox.Show("Error Initializing SDL");
				Sdl.SDL_Quit();
				return;
			}

		   //Comments mostly from tutorial
			/* Rather than set the video properties up in the constructor, I set
			   them in setVideo. The reason for this is that 2 pointers are used
			   to interact with SDL structures. Once used they convert their
			   handles into vidInfo and surface tamer variables. That this
			   occurs inside the function means the pointers will release
			   their memory on function exit.
			*/
			setSDLVideo();
			/* openGL is not part of SDL, rather it runs in a window handled
			   by SDL. here we set up some openGL state
			*/
			setOpenGL();
			game = new Game();
			//initialize game
			while (!game.finP)
			{
				tick(Sdl.SDL_GetTicks() - TimeCatcher);//updates the game object
				TimeCatcher = Sdl.SDL_GetTicks();//stores the current time
				Sdl.SDL_Delay(1);//release the thread
			}
			//When that loop is done this gets executed immediately
			Sdl.SDL_Quit();
			return;
		}
		private void setSDLVideo()
		{
			Sdl.SDL_putenv("SDL_VIDEO_CENTERED=center");
			IntPtr ptr = IntPtr.Zero;
			ptr = Sdl.SDL_GetVideoInfo();
			if (ptr == IntPtr.Zero)
			{
				Console.WriteLine("Error querying video info");
				Sdl.SDL_Quit();
				return;
			}
			videoinfo = (Sdl.SDL_VideoInfo)Marshal.PtrToStructure(ptr, typeof(Sdl.SDL_VideoInfo));
			/* according to the SDL documentaion, the flags parameter passed to setVideoMode
			   affects only the 2D SDL surface, not the openGL. To set their properties
			   use the syntax below. We enable vsync because we are running the loop
			   unfettered and we don't want the loop redrawing the buffer
			   while it is being written to screen
			*/
			//This should run without explicitly setting it.. hopefully
			//Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_SWAP_CONTROL, 1);//enable vsync How do I do this in windows?
			Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_RED_SIZE, 8);
			Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_GREEN_SIZE, 8);
			Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_BLUE_SIZE, 8);
			Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_DEPTH_SIZE, 16);
			Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_MULTISAMPLEBUFFERS, 1);
			Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_MULTISAMPLESAMPLES, 2);
			Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_DOUBLEBUFFER, 1);

			/* the setVideoMode function returns the current frame buffer as an
			   SDL_Surface. Again, we grab a pointer to it, then place its
			   content into the non pointery surface variable. I say 'non-pointery',
			   but this SDL variable must have a pointer in it because it can
			   access the current pixels in the framebuffer.
			*/
			ptr=IntPtr.Zero;
			ptr=Sdl.SDL_SetVideoMode(width, height, bpp, flags);
			if(ptr==IntPtr.Zero){
				Console.WriteLine("Error qsetting the video mode");
				Sdl.SDL_Quit();
				return;
			}
			surface=(Sdl.SDL_Surface)Marshal.PtrToStructure(ptr, typeof(Sdl.SDL_Surface));
		}
		private void setOpenGL()
		{
			resize(width, height);
			Gl.glMatrixMode(Gl.GL_MODELVIEW);
			Gl.glLoadIdentity();
			Gl.glEnable(Gl.GL_DOUBLEBUFFER);
			Gl.glEnable(Gl.GL_DEPTH_TEST);
			Gl.glShadeModel(Gl.GL_SMOOTH);
			Gl.glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
			Gl.glClearDepth(1.0f);
			Gl.glDepthFunc(Gl.GL_LEQUAL);
			Gl.glHint(Gl.GL_PERSPECTIVE_CORRECTION_HINT, Gl.GL_NICEST);
		}
		private void resize(int width, int height)
		{
			if (height == 0) height = 1;//avoid divide by zero
			/* For some reason my system is squashing the cube. To rectify
			   the situation I have altered the width/height ratio by
			   0.09. I think the problem is either that I am running
			   dual monitors with different resolutions, or Tao.SDL is
			   playing funny. The same nehe cube runs true when run
			   SDL on C++.
			*/
			float ratio = (float)(width / height) + 0.09f;
			Gl.glViewport(0, 0, width, height);
			Gl.glMatrixMode(Gl.GL_PROJECTION);
			Gl.glLoadIdentity();
			Glu.gluPerspective(55.0f, ratio, 0.1f, 50.0f);
			Gl.glMatrixMode(Gl.GL_MODELVIEW);
			Gl.glLoadIdentity();
		}
		private void tick(long delay)
		{
			Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
			Gl.glLoadIdentity();
			game.tick(delay);
			Gl.glFlush();
			Sdl.SDL_GL_SwapBuffers();
		}
		[STAThread]
		public static void Main()
		{
			//for attaching a console
			AttachConsole(ATTACH_PARENT_PROCESS);
			//regular code
			Program program = new Program();
		}
	}
}

Game.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tao.Sdl;
using Tao.OpenGl;
using System.Runtime.InteropServices;
namespace TaoTest
{
	class Game
	{
		private bool fin = false;
		private Sdl.SDL_Event e;
		// used to rotate the cube
		private float xrot=0.0f;
		private float yrot=0.0f;
		private short xmot;
		private short ymot;
	  
	  
		/* The constructor is called from the Program constructor prior to the
		   setting up of the game loop. Here we use it to hide the cursor.
		   The first two functions should be called together if you want
		   to both hide the cursor, and keep it centered on screen. If the
		   second function is omitted the cursor will still move about the
		   screen, it will not be seen, but when it hits a screen edge and can
		   go no further, it will stop firing events that would push it off the
		   screen; not such a good idea when the monster is behind you and you
		   need to be able to move the mouse in that direction and have your
		   avatar turn around.
		   A new event structure is initialized here and re-used for all events
		   rather than constructing a new event every game loop
		*/
		public Game(){
			Sdl.SDL_ShowCursor(Sdl.SDL_DISABLE);
			Sdl.SDL_WM_GrabInput(Sdl.SDL_GRAB_ON);
			e=new Sdl.SDL_Event();
		}
		/* Use a C# property to get and set the loop variable fin. Properties,
		   as oppesed to methods are compiled as assignments rather than pass
		   by value functions. Kinda cooler even than Java.
		*/
		public bool finP
		{
			get { return fin; }
			set { fin = value; }
		}
		/* pollEvents is called from the tick function in this class.
		   It will pop events off the queue untill it is empty. Any event of
		   interest will be caught in the switch statement and its case
		   handled. Actually, the SDL_QUIT will never eventuate because the window
		   is frameless and therefore has no quit button. You need to press
		   escape

		   At this stage the events, when triggered, write to the console at the
		   bottom of the MonoDevelop IDE. To make better sense of the output
		   you might like to comment the first two Console.WriteLine statements
		   pertaining to dx and dy because thay are called every loop and somewhat
		   swamp the key and mouse button presses in the Console. Also note that
		   I do not use wasd, I use efa and space.
		*/
		private void pollEvents()
		{
			while (Sdl.SDL_PollEvent(out e) == 1)
			{
				switch (e.type)
				{
					case Sdl.SDL_QUIT:
						fin = true;
						break;
					case Sdl.SDL_KEYDOWN:
						if (e.key.keysym.sym == Sdl.SDLK_ESCAPE) finP = true;//works
						if (e.key.keysym.sym == Sdl.SDLK_e) Console.WriteLine("Moving ford");
						if (e.key.keysym.sym == Sdl.SDLK_f) Console.WriteLine("Moving back");
						if (e.key.keysym.sym == Sdl.SDLK_a) Console.WriteLine("Strafing left");
						if (e.key.keysym.sym == Sdl.SDLK_SPACE) Console.WriteLine("Strafing right");
						break;
					case Sdl.SDL_MOUSEBUTTONDOWN:
						if (e.button.button == Sdl.SDL_BUTTON_LEFT) Console.WriteLine("Let the shoosting begin");
						if (e.button.button == Sdl.SDL_BUTTON_RIGHT) Console.WriteLine("Using an object");
						break;
					case Sdl.SDL_MOUSEMOTION:
						//Console.WriteLine("Heading to {0}", e.motion.xrel);
						//Console.WriteLine("Pitch to {0}", e.motion.yrel);
						xmot = e.motion.xrel;
						ymot = e.motion.yrel;
						break;
				}
			}
		}
		private void draw()
		{
			Gl.glEnable(Gl.GL_TEXTURE_2D);
			IntPtr fontptr = SdlTtf.TTF_OpenFont("c:\\windows\\fonts\\calibri.ttf",12);
			//SdlTtf.TTF_Font font = (SdlTtf.TTF_Font)Marshal.PtrToStructure(fontptr,typeof(SdlTtf.TTF_Font));
			Sdl.SDL_Color color = new Tao.Sdl.Sdl.SDL_Color(255, 255, 255);
			int[] texIDs = new int[1];
			Gl.glGenTextures(1, texIDs);
			IntPtr surfptr = SdlTtf.TTF_RenderText_Blended(fontptr, "test", color);
			//Program.surface = (Sdl.SDL_Surface)Marshal.PtrToStructure(surfptr, typeof(Sdl.SDL_Surface));
			Gl.glBindTexture(Gl.GL_TEXTURE_2D, texIDs[0]);
			Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, 4, Program.surface.w, Program.surface.h, 0, Gl.GL_BGRA, Gl.GL_UNSIGNED_BYTE, Program.surface.pixels);
			//These set bilinear processing on both large and small textures
			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);
		  
			float textWidth = 12.0f;
			float textHeight = 1.5f;
			float textXOrigin = 0.0f;
			float textYOrigin = 0.0f;
			float textZOrigin = 0.0f;

			//Gl.glTranslatef(0.0f, 0.0f, -10.0f);
			//Gl.glRotatef(xrot += 0.03f, (float)xmot, 0.0f, 0.0f);//md with event info from mouse
			//Gl.glRotatef(yrot += 0.03f, 0.0f, (float)ymot, 0.0f);
			Gl.glBegin(Gl.GL_QUADS);
			/*
			// Front Face
			Gl.glNormal3f(0.0f, 0.0f, 1.0f);
			Gl.glColor3f(0.2f, 0.5f, 0.2f);
			Gl.glVertex3f(-1.0f, -1.0f, 1.0f);
			Gl.glVertex3f(1.0f, -1.0f, 1.0f);
			Gl.glVertex3f(1.0f, 1.0f, 1.0f);
			Gl.glVertex3f(-1.0f, 1.0f, 1.0f);
			// Back Face
			Gl.glNormal3f(0.0f, 0.0f, -1.0f);
			Gl.glColor3f(0.5f, 0.2f, 0.5f);
			Gl.glVertex3f(-1.0f, -1.0f, -1.0f);
			Gl.glVertex3f(-1.0f, 1.0f, -1.0f);
			Gl.glVertex3f(1.0f, 1.0f, -1.0f);
			Gl.glVertex3f(1.0f, -1.0f, -1.0f);
			// Top Face
			Gl.glNormal3f(0.0f, 1.0f, 0.0f);
			Gl.glColor3f(0.7f, 0.5f, 0.2f);
			Gl.glVertex3f(-1.0f, 1.0f, -1.0f);
			Gl.glVertex3f(-1.0f, 1.0f, 1.0f);
			Gl.glVertex3f(1.0f, 1.0f, 1.0f);
			Gl.glVertex3f(1.0f, 1.0f, -1.0f);
			// Bottom Face
			Gl.glNormal3f(0.0f, -1.0f, 0.0f);
			Gl.glColor3f(0.2f, 0.2f, 0.8f);
			Gl.glVertex3f(-1.0f, -1.0f, -1.0f);
			Gl.glVertex3f(1.0f, -1.0f, -1.0f);
			Gl.glVertex3f(1.0f, -1.0f, 1.0f);
			Gl.glVertex3f(-1.0f, -1.0f, 1.0f);
			// Right face
			Gl.glNormal3f(1.0f, 0.0f, 0.0f);
			Gl.glColor3f(0.2f, 0.5f, 0.5f);
			Gl.glVertex3f(1.0f, -1.0f, -1.0f);
			Gl.glVertex3f(1.0f, 1.0f, -1.0f);
			Gl.glVertex3f(1.0f, 1.0f, 1.0f);
			Gl.glVertex3f(1.0f, -1.0f, 1.0f);
			// Left Face
			Gl.glNormal3f(-1.0f, 0.0f, 0.0f);
			Gl.glColor3f(0.8f, 0.2f, 0.6f);
			Gl.glVertex3f(-1.0f, -1.0f, -1.0f);
			Gl.glVertex3f(-1.0f, -1.0f, 1.0f);
			Gl.glVertex3f(-1.0f, 1.0f, 1.0f);
			Gl.glVertex3f(-1.0f, 1.0f, -1.0f);
			*/
			//Textstuffs
			Gl.glTexCoord2f(0.0f, 0.0f);  Gl.glVertex2f(textXOrigin, textYOrigin);
			Gl.glTexCoord2f(0.0f, 1.0f);  Gl.glVertex2f(textXOrigin, textYOrigin + 2);
			Gl.glTexCoord2f(1.0f, 1.0f);  Gl.glVertex2f(textXOrigin - 12, textYOrigin + 2);
			Gl.glTexCoord2f(0.0f, 1.0f);  Gl.glVertex2f(textXOrigin - 12, textYOrigin);
			Gl.glDisable(Gl.GL_TEXTURE_2D);
		  
			Gl.glEnd();
		  
		}
		/* Events will basically drive the program logic, which in
		   turn determines what the game class decides to draw. Once the
		   logic is in place, the drawing can begin.
		*/
		public void tick(long delay)
		{
			pollEvents();
			draw();
		}
	}
}

What tutorial should I use?

27 August 2009 - 04:51 AM

I'm planning on learning DirectX (in C#), but I'm really not sure what tutorial I should use. There are a number of tutorials online, but they all follow different coding styles, and do things differently, and I'm really not sure which one would be a better idea to follow. I figured that someone here could point me in the direction of a tutorial that the community here thinks is a good one to use. This isn't the first time I've attempted to learn DirectX but I never get past the first little bit because unlike most APIs, the DX API is so damned confusing and complex that I really can't figure much of it out on my own.

Drawing over top of a 3D game

12 November 2008 - 04:28 PM

Alright, I'm thinking of making a simple ping notification program that is supposed to run on top of a game, and show me my ping to the game server as I'm playing. However, I have not been able to find any info on how to actually draw over top of a game. I've seen programs like FRAPS draw on top of a game (I've seen a few others do similar things as well), so I've been wondering how exactly to do this. I'm assuming that it is possible to hook the draw function and insert my own code, but I'm hoping that's not the only way, since I know jack all about hooking function calls.

I'm bored, give me some programming ideas.

26 March 2008 - 07:08 PM

Keep in mind that I'd like to do as much or all of it in assembly. I'm just looking to get some small project to help get me back into programming. I'm looking eventually to try my hand at OS Development (there's a boatload of info on that stuff, actually). But for now, I need to get back into programming. There's a lot of assembly required when writing an OS, so I'd like to just do stuff in assembly. Anyway, you can bombard me with ideas now.

PARTNERS