1. One of the important concepts in OpenGL: Surround.
2. Detailed introduction of GL11. Gl_triangle_fan and Gl11.gl_triangle_strip parameters.
1. Surround
We know that there are two ways to draw a triangle: clockwise and counterclockwise. As shown in the following illustration:
In OpenGL, the specified order of vertices and the combination of directions are called wrapping (winding).
Therefore, the left triangle in the image above is referred to as having a counterclockwise wrapping direction, and the right triangle is considered to have a clockwise wrapping direction. By default, OpenGL considers polygons to be surrounded in a counterclockwise direction. This means that the triangle on the left side of the diagram is the proof of this triangle, and the triangle on the right is the back of the triangle. (Is it not understandable ...) Haha, you must be in the two-dimensional plane to consider the problem, change to three-dimensional plane, and then try again. )
Why is this question so important? Soon you will see that you often want to set different physical properties for the front and back of a polygon. You can hide the back of a polygon (this can reduce rendering work), or set it a different color and reflection property (this does not understand, look forward to the tutorial bar J). In a scene, it is important to keep all the polygons in a consistent surround, and to use a front polygon to draw the appearance of all solid objects.
Of course, if you want to change this default behavior of OpenGL, you can call the following function: Gl11.glfrontface (GL11.GL_CW), GL_CW parameter tells OpenGL that the polygon that surrounds clockwise will be considered positive. You can use the GL11.GL_CCW parameter to restore the front face back to a counterclockwise surround. (It is not recommended to change the default behavior, or the conventional good, otherwise people do not understand your code).
2.gl11.gl_triangle_fan
In many cases, we're going to draw multiple triangles, but these triangles have a central point around them, and how do we draw them? According to the method we talked about last time, there is only one vertex data that specifies a triangle first, then the triangle is drawn, then the specified vertex information is cleared, and then a triangle is drawn ... So, a picture of one, we can imagine, for the center vertex, we have to assign a number of times, and this time and time again the efficiency of the triangle is certainly very low. Now we use the Gl11.gl_triangle_fan parameter to draw multiple triangles around a center point. The code is as follows:
Package Com.helloworld.fneg;
import java.util.Random;
import Com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.backends.jogl.JoglApplication;
import Com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL11;
import Com.badlogic.gdx.graphics.Mesh;
import Com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
/**
*@Copyright: Copyright (c) 2008-2100
*@Company: Sagret
*@Author: Fengcunhan fengcunhan@gmail.com
*@Package: Com.helloworld.fneg
*@FileName: Fan**.java
*@Time: 2011-1-13
*@User: Feng
*/
Public class Fantriangle implements Applicationlistener {
Private Mesh Dodecagonmesh;
Private float [] vertices;
Private Short [] indexs;
Private Random random;
@Override
Public void Create () {
TODO Auto-generatedmethod stub
dodecagonmesh=New Mesh (true, 32,8, new Vertexattribute (usage.position, 3, "point"), NewVertexattribute (usage.colorpacked, 4, "color"));
float bordlength=0.5f;
float angleincrease= (float) ((60f/360f) *2.0f*3.14f);
random=New Random ();
int i=0;
vertices=new float[32];
vertices[i++]=0;
vertices[i++]=0;
vertices[i++]=0;
Vertices[i++]=color.tofloatbits (255, 0, 0,0);//Set the color of the center point
Set the color of a hexagon six vertices
for (float angle=0;angle<= (2.0f*3.14f); angle+=angleincrease) {
float x= (float) (Bordlength*math.cos (angle));
float y= (float) (bordlength*math.sin (angle));
float z=0;
Vertices[i++]=x;
Vertices[i++]=y;
Vertices[i++]=z;
Vertices[i++]=color.tofloatbits (Random.nextint (255), Random.nextint (255), Random.nextint (255), 0);
}
indexs=New short []{0,1,2,3,4,5,6,7};
Dodecagonmesh.setvertices (vertices);
Dodecagonmesh.setindices (INDEXS);
}
@Override
Public void Resume () {
TODO Auto-generatedmethod stub
}
@Override
Public void render () {
TODO Auto-generatedmethod stub
Draw a hexagon
Dodecagonmesh.render (Gl11.gl_triangle_fan, 0, 8);
}
@Override
Public void Resize (int width, int height) {
TODO Auto-generatedmethod stub
}
@Override
Public void Pause () {
TODO Auto-generatedmethod stub
}
@Override
Public void Dispose () {
TODO Auto-generatedmethod stub
}
/**
* @param args
* @Description:
*/
Public Static void Main (string[] args) {
TODO Auto-generatedmethod stub
New Joglapplication (new fan** (), "Hello World", 480, 480, false);
}
}
To run the code, we get the following figure (it may be different from me because the color is random):
With the same code, using the gl11.gl_triangle_strip parameter, the following figure is obtained :
So how do you get a hexagon using the gl11.gl_triangle_strip parameter? Let's get this done by ourselves. I hope you can put it in the reply.
Reprint Source: http://space.189works.com/space-uid-10009991.html