Want an OpenGL 2D example (VC++, draw a rectangle)

前端 未结 2 1375
攒了一身酷
攒了一身酷 2021-01-05 13:24

I want to create a high performace 2D program.

I\'m using VC++2008. Suppose I have created the main window. What I want is to draw a red rectangle (top left: 10,20,

2条回答
  •  盖世英雄少女心
    2021-01-05 13:49

    Thank SuperMaximo93. I fininally got my code to work. Here is my code on window's size is changed. The function below is called in WM_SIZE's handler.

    void OpenGlWindow::Resize(HWND hwnd)
    {
        // Get new window size
        RECT rect;
        int width, height;
    
        GetClientRect(hwnd, &rect);
        width = rect.right;
        height = rect.bottom;
    
        glClearColor(0.0, 0.0, 0.0, 0.0);  //Set the cleared screen colour to black
        glViewport(0, 0, width, height);   //This sets up the viewport so that the coordinates (0, 0) are at the top left of the window
    
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
    
        //Back to the modelview so we can draw stuff 
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    
        glRotatef(180, 1.0f, 0, 0);
        glTranslatef(-1.0f, -1.0f, 0);
        glScalef(2.0f/width, 2.0f/height, 1);
    
        glTranslatef(2.0f, 2.0f, 0);
    }
    

提交回复
热议问题