为了测试我想到的游戏艺术风格,我想以像素艺术形式渲染3D世界.例如,拍摄这样的场景(但是使用某种颜色/样式进行渲染,以便在像素化后看起来很好):
让它看起来像这样:
通过使用不同的造型3D源的方式,我认为像素化输出看起来不错.当然要获得这种效果,只需将图像缩小到~80p,然后使用最近邻重新采样将其放大到1080p.但是直接渲染到80p画布并且只是进行升级更有效.
这通常不是人们如何使用着色器来调整最近邻格式的位图大小,但是它的性能优于我发现实时进行这种转换的任何其他方式.
我的位图缓冲区存储在row major中,因为r1, g1, b1, a1, r2, g2, b2, a2...
我正在使用gpu.js,它实际上将这个JS函数转换为着色器.我的目标是获取一个位图并以最大邻域缩放返回一个位图,因此每个像素变为2x2平方或3x3,依此类推.假设inputBuffer
是由该setOutput
方法确定的输出大小的缩放分数.
var pixelateMatrix = gpu.createKernel(function(inputBuffer, width, height, scale) {
var y = Math.floor((this.thread.x / (width[0] * 4)) / scale[0]);
var x = Math.floor((this.thread.x % (width[0] * 4)) / scale[0]);
var remainder = this.thread.x % 4;
return inputBuffer[(x * y) + remainder];
}).setOutput([width * height * 4]);
Run Code Online (Sandbox Code Playgroud)
请记住,它正在遍历全尺寸输出的新缓冲区,因此我必须找到
sourceBuffer
基于当前索引 的较小坐标outputBuffer
(由lib公开的索引this.thread.x
).
这不是使最近邻居高档,而是制作一个漂亮的小彩虹(上面是小正常渲染,下面是着色器的结果,右边你可以看到一些调试记录,包含有关输入和输出缓冲区的统计数据) ): …
我正在使用下面的算法来生成四边形,然后渲染这些四边形来制作这样的轮廓
在图像上看到的问题是,当线条总是应该具有相同的宽度时,线条太薄.我的算法找到4
第一个2
顶点,然后下一个顶点的顶点是前一个顶点2
.这会创建连接线,但似乎并不总是有效.我怎么能解决这个问题?
这是我的算法:
void OGLENGINEFUNCTIONS::GenerateLinePoly(const std::vector<std::vector<GLdouble>> &input,
std::vector<GLfloat> &output, int width)
{
output.clear();
if(input.size() < 2)
{
return;
}
int temp;
float dirlen;
float perplen;
POINTFLOAT start;
POINTFLOAT end;
POINTFLOAT dir;
POINTFLOAT ndir;
POINTFLOAT perp;
POINTFLOAT nperp;
POINTFLOAT perpoffset;
POINTFLOAT diroffset;
POINTFLOAT p0, p1, p2, p3;
for(unsigned int i = 0; i < input.size() - 1; ++i)
{
start.x = static_cast<float>(input[i][0]);
start.y = static_cast<float>(input[i][1]);
end.x = static_cast<float>(input[i + 1][0]);
end.y = static_cast<float>(input[i + …
Run Code Online (Sandbox Code Playgroud) 尽管 OpenGL 的功能很简单,但仍然让我感到困惑,但我开始学习它是如何工作的。
我正在寻找一个离屏渲染的最小示例来帮助我入门。
我的应用程序将获取一堆三角形以及有关如何相对于相机定位它们的信息,并将渲染结果保存到图像文件中。目前没有照明、材质或后期处理。
我观看了有关创建离屏上下文、创建 FBO、渲染到纹理等的教程。我不介意使用 QT,因为它方便地提供 OpenGL 工具、窗口和 QImage。据我了解,为了能够对渲染图像进行图像处理,您需要将渲染目标设置为纹理,然后使用着色器,最后将纹理读取到数组中。
试图把事情放在一起从来没有让我有一个好的起点。我要么陷入设置依赖项的困境,要么黑屏,要么盯着那些除了我需要的东西之外做了太多事情的项目。
更新1:可以正常工作了。
#include <QtGui/QGuiApplication>
#include <QtGui/QSurfaceFormat>
#include <QtGui/QOffscreenSurface>
#include <QtGui/QOpenGLFunctions_4_3_Core>
#include <QtGui/QOpenGLFramebufferObject>
#include <QtGui/QOpenGLShaderProgram>
#include <QDebug>
#include <QImage>
#include <QOpenGLBuffer>
int main(int argc, char* argv[])
{
QGuiApplication a(argc, argv);
QSurfaceFormat surfaceFormat;
surfaceFormat.setMajorVersion(4);
surfaceFormat.setMinorVersion(3);
QOpenGLContext openGLContext;
openGLContext.setFormat(surfaceFormat);
openGLContext.create();
if(!openGLContext.isValid()) return -1;
QOffscreenSurface surface;
surface.setFormat(surfaceFormat);
surface.create();
if(!surface.isValid()) return -2;
openGLContext.makeCurrent(&surface);
QOpenGLFunctions_4_3_Core f;
if(!f.initializeOpenGLFunctions()) return -3;
qDebug() << QString::fromLatin1((const char*)f.glGetString(GL_VERSION));
QSize vpSize = QSize(100, 200);
qDebug("Hi");
QOpenGLFramebufferObjectFormat fboFormat;
fboFormat.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
QOpenGLFramebufferObject fbo(vpSize, fboFormat); …
Run Code Online (Sandbox Code Playgroud) 我正在尝试实现GLSL着色器,该着色器将突出显示渲染的3D网格的外部边缘。问题是我无权访问OpenGL客户端代码,因此只能在GLSL着色器中完成。
我的第一个尝试是从Unity使用/采用此着色器,并在OpenGL GLSL中进行操作。这里看起来应该是这样:
这是我得到的:
我不确定我是否可以正确计算内容,但是如您所见,输出与我的期望值相差甚远。
这是食人魔的材料
material Chassis
{
technique
{
pass standard
{
cull_software back
scene_blend zero one
}
pass psssm
{
cull_software front
scene_blend src_alpha one_minus_src_alpha
vertex_program_ref reflection_cube_specularmap_normalmap_vs100
{
param_named_auto modelViewProjectionMatrix worldviewproj_matrix
param_named_auto normalMatrix inverse_transpose_world_matrix
param_named_auto modelView worldview_matrix
param_named_auto camera_world_position camera_position
param_named_auto inverse_projection_matrix inverse_projection_matrix
param_named_auto projection_matrix projection_matrix
param_named_auto p_InverseModelView inverse_worldview_matrix
}
fragment_program_ref reflection_cube_specularmap_normalmap_fs100
{
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是顶点着色器
material Chassis
{
technique
{ …
Run Code Online (Sandbox Code Playgroud)