Fragment shader does not show any colour when compiled with vs2013 - opengl

When compiled with vs2010, the fragment shader works, but when I compiled and run in vs 2013, it's grey.
My fragment shader converts the yuv texture into rgb
Below is my fragment code
const char *FProgram =
"uniform sampler2D Ytex;\n"
"uniform sampler2D Utex;\n"
"uniform sampler2D Vtex;\n"
"void main(void) {\n"
" vec4 c = vec4((texture2D(Ytex, gl_TexCoord[0]).r - 16./255.) * 1.164);\n"
" vec4 U = vec4(texture2D(Utex, gl_TexCoord[0]).r - 128./255.);\n"
" vec4 V = vec4(texture2D(Vtex, gl_TexCoord[0]).r - 128./255.);\n"
" c += V * vec4(1.596, -0.813, 0, 0);\n"
" c += U * vec4(0, -0.392, 2.017, 0);\n"
" c.a = 1.0;\n"
" gl_FragColor = c;\n"
"}\n";
glClearColor(0, 0, 0, 0);
PHandle = glCreateProgram();
FSHandle = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(FSHandle, 1, &FProgram, NULL);
glCompileShader(FSHandle);
glAttachShader(PHandle, FSHandle);
glLinkProgram(PHandle);
glUseProgram(PHandle);
glDeleteProgram(PHandle);
glDeleteProgram(FSHandle);
This is my texture code, I receive linesize and yuv frame data from ffmpeg and make into texture. Everything works fine in VS 2010 computer, but when compiled and run in vs2013 computer, it is grey (black n white), no colour
/* Select texture unit 1 as the active unit and bind the U texture. */
glPixelStorei(GL_UNPACK_ROW_LENGTH, linesize1);
glActiveTexture(GL_TEXTURE1);
i = glGetUniformLocation(PHandle, "Utex");
glUniform1i(i, 1); /* Bind Utex to texture unit 1 */
glBindTexture(GL_TEXTURE_2D, 1);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width / 2, height / 2, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, frame1);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
/* Select texture unit 2 as the active unit and bind the V texture. */
glPixelStorei(GL_UNPACK_ROW_LENGTH, linesize2);
glActiveTexture(GL_TEXTURE2);
i = glGetUniformLocation(PHandle, "Vtex");
glUniform1i(i, 2); /* Bind Vtext to texture unit 2 */
glBindTexture(GL_TEXTURE_2D, 2);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width / 2, height / 2, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, frame2);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
/* Select texture unit 0 as the active unit and bind the Y texture. */
glPixelStorei(GL_UNPACK_ROW_LENGTH, linesize0);
glActiveTexture(GL_TEXTURE0);
i = glGetUniformLocation(PHandle, "Ytex");
glUniform1i(i, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width, height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, frame0);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glClear(GL_COLOR_BUFFER_BIT);
/* Draw image (again and again). */
glBegin(GL_QUADS);
glTexCoord2i(0, 0);
glVertex2i(-w / 2, h / 2);
glTexCoord2i(1, 0);
glVertex2i(w / 2, h / 2);
glTexCoord2i(1, 1);
glVertex2i(w / 2, -h / 2);
glTexCoord2i(0, 1);
glVertex2i(-w / 2, -h / 2);
glEnd();

Related

opengl won't overlap more than 1 texture

I'm trying to create an opengl program that creates a 2d square, and applies 2 textures on it.
I followed this tutorial: https://learnopengl.com/Getting-started/Textures
This is my fragment shader:
#version 330 core
//in vec3 Color;
in vec2 TexCoord;
out vec4 FragColor;
uniform sampler2D Texture1;
uniform sampler2D Texture2;
void main()
{
FragColor = mix(texture(Texture1, TexCoord), texture(Texture2, TexCoord), 0.5);
}
This is the code that sends the textures and the uniforms:
GLuint Tex1, Tex2;
int TexWidth, TexHeight, TexNrChannels;
unsigned char* TexData = stbi_load("container.jpg", &TexWidth, &TexHeight, &TexNrChannels, 0);
glGenTextures(1, &Tex1);
glBindTexture(GL_TEXTURE_2D, Tex1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TexWidth, TexHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, TexData);
glGenerateMipmap(GL_TEXTURE_2D);
glUniform1i(glGetUniformLocation(Program, "Texture1"), 0);
stbi_image_free(TexData);
TexData = stbi_load("awesomeface.png", &TexWidth, &TexHeight, &TexNrChannels, 0);
glGenTextures(1, &Tex2);
glBindTexture(GL_TEXTURE_2D, Tex2);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TexWidth, TexHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, TexData);
glGenerateMipmap(GL_TEXTURE_2D);
glUniform1i(glGetUniformLocation(Program, "Texture2"), 1);
stbi_image_free(TexData);
And this is the render loop:
while (!glfwWindowShouldClose(window))
{
processInput(window);
// GL render here
glClearColor(0.05f, 0.0f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, Tex1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, Tex2);
glUseProgram(Program);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(window);
glfwPollEvents();
}
When I run it, only the first texture shows up on the square, and the a argument (last argument) of the mix function in the shader won't make a difference for any value.
I tried activating (glActiveTexture + GlBindTexture) the second texture first in the render loop, and it caused the second texture to be shown exclusively.
How can I make the textures mix together like in the tutorial?
If this approach is wrong, I would like to learn about another way to accomplish the same result.
glUniform1i set a value in the default uniform block of the currently installed program. You have to install the program with glUseProgram, before you can set the value of a uniform variable:
GLint t1_loc = glGetUniformLocation(Program, "Texture1");
GLint t2_loc = glGetUniformLocation(Program, "Texture2");
glUseProgram(Program);
glUniform1i(t1_loc, 0);
glUniform1i(t2_loc, 1);
Alternatively you can use glProgramUniform1i:
glProgramUniform1i(Program, t1_loc, 0);
glProgramUniform1i(Program, t2_loc, 1);

Changing internal format causes texture to have incorrect parameters

I set up framebuffer texture on which to draw a scene on as follows:
glGenFramebuffers(1, &renderFBO);
glGenTextures(1, &renderTexture);
glBindTexture(GL_TEXTURE_2D, renderTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, W_WIDTH, W_HEIGHT, 0, GL_RGBA, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
int width, height;
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);
cout << "width: " << width << " height: " << height << endl;
glBindFramebuffer(GL_FRAMEBUFFER, renderFBO);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, renderTexture, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
By validating width and height, they do indeed have the correct dimensions, 1024, 768.
Now I am trying to set up a second texture, to be used as a counter, meaning I am trying to count the occurences of every color value, by incrementing the appropriate texel in the texture. Since I am trying to count the colors, I use a 2D texture of size 256 x 3. In each cell I want a single unsigned integer, showing the number of times a specific color value has appeared up until that point. For instance, if the color (255,5,3) was encountered, I would to increment the numbers inside tex[255][0], tex[5][0], tex[3][0]
num_bins = 256;
GLuint* hist_data = new GLuint[num_bins * color_components]();
glGenTextures(1, &tex_output);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex_output);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R32UI, 256, 3, 0, GL_RED, GL_UNSIGNED_INT, hist_data);
glBindImageTexture(0, tex_output, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R32UI);
int width, height;
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);
cout << "width: " << width << " height: " << height << endl;
I used the sized and base internal format correspondence shown in the opengl specification. However when this code executes the output values are width=0, height=0. The input data doesn't seem to be the issue, because I tried providing NULL as the last argument and it still didn't work. What am I missing here?
EDIT:
Incrementation of the values in the texture is supposed to happen inside the following compute shader:
#version 450
layout(local_size_x = 1, local_size_y = 1) in;
layout(rgba32f, binding = 0) uniform image2D img_input;
layout(r32ui, binding = 1) uniform uimage2D img_output;
void main() {
// grabbing pixel value from input image
vec4 pixel_color = imageLoad(img_input, ivec2(gl_GlobalInvocationID.xy));
vec3 rgb = round(pixel_color.rgb * 255);
ivec2 r = ivec2(rgb.r, 0);
ivec2 g = ivec2(rgb.g, 1);
ivec2 b = ivec2(rgb.b, 2);
uint inc = 1;
imageAtomicAdd(img_output, r, inc);
imageAtomicAdd(img_output, g, inc);
imageAtomicAdd(img_output, b, inc);
}
You get a GL_INVALID_OPERATION error, because the texture format argument doesn't correspond with the internal format of the texture. For an unsigned integral format you have to use GL_RED_INTEGER instead of GL_RED (see glTexImage2D):
glTexImage2D(GL_TEXTURE_2D, 0, GL_R32UI, 256, 3, 0, GL_RED, GL_UNSIGNED_INT, hist_data);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R32UI, 256, 3, 0, GL_RED_INTEGER, GL_UNSIGNED_INT, hist_data);
I recommend using Debug Output, it saves a lot of time and lets you find such errors quickly.

Color error with OpenGL Texture Formats

I'm trying to load an image using FreeImage and then generate an OpenGL texture. It loads the image and generates a texture, but there's an issue with the colors.
Here's the original image:
and here's the result:
Texture loading code:
void TextureManager::LoadTexture(std::string id, std::string filePath){
Texture tex;
tex.TextureId = 0;
FIBITMAP* image = FreeImage_Load(FreeImage_GetFileType(filePath.c_str(), 0), filePath.c_str());
if (FreeImage_GetBPP(image) != 32) {
image = FreeImage_ConvertTo32Bits(image);
}
FreeImage_FlipVertical(image);
tex.Width = FreeImage_GetWidth(image);
tex.Height = FreeImage_GetHeight(image);
glGenTextures(1, &tex.TextureId);
glBindTexture(GL_TEXTURE_2D, tex.TextureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,tex.Width, tex.Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (void*)FreeImage_GetBits(image));
AddTextureToMap(id, tex);
FreeImage_Unload(image);
}
Here's the code that's drawing it:
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glBindTexture(GL_TEXTURE_2D, TextureManager.TextureMap[textureSelector].TextureId);
glBegin(GL_TRIANGLES);
glTexCoord2d(0, 0);
glVertex3f(-0.75, 0.75, 0);//top left
glTexCoord2d(1, 0);
glVertex3f(0.75, 0.75, 0);//top right
glTexCoord2d(0, 1);
glVertex3f(-0.75, -0.75, 0);//bottom left
glTexCoord2d(1, 0);
glVertex3f(0.75, 0.75, 0);//top right
glTexCoord2d(1, 1);
glVertex3f(0.75, -0.75, 0);//bottom right
glTexCoord2d(0, 1);
glVertex3f(-0.75, -0.75, 0);//bottom left
glEnd();
glFinish();
I'm pretty sure it's an issue with the internal format in glTexImage2D, but I'm not sure which format I'd use if that's the case. My question is: Is it the format, if so, what should I use; or is there another issue that's causing this?
Try to use this instead
The difference is because many image formats use reverse byte order, and you should use GL_BGR_EXT or GL_BGRA_EXT image formats instead GL_RGB or GL_RGBA
void TextureManager::LoadTexture(std::string id, std::string filePath){
Texture tex;
tex.TextureId = 0;
FIBITMAP* image = FreeImage_Load(FreeImage_GetFileType(filePath.c_str(), 0), filePath.c_str());
if (FreeImage_GetBPP(image) != 32) {
image = FreeImage_ConvertTo32Bits(image);
}
FreeImage_FlipVertical(image);
tex.Width = FreeImage_GetWidth(image);
tex.Height = FreeImage_GetHeight(image);
glGenTextures(1, &tex.TextureId);
glBindTexture(GL_TEXTURE_2D, tex.TextureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,tex.Width, tex.Height, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, (void*)FreeImage_GetBits(image));
AddTextureToMap(id, tex);
FreeImage_Unload(image);
}
If the RGBA order is not correct, you can change the color in fragment shader like
#version 130
uniform sampler2D tex;
in vec2 texCoordsVarying;
out vec4 color;
void main() {
vec4 c2 = texture(tex, texCoordsVarying);
color = vec4(c2.g, c2.b, c2.a, c2.r);
}

OpenGL: Can not write to framebuffer

I've been struggling with this for a while, without any positive result. I've also implemented framebuffers with multiple render targets before, without any issues, but right now it wont work for some reason.
Here's the code for initialization:
// LOAD TEXTURE 1
glGenTextures(1, &m_Texture1);
glBindTexture(GL_TEXTURE_2D, m_Texture1);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, size.x, size.y, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
// LOAD TEXTURE 2
glGenTextures(1, &m_Texture2);
glBindTexture(GL_TEXTURE_2D, m_Texture2);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, size.x, size.y, 0, GL_RGB, GL_FLOAT, NULL);
// LOAD TEXTURE 3
glGenTextures(1, &m_Texture3);
glBindTexture(GL_TEXTURE_2D, m_Texture3);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F_ARB, size.x, size.y, 0, GL_RGB, GL_FLOAT, NULL);
// Load FBO
glGenFramebuffers(1, &m_fbo);
glBindFramebuffer(GL_FRAMEBUFFER_EXT, m_fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, m_Texture1, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_TEXTURE_2D, m_Texture2, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_TEXTURE_2D, m_Texture3, 0);
glBindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
And for drawing:
glBindFramebuffer(GL_FRAMEBUFFER_EXT, m_fbo);
/// Draw the buffers, 3 color textures
GLenum buffers[] = { GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT };
glDrawBuffers(3, buffers);
/// Clear all the buffers
GLfloat color1[] = { 1.0f, 1.0, 0.0, 1.0 };
GLfloat color2[] = { 0.0f, 1.0, 0.0, 1.0 };
GLfloat color3[] = { 0.0f, 0.0, 1.0, 1.0 };
glClearBufferfv(GL_COLOR, 0, color1);
glClearBufferfv(GL_COLOR, 1, color2);
glClearBufferfv(GL_COLOR, 2, color3);
glBindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
I'm using gDEBugger for debugging buffers etc, and the 3 textures should be yellow, green and blue if i'm not right? But they are black for some reason...

Using FBO to render YUV data

I was able to render YUV image on the screen using shaders.
To improve performance I want to use FBO. But I am not able to do it.
My initialization code is as below
void opengl_init(int w, int h) {
glGenFramebuffers(1, &framebuffer);
checkGlError("glGenFramebuffers");
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
checkGlError("glBindFramebuffer");
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &gYTexture);
glBindTexture(GL_TEXTURE_2D, gYTexture);
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);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, FRAME_WIDTH, FRAME_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glBindTexture(GL_TEXTURE_2D, 0);
glGenRenderbuffers(1, &colorbuffer);
checkGlError("glGenRenderbuffers");
glBindRenderbuffer(GL_RENDERBUFFER, colorbuffer);
checkGlError("glBindRenderbuffer");
glRenderbufferStorage(GL_RENDERBUFFER,
GL_RGB565, FRAME_WIDTH, FRAME_HEIGHT);
checkGlError("glRenderbufferStorage");
glFramebufferRenderbuffer(GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0,
GL_RENDERBUFFER, depthbuffer);
checkGlError("glFramebufferRenderbuffer");
glFramebufferTexture2D(GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
gYTexture, 0);
checkGlError("glFramebufferTexture2D");
int status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
printf("Framebuffer is not complete: *status* = %d", status);
if (status != GL_FRAMEBUFFER_COMPLETE) {
printf("Framebuffer is not complete: *status* = %d", status);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
checkGlError("glBindFramebuffer");
glGenBuffers(1, ioBuf);
checkGlError("glGenBuffers");
glActiveTexture(GL_TEXTURE1);
glGenTextures(1, &gUTexture);
glBindTexture(GL_TEXTURE_2D, gUTexture);
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);
glActiveTexture(GL_TEXTURE2);
glGenTextures(1, &gVTexture);
glBindTexture(GL_TEXTURE_2D, gVTexture);
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);
glViewport(0, 0, w, h);
checkGlError("glViewport");
}
I render my frame using the below code
void opengl_renderframe(void *yuvbuf,int framewidth, int frameheight) {
static void *ubuf, *vbuf;
ubuf = (char *)yuvbuf + (framewidth * frameheight);
vbuf = (char *)ubuf + ((framewidth * frameheight) / 4);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
checkGlError("glBindFramebuffer");
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
checkGlError("glClearColor");
glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
checkGlError("glClear");
glUseProgram(gProgram);
checkGlError("glUseProgram");
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, gYTexture);
glBindBuffer(GL_ARRAY_BUFFER, ioBuf);
checkGlError("glBindBuffer");
glBufferData(GL_ARRAY_BUFFER, (FRAME_WIDTH * FRAME_HEIGHT) , yuvbuf, GL_STATIC_DRAW);
checkGlError("glBufferData");
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, FRAME_WIDTH, FRAME_HEIGHT, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, NULL);
checkGlError("glTexImage2D");
glBindBuffer(GL_ARRAY_BUFFER, 0);
checkGlError("glBindBuffer");
glUniform1i(Ysamp, 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, gUTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, framewidth/2, frameheight/2, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, ubuf );
glUniform1i(Usamp, 1);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, gVTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, framewidth/2, frameheight/2, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, vbuf );
glUniform1i(Vsamp, 2);
glVertexAttribPointer(gvPositionHandle, 2, GL_FLOAT, GL_FALSE, 0, gTriangleVertices);
checkGlError("glVertexAttribPointer");
glEnableVertexAttribArray(gvPositionHandle);
checkGlError("glEnableVertexAttribArray");
glVertexAttribPointer(gaTextureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0, gTriangleVertices2);
checkGlError("glVertexAttribPointer");
glEnableVertexAttribArray(gaTextureCoordHandle);
checkGlError("glEnableVertexAttribArray");
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
checkGlError("glDrawArrays");
glBindFramebuffer(GL_FRAMEBUFFER, 0);
checkGlError("glBindFramebuffer 0");
}
my fragment shader to render only Y data is as below
static const char gGrayScaleFragmentShader[] =
"precision mediump float;\n"
"varying vec2 vTextureCoord;\n"
"uniform sampler2D sTexture;\n"
"void main() {\n"
" gl_FragColor = texture2D(sTexture, vTextureCoord);\n"
"}\n";