2014-07-08 36 views
0

我正在使用OpenGL製作類似於CAD/CAM軟件的東西。
起初我只是使用glBegin和glEnd,它工作正常,但當有很多頂點時它會變慢,所以我做了搜索,發現有一些叫做vertexbuffer的東西。
所以我做了一個簡單的程序來測試它,它是寫在C#和sharpgl爲什麼在使用vertexbuffer時會出現漸變色線?

//create a new buffer 
gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, vertexId); 
gl.BufferData(OpenGL.GL_ARRAY_BUFFER, vertexSize + colorSize, new IntPtr(0),OpenGL.GL_STREAM_DRAW); 
gl.VertexPointer(2, OpenGL.GL_FLOAT, 0, new IntPtr(0)); 
gl.ColorPointer(3, OpenGL.GL_UNSIGNED_BYTE, 0, new IntPtr(vertexSize)); 
//update vertex buffer 
public int UpdateVertexAt(float[] segments,int offset) 
    { 
     int len = segments.Length * sizeof(float); 
     unsafe 
     { 
      fixed(float * p = segments) 
      { 
       IntPtr ptr = new IntPtr(p); 
       gl.BufferSubData(OpenGL.GL_ARRAY_BUFFER, offset, len, ptr); 
      } 
     } 
     c += segments.Length/2; 
     return len; 
    } 
//update color 
public int UpdateColorAt(byte[] rgb, int offset) 
    { 
     // gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, colorId); 
     unsafe 
     { 
      fixed (byte* p = rgb) 
      { 
       IntPtr ptr = new IntPtr(p); 
       gl.BufferSubData(OpenGL.GL_ARRAY_BUFFER, offset, rgb.Length, ptr); 
      } 
     } 
     return rgb.Length; 
    } 
//draw scene 
    public void Flush() 
    { 
     gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY); 
     gl.EnableClientState(OpenGL.GL_COLOR_ARRAY); 
     gl.DrawArrays(OpenGL.GL_LINES, 0, 2 * c); 
     gl.DisableClientState(OpenGL.GL_COLOR_ARRAY); 
     gl.DisableClientState(OpenGL.GL_VERTEX_ARRAY); 
    } 

問題我開始畫線,它看起來werid而不是在正確的地方,當我畫的第一行它dosent節目,但是當我畫sconed線和apperes像

enter image description here

我檢查使用gl.GetBufferSubData頂點和顏色,每一件事情看起來正確的,但在使用時 gl.ColorPointer(3,OpenGL.GL_UNSIGNED_BYTE ,0,新的IntPtr(0)),該行s的拉到位,每一件事情是蠻好的,除了顏色,我認爲這是因爲它的讀取頂點顏色

enter image description here

所以我在做什麼錯在這裏?

回答

0

我發現了這個問題,我使用了1種顏色作爲開始和結束點,那是什麼導致了漸變色

相關問題