2 * Copyright 2008, Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Alexandre Deckner <alex@zappotek.com>
9 #include "RenderView.h"
11 #include "BitmapTexture.h"
13 #include "MeshInstance.h"
14 #include "StaticMesh.h"
15 #include "VideoFileTexture.h"
20 #include <TranslationKit.h>
21 #include <TranslationUtils.h>
25 RenderView::RenderView(BRect frame
)
27 BGLView(frame
, "renderView", B_FOLLOW_ALL
, B_WILL_DRAW
,
28 BGL_RGB
| BGL_DOUBLE
| BGL_DEPTH
),
31 fStopRendering(false),
39 RenderView::~RenderView()
47 RenderView::AttachedToWindow()
49 BGLView::AttachedToWindow();
53 if (_CreateRenderThread() != B_OK
)
54 printf("Error trying to start the render thread!\n");
59 RenderView::_CreateRenderThread()
61 fRenderThread
= spawn_thread(RenderView::_RenderThreadEntry
, "renderThread",
62 B_NORMAL_PRIORITY
, this);
64 if (fRenderThread
< 0)
67 return resume_thread(fRenderThread
);
72 RenderView::_StopRenderThread()
75 fStopRendering
= true;
78 if (fRenderThread
>= 0)
79 wait_for_thread(fRenderThread
, NULL
);
84 RenderView::_RenderThreadEntry(void* pointer
)
86 return reinterpret_cast<RenderView
*>(pointer
)->_RenderLoop();
91 RenderView::_RenderLoop()
93 fLastFrameTime
= system_time();
102 void RenderView::_InitGL(void)
106 float position
[] = {0.0, 3.0, 6.0, 0.0};
107 float local_view
[] = {0.0, 0.0};
109 glLightfv(GL_LIGHT0
, GL_POSITION
, position
);
110 glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER
, local_view
);
112 float white
[3] = {1.0, 1.0, 1.0};
115 glLightfv(GL_LIGHT0
, GL_SPECULAR
, white
);
116 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, white
);
117 glLightfv(GL_LIGHT0
, GL_AMBIENT
, white
);
119 glMaterialf(GL_FRONT
, GL_SHININESS
, 0.6 * 128.0);
121 glClearColor(0.0f
, 0.0f
, 0.0f
, 0.0f
);
122 glEnable(GL_CULL_FACE
);
123 glEnable(GL_DEPTH_TEST
);
124 glEnable(GL_LIGHTING
);
125 glEnable(GL_TEXTURE_2D
);
127 fNextRes
.Set(Bounds().Width(), Bounds().Height());
135 RenderView::_CreateScene()
137 Texture
* texture
= new BitmapTexture(
138 BTranslationUtils::GetBitmap(B_PNG_FORMAT
, "texture"));
140 float spacing
= 1.6f
;
141 float timeSpacing
= 5.0f
;
142 float yOffset
= -1.0f
;
145 Mesh
* mesh
= new StaticMesh("LetterH");
146 MeshInstance
* instance
= new MeshInstance(mesh
, texture
,
147 Vector3(-3.6 * spacing
, yOffset
, zOffset
),
148 Quaternion(0, 0, 0, 1), 0.0f
);
149 fMeshInstances
.push_back(instance
);
150 mesh
->ReleaseReference();
152 mesh
= new StaticMesh("LetterA");
153 instance
= new MeshInstance(mesh
, texture
,
154 Vector3(-1.6 * spacing
, yOffset
, zOffset
),
155 Quaternion(0, 0, 0, 1), 1.0f
* timeSpacing
);
156 fMeshInstances
.push_back(instance
);
157 mesh
->ReleaseReference();
159 mesh
= new StaticMesh("LetterI");
160 instance
= new MeshInstance(mesh
, texture
,
161 Vector3(0 * spacing
, yOffset
, zOffset
),
162 Quaternion(0, 0, 0, 1), 2.0f
* timeSpacing
);
163 fMeshInstances
.push_back(instance
);
164 mesh
->ReleaseReference();
166 mesh
= new StaticMesh("LetterK");
167 instance
= new MeshInstance(mesh
, texture
,
168 Vector3(1.5 * spacing
, yOffset
, zOffset
),
169 Quaternion(0, 0, 0, 1), 3.0f
* timeSpacing
);
170 fMeshInstances
.push_back(instance
);
171 mesh
->ReleaseReference();
173 mesh
= new StaticMesh("LetterU");
174 instance
= new MeshInstance(mesh
, texture
,
175 Vector3(3.4 * spacing
, yOffset
, zOffset
),
176 Quaternion(0, 0, 0, 1), 4.0f
* timeSpacing
);
177 fMeshInstances
.push_back(instance
);
178 mesh
->ReleaseReference();
179 texture
->ReleaseReference();
181 fMainCamera
= new Camera(Vector3(0, 0, 0), Quaternion(0, 0, 0, 1), 50);
186 RenderView::_DeleteScene()
188 MeshInstanceList::iterator it
= fMeshInstances
.begin();
189 for (; it
!= fMeshInstances
.end(); it
++) {
192 fMeshInstances
.clear();
197 RenderView::_UpdateViewport()
199 if (fNextRes
!= fRes
&& fNextRes
.x
>= 1.0 && fNextRes
.y
>= 1.0) {
200 glViewport(0, 0, (GLint
) fNextRes
.x
+ 1, (GLint
) fNextRes
.y
+ 1);
208 RenderView::_UpdateCamera()
210 // TODO: take camera orientation into account
211 glMatrixMode(GL_PROJECTION
);
213 gluPerspective(fMainCamera
->FieldOfView(), fNextRes
.x
/ fNextRes
.y
,
214 fMainCamera
->Near(), fMainCamera
->Far());
215 glMatrixMode(GL_MODELVIEW
);
220 RenderView::_Render()
226 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
229 bigtime_t time
= system_time();
230 float deltaTime
= 0.000001 * (float)(time
- fLastFrameTime
);
231 fLastFrameTime
= time
;
233 MeshInstanceList::iterator it
= fMeshInstances
.begin();
234 for (; it
!= fMeshInstances
.end(); it
++) {
235 (*it
)->Update(deltaTime
);
239 if (fStopRendering
) {
244 SwapBuffers(false); // true = vsync
250 RenderView::FrameResized(float width
, float height
)
253 fNextRes
.Set(width
, height
);
255 BGLView::FrameResized(width
, height
);
260 RenderView::ErrorCallback(unsigned long error
)
262 fprintf(stderr
, "OpenGL error (%lu): %s\n", error
, gluErrorString(error
));