Want to use the OpenGL 2D example (VC ++, draw a rectangle)

I want to create a high-performance 2D program.

I am using VC ++ 2008. Suppose I created a main window. I want to draw a red rectangle (top left: 10.20, bottom right: 200, 300) in the client window area of โ€‹โ€‹the main window, as the FillRect () API does, but using OpenGL.

void InitOpenGL(HWND hwnd) { .... // What should I do? // Only for 2D drawing, and the (0,0) should be the top left point. } // the function below will be called in the WM_PAINT handler void DrawRectWithOpenGL(RECT* pRect) { .... // What should I do? } 

EDIT1: Is there a 2D drawing API or lib in OpenGL like Direct2D / DirectWrite?

+4
source share
2 answers

I highly recommend that you take a look at some OpenGL tutorials ( http://nehe.gamedev.net/ is a good site, it will tell you everything you need to know), but if you had OpenGL configured correctly in your code, you would have done that something like that in my DrawRectWithOpenGL function (I'm not a C ++ programmer, but I think my code looks right for C ++):

 void DrawRectWithOpenGL(RECT* pRect) { glPushMatrix(); //Make sure our transformations don't affect any other transformations in other code glTranslateF(pRect->x, pRect->y); //Translate rectangle to its assigned x and y position //Put other transformations here glBegin(GL_QUADS); //We want to draw a quad, ie shape with four sides glColor3F(1, 0, 0); //Set the colour to red glVertex2F(0, 0); //Draw the four corners of the rectangle glVertex2F(0, pRect->h); glVertex2F(pRect->w, pRect->h); glVertex2F(pRect->w, 0); glEnd(); glPopMatrix(); } 

OpenGL does not have a 2D drawing API, but it is easy to execute 2D objects by simply ignoring the Z axis (or simply using the Z axis to ensure that the sprites are drawn at the correct depth, i.e. you can draw a background after the foreground, because the background depth is set so that it lies behind the foreground). Hope this helps. Definitely take the time to look at some lessons and everything will become clear.

EDIT : To configure OpenGL for a 2D drawing, you need to set up spelling projection (the tutorials I linked use predictive projection, sorry, without mentioning this). Here is an example of my code that does this (which should be somewhere in your InitOpenGL function after creating your window):

 glClearColor(0.0, 0.0, 0.0, 0.0); //Set the cleared screen colour to black glViewport(0, 0, screenwidth, screenheight); //This sets up the viewport so that the coordinates (0, 0) are at the top left of the window //Set up the orthographic projection so that coordinates (0, 0) are in the top left //and the minimum and maximum depth is -10 and 10. To enable depth just put in //glEnable(GL_DEPTH_TEST) glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, screenwidth, screenheight, 0, -10, 10); //Back to the modelview so we can draw stuff glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT); //Clear the screen and depth buffer 

So, initialization sorting. You should be able to use values โ€‹โ€‹such as 100 and 200, and display them correctly on the screen, i.e. Coordinates are in pixels.

To handle window resizing, you have to find the window size (using an external library. I'm not sure you can do it in OpenGL, although glutGet (GLUT_WINDOW_WIDTH) and glutGet (GLUT_WINDOW_HEIGHT) from GLUT may work, but I haven't tested it), and then call glViewport and glOrtho again with the updated window size.

Again, you will have to use an external library to find out when the window is resized. I'm not sure what you could use, although if you use SDL then it might have some event handling (and maybe it will be able to return the width and height of the screen if glutGet doesn't work).

Hope this helps!

+8
source

Thanks to SuperMaximo93. I finally got my code to work. Here is my window resize code. The function below is called in the WM_SIZE 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); } 
+2
source

All Articles