OpenGL es 2.0绘制红色线条

时间:2016-11-18 06:34:33

标签: opengl-es-2.0

我有如下的着色器代码:

static const char s_v_shader[] =
"attribute vec4 vPosition; \n"
"attribute vec2 my_Texcoor;     \n"
"uniform   mat4 u_TransMatrix;   \n"
"varying vec2 vTexcoor;         \n"
"void main() \n"
"{ \n"
"  vTexcoor = my_Texcoor;       \n"
" gl_Position = u_TransMatrix*vPosition; \n"
"} \n";

static const char s_f_shader[] =
"precision mediump float;\n"
"uniform sampler2D my_Sampler;                    \n"
"varying vec2 vTexcoor;                           \n"

"void main() \n"
"{ \n"
" vec4 tex = texture2D(my_Sampler, vTexcoor);    \n"
"  gl_FragColor = tex;                            \n"
//" gl_FragColor = vec4 ( 1.0, 0.0, 0.0, 1.0 );\n"
"} \n";

我想在此纹理上绘制线条并将红色填充到线条上。 我能画线,但颜色总是黑色。

请有人帮我用红色或黄色或绿色为线条着色。

3 个答案:

答案 0 :(得分:0)

您无法绘制到纹理。您可以绘制到具有附加纹理的FBO(帧缓冲对象)。

要实现这一点,您只需要生成一个新的帧缓冲区GLuint bufferID; glGenFramebuffers(1, &bufferID);,然后您需要绑定缓冲区并将纹理附加到它glBindFramebuffer(GL_FRAMEBUFFER, bufferID) glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureID, 0)。现在,您可以使用命令绘制线条,通常可以绘制到此缓冲区。在这种情况下,根本不需要在着色器中使用纹理,您只需要输出颜色。但这只会绘制到纹理,如果你需要显示它,那么你将需要绑定你的主缓冲区并使用着色器和纹理,并使用你拥有的纹理绘制一个全屏矩形。

另一方面,如果您只想在其上绘制纹理和线条,那么您需要进行2次绘制调用。首先在着色器中使用纹理绘制一个矩形,在背景中绘制纹理。然后使用着色器绘制一条线,该着色器只接收颜色,没有纹理。

答案 1 :(得分:0)

With reference to above shaders, below is the code

vertShaderNum = glCreateShader(GL_VERTEX_SHADER);
pixelShaderNum = glCreateShader(GL_FRAGMENT_SHADER);
CompileShader(s_v_shader, vertShaderNum);
CompileShader(s_f_shader, pixelShaderNum);
programHandle = glCreateProgram();
glAttachShader(programHandle, vertShaderNum);
glAttachShader(programHandle, pixelShaderNum);
glBindAttribLocation ( programHandle, 0, "vPosition" );
glLinkProgram(programHandle);
glUseProgram(programHandle);

locVertices = glGetAttribLocation(programHandle, "vPosition");
locTexcoord = glGetAttribLocation(programHandle, "my_Texcoor");
locTransformMat = glGetUniformLocation(programHandle, "u_TransMatrix");
locSampler = glGetUniformLocation(programHandle, "my_Sampler");

glGenTextures(1, &gTexObj);
        glBindTexture(GL_TEXTURE_2D, gTexObj);

const GLfloat vertices[][2] = {
                        { -1.0f, -1.0f},
                        {  1.0f, -1.0f},
                        { -1.0f, 1.0f},
                        {  1.0f,  1.0f}
                };

const GLfloat texcoords[][2] = {
        { 0.0f, 1.0f},
        { 1.0f, 1.0f},
        { 0.0f, 0.0f},
        { 1.0f, 0.0f}
};
GLfloat transformMatrix[16] =
{
        1.0f, 0.0f, 0.0f, 0.0f,
        0.0f, 1.0f, 0.0f, 0.0f,
        0.0f, 0.0f, 1.0f, 0.0f,
        0.0f, 0.0f, 0.0f, 1.0f
};
glUniformMatrix4fv(locTransformMat, 1, GL_FALSE, transformMatrix);
glUniform1i(locSampler, 0);

Now in infinite loop
While(1)
{

glEnableVertexAttribArray(locVertices);
        glEnableVertexAttribArray(locTexcoord);

        // set data in the arrays.
        glVertexAttribPointer(locVertices, 2, GL_FLOAT, GL_FALSE, 0, &vertices[0][0]);
        glVertexAttribPointer(locTexcoord, 2, GL_FLOAT, GL_FALSE, 0, &texcoords[0][0]);

=========> I am rendering camera video logic......................

float vVertices[] =
        {
                -0.85f, -0.9f, -0.6f, -0.5f,
        };
        glVertexAttribPointer ( 0, 2, GL_FLOAT, GL_FALSE, 0, vVertices );
        glEnableVertexAttribArray ( 0 );
        lDrawArrays ( GL_LINES , 0, 2 );
        glLineWidth( width_test );

Till now everything is good. camera Video is rendered on display.
Line is drawn on Video texture.
Now I want to color the line............. I am stuck as there is no API call support to color the line in opengles 2.0....

答案 2 :(得分:0)

@Matic
static const char s_v_shader[] =
"attribute vec4 vPosition; \n"
"attribute vec2 my_Texcoor;     \n"
"uniform   mat4 u_TransMatrix;   \n"
"varying vec2 vTexcoor;         \n"
"void main() \n"
"{ \n"
"  vTexcoor = my_Texcoor;       \n"
" gl_Position = u_TransMatrix*vPosition; \n"
"} \n";
static const char s_f_shader[] =
"precision mediump float;\n"
"uniform lowp vec4 Texture;  \n "
"uniform sampler2D my_Sampler;                    \n"
"varying vec2 vTexcoor;                           \n"

"void main() \n"
"{ \n"
" vec4 tex = texture2D(my_Sampler, vTexcoor);    \n"
"  gl_FragColor = tex;                            \n"
"} \n";
#endif

static const char s_v_shader_new[] =
"attribute vec4 vPosition_new; \n"
"attribute vec2 my_Texcoor_new;     \n"
"uniform   mat4 u_TransMatrix_new;   \n"
"varying vec2 vTexcoor_new;         \n"
"void main() \n"
"{ \n"
"  vTexcoor_new = my_Texcoor_new;       \n"
" gl_Position = u_TransMatrix_new*vPosition_new; \n"
"} \n";


static const char s_f_shader_new[] =
"precision mediump float;\n"
//"uniform sampler2D my_Sampler_new;                    \n"
"uniform lowp vec4 uniformColor;\n"
//"uniform vec4 uniformColor;\n"
"varying vec2 vTexcoor_new;                           \n"

"void main() \n"
"{ \n"
" gl_FragColor = vec4 ( 1.0, 0.0, 0.0, 1.0 );\n"
"} \n";
#endif

const GLfloat vertices[][2] = {
    { -1.0f, -1.0f},
    {  1.0f, -1.0f},
    { -1.0f, 1.0f},
    {  1.0f,  1.0f}
};

const GLfloat texcoords[][2] = {
    { 0.0f, 1.0f},
    { 1.0f, 1.0f},
    { 0.0f, 0.0f},
    { 1.0f, 0.0f}
};

// Triangle Vertex colors.
const GLfloat color[][3] = {
    {1.0f, 0.0f, 0.0f},
    {0.0f, 1.0f, 0.0f},
    {0.0f, 0.0f, 1.0f},
    {1.0f, 1.0f, 0.0f}
};

// Start with an identity matrix.
GLfloat transformMatrix[16] =
{
    1.0f, 0.0f, 0.0f, 0.0f,
    0.0f, 1.0f, 0.0f, 0.0f,
    0.0f, 0.0f, 1.0f, 0.0f,
    0.0f, 0.0f, 0.0f, 1.0f
};

void LoadShaders_new(const char * vShader, const char * pShader)
{
        vertShaderNum_new = glCreateShader(GL_VERTEX_SHADER);
        pixelShaderNum_new = glCreateShader(GL_FRAGMENT_SHADER);
#if 1
        if (CompileShader(vShader, vertShaderNum_new) == 0)
        {
                printf("%d: PS compile failed.\n", __LINE__);
                return;
        }
#endif
        if (CompileShader(pShader, pixelShaderNum_new) == 0)
        {
                printf("%d: VS compile failed.\n", __LINE__);
                return;
        }

        programHandle_new = glCreateProgram();

        glAttachShader(programHandle_new, vertShaderNum_new);
        glAttachShader(programHandle_new, pixelShaderNum_new);

        // Bind vPosition to attribute 0
     //   glBindAttribLocation ( programHandle_new, 0, "vPosition_new" );
#if 1
    glLinkProgram(programHandle_new);
        // Check if linking succeeded.
        GLint linked = 0;
        glGetProgramiv(programHandle_new, GL_LINK_STATUS, &linked);
        if (!linked)
        {
                printf("%d: Link failed.\n", __LINE__);
                // Retrieve error buffer size.
                GLint errorBufSize, errorLength;
                glGetShaderiv(programHandle_new, GL_INFO_LOG_LENGTH, &errorBufSize);

                char * infoLog = (char*)malloc(errorBufSize * sizeof (char) + 1);
                if (infoLog)
                {
                        // Retrieve error.
                        glGetProgramInfoLog(programHandle_new, errorBufSize, &errorLength, infoLog);
                        infoLog[errorBufSize + 1] = '\0';
                        fprintf(stderr, "%s", infoLog);

                        free(infoLog);
                }

                return;
        }
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
     //   glUseProgram(programHandle_new);
#endif
}



// Wrapper to load vetex and pixel shader.
void LoadShaders(const char * vShader, const char * pShader)
{
    vertShaderNum = glCreateShader(GL_VERTEX_SHADER);
    pixelShaderNum = glCreateShader(GL_FRAGMENT_SHADER);

    if (CompileShader(vShader, vertShaderNum) == 0)
    {
        printf("%d: PS compile failed.\n", __LINE__);
        return;
    }

    if (CompileShader(pShader, pixelShaderNum) == 0)
    {
        printf("%d: VS compile failed.\n", __LINE__);
        return;
    }

    programHandle = glCreateProgram();

    glAttachShader(programHandle, vertShaderNum);
    glAttachShader(programHandle, pixelShaderNum);

    // Bind vPosition to attribute 0
    glBindAttribLocation ( programHandle, 0, "vPosition" );


    glLinkProgram(programHandle);
    // Check if linking succeeded.
    GLint linked = 0;
    glGetProgramiv(programHandle, GL_LINK_STATUS, &linked);
    if (!linked)
    {
        printf("%d: Link failed.\n", __LINE__);
        // Retrieve error buffer size.
        GLint errorBufSize, errorLength;
        glGetShaderiv(programHandle, GL_INFO_LOG_LENGTH, &errorBufSize);

        char * infoLog = (char*)malloc(errorBufSize * sizeof (char) + 1);
        if (infoLog)
        {
            // Retrieve error.
            glGetProgramInfoLog(programHandle, errorBufSize, &errorLength, infoLog);
            infoLog[errorBufSize + 1] = '\0';
            fprintf(stderr, "%s", infoLog);

            free(infoLog);
        }

        return;
    }
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glUseProgram(programHandle);
}


main()

{

LoadShaders(s_v_shader, s_f_shader);
LoadShaders_new(s_v_shader_new, s_f_shader_new);

// Grab location of shader attributes.
    locVertices = glGetAttribLocation(programHandle, "vPosition");
    //locColors   = glGetAttribLocation(programHandle, "my_Color");
    locTexcoord = glGetAttribLocation(programHandle, "my_Texcoor");
    // Transform Matrix is uniform for all vertices here.
    locTransformMat = glGetUniformLocation(programHandle, "u_TransMatrix");
    locSampler = glGetUniformLocation(programHandle, "my_Sampler");

    /* Create the texture. */
    glGenTextures(1, &gTexObj);
    glBindTexture(GL_TEXTURE_2D, gTexObj);
    if (gTexObj == 0)
    {
        printf("Could not load the texture \n");
        return -1;
    }

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);    

    glUniformMatrix4fv(locTransformMat, 1, GL_FALSE, transformMatrix);


    glUniform1i(locSampler, 0);


    glClearColor(0.0f, 0.5f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);


while(1)


{
        glEnableVertexAttribArray(locVertices);
    glEnableVertexAttribArray(locTexcoord);

    // set data in the arrays.
    glVertexAttribPointer(locVertices, 2, GL_FLOAT, GL_FALSE, 0, &vertices[0][0]);
    glVertexAttribPointer(locTexcoord, 2, GL_FLOAT, GL_FALSE, 0, &texcoords[0][0]);

>>>>>>>>>>>>>>>>>>>>>> Render Video logic on Texture <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<



float vVertices[] =
    {
        -0.85f, -0.9f, -0.6f, -0.5f,
    };


    glUseProgram(programHandle_new); >>>>>>>>>>>>>>>>>>>>>>> Video render stops display is not streaming
    glVertexAttribPointer ( 0, 2, GL_FLOAT, GL_FALSE, 0, vVertices );
        glEnableVertexAttribArray (0 );


    glDrawArrays ( GL_LINES , 0, 2 );
    glLineWidth( width_test );


}



}


}