2014-01-29 142 views
1

我正嘗試創建一條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); 

即使用的是相對規模。但是,我沒有做任何事情似乎工作。

+0

你明白透視投影是什麼,你呢? –

+2

將一個投影矩陣乘以另一個投影矩陣是沒有意義的。註釋掉你的gluOrtho2D或gluPerspective呼叫。 –

+0

Andon,當我註釋掉gluPerspective調用時,屏幕上沒有呈現任何內容。帕維爾,我明白,我正在基於我使用gluOrtho2D或glOrtho定義的世界座標系統在屏幕上投影圖像。 –

回答

1
  1. 設置的區域中,或視口,在該窗口內繪製到

    gl.glViewport(0, 0, width, height); //in pixels 
    
  2. 設置突起,以便你的OpenGL座標是在像素(符合該視口寬度/高度)

    //we want to modify the projection matrix (without this, mesh normals will break) 
    gl.glMatrixMode(GL_PROJECTION); 
    
    //clear any previous transforms the projection matrix may contain (otherwise it would be combined with the following glOrtho matrix) 
    gl.glLoadIdentity(); 
    
    //set the projection (could use glTranslate/glScale but this utility function is simpler) 
    gl.glOrtho(0, width, 0, height, -1, 1); //left,right,bottom,top,front,back 
    
    //common practice to leave modelview as the current matrix for editing 
    gl.glMatrixMode(GL_MODELVIEW); 
    

    現在查看量是由glOrtho定義的框。

    請注意,(0, 0)目前是左下角。如果你正在做任何GUI的東西,這是非常有用的翻轉這一點,並使我們從左到右讀下來的原點左上。沒有什麼能阻止你通過繪圖中途改變 - 用一些任意的座標系統做一些3D的東西,然後繪製HUD並設置投影以匹配像素。

  3. 在您的顯示功能提請你行...

    gl.glBegin(GL_LINES); 
    gl.glVertex2f(180, 15); 
    gl.glVertex2f(10,145); 
    gl.glEnd(); 
    

    ,或者展示使用的翻譯

    gl.glLoadIdentity(); //remove the following translate that's still in the modelview matrix from the previous display() call 
    gl.glTranslatef(180, 15); //modifies the modelview matrix 
    gl.glBegin(GL_LINES); 
    gl.glVertex2f(0, 0); 
    gl.glVertex2f(-170, 130); 
    gl.glEnd(); 
    

    你能想到的翻譯移動視圖下來並向左或向下移動模型(該行)並向右移動

+0

非常感謝您的幫助。這似乎奏效了。 –