2017-09-11 9 views
0

다양한 소스에 대한 지침을 따르려고했으나 성공하지 못했습니다. 저는 WebGL과 OpenGL을 처음 접했습니다. 나는이 코드를 제공 받았고 이후로 개조했다. 누구든지 공유하고 싶은 자원이 있다면 직접 질문에 대답 할 수 있습니다.이 삼각형을 채우지 않고 테두리 만 만들려면 어떻게해야합니까?

<!DOCTYPE html> 
<html> 
<head> 
</head> 
<body> 
    <!--Include A/S WebGL support libraries--> 
    <script type="text/javascript" src="../Common/webgl-utils.js"></script> 
    <script type="text/javascript" src="../Common/initShaders.js"></script> 
    <script type="text/javascript" src="../Common/MV.js"></script> 
    <script type="text/javascript" src="../Common/webgl-debug.js"></script> 
    <script type="text/javascript" src="assignment1.js"></script> 
    <script id="vertex-shader" type="x-shader/x-vertex"> 
     // GLSL vertex shader code 
     attribute vec4 vPosition; 
     void main() 
     { 
      gl_Position = vPosition; 
     } 
    </script> 
    <script id="fragment-shader" type="x-shader/x-fragment"> 
     // GLSL fragment shader code 
     precision mediump float; 
     uniform float u_time; 
     void main() 
     { 
      gl_FragColor = vec4(abs(sin(u_time)), 1.0, 1.0, 1.0); 
     } 
    </script> 
<canvas id="gl-canvas" width="512" height=" 512">> 
Oops ... your browser doesn't support the HTML5 canvas element 
</canvas> 
</body> 
</html> 
여기

내 자바 스크립트 :

var gl; 
var points; 

window.onload = function init(){ 
    var canvas = document.getElementById("gl-canvas"); 

    // gl = WebGLUtils.setupWebGL(canvas); // More efficient 
    gl = WebGLDebugUtils.makeDebugContext(canvas.getContext("webgl")); // For debugging 
    if (!gl) { alert("WebGL isn't available"); 
       } 

    // Four 2D Vertices using Angel/Shreiner utility class vac2 
    var vertices = [   
     vec2(-0.5, -0.5), 
     vec2(-0.5, 0.5), 
     vec2(0.5, 0.5), 
     vec2(0.5, -0.5) 
    ]; 


    // Configure WebGL 

    gl.viewport(0, 0, canvas.width, canvas.height); 
    gl.clearColor(0.0, 0.0, 0.0, 0.0); 

    // Load shaders and initialize attribute buffers using A/S utility initShaders 

    var program = initShaders(gl, "vertex-shader", "fragment-shader"); 
    gl.useProgram(program); 

    // Load the data into the GPU using A/S flatten function 

    var bufferId = gl.createBuffer(); 
    gl.bindBuffer(gl.ARRAY_BUFFER, bufferId); 
    gl.bufferData(gl.ARRAY_BUFFER, flatten(vertices), gl.STATIC_DRAW); 


    // Associate our shader variables with our data buffer 

    var vPosition = gl.getAttribLocation(program, "vPosition"); 
    gl.vertexAttribPointer(
     vPosition, // Specifies the index of the generic vertex attribute to be modified. 
     2,   // Specifies the number of components per generic vertex attribute. 
        // Must be 1, 2, 3, or 4. 
     gl.FLOAT, // Specifies the data type of each component in the array. 
      // GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT, GL_FIXED, or GL_FLOAT. 
     false,  // Specifies whether fixed-point data values should be normalized (GL_TRUE) 
      // or converted directly as fixed-point values (GL_FALSE) when they are accessed. 
     0,   // Specifies the byte offset between consecutive generic vertex attributes. 
      // If stride is 0, the generic vertex attributes are understood 
      // to be tightly packed in the array. 
     0   // Specifies a pointer to the first component 
      // of the first generic vertex attribute in the array. 
         ); 
    gl.enableVertexAttribArray(vPosition);  

    render(); 
}; 

function render() { 
    gl.clear(gl.COLOR_BUFFER_BIT); 
    gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4); 
} 

답변

2

윤곽 대 단색을 할 쉽게 방법이 없습니다

여기 내 HTML입니다.

옵션은 당신이 그리려는 내용에 따라 공급 다른 정점 또는 인덱스를 변경해야합니다 gl.LINES

를 사용

  1. 그리기 라인입니다. 예를 들어 위의 그림은 TRIANGLE_STRIP이지만 그림과 같은 쿼드를 그리려면이 경우에 LINE_LOOP을 사용할 수 있습니다. 보다 복잡한 도형의 경우 다른 인덱스를 제공해야 할 가능성이 큽니다.

    gl.LINES, gl.LINE_LOOPgl.LINE_STRIP의 문제는 WebGL이 1 픽셀 너비의 선만 지원합니다.

  2. 이 윤곽을 가진 텍스처를 확인, 데이터

    이 일반적인 방법에 적용합니다. textures and texture coordinates에 대해 알아야합니다. 텍스처를 사용하여 오직 텍스처의 가장자리 근처에 특정 색을 그리하면 계산 텍스쳐 좌표 사용하는 실제 텍스처 참조 좌표 상술하지만 대신으로

  3. 이것은 동일한 절차 적 텍스처

    만들기 .

  4. 계산 프래그먼트 쉐이더

    의 윤곽 셰이더 윤곽선을 계산하는 방법이있다. 그들은 더 많은 데이터를 제공해야합니다. Here's one

  5. Compute vertices that draw outlines

    이 대부분의 3D 라이브러리 (및 2D 라이브러리) 할 것입니다