How to create a new thread in JOGL??

I use netbean6.1 to do this work.

public void display(GLAutoDrawable drawable) {
//I want to create a new thread here to draw an object
Thread dt = new Thread(new DrawThread(gl,2),“Thread0”); //create a thread,DrawThread is a class I define
dt.start(); //it never draw
//dt.run(); it drew what I want but the main thread was blocked
//How should I do if I want to view the procedure of drawing an object?
}

help me…

OpenGL operations can only be done on the thread that has the OpenGL context as ‘current’. If you’re using the GLEventListener interface (which I assume you are because you have a display(GLAutoDrawable) method), then the context is current when any of the interface’s methods are called: display(), init(), reshape()…
It is in these methods that you should do your OpenGL operations.

Basically, you can’t create a new thread and make that do the drawing for you because that new thread will not have a ‘current’ OpenGL context. If you want, you can perform a context switch (eg: make the ‘current’ thread release the context, and then make it ‘current’ on the thread you made before it does it’s rendering), but doing so isn’t the way to make your program multi-threaded.

The display()-method is already called from a dedicated thread (normally the AWT Event Thread), so most of the time you don’t need to mess with that. I would advice not to touch the opengl thread handling and move non-gl code like image loading, game AI, physics calculation etc. to a different thread, if you want to utilize multithreading.

In the circumstance ,how could I make a MultiThead JOGL program?

  1. What are you trying to do?
  2. Why does it have to be multithreaded?

You don’t really make a multi-threaded JOGL program (ie: multiple rendering threads, because that doesn’t work), but rather a multi-threaded program with one thread dedicated to OpenGL rendering (eg: one thread for OpenGL operations, another for audio, and however many else you need for the rest of your program).

But what puzzles me is that how I could run a car beside my car in a car racing game,isn’t it using a new thread to run the car?

No, it isn’t.

It’s a common misconception when starting game programming. (Hardware-)Threads are way to expensive and introduce nasty sideeffects. You normally create something like e.g. a CarManager, that is able to update the positions of all the cars. This could then very well be done in a dedicated Thread separated from rendering, but for simple designs you can just call the update()-method of your CarManager first place in display().

Just don’t bother with threads for the beginning, because dealing with concurrency, volatile variables and locked memory access is a task of it’s own.

Thanks…
But how should I code if Iwant to watch the slowly process of drawing an object?

???

I am afraid you need to explain what you want to do in more detail…

As far as I know there is no automatic way of doing this (please correct me if I’m wrong). You have to animate this youself. That means you have to controll how many triangles you draw each frame. If you start with 0 and increase by one for each frame you will have an animation of how it is drawn.