java - Half transparent screen without lights? -
i following tutorial: https://github.com/mattdesl/lwjgl-basics/wiki/shaderlesson6 want make 2d lightning whenever render black screen. code:
main class
public class main { private static final int width = 1280; private static final int height = 720; private static int shaderprogram; private static int vertexshader; private static int fragmentshader; private static terrain terrain; public static final float default_light_z = 0.075f; public static final vector4f light_color = new vector4f(1f, 0.8f, 0.6f, 1f); public static final vector3f light_pos = new vector3f(0, 0f, default_light_z); public static final vector4f ambient_color = new vector4f(0.6f, 0.6f, 1f, 0.2f); public static final vector3f falloff = new vector3f(.4f, 3f, 20f); public static void main(string[] args) { initdisplay(); initgl(); terrain = new terrain(); terrain.createterrain(); createshader(); gameloop(); removeshader(); } private static void gameloop() { while (!display.iscloserequested()) { glclear(gl_color_buffer_bit); gluseprogram(shaderprogram); float x = mouse.getx() / (float)display.getwidth(); float y = mouse.gety() / (float)display.getheight(); light_pos.x = x; light_pos.y = y; int location_lightposition = glgetuniformlocation(shaderprogram, "lightpos"); int location_resolution = glgetuniformlocation(shaderprogram, "resolution"); int location_lightcolor = glgetuniformlocation(shaderprogram, "lightcolor"); int location_ambientcolor = glgetuniformlocation(shaderprogram, "ambientcolor"); int location_falloff = glgetuniformlocation(shaderprogram, "falloff"); gluniform3f(location_lightposition, light_pos.x, light_pos.y, light_pos.z); gluniform2f(location_resolution, width, height); gluniform3f(location_lightcolor, light_color.x, light_color.y, light_color.z); gluniform4f(location_ambientcolor, ambient_color.x, ambient_color.y, ambient_color.z, ambient_color.w); gluniform3f(location_falloff, falloff.x, falloff.y, falloff.z); terrain.drawterrain(); gluseprogram(0); display.update(); display.sync(60); } } private static void initgl() { glmatrixmode(gl_projection); glloadidentity(); glortho(0, width, 0, height, -1, 1); glmatrixmode(gl_modelview); gldisable(gl_depth_test); glenable(gl_texture_2d); glclearcolor(0, 0, 0, 1); } private static void initdisplay() { try { display.setdisplaymode(new displaymode(width, height)); display.create(); } catch (lwjglexception e) { e.printstacktrace(); } } private static void createshader() { shaderprogram = glcreateprogram(); vertexshader = glcreateshader(gl_vertex_shader); fragmentshader = glcreateshader(gl_fragment_shader); stringbuilder vertexshadersource = new stringbuilder(); stringbuilder fragmentshadersource = new stringbuilder(); try { bufferedreader reader = new bufferedreader(new filereader("src/me/mateo226/shaders/vertexshader.txt")); string line; while ((line = reader.readline()) != null) { vertexshadersource.append(line).append("\n"); } reader.close(); } catch (ioexception e) { system.err.println("vertex shader wasn't loaded properly!"); display.destroy(); system.exit(1); } try { bufferedreader reader = new bufferedreader(new filereader("src/me/mateo226/shaders/fragmentshader.txt")); string line; while ((line = reader.readline()) != null) { fragmentshadersource.append(line).append("\n"); } reader.close(); } catch (ioexception e) { system.err.println("fragment shader wasn't loaded properly!"); display.destroy(); system.exit(1); } glshadersource(vertexshader, vertexshadersource); glcompileshader(vertexshader); if (glgetshader(vertexshader, gl_compile_status) == gl_false) { system.out.println(gl20.glgetshaderinfolog(vertexshader, 500)); system.err.println("vertex shader not compiled!"); } glshadersource(fragmentshader, fragmentshadersource); glcompileshader(fragmentshader); if (glgetshader(fragmentshader, gl_compile_status) == gl_false) { system.out.println(gl20.glgetshaderinfolog(fragmentshader, 500)); system.err.println("fragment shader not compiled!"); } glattachshader(shaderprogram, vertexshader); glattachshader(shaderprogram, fragmentshader); gllinkprogram(shaderprogram); glvalidateprogram(shaderprogram); } private static void removeshader() { gldeleteprogram(shaderprogram); gldeleteshader(vertexshader); gldeleteshader(fragmentshader); display.destroy(); } }
this how load textures, bind , draw them in terrain class:
private texture tex; private texture texnormal; tex = textureloader.gettexture("png", new fileinputstream(new file("res/wall.png"))); texnormal = textureloader.gettexture("png", new fileinputstream(new file("res/wallnormal.png"))); gl13.glactivetexture(gl13.gl_texture1); texnormal.bind(); gl13.glactivetexture(gl13.gl_texture0); tex.bind(); glbegin(gl_quads); { gltexcoord2f(0, 0); glvertex2f(x, y); gltexcoord2f(0, 1); glvertex2f(x, y + height); gltexcoord2f(1, 1); glvertex2f(x + width, y + height); gltexcoord2f(1, 0); glvertex2f(x + width, y); } glend();
i don't think problem in above code, in shaders:
vertex:
#version 400 core varying vec2 vtexcoord; void main() { gl_texcoord[0] = gl_multitexcoord0; vtexcoord = gl_texcoord[0].xy; gl_position = ftransform(); }
fragment
#version 400 core varying vec4 vcolor; varying vec2 vtexcoord; //our texture samplers uniform sampler2d u_texture; //diffuse map uniform sampler2d u_normals; //normal map //values used shading algorithm... uniform vec2 resolution; //resolution of screen uniform vec3 lightpos; //light position, normalized uniform vec4 lightcolor; //light rgba -- alpha intensity uniform vec4 ambientcolor; //ambient rgba -- alpha intensity uniform vec3 falloff; //attenuation coefficients void main(void) { //rgba of our diffuse color vec4 diffusecolor = texture2d(u_texture, vtexcoord); //rgb of our normal map vec3 normalmap = texture2d(u_normals, vtexcoord).rgb; //the delta position of light vec3 lightdir = vec3(lightpos.xy - (gl_fragcoord.xy / resolution.xy), lightpos.z); //correct aspect ratio lightdir.x *= resolution.x / resolution.y; //determine distance (used attenuation) before normalize our lightdir float d = length(lightdir); //normalize our vectors vec3 n = normalize(normalmap * 2.0 - 1.0); vec3 l = normalize(lightdir); //pre-multiply light color intensity //then perform "n dot l" determine our diffuse term vec3 diffuse = (lightcolor.rgb * lightcolor.a) * max(dot(n, l), 0.0); //pre-multiply ambient color intensity vec3 ambient = ambientcolor.rgb * ambientcolor.a; //calculate attenuation float attenuation = 1.0 / ( falloff.x + (falloff.y*d) + (falloff.z*d*d) ); //the calculation brings vec3 intensity = ambient + diffuse * attenuation; vec3 finalcolor = diffusecolor.rgb * intensity; gl_fragcolor = vcolor * vec4(finalcolor, diffusecolor.a); //gl_fragcolor = texture2d(u_texture, vtexcoord); }
i know there lot of code in here, , can move pastebin if want! please stuck, help! appreciated! edit: realised had varying "vcolor" in fragment shader , not vertex shader. in vertex shader added vcolor = gl_color;
have half transparent picture no lights! :) still need though!
Comments
Post a Comment