每在OpenGL中的像素绘制 [英] per pixel drawing in OpenGL

查看:642
本文介绍了每在OpenGL中的像素绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前写的小游戏'每像素的图纸, 我的意思是与一些与setPixel(X,Y,色)功能或这些

I used to write small games with 'per pixel' drawing, I mean with some SetPixel(x,y,color) function or such.

我也有兴趣在OpenGL,但不知道它了。

I am also interested in OpenGL but do not know it much.

这是个好(快)的方式来做到每个像素绘制在OpenGL?

Is it a good (fast) way to do a per pixel drawing in OpenGL ?

这将是很好的例子,使用纹理的四边形作为一个精灵, 或整个应用背景画面,与可能性 设定不同的像素具有某种我自己SetPixel套路 我会写......或任何其他方式 - 但它应该是有效的 尽可能多地罐

It would be good for example to use textured quads as a sprites, or whole application background screen, with possibility to set distinct pixel with some kind of my own SetPixel routine i would write... or any other way - but it should be efficient as much as it cans

(特别是具有即时通讯感兴趣的基本fundamenta 1.0版本OGL的)

(especialy im interested in basic fundamenta 1.0 version of OGL)

推荐答案

您可以设置投影,将地图的顶点坐标1:1像素坐标:

You can set a projection that will map vertex coordinates 1:1 to pixel coordinates:

glViewport(0, 0, window_width, window_height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, window_width, 0, window_height, -1, 1);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

从这里开始,顶点的X,Y坐标是在与在左下角的原点的像素。从理论上讲,你可以使用即时模式GL_POINT元。但它是一个更好的主意来批的事情了。相反,发送每个点的创建来逐个你想画点的数组:

From here on, vertex X,Y coordinates are in pixels with the origin in the lower left corner. In theory you could use the immediate mode with GL_POINT primitives. But it's a much better idea to batch things up. Instead of sending each point indivisually create an array of all the points you want to draw:

struct Vertex
{
    GLfloat x,y,red,green,blue;
};

std::vector<Vertex> vertices;
/* fill the vertices vector */

这可以OpenGL的点...

This you can OpenGL point to…

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);

/* Those next two calls don't copy the data, they set a pointer, so vertices must not be deallocated, as long OpenGL points to it! */
glVertexPointer(2, GL_FLOAT, sizeof(Vertex), &vertices[0].x);
glColorPointer(3, GL_FLOAT, sizeof(Vertex), &vertices[0].red);

......并把它接入和借鉴一切有一个呼叫:

…and have it access and draw it all with a single call:

glDrawArrays(GL_POINTS, 0, vertices.size();

这篇关于每在OpenGL中的像素绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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