Changeset 1730


Ignore:
Timestamp:
Aug 13, 2012, 2:45:12 AM (11 years ago)
Author:
sam
Message:

tutorial: make the FBO example display something at last, so we can port
it to DirectX.

Location:
trunk/tutorial
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tutorial/08_fbo.cpp

    r1647 r1730  
    1515#include "core.h"
    1616#include "loldebug.h"
     17#include "lolgl.h"
    1718
    1819using namespace std;
     
    3031    FBO()
    3132    {
    32         m_vertices << vec2( 0.0,  0.8);
    33         m_vertices << vec2(-0.8, -0.8);
    34         m_vertices << vec2( 0.8, -0.8);
     33        m_vertices << vec2( 1.0,  1.0);
     34        m_vertices << vec2(-1.0, -1.0);
     35        m_vertices << vec2( 1.0, -1.0);
     36        m_vertices << vec2(-1.0, -1.0);
     37        m_vertices << vec2( 1.0,  1.0);
     38        m_vertices << vec2(-1.0,  1.0);
    3539        m_ready = false;
     40    }
     41
     42    virtual void TickGame(float seconds)
     43    {
     44        WorldEntity::TickGame(seconds);
     45
     46        m_time += seconds;
     47        m_hotspot = 0.4f * vec3(lol::cos(m_time * 2.f) + lol::cos(m_time * 3.3f),
     48                                lol::sin(m_time * 4.4f) + lol::sin(m_time * 3.2f),
     49                                lol::sin(m_time * 5.f));
     50        m_color = 0.25f * vec3(3.f + lol::sin(m_time * 4.5f + 1.f),
     51                               3.f + lol::sin(m_time * 2.8f + 1.3f),
     52                               3.f + lol::sin(m_time * 3.7f));
    3653    }
    3754
     
    4461            m_shader = Shader::Create(lolfx_08_fbo);
    4562            m_coord = m_shader->GetAttribLocation("in_Position", VertexUsage::Position, 0);
     63            m_uni_point = m_shader->GetUniformLocation("in_Point");
     64            m_uni_color = m_shader->GetUniformLocation("in_Color");
     65            m_uni_flag = m_shader->GetUniformLocation("in_Flag");
     66            m_uni_texture = m_shader->GetUniformLocation("in_Texture");
    4667
    4768            m_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position));
     
    5374
    5475            m_fbo = new FrameBuffer(Video::GetSize());
     76            m_fbo->Bind();
     77            glClearColor(0.0, 0.0, 0.0, 1.0f);
     78            glClearDepth(1.0f);
     79            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     80            m_fbo->Unbind();
    5581
    5682            m_ready = true;
     
    6086
    6187        m_fbo->Bind();
     88        /* FIXME: we should just disable depth test in the shader */
     89        glClear(GL_DEPTH_BUFFER_BIT);
    6290        m_shader->Bind();
     91        m_shader->SetUniform(m_uni_flag, 0.f);
     92        m_shader->SetUniform(m_uni_point, m_hotspot);
     93        m_shader->SetUniform(m_uni_color, m_color);
    6394        m_vdecl->SetStream(m_vbo, m_coord);
    6495        m_vdecl->Bind();
    65         m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 1);
     96        m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 2);
    6697        m_vdecl->Unbind();
    6798        m_shader->Unbind();
     
    69100
    70101        m_shader->Bind();
     102        m_shader->SetUniform(m_uni_flag, 1.f);
     103        m_shader->SetTexture(m_uni_texture, m_fbo->GetTexture(), 0);
    71104        m_vdecl->SetStream(m_vbo, m_coord);
    72105        m_vdecl->Bind();
    73         m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 1);
     106        m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 2);
    74107        m_vdecl->Unbind();
    75108        m_shader->Unbind();
     
    80113    Shader *m_shader;
    81114    ShaderAttrib m_coord;
     115    ShaderUniform m_uni_flag, m_uni_point, m_uni_color, m_uni_texture;
    82116    VertexDeclaration *m_vdecl;
    83117    VertexBuffer *m_vbo;
    84118    FrameBuffer *m_fbo;
     119    double m_time;
     120    vec3 m_hotspot, m_color;
    85121    bool m_ready;
    86122};
     
    96132#endif
    97133
    98     new DebugFps(5, 5);
    99134    new FBO();
    100135
  • trunk/tutorial/08_fbo.lolfx

    r1574 r1730  
    33#version 120
    44
     5attribute vec2 in_Position;
     6
     7varying vec2 pass_Position;
     8
    59void main()
    610{
    7     gl_Position = gl_Vertex;
    8     gl_TexCoord[0] = gl_MultiTexCoord0;
     11    pass_Position = in_Position;
     12    gl_Position = vec4(in_Position, 0.0, 1.0);
    913}
    1014
     
    1317#version 120
    1418
    15 uniform sampler2D texture;
     19uniform sampler2D in_Texture;
     20uniform float in_Flag;
     21uniform vec3 in_Point;
     22uniform vec3 in_Color;
     23
     24varying vec2 pass_Position;
    1625
    1726void main(void)
    1827{
    19     gl_FragColor = vec4(texture2D(texture, gl_TexCoord[0].xy).xyz, 1.0);
     28    if (in_Flag == 0.0)
     29    {
     30        float s = 4.0 + 4.0 * in_Point.z;
     31        vec2 p = pass_Position - in_Point.xy * 0.8;
     32        float f = clamp(1.0 - dot(s * p, s * p), 0.0, 1.0);
     33        f = sqrt(f);
     34        gl_FragColor = vec4(f * in_Color, f + 0.1);
     35    }
     36    else
     37    {
     38        vec2 texcoords = pass_Position * 0.5 + vec2(0.5, 0.5);
     39        gl_FragColor = vec4(texture2D(in_Texture, texcoords).xyz, 1.0);
     40    }
    2041}
    2142
Note: See TracChangeset for help on using the changeset viewer.