On Monday, 3 April 2023 at 23:38:52 UTC, Marcone wrote:
What do you think about using Chat GPT to create functions in
D? I asked Chat-GPT to create a function in D that was similar
to the rsplit() function in Python. It returned this code to me:
import std.algorithm;
import std.array;
import std.string;
string[] rsplit(string input, string separator, size_t maxsplit
= size_t.max) {
auto splitter = splitter(input, separator,
SplitterFlag.all);
auto results = splitter.array;
if (maxsplit < results.length - 1)
results = results[results.length - maxsplit - 1 .. $];
return results;
}
Recently, it helped me figure out an OpenGL task using d.
This is the code it suggests after I provide some details to
it about the case and the error I get:
```d
Slice!(RCI!ubyte, 3LU, Contiguous) plot2imslice() @nogc
nothrow
{
import mir.rc;
Slice!(RCI!ubyte, 3LU, Contiguous) imgslice =
rcslice!ubyte([height, width, 3], 0);
glfwMakeContextCurrent(_glfwWindow);
// create and bind framebuffer
GLuint fbo;
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
// create and bind texture
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,
GL_RGB, GL_UNSIGNED_BYTE, null);
glFramebufferTexture2D(GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
// render scene to framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
// render your scene here
// read pixel data from framebuffer
glReadPixels(0, 0, width, height, GL_RGB,
GL_UNSIGNED_BYTE, imgslice.ptr);
// flip image vertically
for (size_t y = 0; y < height / 2; y++) {
for (size_t x = 0; x < width; x++) {
// swap rows
swap(imgslice[y][x], imgslice[height - y - 1][x]);
}
}
// unbind framebuffer and cleanup
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDeleteTextures(1, &texture);
glDeleteFramebuffers(1, &fbo);
return imgslice;
}
```
And this is the code after required modifications:
```d
auto plot2imslice() @nogc nothrow
{
import mir.rc;
Slice!(RCI!ubyte, 3LU, Contiguous) imgslice =
rcslice!ubyte([height, width, 3], 0);
glfwMakeContextCurrent(_glfwWindow);
// create and bind framebuffer
GLuint fbo;
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
// create and bind texture
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height,
0, GL_RGB, GL_UNSIGNED_BYTE, null);
glFramebufferTexture2D(GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
// render scene to framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
// render scene here
render();
// read pixel data from framebuffer
glReadPixels(0, 0, width, height, GL_RGB,
GL_UNSIGNED_BYTE, imgslice.ptr);
import mir.utility : swap;
// flip image vertically to correct generated image
foreach (y; 0..height / 2) {
each!swap(imgslice[y], imgslice[height - y - 1]);
}
// unbind framebuffer and cleanup
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDeleteTextures(1, &texture);
glDeleteFramebuffers(1, &fbo);
return imgslice;
}
```
This comment of chatGPT saved the day :) // render your scene here