我正嘗試創建一條400x300的屏幕,其中一條線從點(180,15)移動到點(10,145)。這裏是我的顯示功能,我創建了行:在OpenGL中繪製一條座標以像素爲單位的座標線
@Override
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2(); // get the OpenGL 2 graphics context
gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear color and depth buffers
gl.glLoadIdentity(); // reset the model-view matrix
gl.glColor3f(1.0f, 0.0f, 0.0f); //set color of line
gl.glTranslatef(0.0f, 0.0f, -6.0f); // translate into the screen
gl.glBegin(GL_LINES);
gl.glVertex2f(180, 15); //specify line-segment geometry must use relative values
gl.glVertex2f(10,145);
gl.glEnd();
gl.glFlush();
}
這是初始化函數,我首先定義屏幕的正投影參數:
@Override
public void init(GLAutoDrawable drawable) {
GL2 gl = (GL2) drawable.getGL(); // get the OpenGL graphics context
drawable.setGL(new DebugGL2(gl)); //set debugger. This will come in handy.
glu = new GLU(); // get GL Utilities
gl.glClearDepth(1.0f); // set clear depth value to farthest
gl.glEnable(GL_DEPTH_TEST); // enables depth testing
gl.glClearColor(1.0f, 1.0f, 1.0f, 0.0f); // sets base color for glClear()
gl.glDepthFunc(GL_LEQUAL); // the type of depth test to do
gl.glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // best perspective correction
gl.glShadeModel(GL_SMOOTH); // blends colors nicely, and smoothes out lighting
//orthographic parameters set here
glu.gluOrtho2D(0, 200, 0, 150);
}
,這是重塑功能從(我理解的)在調整屏幕大小時被調用。
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
GL2 gl = drawable.getGL().getGL2(); // get the OpenGL 2 graphics context
if (height == 0) height = 1; // prevent divide by zero
float aspect = (float)width/height;
glu = new GLU();
// Set the view port (display area) to cover the entire window
gl.glViewport(0, 0, width, height);
// Setup perspective projection, with aspect ratio matches viewport
glu.gluOrtho2D(0, 200, 0, 150); //set 2-D orthographic context
glu.gluPerspective(45.0, aspect, 0.1, 100.0); // fovy, aspect, zNear, zFar
// Enable the model-view transform
gl.glMatrixMode(GL_MODELVIEW);
gl.glLoadIdentity(); // reset
}
我也嘗試使用:
gl.glMatrixMode(GL_PROJECTION);
gl.glOrtho(0,200,150,0,-1,1);
和
gl.glMatrixMode(GL_PROJECTION);
gl.glOrtho(-1,1,-1,1,-1,1);
即使用的是相對規模。但是,我沒有做任何事情似乎工作。
你明白透視投影是什麼,你呢? –
將一個投影矩陣乘以另一個投影矩陣是沒有意義的。註釋掉你的gluOrtho2D或gluPerspective呼叫。 –
Andon,當我註釋掉gluPerspective調用時,屏幕上沒有呈現任何內容。帕維爾,我明白,我正在基於我使用gluOrtho2D或glOrtho定義的世界座標系統在屏幕上投影圖像。 –