想要一个OpenGL的2D例子(VC ++,绘制一个矩形) [英] Want an OpenGL 2D example (VC++, draw a rectangle)

查看:257
本文介绍了想要一个OpenGL的2D例子(VC ++,绘制一个矩形)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个高服务表现2D节目。

I want to create a high performace 2D program.

我用VC ++ 2008。假设我已经创建了主窗口。我想是画一个红色矩形(左上角:10,20,右下:200,300)。在像什么fillRect方法()API,但不使用OpenGL主窗口的客户区

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, bottom right: 200, 300) in the main window's client area like what 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:是否有OpenGL的一个2D绘图API或LIB类似的Direct2D / DirectWrite的

Does OpenGL have a 2D drawing API or lib like Direct2D/DirectWrite?

推荐答案

我强烈建议你看看一些OpenGL教程(的http:// nehe.gamedev.net/ 是一个很好的网站,它会告诉你一切你需要知道的),但假设你有正确设置你的code的OpenGL,你会做这样的事在您DrawRectWithOpenGL功能(我不是一个C ++程序员,但我认为我的code相适合C ++):

I strongly suggest you take a look at some OpenGL tutorials (http://nehe.gamedev.net/ is a good site, it'll tell you everything you need to know), but assuming you have OpenGL set up correctly in your code, you'd do something like this in your 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, i.e. 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不具有2D绘图API,但由只忽略Z轴(或只是使用Z轴为确保精灵绘制在正确的深度,即所以可以得出其容易做到的事情2D前景后背景因为背景的深度被设定为使得它位于前景后面)。希望这可以帮助。肯定花时间看一些教程,它都将变得清晰起来。

OpenGL doesn't have a 2D drawing API but its easy to do 2D things by just ignoring the Z axis (or just using the Z axis for making sure that sprites are drawn at the correct depth, i.e. so you can draw the background after the foreground because the background's depth is set so it lies behind the foreground). Hope this helps. Definitely take the time to look at some tutorials and it will all become clear.

修改: 对于设置OpenGL进行2D绘图,你需要建立一个正投影(我挂教程使用视野下的投影,遗憾的是没有提)。下面是我的一些code,这是否(这应该是放在你的InitOpenGL功能,您创建了窗口之后)的一个例子:

EDIT: For setting up OpenGL for 2D drawing, you need to set up an orthographic projection (the tutorials that I linked to uses perspecive projection, sorry for not mentioning that). Here's an example of some of my code that does this (which should be somewhere in your InitOpenGL function, after you have created 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

这就是初始化排序。您应该能够使用的值像100和200,并让它们在屏幕上正确显示,即坐标以像素为单位。

So that's the initialisation sorted. You should be able to use values like 100 and 200 and have them appear correctly on the screen, i.e. the coordinates are in pixels.

要处理的窗口大小的变化,你会做的是找到窗口的大小(使用外部库。我不知道,你可以在OpenGL做到这一点,虽然glutGet(GLUT_WINDOW_WIDTH)和glutGet(GLUT_WINDOW_HEIGHT)的转运蛋白可能的工作,但我还没有测试它),然后再次调用glViewport和glOrtho与更新的窗口大小。

To handle window size changes, what you would do is find the size of the window (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 might work, but I haven't tested it), and then call glViewport and glOrtho again with the updated window size.

此外,你必须使用一个外部库来找出到底的的窗口大小的变化。我不知道,你可以使用的,但如果你碰巧使用SDL那么可能有一些事件处理它(并可能能够返回屏幕的宽度和高度,如果glutGet不工作)。

Again, you'd have to use an external library to find out exactly when the window size changes. I'm not sure what you could use, although if you happen to be using SDL then that might have some event handling for it (and may be able to return the screen width and height, if glutGet doesn't work).

我希望这有助于!

这篇关于想要一个OpenGL的2D例子(VC ++,绘制一个矩形)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆