[LibGDX] Calling LWJGL/OpenGL glBegin

I am trying to render in GL_TRANGLE_FANS for one of my projects, but my problem is I can’t seem to call OpenGL’s glBegin(int) using Gdx.gl or any others (gl10, 20, 11, common).

Apparently you can use Meshes, however with the way I need to do things I need to use OpenGL’s state base rendering (glBegin(GL_whatever_shape);//choose vertex3f’s;glEnd();).

What do you guys think?

Thanks,
-cMp

I can’t speak to using LibGDX, but the OpenGL functions you are trying to call are part of the fixed pipeline and are deprecated. Maybe LibGDX only exposes relevant functionality based on the OpenGL version you’re working in?

Aside from that, I would like to steer you away from the deprecated functions. For every function which has been placed on the endangered species list, there now exists a newer and better way of accomplishing the same goal. I’m curious why you need to use the older methodology.

Wow really, those are deprecated? What are the new functions?

I was interested in using those functions because I was trying to draw a 2d light source with this code:


gl.glBegin(GL.GL_TRIANGLE_FAN);
  {
    gl.glColor4f(0f, 0f, 0f,  intensity);
    gl.glVertex3f(center.x, center.y, depth);
      
    gl.glColor4f(0f, 0f, 0f, 0f);
      
    for (float angle=0; angle<=Math.PI*2; angle+=((Math.PI*2)/numSubdivisions) )
    {
      gl.glVertex3f( radius*(float)Math.cos(angle) + center.x,
                     radius*(float)Math.sin(angle) + center.y, depth);  
    }
      
    gl.glVertex3f(center.x+radius, center.y, depth);
  }
    gl.glEnd();

make sure your alpha blend in enabled and i would assume you would want to use 1f, 1f, 1f for your rgb’s since 0f, 0f, 0f is black.


GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
			Gl11.glEnable(GL11.GL_BLEND);
			GL11.glBegin(GL11.GL_TRIANGLE_FAN);
			  {
				
			    GL11.glColor4f(1f, 1f, 1f,  intensity);
			    GL11.glVertex3f(center.x,center.y, 0f);
			      
			    GL11.glColor4f(1f, 1f, 1f, 0f);
			      
			    for (float angle=0; angle<=Math.PI*2; angle+=((Math.PI*2)/subdivisions) )
			    {
			      GL11.glVertex3f( radius *(float)Math.cos(angle)+ center.x,
			                     radius * (float)Math.sin(angle)+ center.y, 0);  
			    }
			      
			    GL11.glVertex3f(radius + center.x, center.y, 0);
			  }
			    GL11.glEnd();

keep in mind that this method of rendering has been deprecated for a long time. with lower subdivisions it will look a little weird that’s because the alpha is blended for each triangle separately making the fact that there are many different triangles apparent

edit: Just realized your talking about using LWJGL through LibGDX. dunno how you would do this in LibGDX this is just the straight LWJGL way to do it. If it exists in LibGDX i would assume it would be very similar to the above code though

Where are you getting that code? Immediate mode was deprecated years ago; and is not supported by OpenGL ES.

The modern programmable pipeline is quite a bit more complicated – you need to set up shaders, VBOs, and all that jazz.

Fortunately, LibGDX includes a lot of utilities that make it easy to work in the programmable pipeline. For example, the ImmediateModeRenderer, which mimics the immediate mode you’re using there.

ImmediateModeRenderer r;

... create()
	if (Gdx.graphics.isGL20Available()) {
	    //normals=false, colors=true, numTexCoords=none 
	    r = new ImmediateModeRenderer20(false, true, 0); 
	} else {
	    r = new ImmediateModeRenderer10();
	}


... render()
	//enable srcOver blending
	Gdx.gl.glEnable(GL10.GL_BLEND);
	Gdx.gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);

	//for example, this can use your OrthographicCamera
	r.begin(camera.combined, GL20.GL_TRIANGLE_FAN);

	...

	//push our vertex data here...

	//.. similar to glColor4f
	r.color(0f, 0f, 0f, intensity);
	
	//.. similar to glVerte3f
	r.vertex(center.x, center.y, depth);
	
	...

	//flush the renderer
	r.end();

Ideally, if you plan to render many of these lights, it would be best to use GL_TRIANGLES and batch them into a single render call.

For more in-depth information on meshes:

All glBegin … glEnd and loads of other functions are depricated.
Replacements are vbo’s and shaders.

As RobinB said, you’ll want to use Vertex Array Objects, Vertex Buffer Objects and Shaders. These are the meat and potatoes of the new OpenGL pipeline. The process involves stuffing a bunch of information like Vertices into a buffer and then a script (called a shader) processes the data for rasterization. As DaveDes said, LibGDX has some utilities to make your life easier. It’s also going to require using matrices to position your objects in the 3D world. Again, LibGDX likely has a lot of that hard work done for you.
But don’t let all those long words scare you away. Just take it piece by piece and you’ll find it’s really not that bad. A little more work than the fixed pipeline, but easily doable.

Billboarding
This is likely getting a bit ahead of you, since you’ll want to get the basics of working VAOs, VBOs and Shaders down first. So this is for after you go down that dark road.

It looks like you’re putting together some billboards. This is the process of taking a flat panel and making it always face the camera. You can think of billboarding like a sign on the side of the road, only these signs would always face you no matter where you stand. Typically this is done with a quad and a transparent image instead of a disc. Rendering a quad is a lot faster, since it’s only 4 vertices. Though it’s not limited to just quads, so if you really want to use a disc, then you can still use the methods I’m going to list, to help tackle your problem.

Easy Method
Just don’t billboard. Stuff a sphere into a VAO/VBO and render it where you need. I bring this up, because if you just want something to represent where your light is for development purposes, there’s no reason to go mad over billboards just yet. If you want to use this in actual game play, then it’s probably not the best method. Especially if you’d be using a lot of them, then you would need to use instanced rendering and blah blah… so no.

Medium Method
Stuff a quad into a VAO/VBO. Pretty simple so far.
The next part is a little more tricky and requires using something called a “Look At” matrix. This does what the name implies. You feed it 3D vectors called Eye, At and Up. The matrix then does some behind the scenes magic and makes the object face where you want it to. This is perfect for billboards, just tell it to look at the camera and shazam instant billboard.

The previously mentioned vectors are
Eye: The location of the object.
At: A point the object is looking at
Up: Which direction the top of the object is facing (sort of).

Note: sometimes people refer to these vectors in different names like “right, center and up”. I imagine LibGDX has already implemented the math for this and you just need to figure out the syntax for calling it.

Difficult Method
My favorite method for handling billboards expands on the previous example. Though instead of flipping back and forth between the CPU and GPU, we simply feed a bunch of 3D vectors to the GPU and then use something called a Geomtry shader to create the actual billboards.
What makes this somewhat difficult is you’ll first need to learn how to use Geometry shaders. Then you’ll need to figure out how to calculate the Eye, At and Up vectors in the shader. So you’ll need to know a bit of basic vector math.
It’s not terribly complicated, it just requires a lot of basic knowledge about a bunch of different things.

The really nice part about this is being able to deal with billboards as single points, which becomes very useful when you get into particles systems. It also leaves the door open for some advanced particle systems which exist solely on the GPU (which means stupid fast).

You’ll probably want to work your way up to this though. I mention this mostly as something to keep in mind, so you don’t get too entrenched in a more basic method.

1 Like

Thanks for the reply Redocdam, I can tell you put work into it :). I am just trying to do 2d geometry shadow mapping though, but this information is enormously helpful! I will go on and face the world of OpenGL!!! Thou hast strengthened me!