Update
parent
b8346976b6
commit
2999206eb1
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,5 @@
|
||||
varying vec3 color;
|
||||
|
||||
void main() {
|
||||
gl_FragColor = vec4(color, 1);
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
varying vec3 color;
|
||||
|
||||
void main() {
|
||||
color = gl_Color.rgb;
|
||||
gl_Position = ftransform();
|
||||
}
|
Binary file not shown.
After Width: | Height: | Size: 230 KiB |
@ -0,0 +1,97 @@
|
||||
package main;
|
||||
|
||||
import org.lwjgl.opengl.Display;
|
||||
import org.newdawn.slick.util.ResourceLoader;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
import static org.lwjgl.opengl.GL11.GL_FALSE;
|
||||
import static org.lwjgl.opengl.GL20.*;
|
||||
|
||||
/**
|
||||
* @Author: WangYuyang
|
||||
* @Date: 2021/11/18-16:57
|
||||
* @Project: Assignment3
|
||||
* @Package: main
|
||||
* @Description:
|
||||
**/
|
||||
public class ShaderLoader {
|
||||
public static HashMap<Integer, Integer> shaders = new LinkedHashMap<>();
|
||||
|
||||
public static void loadShaders() {
|
||||
loadShader(0, "shader.vs", "shader.fs");
|
||||
|
||||
|
||||
System.out.println("Shaders load ok.");
|
||||
}
|
||||
|
||||
private static void loadShader(Integer id, String filename_vs, String filename_fs) {
|
||||
int shaderProgram = glCreateProgram();
|
||||
int vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
||||
int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
StringBuilder vertexShaderSource = new StringBuilder();
|
||||
StringBuilder fragmentShaderSource = new StringBuilder();
|
||||
BufferedReader reader = null;
|
||||
try {
|
||||
|
||||
reader = new BufferedReader(new FileReader("res/shaders/" + filename_vs));
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
vertexShaderSource.append(line).append('\n');
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.err.println("Vertex shader wasn't loaded properly.");
|
||||
e.printStackTrace();
|
||||
Display.destroy();
|
||||
System.exit(1);
|
||||
} finally {
|
||||
if (reader != null) {
|
||||
try {
|
||||
reader.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
BufferedReader reader2 = null;
|
||||
try {
|
||||
reader2 = new BufferedReader(new FileReader("res/shaders/" + filename_fs));
|
||||
String line;
|
||||
while ((line = reader2.readLine()) != null) {
|
||||
fragmentShaderSource.append(line).append('\n');
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.err.println("Fragment shader wasn't loaded properly.");
|
||||
Display.destroy();
|
||||
System.exit(1);
|
||||
} finally {
|
||||
if (reader2 != null) {
|
||||
try {
|
||||
reader2.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
glShaderSource(vertexShader, vertexShaderSource);
|
||||
glCompileShader(vertexShader);
|
||||
if (glGetShaderi(vertexShader, GL_COMPILE_STATUS) == GL_FALSE) {
|
||||
System.err.println("Vertex shader wasn't able to be compiled correctly.");
|
||||
}
|
||||
glShaderSource(fragmentShader, fragmentShaderSource);
|
||||
glCompileShader(fragmentShader);
|
||||
if (glGetShaderi(fragmentShader, GL_COMPILE_STATUS) == GL_FALSE) {
|
||||
System.err.println("Fragment shader wasn't able to be compiled correctly.");
|
||||
}
|
||||
glAttachShader(shaderProgram, vertexShader);
|
||||
glAttachShader(shaderProgram, fragmentShader);
|
||||
glLinkProgram(shaderProgram);
|
||||
glValidateProgram(shaderProgram);
|
||||
System.out.println("Id: " + shaderProgram + " <> ValidateProgram finish.");
|
||||
shaders.put(id, shaderProgram);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue