2012-11-14 201 views
0
private void init_video() { 
    uint32 video_flags = SurfaceFlag.SWSURFACE | SurfaceFlag.OPENGL; 
    screen = Screen.set_video_mode (SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, video_flags); 
    if (screen == null) stderr.printf ("Could not set video mode.\n"); 

    glClearColor (0.0f, 0.0f, 0.0f, 1.0f); 
    glMatrixMode (GL_PROJECTION); 
    glLoadIdentity(); 
    glFrustum (-2.0f, 2.0f, -2.0f, 2.0f, 1.0f, 300.0f); 
    glMatrixMode (GL_MODELVIEW); 
    glEnable (GL_DEPTH_TEST); 
    glEnable (GL_TEXTURE_2D); 

    SDL.WindowManager.set_caption ("Title", "Title"); 
} 

這是我爲正在製作的遊戲設置相機的功能。我有這個功能來繪製一個立方體:在3D上下文中繪製2D點

public void draw() { 
    glLoadIdentity(); 
    glTranslatef (x, y, z); 
    glScalef (size, size, size); 
    glRotatef (x_angle, 1.0f, 0.0f, 0.0f); 
    glRotatef (y_angle, 0.0f, 1.0f, 0.0f); 
    glRotatef (z_angle, 0.0f, 0.0f, 1.0f); 
    glBegin (GL_QUADS); 

    /* Front face */ 
    glColor3f (1.0f, 0.0f, 0.0f); 
    glVertex3f (0.5f, 0.5f, 0.5f); 
    glVertex3f (-0.5f, 0.5f, 0.5f); 
    glVertex3f (-0.5f, -0.5f, 0.5f); 
    glVertex3f (0.5f, -0.5f, 0.5f); 

    /* Left face */ 
    glColor3f (0.0f, 1.0f, 0.0f); 
    glVertex3f (-0.5f, 0.5f, 0.5f); 
    glVertex3f (-0.5f, -0.5f, 0.5f); 
    glVertex3f (-0.5f, -0.5f, -0.5f); 
    glVertex3f (-0.5f, 0.5f, -0.5f); 

    /* Back face */ 
    glColor3f (0.0f, 0.0f, 1.0f); 
    glVertex3f (0.5f, 0.5f, -0.5f); 
    glVertex3f (-0.5f, 0.5f, -0.5f); 
    glVertex3f (-0.5f, -0.5f, -0.5f); 
    glVertex3f (0.5f, -0.5f, -0.5f); 

    /* Right face */ 
    glColor3f (1.0f, 1.0f, 0.0f); 
    glVertex3f (0.5f, 0.5f, 0.5f); 
    glVertex3f (0.5f, -0.5f, 0.5f); 
    glVertex3f (0.5f, -0.5f, -0.5f); 
    glVertex3f (0.5f, 0.5f, -0.5f); 

    /* Top face */ 
    glColor3f (0.0f, 1.0f, 1.0f); 
    glVertex3f (0.5f, 0.5f, 0.5f); 
    glVertex3f (-0.5f, 0.5f, 0.5f); 
    glVertex3f (-0.5f, 0.5f, -0.5f); 
    glVertex3f (0.5f, 0.5f, -0.5f); 

    /* Bottom face */ 
    glColor3f (1.0f, 0.0f, 1.0f); 
    glVertex3f (0.5f, -0.5f, 0.5f); 
    glVertex3f (-0.5f, -0.5f, 0.5f); 
    glVertex3f (-0.5f, -0.5f, -0.5f); 
    glVertex3f (0.5f, -0.5f, -0.5f); 
    glEnd(); 
} 

如何繪製點,就好像它是一個2D上下文?有任何想法嗎?

+0

什麼是2D上下文? OpenGL中的2D點只是z值爲'0'的3D點。要真正看待事物,就好像你在用2D繪圖一樣,你需要設置一個正確的投影矩陣。不相關:整個'glBegin/glEnd' API已棄用。 – pmr

回答

3

在OpenGL中沒有任何2D環境。一切都是3D。 但可以使用平行投影模擬2D與glOrtho

// Setup Orthogonal projection to window coordiantes 
glMatrixMode(GL_PROJECTION); 
glLoadIdentity(); 
glOrtho(0, WindowWidth-1, 0, WindowHeight-1, -1, 1); 
glMatrixMode(GL_MODELVIEW); 
glLoadIdentity(); 

// draw quad in screen coodinates 
glBegin(GL_QUADS); 
    glColor3f(1.0f, 1.0f, 1.0f); 
    glVertex2i(10, 10); 
    glVertex2i(10, 80); 
    glVertex2i(80, 80); 
    glVertex2i(80, 10); 
glEnd(); 

glVertex2就像glVertex3與第三coorinate設置爲0。

在OpenGL中4 所有上述命令被刪除。但是glOrtho文檔向您展示了您需要創建的矩陣,以在窗口座標中設置渲染。