opengl - Changing color using a shader -
i writing deferred shader , 1 of first steps, familiar glsl , using shaders , framebuffer trying change color of mesh through shader. have linked 1 of buffers calling gldrawbuffers array holds attachement , binding texture framebuffer:
glreadbuffer(gl_none); glint color_loc = glgetfragdatalocation(pass_prog,"out_color"); glenum draws [1]; draws[color_loc] = gl_color_attachment0; gldrawbuffers(1, draws); glbindtexture(gl_texture_2d, diffusetexture); glframebuffertexture(gl_framebuffer, draws[color_loc], diffusetexture, 0);
i have out_color variable in fragment shader (otherwise wouldn't compile), can't manage change color of mesh setting through variable inside shader. has idea why , explain me?
thanks
edit: shaders: vertex shader
#version 330 uniform mat4x4 u_model; uniform mat4x4 u_view; uniform mat4x4 u_persp; uniform mat4x4 u_invtrans; in vec3 position; in vec3 normal; out vec3 fs_normal; out vec4 fs_position; void main(void) { fs_normal = (u_invtrans*vec4(normal,0.0f)).xyz; vec4 world = u_model * vec4(position, 1.0); vec4 camera = u_view * world; fs_position = camera; gl_position = u_persp * camera; }
fragment shader
#version 330 uniform float u_far; in vec3 fs_normal; in vec4 fs_position; out vec4 out_normal; out vec4 out_position; out vec4 out_color; void main(void) { out_normal = vec4(normalize(fs_normal),0.0f); out_position = vec4(fs_position.xyz,1.0f); //tuck position 0 1 range out_color = vec4(1.0f, 0.0f, 0.0f, 1.0f);//first 3 diffuse, last specular }
and not doing deferred shader in order learn glsl. learning glsl in order make deferred shader. =)
more source code setting textures:
glactivetexture(gl_texture0); glgentextures(1, &depthtexture); glgentextures(1, &normaltexture); glgentextures(1, &positiontexture); glgentextures(1, &diffusetexture); //depth texture glbindtexture(gl_texture_2d, depthtexture); gltexparameteri(gl_texture_2d, gl_texture_min_filter, gl_nearest); gltexparameteri(gl_texture_2d, gl_texture_mag_filter, gl_nearest); gltexparameterf(gl_texture_2d, gl_texture_wrap_s, gl_clamp); gltexparameterf(gl_texture_2d, gl_texture_wrap_t, gl_clamp); gltexparameteri(gl_texture_2d, gl_depth_texture_mode, gl_intensity); glteximage2d( gl_texture_2d, 0, gl_depth_component, w, h, 0, gl_depth_component, gl_float, 0); //normal texture glbindtexture(gl_texture_2d, normaltexture); gltexparameteri(gl_texture_2d, gl_texture_min_filter, gl_linear); gltexparameteri(gl_texture_2d, gl_texture_mag_filter, gl_linear); gltexparameterf(gl_texture_2d, gl_texture_wrap_s, gl_clamp); gltexparameterf(gl_texture_2d, gl_texture_wrap_t, gl_clamp); glteximage2d( gl_texture_2d, 0, gl_rgb32f , w, h, 0, gl_rgba, gl_float,0); //position texture glbindtexture(gl_texture_2d, positiontexture); gltexparameteri(gl_texture_2d, gl_texture_min_filter, gl_linear); gltexparameteri(gl_texture_2d, gl_texture_mag_filter, gl_linear); gltexparameterf(gl_texture_2d, gl_texture_wrap_s, gl_clamp); gltexparameterf(gl_texture_2d, gl_texture_wrap_t, gl_clamp); glteximage2d( gl_texture_2d, 0, gl_rgb32f , w, h, 0, gl_rgba, gl_float,0); //diffuse texture glbindtexture(gl_texture_2d, diffusetexture); gltexparameteri(gl_texture_2d, gl_texture_min_filter, gl_linear); gltexparameteri(gl_texture_2d, gl_texture_mag_filter, gl_linear); gltexparameterf(gl_texture_2d, gl_texture_wrap_s, gl_clamp); gltexparameterf(gl_texture_2d, gl_texture_wrap_t, gl_clamp); glteximage2d( gl_texture_2d, 0, gl_rgb32f , w, h, 0, gl_rgba, gl_float,0); //create framebuffer object glgenframebuffers(1, &fbo); glbindframebuffer(gl_framebuffer, fbo); //instruct opengl won't bind color texture binded fbo glreadbuffer(gl_none); glint normal_loc = glgetfragdatalocation(pass_prog,"out_normal"); glint position_loc = glgetfragdatalocation(pass_prog,"out_position"); glint color_loc = glgetfragdatalocation(pass_prog,"out_color"); glenum draws [3]; draws[normal_loc] = gl_color_attachment0; draws[position_loc] = gl_color_attachment1; draws[color_loc] = gl_color_attachment2; gldrawbuffers(3, draws); glbindtexture(gl_texture_2d, depthtexture); glframebuffertexture(gl_framebuffer, gl_depth_attachment, depthtexture, 0); glbindtexture(gl_texture_2d, normaltexture); glframebuffertexture(gl_framebuffer, draws[normal_loc], normaltexture, 0); glbindtexture(gl_texture_2d, positiontexture); glframebuffertexture(gl_framebuffer, draws[position_loc], positiontexture, 0); glbindtexture(gl_texture_2d, diffusetexture); glframebuffertexture(gl_framebuffer, draws[color_loc], diffusetexture, 0); check fbo status fbostatus = glcheckframebufferstatus(gl_framebuffer); if(fbostatus != gl_framebuffer_complete) { printf("gl_framebuffer_complete failed, cannot use fbo\n"); checkframebufferstatus(fbostatus); } switch window-system-provided framebuffer glclear(gl_depth_buffer_bit|gl_depth_buffer_bit); glbindframebuffer(gl_framebuffer, 0); glbindtexture(gl_texture_2d, 0);
edit: solved it. thanks.
you should this. assume out_color declared out vec4 out_color:
compile shaders , attach them program. glbindfragdatalocation(program, 0, "out_color"); gllinkprogram(program); gluseprogram(program); glenum drawbuffers[1] = { gl_color_attachment0 } gldrawbuffers(1, drawbuffers); setup fbo.
the should work, if doesn't, post fragment shader , more source code. way, should creating deferred renderer learn glsl not idea. there easier ways learn glsl.
Comments
Post a Comment