2011-03-24 2 views
1

나는이 내가 LINE_STRIP 사용하는 경우 내가 enter image description here일련의 수평선을 그리는 방법은 무엇입니까? 내가 운동을 배우는는 OpenGL에 새로운 오전

을 가지고 무엇을 정점 위치

를 포함 MXN 매트릭스의 그리드에서 수평 라인의 집합을 그릴하기로 결정 enter image description here

코드 조각 큰 것 정점 배열과 인덱스를 사용하여, 나는 많은 감사합니다 내가보고 코드 예제 어떤 도움을 플레이 할 필요가 교과서에서 불과 개념을 얻을 수있을 것 캔트 !


@Thomas 는

points.beginUpdateVertices(); 
for (int n = 0; n < totalPoints; n++) { 
    points.updateVertex(n, pointsPos[indices[n]].x, pointsPos[indices[n]].y, pointsPos[indices[n]].z); 
} 
points.endUpdateVertices(); 

이 작업을 수행하여 다음과 같은 코드를 다음

totalPoints = GRID_ROWS * 2 * (GRID_COLUMNS - 1); 
indices = new int[totalPoints]; 

points = new GLModel(this, totalPoints, LINES, GLModel.DYNAMIC); 
int n = 0; 
points.beginUpdateVertices(); 
for (int row = 0; row < GRID_ROWS; row++) { 
    for (int col = 0; col < GRID_COLUMNS - 1; col++) { 
    int rowoffset = row * GRID_COLUMNS; 
    int n0 = rowoffset + col; 
    int n1 = rowoffset + col + 1; 

    points.updateVertex(n, pointsPos[n0].x, pointsPos[n0].y, pointsPos[n0].z); 
    indices[n] = n0; 
    n++; 

    points.updateVertex(n, pointsPos[n1].x, pointsPos[n1].y, pointsPos[n1].z); 
    indices[n] = n1; 
    n++; 
    } 
} 
points.endUpdateVertices(); 

I 업데이트하고 그릴과 결과

enter image description here

입니다 작업있어 루프

for (int col = 0; col < GRID_COLUMNS; col++) { 
for (int row = 0; row < GRID_ROWS - 1; row++) { 
    int offset = col * GRID_ROWS; 
    int n0 = offset + row; 
    int n1 = offset + row + 1; 
    indices[n++] = n0; 
    indices[n++] = n1; 
    } 
} 

의 중첩을 변경하여


수정이 지금은

감사 agin 행과 열의 수에는 제한이 없습니다!

답변

4

각 세그먼트에 대해 선을 그려 색인을 다시 사용해야합니다. 즉 첫 번째 부분에 대해 (0,1), (1,2), (2,3) 등의 선을 그립니다. .

편집 :

당신이 4 × 5 배열 (4 선, 라인 당 5 정점)가 가정하자. 그런 다음이 (의사 코드) 등의 지표를 계산할 수 :

Vertex[] v = new Vertex[20]; // 20 vertices in the grid 
for(int row = 0; row < numrows; row++) // numrows = 4 
{ 
    int rowoffset = row * numcols ; //0, 4, 8, 12 
    for(int col = 0; col < (numcols - 1); col++) //numcols = 5 
    { 
    addLineIndices(rowoffset + col, rowoffset + col +1); //adds (0,1), (1,2), (2,3) and (3, 4) for the first row 
    } 
} 

그런 다음 예에서 numrows * (numcols - 1) linesegments (GL_LINES), 즉 16 그리기 호출을 실행합니다. addLineIndices은 하나의 선 세그먼트에 대한 색인 쌍을 색인 배열에 추가 한 다음 그리기 호출에 제공하는 함수입니다.

+0

고마워요 매력처럼 작동하지만 nxn 격자에서 작동합니다. mxn과 작동하지 않습니다. 행 수가 열의 수와 다른 경우 도면이 지저분 해집니다. –

+0

이것은 n x m에서도 작동합니다. 예를 들어, numcols가 5 일 경우 rowoffset은 0, 5, 10, 15가되어야합니다. 계산 또는 정점 배열에 문제가 있다고 의심됩니다. 질문에 코드를 추가 할 수 있습니까? – Thomas

관련 문제