关于地球初始模型的坐标否限定在世界坐标系的(-1,1)范围内比较困惑,源码中的modelviewmatrix也主要是在设置相机的方位,也就是视点转换,对于模型转换基本未处理,特此加以研究!
世界坐标系的单位区域[-1,1]恰好是窗体的区域,但这并不意味着所有的坐标都要限制在这一范围,模型坐标也好,世界坐标也好,都没有什么限制,关键要看相机能够透视到的范围是多少!
我们只是说世界坐标系的单位区域[-1,1]恰好是窗体的区域,但并不是说世界坐标的范围只能限定在[-1,1],尽管不做任何设置的情况下,在opengl中设置模型时的坐标都限定在[-1,1],但模型的坐标完全可以很大,在世界坐标系中的坐标也可以很大,只不过,需要在设置视图坐标系,建立相机方位进行透视的时候需要将范围设的大些,以便能够将模型完全看到。所以,此处纠正之前的错误认识!
测试代码如下:
import javax.swing.JFrame;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.util.FPSAnimator;
public class TestExtent implements GLEventListener{
GLU glu= new GLU();
private float rtri; //for angle of rotation
@Override
public void display( GLAutoDrawable drawable ) {
GL2 gl = drawable.getGL().getGL2();
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glPushMatrix();
/* double[] params= new double[16];
gl.glGetDoublev(GL2.GL_CURRENT_MATRIX_ARB, params, 0);
for(int i =0;i<16;i++){
System.out.print(params[i]+",");
}*/
gl.glClear (GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT );
// Clear The Screen And The Depth Buffer
gl.glLoadIdentity();
// Reset The View
// gl.glRotatef( rtri, 0.0f, 1.0f, 0.0f );//triangle rotation
gl.glBegin( GL2.GL_TRIANGLES ); // Drawing Using Triangles
/* gl.glColor3f( 1.0f, 0.0f, 0.0f ); //Red
gl.glVertex3f( 0.5f,0.5f,0.0f ); // Top
gl.glColor3f( 0.0f,1.0f,0.0f ); //blue
gl.glVertex3f( -0.5f,0.5f,0.0f ); // Bottom Left
gl.glColor3f( 0.0f,0.0f,1.0f ); //green
gl.glVertex3f( 0f,0.5f,0.0f ); // Bottom Right
*/
gl.glColor3f( 1.0f, 0.0f, 0.0f ); //Red
gl.glVertex3f( 5f,5f,0.0f ); // Top
gl.glColor3f( 0.0f,1.0f,0.0f ); //blue
gl.glVertex3f( -5f,5f,0.0f ); // Bottom Left
gl.glColor3f( 0.0f,0.0f,1.0f ); //green
gl.glVertex3f( 0f,-5f,0.0f ); // Bottom Right
gl.glEnd();
gl.glFlush();
rtri +=0.2f; //assigning the angle
gl.glPopMatrix();
}
@Override
public void dispose( GLAutoDrawable drawable ) {
//method body
}
@Override
public void init( GLAutoDrawable drawable ) {
// method body
}
@Override
public void reshape( GLAutoDrawable drawable, int x, int y, int width, int height ) {
// method body
GL2 gl = drawable.getGL().getGL2();
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0, width/ height, 1.0, 20.0);
glu.gluLookAt(0, 0, 20, 0, 0, 0, 0, 1, 0);
}
public static void main( String[] args ) {
//getting the capabilities object of GL2 profile
final GLProfile profile = GLProfile.get( GLProfile.GL2 );
GLCapabilities capabilities = new GLCapabilities( profile );
// The canvas
final GLCanvas glcanvas = new GLCanvas( capabilities );
TestExtent testExtent = new TestExtent();
glcanvas.addGLEventListener( testExtent );
glcanvas.setSize( 400, 400 );
//creating frame
final JFrame frame = new JFrame ( "Test Extent" );
//adding canvas to it
frame.getContentPane().add( glcanvas );
frame.setSize( frame.getContentPane().getPreferredSize() );
frame.setVisible( true );
//Instantiating and Initiating Animator
final FPSAnimator animator = new FPSAnimator( glcanvas, 300,true );
//animator.start();
}//end of main
}
利用gluLookAt将观察点设置的离模型远些,以便能够完全透视到!