使用OpenGL绘制一个填充的Rectangle [英] Draw a filled Rectangle using OpenGL

查看:101
本文介绍了使用OpenGL绘制一个填充的Rectangle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用OPENGL在vC6中绘制一个填充的矩形..

请帮我这个

I want to draw a filled rectangle in vC6 using OPENGL..
pls help me with this

推荐答案

这是这样的他们提供 glRect 的OpenGL的常见要求就是这样做。 Microsoft提供了几个此功能的版本 [ ^ ]采取不同的数据用于指定矩形边界的类型。
This is such a common requirement for OpenGL that they have provided the glRect that does exactly this. Microsoft have provided several versions of this function[^] that take different data types to specify the bounds of the rectangle.


使用GLUT,以下代码将执行此操作:

(您可能需要获取过剩或freeglut才能够另外,只需使用一个基于框架/对话框的应用程序和你自己的代码来设置窗口,处理密钥,处理空闲时间,处理调整大小等等都是相当啰嗦的。过剩或freeGlut更容易学习曲线)

此代码可以进一步修改,但会留给读者练习..



Using GLUT, the following code will do this:
(you may need to aquire glut or freeglut to be able to compile this. The alternative, just using a frame/dialog based app and your own code to setup the window, process the keys, handle idle times, handle resizing etc, etc is quite long-winded. glut or freeGlut are a much easier learning curve)
This code could be trimmed further, though will be left as an exercise to the reader..

#include <GL/glut.h>
#include <stdlib.h>

/* GLUT callback Handlers */
static void resize(int width, int height)
{
    const float ar = (float) width / (float) height;

    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity() ;
}

static void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBegin(GL_QUADS);
        glColor3d(1,0,0);
        glVertex3f(-1,-1,-10);
        glColor3d(1,1,0);
        glVertex3f(1,-1,-10);
        glColor3d(1,1,1);
        glVertex3f(1,1,-10);
        glColor3d(0,1,1);
        glVertex3f(-1,1,-10);
    glEnd();
    glutSwapBuffers();
}


static void key(unsigned char key, int x, int y)
{
    switch (key)
    {
        case 27 :
        case 'q':
            exit(0);
            break;
    }
    glutPostRedisplay();
}

static void idle(void)
{
    glutPostRedisplay();
}

/* Program entry point */
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("GLUT quadPoly");

    glutReshapeFunc(resize);
    glutDisplayFunc(display);
    glutKeyboardFunc(key);
    glutIdleFunc(idle);

    glClearColor(0,0,0,0);

    glutMainLoop();

    return EXIT_SUCCESS;
}





几个小时 - http://www.codecolony.de/OpenGL/ [ ^ ]

我也会推荐NEHE的openGL tutes,虽然我现在似乎无法访问它们一段时间了。也许你可以访问该页面的谷歌缓存版本



这是另一个哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇一套tutes。我为网站的标题道歉。 OpenGl tutes [ ^ ]



寻找红皮书,蓝皮书或橙皮书(从红色的那个)

这里还有一些关于CP的文章。奇怪的是,搜索词openGL tute通过谷歌运行带来了



似乎Nehe的网站遭遇数据库问题,但现在又回到了网上。我高度推荐他的48个系列。从头开始,逐步完成它们。 GLAUX现在已经很老了,但主要用于图像加载程序。只需要一个bmp或tga加载器,你就可以在大多数时候没有它。 NEHE openGL tutes [ ^ ]



A few tutes - http://www.codecolony.de/OpenGL/[^]
I would have recomended NEHE's openGL tutes too, though I seem unable to access them for some period now. Perhaps you could access a google-cached version of the page

Here's another page with a go-to-whoa! set of tutes. My apologies for the title of the site. OpenGl tutes[^]

Look for the Red Book, the Blue Book or the Orange Book (start with the red one)
There's also a number of articles here on CP for that. Curiously enough, the search term "openGL tute" run through google brings up

It seems that Nehe's site was suffering database problems, though is now back online. I highly reccomend his series of 48 tutes. Start at the beginning and work your way through them. GLAUX is rather old in the tooth now, though is mostly used for image-loading routines. Just get a bmp or tga loader and you'll be able to do without it for the most part. NEHE openGL tutes[^]


我参加聚会已经晚了几年,但由于这是谷歌的第一个结果,我认为最好不要麻烦解决方案。



对于一个纯红色矩形(在我的版本中,假设存在x,y,w(宽度)和h(八个)变量.YMMV ):

I'm years late to the party, but since this is the first google result, I thought it'd be best to have a no hassle solution.

For just a solid red rectangle (in my version, x, y, w(idth), and h(eight) variables are assumed to exist. YMMV):
unsigned int rgba = 0xff0000ff; // red, no alpha
glBegin(GL_QUADS);
glColor4f(((rgba>>24)&0xff)/255.0f,
          ((rgba>>16)&0xff)/255.0f, 
          ((rgba>>8)&0xff)/255.0f,
          (rgba&0xff)/255.0f);
glVertex3f(x,y,0);
glVertex3f(x+w,y,0);
glVertex3f(x+w,y+h,0);
glVertex3f(x,y+h,0);
glEnd();
glColor4f(1, 1, 1, 1);





第三行将允许任何0xRRGGBBAA格式的颜色。

最后一行纯粹存在,因此您的颜色更改不会泄漏到后续渲染中。 (它会)



The third line will allow any color in an 0xRRGGBBAA format.
The last line exists purely so your color change doesn't leak into subsequent renders. (it will)


这篇关于使用OpenGL绘制一个填充的Rectangle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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