Results 1 to 10 of 179

Thread: nxPascal

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #16
    Been coding few days on renderer class using shaders. The rendering queue should in theory already work for 2D, but it's not showing anything yet Shader source compiles, all uniforms and attribs are verified to be correctly initialized, i have checked the math on modelview matrices from multiple examples. Either way, once this works, it will have some cool new 2D capabilities. All 2D drawing will have ambient, diffuse and customizable per vertex colors. May have to stop for today, but i'll show some code snippets in faint hope that anyone finds a mistake:
    2D Vertex shader:
    Code:
    #version 110
    
    attribute vec2 in_position;
    attribute vec2 in_texCoord;
    attribute vec4 in_color;
    varying vec2 texCoord;
    varying vec4 color;
    uniform mat4 pmv;
    
    void main() {
      texCoord = in_texCoord;
      color = in_color;
      gl_Position = pmv * vec4(in_position, 0.0, 0.0);
    }
    2D fragment shader:
    Code:
    #version 110
    
    varying vec2 texCoord;
    varying vec4 color;
    uniform sampler2D texture;
    uniform vec3 ambient;
    uniform vec3 diffuse;
    
    void main() {
      gl_FragColor = texture2D(texture, texCoord) *
        (color * vec4(diffuse, 1.0)) + vec4(ambient, 0.0);
    }
    Code:
      T2DVertex = packed record // size 8 float
        v: TVector2f;
        uv: TVector2f;
        color: TfRGBA;
      end;
    
      va2D: array of T2DVertex;
    
    Last in constructor:
      glBindBuffer(GL_ARRAY_BUFFER, buf2D);
      //glBufferData(GL_ARRAY_BUFFER, sizeof(T2DVertex)*length2D, @va2D[0], GL_STREAM_DRAW);
      glBufferData(GL_ARRAY_BUFFER, sizeof(T2DVertex)*length2D, @va2D[0], GL_DYNAMIC_DRAW); // temporary to see if helps
      glVertexAttribPointer(att_2Dpos, 2, GL_FLOAT, false, sizeof(T2DVertex), nil);
      glEnableVertexAttribArray(att_2Dpos);
      glVertexAttribPointer(att_2Dtex, 2, GL_FLOAT, false, sizeof(T2DVertex), pointer(8));
      glEnableVertexAttribArray(att_2Dtex);
      glVertexAttribPointer(att_2Dcol, 4, GL_FLOAT, false, sizeof(T2DVertex), pointer(16));
      glEnableVertexAttribArray(att_2Dcol);
      glBindBuffer(GL_ARRAY_BUFFER, 0);
    ...
    
    procedure TGLRenderer.SetUniforms;
    var pmv, modelM, projM: TMatrix; normal: TMatrix3f;
    begin
      glGetFloatv(GL_PROJECTION_MATRIX, @projM);
      glGetFloatv(GL_MODELVIEW_MATRIX, @modelM);
      pmv:=multiply(projM, modelM);
      glUniformMatrix4fv(uniPmv2D, 1, bytebool(GL_FALSE), @pmv);
      glUniform1i(uniTex2D, 0);
      with ambient do glUniform3f(uniAmbient2D, r, g, b);
      with diffuse do glUniform3f(uniDiffuse2D, r, g, b);
    end;
    
    procedure TGLRenderer.Render;
    begin
      shader.SelectProgram(0);
      glBindBuffer(GL_ARRAY_BUFFER, buf2D);
      glDrawArrays(polyMode, 0, vCount());
      glBindBuffer(GL_ARRAY_BUFFER, 0);
    end;
    edit: I tested by drawing with glBegin/glEnd style in Render function (without shader) and it shows. So at least it's not fault in buffer data.

    edit2: Fixed Attribs, ambient, diffuse, matrix calculation is all working. Can soon start finishing 3D shader aswell.
    Last edited by User137; 03-04-2013 at 02:42 PM. Reason: small fixes..

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •