Changeset 2898


Ignore:
Timestamp:
Sep 12, 2013, 3:15:09 PM (6 years ago)
Author:
sam
Message:

scene: break text rendering again so that 2D games work properly...

Location:
trunk/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/math/vector.cpp

    r2894 r2898  
    775775    fov_y *= (F_PI / 180.0f);
    776776
    777     float t2 = tanf(fov_y * 0.5f);
     777    float t2 = lol::tan(fov_y * 0.5f);
    778778    float t1 = t2 * width / height;
    779779
     
    787787    float new_fov_y = fov_y * (F_PI / 180.0f);
    788788
    789     float near = (screen_size * .5f) / tanf(new_fov_y * .5f);
     789    float near = (screen_size * .5f) / lol::tan(new_fov_y * .5f);
    790790    float far = near + draw_distance;
    791791
  • trunk/src/scene.cpp

    r2846 r2898  
    5757
    5858private:
    59     static int Compare(void const *p1, void const *p2)
    60     {
    61         Tile const *t1 = (Tile const *)p1;
    62         Tile const *t2 = (Tile const *)p2;
    63 
    64         if (t1->pos.z > t2->pos.z)
    65             return 1;
    66         if (t1->pos.z < t2->pos.z)
    67             return -1;
    68         return 0;
    69     }
    70 
    7159    Array<vec3, vec3, vec4> m_lines;
    7260    Shader *m_line_shader;
     
    188176}
    189177
    190 void Scene::Render() // XXX: rename to Blit()
     178void Scene::RenderPrimitives()
     179{
     180    /* TODO: this should be the main entry for rendering of all
     181     * primitives found in the scene graph. When we have one. */
     182}
     183
     184void Scene::RenderTiles() // XXX: rename to Blit()
    191185{
    192186    RenderContext rc;
    193187
    194188    /* Early test if nothing needs to be rendered */
    195     if (data->m_tiles.Count())
    196     {
    197         rc.SetDepthFunc(DepthFunc::LessOrEqual);
    198         rc.SetBlendFunc(BlendFunc::SrcAlpha, BlendFunc::OneMinusSrcAlpha);
    199         rc.SetAlphaFunc(AlphaFunc::GreaterOrEqual, 0.01f);
    200 
    201         PushCamera(data->m_default_cam);
     189    if (!data->m_tiles.Count())
     190        return;
     191
     192    rc.SetDepthFunc(DepthFunc::LessOrEqual);
     193    rc.SetBlendFunc(BlendFunc::SrcAlpha, BlendFunc::OneMinusSrcAlpha);
     194    rc.SetAlphaFunc(AlphaFunc::GreaterOrEqual, 0.01f);
    202195
    203196#if defined USE_D3D9 || defined _XBOX
    204197#elif !defined HAVE_GLES_2X
    205         glEnable(GL_TEXTURE_2D);
     198    glEnable(GL_TEXTURE_2D);
    206199#endif
    207         if (!data->m_tile_shader)
    208             data->m_tile_shader = Shader::Create(LOLFX_RESOURCE_NAME(tile));
    209 
    210 #if 0
    211         // Randomise, then sort.
    212         for (int i = 0; i < data->m_tiles.Count(); i++)
     200    if (!data->m_tile_shader)
     201        data->m_tile_shader = Shader::Create(LOLFX_RESOURCE_NAME(tile));
     202
     203    ShaderUniform uni_mat, uni_tex, uni_texsize;
     204    ShaderAttrib attr_pos, attr_tex;
     205    attr_pos = data->m_tile_shader->GetAttribLocation("in_Position", VertexUsage::Position, 0);
     206    attr_tex = data->m_tile_shader->GetAttribLocation("in_TexCoord", VertexUsage::TexCoord, 0);
     207
     208    data->m_tile_shader->Bind();
     209
     210    uni_mat = data->m_tile_shader->GetUniformLocation("proj_matrix");
     211    data->m_tile_shader->SetUniform(uni_mat, GetCamera()->GetProjection());
     212    uni_mat = data->m_tile_shader->GetUniformLocation("view_matrix");
     213    data->m_tile_shader->SetUniform(uni_mat, GetCamera()->GetView());
     214    uni_mat = data->m_tile_shader->GetUniformLocation("model_matrix");
     215    data->m_tile_shader->SetUniform(uni_mat, mat4(1.f));
     216
     217    uni_tex = data->m_tile_shader->GetUniformLocation("in_Texture");
     218    data->m_tile_shader->SetUniform(uni_tex, 0);
     219    uni_texsize = data->m_tile_shader->GetUniformLocation("in_TexSize");
     220
     221    for (int buf = 0, i = 0, n; i < data->m_tiles.Count(); i = n, buf += 2)
     222    {
     223        /* Count how many quads will be needed */
     224        for (n = i + 1; n < data->m_tiles.Count(); n++)
     225            if (data->m_tiles[i].tileset != data->m_tiles[n].tileset)
     226                break;
     227
     228        /* Create a vertex array object */
     229        VertexBuffer *vb1 = new VertexBuffer(6 * 3 * (n - i) * sizeof(float));
     230        float *vertex = (float *)vb1->Lock(0, 0);
     231        VertexBuffer *vb2 = new VertexBuffer(6 * 2 * (n - i) * sizeof(float));
     232        float *texture = (float *)vb2->Lock(0, 0);
     233
     234        data->m_tile_bufs.Push(vb1);
     235        data->m_tile_bufs.Push(vb2);
     236
     237        for (int j = i; j < n; j++)
    213238        {
    214             Tile tmp = data->m_tiles[i];
    215             int j = rand<int>() % data->m_tiles.Count();
    216             data->m_tiles[i] = data->m_tiles[j];
    217             data->m_tiles[j] = tmp;
     239            data->m_tiles[i].tileset->BlitTile(data->m_tiles[j].id,
     240                            data->m_tiles[j].pos, data->m_tiles[j].o,
     241                            data->m_tiles[j].scale,
     242                            vertex + 18 * (j - i), texture + 12 * (j - i));
    218243        }
     244
     245        vb1->Unlock();
     246        vb2->Unlock();
     247
     248        /* Bind texture */
     249        data->m_tiles[i].tileset->Bind();
     250        data->m_tile_shader->SetUniform(uni_texsize,
     251                       (vec2)data->m_tiles[i].tileset->GetTextureSize());
     252
     253        /* Bind vertex and texture coordinate buffers */
     254        data->m_tile_vdecl->Bind();
     255        data->m_tile_vdecl->SetStream(vb1, attr_pos);
     256        data->m_tile_vdecl->SetStream(vb2, attr_tex);
     257
     258        /* Draw arrays */
     259        data->m_tile_vdecl->DrawElements(MeshPrimitive::Triangles, 0, (n - i) * 6);
     260        data->m_tile_vdecl->Unbind();
     261        data->m_tiles[i].tileset->Unbind();
     262    }
     263
     264    data->m_tiles.Empty();
     265
     266    data->m_tile_shader->Unbind();
     267
     268#if defined USE_D3D9 || defined _XBOX
     269    /* TODO */
     270#elif !defined HAVE_GLES_2X
     271    glDisable(GL_TEXTURE_2D);
    219272#endif
    220         qsort(&data->m_tiles[0], data->m_tiles.Count(),
    221               sizeof(Tile), SceneData::Compare);
    222 
    223         ShaderUniform uni_mat, uni_tex, uni_texsize;
    224         ShaderAttrib attr_pos, attr_tex;
    225         attr_pos = data->m_tile_shader->GetAttribLocation("in_Position", VertexUsage::Position, 0);
    226         attr_tex = data->m_tile_shader->GetAttribLocation("in_TexCoord", VertexUsage::TexCoord, 0);
    227 
    228         data->m_tile_shader->Bind();
    229 
    230         uni_mat = data->m_tile_shader->GetUniformLocation("proj_matrix");
    231         data->m_tile_shader->SetUniform(uni_mat, GetCamera()->GetProjection());
    232         uni_mat = data->m_tile_shader->GetUniformLocation("view_matrix");
    233         data->m_tile_shader->SetUniform(uni_mat, GetCamera()->GetView());
    234         uni_mat = data->m_tile_shader->GetUniformLocation("model_matrix");
    235         data->m_tile_shader->SetUniform(uni_mat, mat4(1.f));
    236 
    237         uni_tex = data->m_tile_shader->GetUniformLocation("in_Texture");
    238         data->m_tile_shader->SetUniform(uni_tex, 0);
    239         uni_texsize = data->m_tile_shader->GetUniformLocation("in_TexSize");
    240 
    241         for (int buf = 0, i = 0, n; i < data->m_tiles.Count(); i = n, buf += 2)
    242         {
    243             /* Count how many quads will be needed */
    244             for (n = i + 1; n < data->m_tiles.Count(); n++)
    245                 if (data->m_tiles[i].tileset != data->m_tiles[n].tileset)
    246                     break;
    247 
    248             /* Create a vertex array object */
    249             VertexBuffer *vb1 = new VertexBuffer(6 * 3 * (n - i) * sizeof(float));
    250             float *vertex = (float *)vb1->Lock(0, 0);
    251             VertexBuffer *vb2 = new VertexBuffer(6 * 2 * (n - i) * sizeof(float));
    252             float *texture = (float *)vb2->Lock(0, 0);
    253 
    254             data->m_tile_bufs.Push(vb1);
    255             data->m_tile_bufs.Push(vb2);
    256 
    257             for (int j = i; j < n; j++)
    258             {
    259                 data->m_tiles[i].tileset->BlitTile(data->m_tiles[j].id,
    260                                 data->m_tiles[j].pos, data->m_tiles[j].o,
    261                                 data->m_tiles[j].scale,
    262                                 vertex + 18 * (j - i), texture + 12 * (j - i));
    263             }
    264 
    265             vb1->Unlock();
    266             vb2->Unlock();
    267 
    268             /* Bind texture */
    269             data->m_tiles[i].tileset->Bind();
    270             data->m_tile_shader->SetUniform(uni_texsize,
    271                            (vec2)data->m_tiles[i].tileset->GetTextureSize());
    272 
    273             /* Bind vertex and texture coordinate buffers */
    274             data->m_tile_vdecl->Bind();
    275             data->m_tile_vdecl->SetStream(vb1, attr_pos);
    276             data->m_tile_vdecl->SetStream(vb2, attr_tex);
    277 
    278             /* Draw arrays */
    279             data->m_tile_vdecl->DrawElements(MeshPrimitive::Triangles, 0, (n - i) * 6);
    280             data->m_tile_vdecl->Unbind();
    281             data->m_tiles[i].tileset->Unbind();
    282         }
    283 
    284         data->m_tiles.Empty();
    285 
    286         data->m_tile_shader->Unbind();
    287 
    288 #if defined USE_D3D9 || defined _XBOX
    289         /* TODO */
    290 #elif !defined HAVE_GLES_2X
    291         glDisable(GL_TEXTURE_2D);
    292 #endif
    293 
    294         PopCamera(data->m_default_cam);
     273}
     274
     275void Scene::RenderLines() // XXX: rename to Blit()
     276{
     277    RenderContext rc;
     278
     279    if (!data->m_lines.Count())
     280        return;
     281
     282    rc.SetDepthFunc(DepthFunc::LessOrEqual);
     283    rc.SetBlendFunc(BlendFunc::SrcAlpha, BlendFunc::OneMinusSrcAlpha);
     284    rc.SetAlphaFunc(AlphaFunc::GreaterOrEqual, 0.01f);
     285
     286    int linecount = data->m_lines.Count();
     287
     288    if (!data->m_line_shader)
     289        data->m_line_shader = Shader::Create(LOLFX_RESOURCE_NAME(line));
     290
     291    VertexBuffer *vb = new VertexBuffer((sizeof(vec3) + sizeof(vec4)) * 2 * linecount);
     292    float *vertex = (float *)vb->Lock(0, 0);
     293    for (int i = 0; i < linecount; i++)
     294    {
     295        memcpy(vertex, &data->m_lines[i].m1, sizeof(vec3));
     296        vertex += 3;
     297        memcpy(vertex, &data->m_lines[i].m3, sizeof(vec4));
     298        vertex += 4;
     299        memcpy(vertex, &data->m_lines[i].m2, sizeof(vec3));
     300        vertex += 3;
     301        memcpy(vertex, &data->m_lines[i].m3, sizeof(vec4));
     302        vertex += 4;
    295303    }
    296 
    297     if (data->m_lines.Count())
    298     {
    299         rc.SetDepthFunc(DepthFunc::LessOrEqual);
    300         rc.SetBlendFunc(BlendFunc::SrcAlpha, BlendFunc::OneMinusSrcAlpha);
    301         rc.SetAlphaFunc(AlphaFunc::GreaterOrEqual, 0.01f);
    302 
    303         int linecount = data->m_lines.Count();
    304 
    305         if (!data->m_line_shader)
    306             data->m_line_shader = Shader::Create(LOLFX_RESOURCE_NAME(line));
    307 
    308         VertexBuffer *vb = new VertexBuffer((sizeof(vec3) + sizeof(vec4)) * 2 * linecount);
    309         float *vertex = (float *)vb->Lock(0, 0);
    310         for (int i = 0; i < linecount; i++)
    311         {
    312             memcpy(vertex, &data->m_lines[i].m1, sizeof(vec3));
    313             vertex += 3;
    314             memcpy(vertex, &data->m_lines[i].m3, sizeof(vec4));
    315             vertex += 4;
    316             memcpy(vertex, &data->m_lines[i].m2, sizeof(vec3));
    317             vertex += 3;
    318             memcpy(vertex, &data->m_lines[i].m3, sizeof(vec4));
    319             vertex += 4;
    320         }
    321         vb->Unlock();
    322 
    323         data->m_line_shader->Bind();
    324 
    325         ShaderUniform uni_mat, uni_tex;
    326         ShaderAttrib attr_pos, attr_col;
    327         attr_pos = data->m_line_shader->GetAttribLocation("in_Position", VertexUsage::Position, 0);
    328         attr_col = data->m_line_shader->GetAttribLocation("in_Color", VertexUsage::Color, 0);
    329 
    330         data->m_line_shader->Bind();
    331 
    332         uni_mat = data->m_line_shader->GetUniformLocation("proj_matrix");
    333         data->m_line_shader->SetUniform(uni_mat, GetCamera()->GetProjection());
    334         uni_mat = data->m_line_shader->GetUniformLocation("view_matrix");
    335         data->m_line_shader->SetUniform(uni_mat, GetCamera()->GetView());
    336 
    337         data->m_line_vdecl->Bind();
    338         data->m_line_vdecl->SetStream(vb, attr_pos, attr_col);
    339         data->m_line_vdecl->DrawElements(MeshPrimitive::Lines, 0, 2 * linecount);
    340         data->m_line_vdecl->Unbind();
    341         data->m_line_shader->Unbind();
    342 
    343         data->m_lines.Empty();
    344         delete vb;
    345     }
     304    vb->Unlock();
     305
     306    data->m_line_shader->Bind();
     307
     308    ShaderUniform uni_mat, uni_tex;
     309    ShaderAttrib attr_pos, attr_col;
     310    attr_pos = data->m_line_shader->GetAttribLocation("in_Position", VertexUsage::Position, 0);
     311    attr_col = data->m_line_shader->GetAttribLocation("in_Color", VertexUsage::Color, 0);
     312
     313    data->m_line_shader->Bind();
     314
     315    uni_mat = data->m_line_shader->GetUniformLocation("proj_matrix");
     316    data->m_line_shader->SetUniform(uni_mat, GetCamera()->GetProjection());
     317    uni_mat = data->m_line_shader->GetUniformLocation("view_matrix");
     318    data->m_line_shader->SetUniform(uni_mat, GetCamera()->GetView());
     319
     320    data->m_line_vdecl->Bind();
     321    data->m_line_vdecl->SetStream(vb, attr_pos, attr_col);
     322    data->m_line_vdecl->DrawElements(MeshPrimitive::Lines, 0, 2 * linecount);
     323    data->m_line_vdecl->Unbind();
     324    data->m_line_shader->Unbind();
     325
     326    data->m_lines.Empty();
     327    delete vb;
    346328}
    347329
  • trunk/src/scene.h

    r2816 r2898  
    4242
    4343    void Reset();
    44     void Render();
     44
     45    void RenderPrimitives();
     46    void RenderTiles();
     47    void RenderLines();
    4548
    4649    /* FIXME: this should be deprecated -- it doesn't really match
  • trunk/src/ticker.cpp

    r2865 r2898  
    407407
    408408        /* Do this render step */
    409         g_scene->Render();
     409        g_scene->RenderPrimitives();
     410        g_scene->RenderTiles();
     411        g_scene->RenderLines();
    410412    }
    411413
Note: See TracChangeset for help on using the changeset viewer.