Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: texture2D() returning black
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: texture2D() returning black



I literally _just_ figured that out a few minutes ago. Indeed, I needed a call to:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

... doh! :-)

My brain didn't register that glTexParameter() still applies in GLSL. I'm still learning which things are purely in the fixed-function realm and which applies to both fixed-function + GLSL.

Thanks for the response nonetheless,
-Patrick

P.S.

Now that that's working though, I'm getting some weird effects around the texture's border. Trying to write it up now.

On Jul 16, 2010, at 7:01 AM, Paul Sargent wrote:

> You may want to set the filtering mode of the texture. By default it's set to a Mipmap mode, and if you don't define all the levels then the colour returned is undefined.
>
> Paul
>
> On 16 Jul 2010, at 10:24, Patrick Rutkowski wrote:
>
>> Ok, so, I'm having a problem here with texture2D(), it's always returning black. Here's what my code does:
>>
>> (1) In my init_renderer() function I set tex_buff to a 10x10 pixel texture filled with pure yellow.
>> (2) I call glGenTextures() to create my tex_id.
>> (3) I call glActiveTexture(GL_TEXTURE0), which might be redundant.
>> (4) I call glBindTexture(GL_TEXTURE_2D, tex_id)
>> (5) I load tex_buff via glTexImage2D()
>> (6) I call glGetUniformLocation() to get the sampler's location from the shader program, and then I print the location to verify that it's not -1.
>> (7) I call glUseProgram(program)
>> (8) I call glUniform1i(glsl_texture, 0), which if I understand correctly tells that sampler to use GL_TEXTURE0.
>> (9) During render() I then once again call glActiveTexture() and glBindTexture(), which is probably redundant, but why not.
>> (10) I then render the square, and it comes up black.
>>
>> In the fragment shader, if I delete:
>>
>> gl_FragColor = texture2D(texture, frag_pos.xy)
>> ... and instead replace it with:
>> gl_FragColor = vec4(<foo>, <bar>, <baz>, 1.0);
>> ... then the square does indeed show color (foo, bar, baz).
>>
>> I'm just really confused here, think anyone might be able to have a look? The code is pasted below, it's a self contained file. If you build it on any UNIX system, it should run:
>>
>> ==============================================
>>
>> #include <X11/Xlib.h>
>> #include <GL/gl.h>
>> #include <GL/glext.h>
>> #include <GL/glx.h>
>> #include <stdarg.h>
>> #include <stdio.h>
>> #include <stdlib.h>
>>
>> Display* dpy;
>> Window win;
>> GLXContext ctx;
>>
>> GLuint vertex_shader;
>> GLuint fragment_shader;
>> GLuint program;
>>
>> #define TEX_WIDTH  10
>> #define TEX_HEIGHT 10
>>
>> GLint glsl_texture;
>> GLuint tex_id;
>> GLubyte tex_buff[4 * TEX_WIDTH * TEX_HEIGHT];
>>
>> const char* vertex_shader_source =
>>   "\n" "varying vec2 frag_pos;"
>>   "\n" ""
>>   "\n" "void"
>>   "\n" "main(void)"
>>   "\n" "{"
>>   "\n" "    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;"
>>   "\n" "    frag_pos = gl_Position.xy;"
>>   "\n" "}"
>>   "\n"
>> ;
>>
>> const char* fragment_shader_source =
>>   "\n" "varying vec2 frag_pos;"
>>   "\n" "uniform sampler2D texture;"
>>   "\n" ""
>>   "\n" "void"
>>   "\n" "main(void)"
>>   "\n" "{"
>>   "\n" "    gl_FragColor = texture2D(texture, frag_pos.xy);"
>>   "\n" "}"
>>   "\n"
>> ;
>>
>> void
>> die(const char* fmt, ...)
>> {
>>   va_list ap;
>>   va_start(ap, fmt);
>>   vprintf(fmt, ap);
>>   va_end(ap);
>>   printf("\n");
>>   exit(1);
>> }
>>
>> GLuint
>> load_shader(GLenum type, const char* source)
>> {
>>   GLint success;
>>   GLuint shader;
>>
>>   /* Add source */
>>   shader = glCreateShader(type);
>>   glShaderSource(shader, 1, &source, NULL);
>>
>>   /* Compile & report errors */
>>   glCompileShader(shader);
>>   glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
>>   if( ! success )
>>   {
>>       char* log;
>>       GLint log_size;
>>       glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_size);
>>       log = malloc(log_size);
>>       glGetShaderInfoLog(shader, log_size, NULL, log);
>>       die("ERROR: shader failed to compile:\n%s", log);
>>   }
>>
>>   return shader;
>> }
>>
>> GLuint
>> link_program(GLuint first, ...)
>> {
>>   GLint success;
>>   GLuint program;
>>   GLuint shader;
>>   va_list ap;
>>
>>   program = glCreateProgram();
>>
>>   /* Attach all shaders */
>>   va_start(ap, first);
>>   for(shader = first; shader != 0; shader = va_arg(ap, GLuint))
>>       glAttachShader(program, shader);
>>   va_end(ap);
>>
>>   /* Link & report errors */
>>   glLinkProgram(program);
>>   glGetProgramiv(program, GL_LINK_STATUS, &success);
>>   if( ! success )
>>   {
>>       char* log;
>>       GLint log_size;
>>       glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_size);
>>       log = malloc(log_size);
>>       glGetProgramInfoLog(program, log_size, NULL, log);
>>       die("ERROR: program failed to link:\n%s", log);
>>   }
>>
>>   /* Validate */
>>   glValidateProgram(program);
>>   glGetProgramiv(program, GL_VALIDATE_STATUS, &success);
>>   if( ! success )
>>   {
>>       char* log;
>>       GLint log_size;
>>       glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_size);
>>       log = malloc(log_size);
>>       glGetProgramInfoLog(program, log_size, NULL, log);
>>       die("ERROR: program failed to validate:\n%s", log);
>>   }
>>
>>   return program;
>> }
>>
>> void
>> init_renderer(void)
>> {
>>   size_t x, y;
>>
>>   /* Compile Shaders */
>>
>>   vertex_shader =
>>       load_shader(GL_VERTEX_SHADER, vertex_shader_source);
>>
>>   fragment_shader =
>>       load_shader(GL_FRAGMENT_SHADER, fragment_shader_source);
>>
>>   /* Link Program */
>>
>>   program = link_program(vertex_shader, fragment_shader, 0);
>>
>>   /* Generic GL Initialization */
>>
>>   glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
>>
>>   for(y = 0; y < TEX_HEIGHT; y++)
>>   {
>>       for(x = 0; x < TEX_WIDTH; x++)
>>       {
>>           GLubyte* pixel = &tex_buff[x*4 + (4 * y * TEX_WIDTH)];
>>           pixel[0] = 255;
>>           pixel[1] = 255;
>>           pixel[2] = 0;
>>           pixel[3] = 255;
>>       }
>>   }
>>
>>   glActiveTexture(GL_TEXTURE0);
>>
>>   glGenTextures(1, &tex_id);
>>   glBindTexture(GL_TEXTURE_2D, tex_id);
>>
>>   glTexImage2D
>>   (
>>       GL_TEXTURE_2D,
>>       0,
>>       GL_RGBA,
>>       TEX_WIDTH,
>>       TEX_HEIGHT,
>>       0,
>>       GL_RGBA,
>>       GL_UNSIGNED_BYTE,
>>       &tex_buff[0]
>>   );
>>
>>   glsl_texture = glGetUniformLocation(program, "texture");
>>   printf(
>>       "glGetUniformLocation(program, \"texture\"): %d\n", glsl_texture
>>   );
>>
>>   glUseProgram(program);
>>   glUniform1i(glsl_texture, 0);
>> }
>>
>> void
>> render(void)
>> {
>>   glClear(GL_COLOR_BUFFER_BIT);
>>
>>   glUseProgram(program);
>>
>>   glActiveTexture(GL_TEXTURE0);
>>   glBindTexture(GL_TEXTURE_2D, tex_id);
>>
>>   glBegin(GL_POLYGON);
>>     glVertex2f(-1.0f, -1.0f);
>>     glVertex2f(+1.0f, -1.0f);
>>     glVertex2f(+1.0f, +1.0f);
>>     glVertex2f(-1.0f, +1.0f);
>>   glEnd();
>> }
>>
>> void
>> event_loop(void)
>> {
>>   XEvent e;
>>
>>   while(1)
>>   {
>>       if( XCheckMaskEvent(dpy, ~0L, &e) )
>>       {
>>           printf("xevent!\n");
>>       }
>>
>>       render();
>>       glXSwapBuffers(dpy, win);
>>   }
>> }
>>
>> void
>> create_window(void)
>> {
>>   XSetWindowAttributes win_attrs;
>>   unsigned long attrs_mask = CWColormap | CWEventMask;
>>
>>   int attrs[] =
>>   {
>>       GLX_DOUBLEBUFFER,
>>       GLX_RGBA,
>>       GLX_RED_SIZE,   8,
>>       GLX_GREEN_SIZE, 8,
>>       GLX_BLUE_SIZE,  8,
>>       None
>>   };
>>
>>   XVisualInfo* vi = glXChooseVisual
>>   (
>>       dpy,
>>       DefaultScreen(dpy),
>>       attrs
>>   );
>>
>>   if( vi == NULL )
>>       die("glXChooseVisual() failed");
>>
>>   win_attrs.event_mask = KeyPressMask | PointerMotionMask;
>>
>>   win_attrs.colormap = XCreateColormap
>>   (
>>       dpy,
>>       RootWindow(dpy, vi->screen),
>>       vi->visual,
>>       AllocNone
>>   );
>>
>>   win = XCreateWindow
>>   (
>>       dpy,
>>       DefaultRootWindow(dpy),
>>       200,         /* x              */
>>       200,         /* y              */
>>       500,         /* width          */
>>       500,         /* height         */
>>       0,           /* border         */
>>       vi->depth,   /* depth          */
>>       InputOutput, /* class          */
>>       vi->visual,  /* visual         */
>>       attrs_mask,  /* win_attrs mask */
>>       &win_attrs   /* win_attrs      */
>>   );
>>
>>   ctx = glXCreateContext(dpy, vi, NULL, True);
>>   glXMakeCurrent(dpy, win, ctx);
>>
>>   XMapWindow(dpy, win);
>> }
>>
>> int
>> main(void)
>> {
>>   dpy = XOpenDisplay(NULL);
>>   if( dpy == NULL )
>>       die("XOpenDisplay(NULL) failed");
>>
>>   create_window();
>>   init_renderer();
>>   event_loop();
>>
>>   return 0;
>> }
>>
>> _______________________________________________
>> Do not post admin requests to the list. They will be ignored.
>> Mac-opengl mailing list      (email@hidden)
>> Help/Unsubscribe/Update your Subscription:
>>
>> This email sent to email@hidden
>

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Mac-opengl mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >texture2D() returning black (From: Patrick Rutkowski <email@hidden>)
 >Re: texture2D() returning black (From: Paul Sargent <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.