Quellcode durchsuchen

tutorial: speed up texture uploads in the Mandelbrot viewer by only

sending the part we just modified.
legacy
Sam Hocevar sam vor 13 Jahren
Ursprung
Commit
adf2b0de2e
1 geänderte Dateien mit 34 neuen und 14 gelöschten Zeilen
  1. +34
    -14
      test/tutorial/tut03.cpp

+ 34
- 14
test/tutorial/tut03.cpp Datei anzeigen

@@ -37,10 +37,14 @@ public:
{
m_size = size;
m_pixels = new u8vec4[size.x * size.y];
m_frame = 0;
m_frame = -1;
m_center = 0;
m_target = f64cmplx(0.001643721971153, 0.822467633298876);
//m_target = f64cmplx(0.001643721971153, 0.822467633298876);
m_target = f64cmplx(-1.207205434596, 0.315432814901);
//m_target = f64cmplx(-0.79192956889854, -0.14632423080102);
//m_target = f64cmplx(0.3245046418497685, 0.04855101129280834);
//m_target = f64cmplx(0.28693186889504513, 0.014286693904085048);
m_angle = 0.0;
m_radius = 8.0;
m_ready = false;
}
@@ -56,14 +60,17 @@ public:

m_frame = (m_frame + 1) % 4;

double zoom = pow(2.0, -deltams * 0.00015);
double zoom = pow(2.0, -deltams * 0.0005);
m_radius *= zoom;
m_center = (m_center - m_target) * zoom * zoom + m_target;

double step = m_radius / (m_size.x > m_size.y ? m_size.x : m_size.y);
// m_angle -= deltams * 0.00015;
f64cmplx transform = step * f64cmplx(cos(m_angle), sin(m_angle));

for (int j = m_frame / 2; j < m_size.y; j += 2)
//for (int j = ((m_frame + 1) % 4) / 2; j < m_size.y; j += 2)
u8vec4 *m_pixelstart = m_pixels + m_size.x * m_size.y / 4 * m_frame;

for (int j = ((m_frame + 1) % 4) / 2; j < m_size.y; j += 2)
for (int i = m_frame % 2; i < m_size.x; i += 2)
{
double const maxlen = 32;
@@ -71,7 +78,7 @@ public:

f64cmplx delta(i - m_size.x / 2, j - m_size.y / 2);

f64cmplx z0 = m_center + step * delta;
f64cmplx z0 = m_center + transform * delta;
f64cmplx r0 = z0;
//f64cmplx r0(0.28693186889504513, 0.014286693904085048);
//f64cmplx r0(0.001643721971153, 0.822467633298876);
@@ -98,11 +105,11 @@ public:
uint8_t red = r * 255.0f;
uint8_t green = g * 255.0f;
uint8_t blue = b * 255.0f;
m_pixels[j * m_size.x + i] = u8vec4(red, green, blue, 0);
*m_pixelstart++ = u8vec4(red, green, blue, 0);
}
else
{
m_pixels[j * m_size.x + i] = u8vec4(0, 0, 0, 0);
*m_pixelstart++ = u8vec4(0, 0, 0, 0);
}
}
}
@@ -133,10 +140,12 @@ public:

if (!m_ready)
{
/* Create a texture of half the width and twice the height
* so that we can upload four different subimages each frame. */
glGenTextures(1, &m_texid);
glBindTexture(GL_TEXTURE_2D, m_texid);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_size.x, m_size.y, 0,
GL_RGBA, GL_UNSIGNED_BYTE, m_pixels);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_size.x / 2, m_size.y * 2,
0, GL_RGBA, GL_UNSIGNED_BYTE, m_pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

@@ -154,7 +163,16 @@ public:
"uniform sampler2D in_Texture;\n"
"void main(void) {"
" vec2 coord = gl_TexCoord[0].xy;"
" gl_FragColor = texture2D(in_Texture, coord);"
/* gl_FragCoord is centered inside the pixel, so we remove
* 0.5 from gl_FragCoord.x. Also, (0,0) is at the bottom
* left whereas our images have (0,0) at the top left, so we
* _add_ 0.5 to gl_FragCoord.y. */
" float i = mod(gl_FragCoord.x - 0.5, 2.0);"
" float j = mod(gl_FragCoord.y + 0.5 + i, 2.0);"
" coord.y += i + j * 2;"
" coord.y *= 0.25;"
" vec4 p = texture2D(in_Texture, coord);"
" gl_FragColor = p;"
"}"
#else
"void main(float4 in_Position : POSITION,"
@@ -198,14 +216,15 @@ public:

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, m_texid);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_size.x, m_size.y,
glTexSubImage2D(GL_TEXTURE_2D, 0,
0, m_frame * m_size.y / 2, m_size.x / 2, m_size.y / 2,
#if !defined __CELLOS_LV2__
GL_RGBA, GL_UNSIGNED_BYTE,
#else
/* The PS3 is big-endian */
GL_RGBA, GL_UNSIGNED_INT_8_8_8_8,
#endif
m_pixels);
m_pixels + m_size.x * m_size.y / 4 * m_frame);

m_shader->Bind();
#if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
@@ -257,7 +276,7 @@ private:
bool m_ready;

f64cmplx m_center, m_target;
double m_radius;
double m_radius, m_angle;
};

int main()
@@ -270,6 +289,7 @@ int main()

new DebugFps(5, 5);
new Fractal(ivec2(640, 480));
//new DebugRecord("fractalol.ogm", 60.0f);

app.Run();



Laden…
Abbrechen
Speichern