简体   繁体   English

RPI2顶点着色器Texture2D上的openframeworks可能吗?

[英]openframeworks on RPI2 Vertex shader Texture2D possible?

I'm new at OpenGL and glsl and trying to use it on RPI2. 我是OpenGL和glsl的新手,正在尝试在RPI2上使用它。 I did my first practice example on OpenGL 2.1 and glsl version120. 我在OpenGL 2.1和glsl version120上做了我的第一个练习示例。 Now I'm working on OpenGLES 2.0. 现在,我正在开发OpenGLES 2.0。

What I want to do is that store float values to a texture and send it to vertex shader to make lines. 我想做的是将浮点值存储到纹理中,然后将其发送到顶点着色器以制作线条。

So, I made one array for float values to store, and passing these values to one FBO. 因此,我制作了一个数组来存储浮点值,并将这些值传递给一个FBO。 And I send this FBO to UniformTexture. 然后我将此FBO发送到UniformTexture。 But It seems vertex shader doesn't get any values. 但是似乎顶点着色器没有任何值。

OpenGL ES 2.0 document states that "Texture lookup functions are available to both vertex and fragment shaders." OpenGL ES 2.0文档指出“纹理查找功能可用于顶点和片段着色器。” Also I have checked GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS in RPI2 and it returns 8. Unfortunately, I cannot fetch any values form texture that I send to vertex shader. 另外,我已经在RPI2中检查了GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS,它返回8。不幸的是,我无法从发送到顶点着色器的纹理中获取任何值。

What am I missing here? 我在这里想念什么? I posted this question at openframeworks forum as well. 我也在openframeworks论坛上发布了这个问题。 Any help would be appreciated. 任何帮助,将不胜感激。 I really hope. 我真的希望。 Thanks ! 谢谢 !

vertex shader code 顶点着色器代码

precision highp float;

uniform mat4 modelViewProjectionMatrix;
uniform sampler2D valTex;
uniform vec2 spaceSize;

attribute vec4 position;
attribute vec2 texcoord;

void main() {

    vec4 pixPos = position;

    vec2 valAmt = texture2D(valTex, texcoord).xy;
    valAmt.x *= spaceSize.x;
    valAmt.y *= spaceSize.y;

    vec2 nPos;
    nPos.x =  (pixPos.x + valAmt.x);
    nPos.y =  (pixPos.y + valAmt.y);

    if ((v_pos.x <= 1.0)) {
        gl_Position = modelViewProjectionMatrix * vec4(pixPos.x, pixPos.y, 0, 1);
    }else{
        gl_Position = modelViewProjectionMatrix * vec4(nPos.x, nPos.y, 0, 1);
    }
}

Fragment shader 片段着色器

void main() {

    gl_FragColor = vec4(1.0, 1.0, 0.5, 1.0);
}

OFX CODE OFX代码

#include "ofApp.h"

void ofApp::setup(){

    string ShaderFolder;
    ShaderFolder = "shader_gles";
    renderShader.load(ShaderFolder + "/fluidRender.vert", ShaderFolder + "/fluidRender.frag");

    renderFbo.allocate(spaceSize, spaceSize, GL_RGB); //Render to Screen 
    renderFbo.begin();
    ofClear(0);
    renderFbo.end();

    valTex.allocate(gridSize, gridSize, GL_RGB); //Store values from array
    valTex.begin();
    ofClear(0);
    valTex.end();

    int vertexNum = gridSize * gridSize;

    vector<float> val(vertexNum * 3); //Store values

    for (int x = 0; x < gridSize; x++) {
        for (int y = 0; y < gridSize; y++) {
            val[((x * 3 * gridSize) + y * 3) + 0] = 200.0; //x pos value
            val[((x * 3 * gridSize) + y * 3) + 1] = 200.0; //y pos value
            val[((x * 3 * gridSize) + y * 3) + 2] = 0.0;
        }
    }
    valTex.getTexture().loadData(val.data(), gridSize, gridSize, GL_RGB);

    mesh.setMode(OF_PRIMITIVE_LINES);

    for (int x = 0; x < gridSize; x++) {
        for (int y = 0; y < gridSize; y++) {

            mesh.addVertex(ofVec3f(x * cellSize, y * cellSize)); //center vertex
            mesh.addTexCoord(ofVec2f(1.1, 1.1));

            mesh.addVertex(ofVec3f(x * cellSize, y * cellSize)); //val vertex
            mesh.addTexCoord(ofVec2f(x / gridSize - 1, y / gridSize - 1)); //normalize texcoord

            mesh.addIndex((x * 2) * gridSize + (y * 2) + 0);
            mesh.addIndex((x * 2) * gridSize + (y * 2) + 1);
        }
    }
    glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &vertex_texture_units);
}

void ofApp::update(){
    vertexRender();
}

void ofApp::draw(){
    ofBackground(0);
    renderFbo.draw(0, 0);
    ofDrawBitmapString("v_tex_unit : " + ofToString(vertex_texture_units), 15, 15);
}

void ofApp::vertexRender(){
    renderFbo.begin();

    ofClear(0);
    renderShader.begin();
    renderShader.setUniformTexture("velTex", valTex.getTexture(), 0);
    renderShader.setUniform2f("spaceSize", spaceSize, spaceSize);
    mesh.draw();
    renderShader.end();

    renderFbo.end();
}

NOTE Not GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS in description. 注意:说明中不是GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS。 It is GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS in the code. 代码中为GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS。

EDIT 编辑

This screen shot below is what I want to draw using vertex shader. 下面的屏幕截图是我要使用顶点着色器绘制的。 Currently in this case, I send mouse x and y position float value (0 to screen size which is 960.0 in this case) to uniform to display value vertex. 目前,在这种情况下,我将鼠标的x和y位置浮动值(在这种情况下,将0传递给屏幕尺寸为960.0)以统一显示值顶点。 Each line has two vertex. 每条线都有两个顶点。 One has fixed position which is from 'addVertex(x,y)' function. 一个具有固定位置,该位置来自“ addVertex(x,y)”函数。

mesh.addVertex(ofVec3f(x * cellSize, y * cellSize));

Another one has 'fixed position value + mouse position value'. 另一个具有“固定位置值+鼠标位置值”。 So If mouse moves, It makes a line connecting fixed position vertex and 'fixed position + mouse position' vertex. 因此,如果鼠标移动,它将形成一条连接固定位置顶点和“固定位置+鼠标位置”顶点的线。

renderShader.setUniform2f("mousePos", mouseX, mouseY);

in GLSL 在GLSL中

vec2 pixPos = position;
vec2 nPos;
nPos.x =  (pixPos.x + mousePos.x);
nPos.y =  (pixPos.y + mousePos.y);

vertex_mouse_position //<-sccreen shot (I can't embed pictures directly yet. sorry.) vertex_mouse_position // <-screwen镜头(我还不能直接嵌入图片。对不起。)

Another picture is when I send texture(FBO) to vertex shader. 另一张图片是当我将Texture(FBO)发送到顶点着色器时。 I made an array and set 200.0 float value for each x, y position and load this array to FBO. 我制作了一个数组,并为每个x,y位置设置了200.0浮点值,并将此数组加载到FBO。

vector<float> val(vertexNum * 3); //Store values

    for (int x = 0; x < gridSize; x++) {
        for (int y = 0; y < gridSize; y++) {
            val[((x * 3 * gridSize) + y * 3) + 0] = 200.0; //x pos value
            val[((x * 3 * gridSize) + y * 3) + 1] = 200.0; //y pos value
            val[((x * 3 * gridSize) + y * 3) + 2] = 0.0;
        }
    }
    valTex.getTexture().loadData(val.data(), gridSize, gridSize, GL_RGB);

then send this FBO to vertex shader. 然后将此FBO发送到顶点着色器。

renderShader.setUniformTexture("velTex", valTex.getTexture(), 0);

But, I just get black screen displaying noting. 但是,我只是得到黑屏显示的提示。

vertex_texture vertex_texture

I hope this help you to more understanding for my issue. 希望这有助于您对我的问题有更多的了解。 Thank for reply. 感谢您的答复。 I really appreciate. 我真的很感激。

You are checking the wrong query - check GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS . 您正在检查错误的查询-检查GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS

On most GLES 2.0 implementations this will return zero; 在大多数GLES 2.0实现中,它将返回零。 ie vertex texturing not supported. 即不支持顶点纹理。 It's mandatory in GLES 3.x. 它在GLES 3.x中是必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM