我正在关注本教程: https://github.com/mattdesl/lwjgl-basics/wiki/ShaderLesson6 我想制作2d闪电,但每当我渲染它时,我都会看到黑屏。 这是我的代码:
主要类
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();
}
}
这是我的 我如何在terrain类中加载纹理,绑定和绘制它们:
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();
我不认为问题出在上面的代码中,但是在着色器中:
顶点:
#version 400 core
varying vec2 vTexCoord;
void main() {
gl_TexCoord[0] = gl_MultiTexCoord0;
vTexCoord = gl_TexCoord[0].xy;
gl_Position = ftransform();
}
片段
#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 for shading algorithm...
uniform vec2 Resolution; //resolution of screen
uniform vec3 LightPos; //light position, normalized
uniform vec4 LightColor; //light RGBA -- alpha is intensity
uniform vec4 AmbientColor; //ambient RGBA -- alpha is 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 for aspect ratio
LightDir.x *= Resolution.x / Resolution.y;
//Determine distance (used for attenuation) BEFORE we 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 with intensity
//Then perform "N dot L" to determine our diffuse term
vec3 Diffuse = (LightColor.rgb * LightColor.a) * max(dot(N, L), 0.0);
//pre-multiply ambient color with intensity
vec3 Ambient = AmbientColor.rgb * AmbientColor.a;
//calculate attenuation
float Attenuation = 1.0 / ( Falloff.x + (Falloff.y*D) + (Falloff.z*D*D) );
//the calculation which brings it all together
vec3 Intensity = Ambient + Diffuse * Attenuation;
vec3 FinalColor = DiffuseColor.rgb * Intensity;
gl_FragColor = vColor * vec4(FinalColor, DiffuseColor.a);
//gl_FragColor = texture2D(u_texture, vTexCoord);
}
我知道这里有很多代码,如果你愿意,我可以把它移到pastebin!
请我卡住,帮帮忙吧!
任何帮助表示赞赏!
编辑:我刚刚意识到我已经变化了" vColor"在我的片段着色器中,而不是我的顶点着色器。在我的顶点着色器中,我添加了vColor = gl_Color;
现在我有一张半透明的图片,没有灯光! :)虽然仍然需要帮助!