You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.1 KiB

  1. export enum ShaderType {
  2. VERTEX,
  3. FRAGMENT,
  4. }
  5. export const createShader = (
  6. gl: WebGLRenderingContext,
  7. type: ShaderType,
  8. source: string
  9. ): WebGLShader | null => {
  10. const SHADER_TYPE_TO_GL_TYPE = {
  11. [ShaderType.VERTEX]: gl.VERTEX_SHADER,
  12. [ShaderType.FRAGMENT]: gl.FRAGMENT_SHADER,
  13. };
  14. const shader = gl.createShader(SHADER_TYPE_TO_GL_TYPE[type]);
  15. gl.shaderSource(shader, source);
  16. gl.compileShader(shader);
  17. const success = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
  18. if (!success) {
  19. console.error(gl.getShaderInfoLog(shader));
  20. gl.deleteShader(shader);
  21. return null;
  22. }
  23. return shader;
  24. };
  25. export const createProgram = (
  26. gl: WebGLRenderingContext,
  27. vertexShader: WebGLShader,
  28. fragmentShader: WebGLShader
  29. ): WebGLProgram | null => {
  30. const program = gl.createProgram();
  31. gl.attachShader(program, vertexShader);
  32. gl.attachShader(program, fragmentShader);
  33. gl.linkProgram(program);
  34. const success = gl.getProgramParameter(program, gl.LINK_STATUS);
  35. if (!success) {
  36. console.error(gl.getProgramInfoLog(program));
  37. gl.deleteProgram(program);
  38. return null;
  39. }
  40. return program;
  41. };