Skip to content

Commit ef6ea6e

Browse files
author
Konstantin Zverev
committed
added triangle light tutorial
1 parent d09a44a commit ef6ea6e

File tree

6 files changed

+387
-3
lines changed

6 files changed

+387
-3
lines changed

tutorials/triangle/main.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <cassert>
55
#include <iostream>
66
#include <memory>
7-
#include "shader_manager.h"
7+
#include "../tools/shader_manager.h"
88

99
using namespace RadeonRays;
1010

@@ -121,12 +121,10 @@ int main(int argc, char* argv[])
121121
nativeidx = idx;
122122
}
123123
}
124-
125124
assert(nativeidx != -1);
126125
IntersectionApi* api = IntersectionApi::Create(nativeidx);
127126

128127
Shape* shape = api->CreateMesh(g_vertices, 3, 3 * sizeof(float), g_indices, 0, g_numfaceverts, 1);
129-
130128
assert(shape != nullptr);
131129
api->AttachShape(shape);
132130

tutorials/triangle_light/main.cpp

Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
#include "radeon_rays.h"
2+
#include <GL/glew.h>
3+
#include <GLUT/GLUT.h>
4+
#include <cassert>
5+
#include <iostream>
6+
#include <memory>
7+
#include "../tools/shader_manager.h"
8+
9+
using namespace RadeonRays;
10+
11+
namespace {
12+
float const g_vertices[] = {
13+
-1.f,-1.f,0.f,
14+
1.f,-1.f,0.f,
15+
0.f,1.f,0.f,
16+
};
17+
float const g_normals[] = {
18+
0.f,0.f,1.f,
19+
0.f,0.f,1.f,
20+
0.f,0.f,1.f,
21+
};
22+
int const g_indices[] = { 0, 1, 2 };
23+
const int g_numfaceverts[] = { 3 };
24+
const float3 g_color = { 1.f, 0.f, 0.f, 1.f };
25+
GLuint g_vertex_buffer, g_index_buffer;
26+
GLuint g_texture;
27+
int g_window_width = 640;
28+
int g_window_height = 480;
29+
std::unique_ptr<ShaderManager> g_shader_manager;
30+
}
31+
32+
float3 ConvertFromBarycentric(const float* vec, const int* ind, int prim_id, const float4& uvwt)
33+
{
34+
float3 a = { vec[ind[prim_id * 3] * 3],
35+
vec[ind[prim_id * 3] * 3 + 1],
36+
vec[ind[prim_id * 3] * 3 + 2], };
37+
38+
float3 b = { vec[ind[prim_id * 3 + 1] * 3],
39+
vec[ind[prim_id * 3 + 1] * 3 + 1],
40+
vec[ind[prim_id * 3 + 1] * 3 + 2], };
41+
42+
float3 c = { vec[ind[prim_id * 3 + 2] * 3],
43+
vec[ind[prim_id * 3 + 2] * 3 + 1],
44+
vec[ind[prim_id * 3 + 2] * 3 + 2], };
45+
return a * (1 - uvwt.x - uvwt.y) + b * uvwt.x + c * uvwt.y;
46+
}
47+
48+
void InitGl()
49+
{
50+
g_shader_manager.reset(new ShaderManager());
51+
52+
glClearColor(0.0, 0.0, 0.0, 0.0);
53+
glCullFace(GL_NONE);
54+
glDisable(GL_DEPTH_TEST);
55+
glEnable(GL_TEXTURE_2D);
56+
57+
glGenBuffers(1, &g_vertex_buffer);
58+
glGenBuffers(1, &g_index_buffer);
59+
60+
// create Vertex buffer
61+
glBindBuffer(GL_ARRAY_BUFFER, g_vertex_buffer);
62+
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_index_buffer);
63+
64+
float quad_vdata[] =
65+
{
66+
-1, -1, 0.5, 0, 0,
67+
1, -1, 0.5, 1, 0,
68+
1, 1, 0.5, 1, 1,
69+
-1, 1, 0.5, 0, 1
70+
};
71+
72+
GLshort quad_idata[] =
73+
{
74+
0, 1, 3,
75+
3, 1, 2
76+
};
77+
78+
// fill data
79+
glBufferData(GL_ARRAY_BUFFER, sizeof(quad_vdata), quad_vdata, GL_STATIC_DRAW);
80+
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(quad_idata), quad_idata, GL_STATIC_DRAW);
81+
82+
glBindBuffer(GL_ARRAY_BUFFER, 0);
83+
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
84+
85+
86+
glGenTextures(1, &g_texture);
87+
glBindTexture(GL_TEXTURE_2D, g_texture);
88+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
89+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
90+
91+
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, g_window_width, g_window_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
92+
93+
glBindTexture(GL_TEXTURE_2D, 0);
94+
}
95+
96+
void DrawScene()
97+
{
98+
99+
glDisable(GL_DEPTH_TEST);
100+
glViewport(0, 0, g_window_width, g_window_height);
101+
102+
glClear(GL_COLOR_BUFFER_BIT);
103+
104+
glBindBuffer(GL_ARRAY_BUFFER, g_vertex_buffer);
105+
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_index_buffer);
106+
107+
GLuint program = g_shader_manager->GetProgram("simple");
108+
glUseProgram(program);
109+
110+
GLuint texloc = glGetUniformLocation(program, "g_Texture");
111+
assert(texloc >= 0);
112+
113+
glUniform1i(texloc, 0);
114+
115+
glActiveTexture(GL_TEXTURE0);
116+
glBindTexture(GL_TEXTURE_2D, g_texture);
117+
118+
GLuint position_attr = glGetAttribLocation(program, "inPosition");
119+
GLuint texcoord_attr = glGetAttribLocation(program, "inTexcoord");
120+
121+
glVertexAttribPointer(position_attr, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 5, 0);
122+
glVertexAttribPointer(texcoord_attr, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void*)(sizeof(float) * 3));
123+
124+
glEnableVertexAttribArray(position_attr);
125+
glEnableVertexAttribArray(texcoord_attr);
126+
127+
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, nullptr);
128+
129+
glDisableVertexAttribArray(texcoord_attr);
130+
glBindTexture(GL_TEXTURE_2D, 0);
131+
glBindBuffer(GL_ARRAY_BUFFER, 0);
132+
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
133+
glUseProgram(0);
134+
135+
glFinish();
136+
137+
138+
glutSwapBuffers();
139+
}
140+
141+
int main(int argc, char* argv[])
142+
{
143+
// GLUT Window Initialization:
144+
glutInit(&argc, (char**)argv);
145+
glutInitWindowSize(640, 480);
146+
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
147+
glutCreateWindow("Triangle");
148+
#ifndef __APPLE__
149+
GLenum err = glewInit();
150+
if (err != GLEW_OK)
151+
{
152+
std::cout << "GLEW initialization failed\n";
153+
return -1;
154+
}
155+
#endif
156+
157+
InitGl();
158+
159+
int nativeidx = -1;
160+
// Always use OpenCL
161+
IntersectionApi::SetPlatform(DeviceInfo::kOpenCL);
162+
163+
for (auto idx = 0U; idx < IntersectionApi::GetDeviceCount(); ++idx)
164+
{
165+
DeviceInfo devinfo;
166+
IntersectionApi::GetDeviceInfo(idx, devinfo);
167+
168+
if (devinfo.type == DeviceInfo::kGpu && nativeidx == -1)
169+
{
170+
nativeidx = idx;
171+
}
172+
}
173+
174+
assert(nativeidx != -1);
175+
IntersectionApi* api = IntersectionApi::Create(nativeidx);
176+
177+
//Shapes
178+
Shape* shape = api->CreateMesh(g_vertices, 3, 3 * sizeof(float), g_indices, 0, g_numfaceverts, 1);
179+
assert(shape != nullptr);
180+
api->AttachShape(shape);
181+
182+
//point light position
183+
float3 light = { 0.f, 0.f, 0.25f };
184+
185+
const int k_raypack_size = g_window_height * g_window_width;
186+
// Rays
187+
std::vector<ray> rays(k_raypack_size);
188+
189+
// Prepare the ray
190+
for (int i = 0; i < g_window_height; ++i)
191+
for (int j = 0; j < g_window_width; ++j)
192+
{
193+
const float xstep = 2.f / (float)g_window_width;
194+
const float ystep = 2.f / (float)g_window_height;
195+
float x = -1.f + xstep * (float)j;
196+
float y = -1.f + ystep * (float)i;
197+
float z = 1.f;
198+
rays[i * g_window_width + j].o = float3(x, y, z, 1000.f);
199+
rays[i * g_window_width + j].d = float3{0.f, 0.f, -1.f};
200+
}
201+
202+
// Intersection and hit data
203+
std::vector<Intersection> isect(k_raypack_size);
204+
205+
Buffer* ray_buffer = api->CreateBuffer(rays.size() * sizeof(ray), rays.data());
206+
Buffer* isect_buffer = api->CreateBuffer(isect.size() * sizeof(Intersection), nullptr);
207+
208+
api->Commit();
209+
api->QueryIntersection(ray_buffer, k_raypack_size, isect_buffer, nullptr, nullptr);
210+
Event* e = nullptr;
211+
Intersection* tmp = nullptr;
212+
api->MapBuffer(isect_buffer, kMapRead, 0, isect.size() * sizeof(Intersection), (void**)&tmp, &e);
213+
e->Wait();
214+
api->DeleteEvent(e);
215+
e = nullptr;
216+
217+
for (int i = 0; i < k_raypack_size; ++i)
218+
{
219+
isect[i] = tmp[i];
220+
}
221+
222+
std::vector<unsigned char> tex_data(k_raypack_size * 4);
223+
for (int i = 0; i < k_raypack_size; ++i)
224+
{
225+
int shape_id = isect[i].shapeid;
226+
int prim_id = isect[i].primid;
227+
228+
if (shape_id != kNullId)
229+
{
230+
float3 pos = ConvertFromBarycentric(g_vertices, g_indices, prim_id, isect[i].uvwt);
231+
float3 norm = ConvertFromBarycentric(g_normals, g_indices, prim_id, isect[i].uvwt);
232+
norm.normalize();
233+
float3 col = { 0.f, 0.f, 0.f };
234+
float3 light_dir = light - pos;
235+
light_dir.normalize();
236+
float dot_prod = dot(norm, light_dir);
237+
238+
if (dot_prod > 0)
239+
col += dot_prod * g_color;
240+
241+
tex_data[i * 4] = col[0] * 255;
242+
tex_data[i * 4 + 1] = col[1] * 255;
243+
tex_data[i * 4 + 2] = col[2] * 255;
244+
tex_data[i * 4 + 3] = 255;
245+
}
246+
else
247+
{
248+
tex_data[i * 4] = 255;
249+
tex_data[i * 4 + 1] = 255;
250+
tex_data[i * 4 + 2] = 255;
251+
tex_data[i * 4 + 3] = 255;
252+
}
253+
}
254+
glBindTexture(GL_TEXTURE_2D, g_texture);
255+
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, g_window_width, g_window_height, GL_RGBA, GL_UNSIGNED_BYTE, tex_data.data());
256+
glBindTexture(GL_TEXTURE_2D, NULL);
257+
258+
glutDisplayFunc(DrawScene);
259+
glutMainLoop(); //Start the main loop
260+
IntersectionApi::Delete(api);
261+
262+
return 0;
263+
}

tutorials/triangle_light/simple.fsh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**********************************************************************
2+
Copyright (c) 2016 Advanced Micro Devices, Inc. All rights reserved.
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in
12+
all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
THE SOFTWARE.
21+
********************************************************************/
22+
23+
uniform sampler2D g_Texture;
24+
25+
varying vec2 Texcoord;
26+
27+
void main()
28+
{
29+
gl_FragColor = texture2D(g_Texture, Texcoord);
30+
}

tutorials/triangle_light/simple.vsh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**********************************************************************
2+
Copyright (c) 2016 Advanced Micro Devices, Inc. All rights reserved.
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in
12+
all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
THE SOFTWARE.
21+
********************************************************************/
22+
23+
attribute vec3 inPosition;
24+
attribute vec2 inTexcoord;
25+
26+
varying vec2 Texcoord;
27+
28+
void main()
29+
{
30+
Texcoord = inTexcoord;
31+
gl_Position = vec4(inPosition, 1.0);
32+
}
33+

0 commit comments

Comments
 (0)