~thomas-voss/glmark2/build-for-mir

« back to all changes in this revision

Viewing changes to src/scene-build.cpp

  • Committer: Package Import Robot
  • Author(s): Alexandros Frantzis
  • Date: 2011-09-22 11:32:17 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: package-import@ubuntu.com-20110922113217-wvok1qgruexari8d
Tags: 2011.09-0ubuntu1
* New upstream release 2011.09.
* debian/control:
  - Start the description of the glmark2-data package with
    a lowercase letter.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
#include "log.h"
26
26
#include "mat.h"
27
27
#include "stack.h"
 
28
#include "shader-source.h"
28
29
#include <cmath>
29
30
 
30
31
SceneBuild::SceneBuild(Canvas &pCanvas) :
34
35
                                        "Whether to use VBOs for rendering [true,false]");
35
36
    mOptions["interleave"] = Scene::Option("interleave", "false",
36
37
                                           "Whether to interleave vertex attribute data [true,false]");
 
38
    mOptions["model"] = Scene::Option("model", "horse",
 
39
                                      "Which model to use [horse, bunny]");
37
40
}
38
41
 
39
42
SceneBuild::~SceneBuild()
42
45
 
43
46
int SceneBuild::load()
44
47
{
 
48
    mRotationSpeed = 36.0f;
 
49
 
 
50
    mRunning = false;
 
51
 
 
52
    return 1;
 
53
}
 
54
 
 
55
void SceneBuild::unload()
 
56
{
 
57
    mMesh.reset();
 
58
 
 
59
}
 
60
 
 
61
void SceneBuild::setup()
 
62
{
 
63
    using LibMatrix::vec3;
 
64
 
 
65
    Scene::setup();
 
66
 
 
67
    /* Set up shaders */
45
68
    static const std::string vtx_shader_filename(GLMARK_DATA_PATH"/shaders/light-basic.vert");
46
69
    static const std::string frg_shader_filename(GLMARK_DATA_PATH"/shaders/light-basic.frag");
 
70
    static const LibMatrix::vec4 lightPosition(20.0f, 20.0f, 10.0f, 1.0f);
 
71
    static const LibMatrix::vec4 materialDiffuse(1.0f, 1.0f, 1.0f, 1.0f);
 
72
 
 
73
    ShaderSource vtx_source(vtx_shader_filename);
 
74
    ShaderSource frg_source(frg_shader_filename);
 
75
 
 
76
    vtx_source.add_const("LightSourcePosition", lightPosition);
 
77
    vtx_source.add_const("MaterialDiffuse", materialDiffuse);
 
78
 
 
79
    if (!Scene::load_shaders_from_strings(mProgram, vtx_source.str(),
 
80
                                          frg_source.str()))
 
81
    {
 
82
        return;
 
83
    }
 
84
 
47
85
    Model model;
48
 
 
49
 
    if(!model.load_3ds(GLMARK_DATA_PATH"/models/horse.3ds"))
50
 
        return 0;
 
86
    bool modelLoaded(false);
 
87
    const std::string& whichModel(mOptions["model"].value);
 
88
 
 
89
    if (whichModel == "bunny")
 
90
    {
 
91
        // Bunny rotates around the Y axis
 
92
        modelLoaded = model.load_obj(GLMARK_DATA_PATH"/models/bunny.obj");
 
93
    }
 
94
    else
 
95
    {
 
96
        // Default is "horse", so we don't need to look further
 
97
        // Horse rotates around the Y axis
 
98
        modelLoaded = model.load_3ds(GLMARK_DATA_PATH"/models/horse.3ds");
 
99
    }
 
100
 
 
101
    if(!modelLoaded)
 
102
        return;
51
103
 
52
104
    model.calculate_normals();
53
105
 
58
110
 
59
111
    model.convert_to_mesh(mMesh, attribs);
60
112
 
61
 
    if (!Scene::load_shaders_from_files(mProgram, vtx_shader_filename,
62
 
                                        frg_shader_filename))
63
 
    {
64
 
        return 0;
65
 
    }
66
 
 
67
113
    std::vector<GLint> attrib_locations;
68
 
    attrib_locations.push_back(mProgram.getAttribIndex("position"));
69
 
    attrib_locations.push_back(mProgram.getAttribIndex("normal"));
 
114
    attrib_locations.push_back(mProgram["position"].location());
 
115
    attrib_locations.push_back(mProgram["normal"].location());
70
116
    mMesh.set_attrib_locations(attrib_locations);
71
117
 
72
 
    mRotationSpeed = 36.0f;
73
 
 
74
 
    mRunning = false;
75
 
 
76
 
    return 1;
77
 
}
78
 
 
79
 
void SceneBuild::unload()
80
 
{
81
 
    mMesh.reset();
82
 
 
83
 
    mProgram.stop();
84
 
    mProgram.release();
85
 
}
86
 
 
87
 
void SceneBuild::setup()
88
 
{
89
 
    Scene::setup();
90
 
 
91
 
    static const LibMatrix::vec4 lightAmbient(0.0f, 0.0f, 0.0f, 1.0f);
92
 
    static const LibMatrix::vec4 lightDiffuse(0.8f, 0.8f, 0.8f, 1.0f);
93
 
    static const LibMatrix::vec4 lightPosition(20.0f, 20.0f, 10.0f, 1.0f);
94
 
    static const LibMatrix::vec4 materialColor(1.0f, 1.0f, 1.0f, 1.0f);
95
 
 
96
118
    mUseVbo = (mOptions["use-vbo"].value == "true");
97
119
    bool interleave = (mOptions["interleave"].value == "true");
98
120
 
101
123
    else
102
124
        mMesh.build_array(interleave);
103
125
 
 
126
    /* Calculate a projection matrix that is a good fit for the model */
 
127
    vec3 maxVec = model.maxVec();
 
128
    vec3 minVec = model.minVec();
 
129
    vec3 diffVec = maxVec - minVec;
 
130
    mCenterVec = maxVec + minVec;
 
131
    mCenterVec /= 2.0;
 
132
    float diameter = diffVec.length();
 
133
    mRadius = diameter / 2;
 
134
    float fovy = 2.0 * atanf(mRadius / (2.0 + mRadius));
 
135
    fovy /= M_PI;
 
136
    fovy *= 180.0;
 
137
    float aspect(static_cast<float>(mCanvas.width())/static_cast<float>(mCanvas.height()));
 
138
    mPerspective.setIdentity();
 
139
    mPerspective *= LibMatrix::Mat4::perspective(fovy, aspect, 2.0, 2.0 + diameter); 
 
140
 
104
141
    mProgram.start();
105
142
 
106
 
    // Load lighting and material uniforms
107
 
    mProgram.loadUniformVector(lightAmbient, "LightSourceAmbient");
108
 
    mProgram.loadUniformVector(lightPosition, "LightSourcePosition");
109
 
    mProgram.loadUniformVector(lightDiffuse, "LightSourceDiffuse");
110
 
    mProgram.loadUniformVector(materialColor, "MaterialColor");
111
 
 
112
143
    mCurrentFrame = 0;
113
144
    mRotation = 0.0;
114
145
    mRunning = true;
120
151
SceneBuild::teardown()
121
152
{
122
153
    mProgram.stop();
 
154
    mProgram.release();
123
155
 
124
 
    if (mUseVbo)
125
 
        mMesh.delete_vbo();
126
 
    else
127
 
        mMesh.delete_array();
 
156
    mMesh.reset();
128
157
 
129
158
    Scene::teardown();
130
159
}
152
181
    LibMatrix::Stack4 model_view;
153
182
 
154
183
    // Load the ModelViewProjectionMatrix uniform in the shader
155
 
    LibMatrix::mat4 model_view_proj(mCanvas.projection());
156
 
 
157
 
    model_view.translate(0.0f, 0.0f, -2.5f);
 
184
    LibMatrix::mat4 model_view_proj(mPerspective);
 
185
    model_view.translate(-mCenterVec.x(), -mCenterVec.y(), -(mCenterVec.z() + 2.0 + mRadius));
158
186
    model_view.rotate(mRotation, 0.0f, 1.0f, 0.0f);
159
187
    model_view_proj *= model_view.getCurrent();
160
188
 
161
 
    mProgram.loadUniformMatrix(model_view_proj, "ModelViewProjectionMatrix");
 
189
    mProgram["ModelViewProjectionMatrix"] = model_view_proj;
162
190
 
163
191
    // Load the NormalMatrix uniform in the shader. The NormalMatrix is the
164
192
    // inverse transpose of the model view matrix.
165
193
    LibMatrix::mat4 normal_matrix(model_view.getCurrent());
166
194
    normal_matrix.inverse().transpose();
167
 
    mProgram.loadUniformMatrix(normal_matrix, "NormalMatrix");
 
195
    mProgram["NormalMatrix"] = normal_matrix;
168
196
 
169
197
    if (mUseVbo) {
170
198
        mMesh.render_vbo();