source: trunk/src/gradient.cpp @ 2183

Last change on this file since 2183 was 2183, checked in by sam, 7 years ago

build: fix the WTFPL site URL in all code comments.

  • Property svn:keywords set to Id
File size: 3.4 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://www.wtfpl.net/ for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include "core.h"
16#include "lolgl.h"
17
18using namespace std;
19
20extern char const *lolfx_gradient;
21
22namespace lol
23{
24
25/*
26 * Gradient implementation class
27 */
28
29class GradientData
30{
31    friend class Gradient;
32
33private:
34    Shader *shader;
35    VertexDeclaration *m_vdecl;
36    VertexBuffer *m_vbo, *m_cbo;
37};
38
39/*
40 * Public Gradient class
41 */
42
43Gradient::Gradient(vec3 aa, vec3 bb)
44  : data(new GradientData())
45{
46    /* FIXME: this should not be hardcoded */
47    m_position = aa;
48    m_bbox[0] = aa;
49    m_bbox[1] = bb;
50
51    data->shader = NULL;
52}
53
54void Gradient::TickGame(float seconds)
55{
56    Entity::TickGame(seconds);
57}
58
59void Gradient::TickDraw(float seconds)
60{
61    Entity::TickDraw(seconds);
62
63    float const vertex[] = { m_bbox[0].x, m_bbox[0].y, 0.0f,
64                             m_bbox[1].x, m_bbox[0].y, 0.0f,
65                             m_bbox[0].x, m_bbox[1].y, 0.0f,
66                             m_bbox[1].x, m_bbox[1].y, 0.0f,
67                             m_bbox[0].x, m_bbox[1].y, 0.0f,
68                             m_bbox[1].x, m_bbox[0].y, 0.0f, };
69
70    float const color[] = { 0.73f, 0.85f, 0.85f, 1.0f,
71                            0.73f, 0.85f, 0.85f, 1.0f,
72                            0.0f, 0.0f, 1.0f, 1.0f,
73                            0.0f, 0.0f, 1.0f, 1.0f,
74                            0.0f, 0.0f, 1.0f, 1.0f,
75                            0.73f, 0.85f, 0.85f, 1.0f, };
76
77    if (!data->shader)
78    {
79        data->shader = Shader::Create(lolfx_gradient);
80
81        data->m_vbo = new VertexBuffer(sizeof(vertex));
82        data->m_cbo = new VertexBuffer(sizeof(color));
83
84        data->m_vdecl = new VertexDeclaration(VertexStream<vec3>(VertexUsage::Position),
85                                              VertexStream<vec4>(VertexUsage::Color));
86    }
87
88    mat4 model_matrix = mat4(1.0f);
89
90    ShaderUniform uni_mat;
91    ShaderAttrib attr_pos, attr_col;
92    attr_pos = data->shader->GetAttribLocation("in_Vertex", VertexUsage::Position, 0);
93    attr_col = data->shader->GetAttribLocation("in_Color", VertexUsage::Color, 0);
94
95    data->shader->Bind();
96
97    uni_mat = data->shader->GetUniformLocation("proj_matrix");
98    data->shader->SetUniform(uni_mat, Scene::GetDefault()->GetProjMatrix());
99    uni_mat = data->shader->GetUniformLocation("view_matrix");
100    data->shader->SetUniform(uni_mat, Scene::GetDefault()->GetViewMatrix());
101    uni_mat = data->shader->GetUniformLocation("model_matrix");
102    data->shader->SetUniform(uni_mat, model_matrix);
103
104    data->shader->Bind();
105    data->m_vdecl->Bind();
106
107    void *tmp = data->m_vbo->Lock(0, 0);
108    memcpy(tmp, vertex, sizeof(vertex));
109    data->m_vbo->Unlock();
110
111    tmp = data->m_cbo->Lock(0, 0);
112    memcpy(tmp, color, sizeof(color));
113    data->m_cbo->Unlock();
114
115    /* Bind vertex and color buffers */
116    data->m_vdecl->SetStream(data->m_vbo, attr_pos);
117    data->m_vdecl->SetStream(data->m_cbo, attr_col);
118
119    /* Draw arrays */
120    data->m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
121}
122
123Gradient::~Gradient()
124{
125    /* FIXME: destroy shader */
126    delete data;
127}
128
129} /* namespace lol */
Note: See TracBrowser for help on using the repository browser.