实例渲染OpenGL [英] Instance rendering OpenGL

查看:146
本文介绍了实例渲染OpenGL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难弄清楚为什么对 glDrawArraysInstanced 的调用会绘制正确数量的实例,但对象大小错误. 这是我的代码(省略了类声明)

I am having a bad time trying to figure out why the call to glDrawArraysInstanced is drawing the correct number of instances but with wrong sizes of the objects. Here's my code (class declaration ommited)

#include <QtOpenGL/QGLWidget>
#include <QtOpenGL/QGLBuffer>
#include <QtOpenGL/QGLShaderProgram>
#include <iostream>

const float OpenGLViewer::_quad[] = {
    -1.0f, 0.0f, 0.0f,
    -0.9f, 0.0f, 0.0f,
    -1.0f,  1.0f, 0.0f,
    -0.9f,  1.0f, 0.0f
};

const float OpenGLViewer::_offset[] = {
    0.0, 0.2, 0.7, 0.4
};

...

void OpenGLViewer::initializeGL()
{
    QGLFormat glFormat = QGLWidget::format();
    if(!glFormat.sampleBuffers())
        std::cout << "Could not enable sample buffers.";

    glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
    glViewport(0, 0, this->width(), this->height());

    if(!prepareShaderProgram(":/vertex.glsl", ":/fragment.glsl"))
        return;

    if(!_shaderProgram.bind())
    {
        std::cout << "Could not bind shader program to the context.";
        return;
    }

    glGenVertexArrays(1, &_vertexArraysObject);
    glBindVertexArray(_vertexArraysObject);         // BIND

    glGenBuffers(1, &_quadBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, _quadBuffer);     // BIND
    glBufferData(GL_ARRAY_BUFFER, sizeof(_quad), _quad, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    //glVertexAttribDivisorARB(0, 0);

    glGenBuffers(1, &_offsetBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, _offsetBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(_offset), _offset, GL_STATIC_DRAW);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, 0, 0);
    glVertexAttribDivisorARB(1, 1);

    glBindVertexArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

void OpenGLViewer::paintGL()
{
    // Clear the buffer with the current clearing color
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    // Draw stuff
    glBindVertexArray(_vertexArraysObject);

    glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, 4);
}

...

我想做的是使用顶点着色器中的offset值绘制相同的矩形,但在不同的位置.重要的是要说我正在使用 glVertexAttribDivisorARB(1,1)(请注意ARB后缀),因为在Qt + OSX Mavericks中我找不到函数 glVertexAttribDivisor .有什么想法吗?

What I'm trying to do is to draw the same rectangle but at different positions using the value of offset in the vertex shader. It is important to say that I am using glVertexAttribDivisorARB(1, 1) (note the ARB suffix) since in Qt+OSX Mavericks I cannot find the function glVertexAttribDivisor. Any thoughts?

生成的图像看起来像

编辑

忘记包含着色器

// VERTEX shader
#version 330

layout(location = 0) in vec4 vertexPosition;
layout(location = 1) in float offset;

void main()
{
    gl_Position = vec4(vertexPosition.x + offset, vertexPosition.y + offset, vertexPosition.zw);
}

//FRAGMENT shader
#version 330

layout(location = 0, index = 0) out vec4 fragmentColor;

void main(void)
{
    fragmentColor = vec4(1.0, 0.0, 0.0, 1.0);
}

推荐答案

gl_Position = vec4(vertexPosition.x + offset, vertexPosition.y + offset, vertexPosition.zw);

将偏移量添加到y使其向上移动(屏幕外)

adding the offset to y moves it upwards (off screen)

您不应将偏移量添加到y或将其减去:

you should either not add the offset to y or subtract it instead:

gl_Position = vec4(vertexPosition.x + offset, vertexPosition.y - offset, vertexPosition.zw);

这篇关于实例渲染OpenGL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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