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

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

问题描述

我曾经用每像素"绘图来编写小游戏,我的意思是使用一些 SetPixel(x,y,color) 函数之类的.

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

(特别是我对 OGL 的基本基础 1.0 版本感兴趣)

(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天全站免登陆