私はJetbrains Clion IDEを使ってC++でraytracerを書いています。マルチサンプリングアンチエイリアスを有効にして600 * 600のイメージを作成しようとすると、メモリが不足します。私はこのエラーを取得する:私のレンダリング機能のヒープメモリが不足しているのはなぜですか?
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_allocThis application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.
コード:
幅:600 高さ:600 numberOfSamples:80
void Camera::render(const int width, const int height){
int resolution = width * height;
double scale = tan(Algebra::deg2rad(fov * 0.5)); //deg to rad
ColorRGB *pixels = new ColorRGB[resolution];
long loopCounter = 0;
Vector3D camRayOrigin = getCameraPosition();
for (int i = 0; i < width; ++i) {
for (int j = 0; j < height; ++j) {
double zCamDir = (height/2)/scale;
ColorRGB finalColor = ColorRGB(0,0,0,0);
int tempCount = 0;
for (int k = 0 ; k < numberOfSamples; k++) {
tempCount++;
//If it is single sampled, then we want to cast ray in the middle of the pixel, otherwise we offset the ray by a random value between 0-1
double randomNumber = Algebra::getRandomBetweenZeroAndOne();
double xCamDir = (i - (width/2)) + (numberOfSamples == 1 ? 0.5 : randomNumber);
double yCamDir = ((height/2) - j) + (numberOfSamples == 1 ? 0.5 : randomNumber);
Vector3D camRayDirection = convertCameraToWorldCoordinates(Vector3D(xCamDir, yCamDir, zCamDir)).unitVector();
Ray r(camRayOrigin, camRayDirection);
finalColor = finalColor + getColorFromRay(r);
}
pixels[loopCounter] = finalColor/numberOfSamples;
loopCounter++;
}
}
CreateImage::createRasterImage(height, width, "RenderedImage.bmp", pixels);
delete pixels; //Release memory
}
私はC++での初心者ですので、私あなたの助けに本当に感謝しています。私はまた、Microsoft Visual StudioのC#で同じことをやろうとしましたが、メモリ使用量は200MBを超えることはありませんでした。私はいくつかの間違いをしているように感じる。あなたが私を助けたいと思えば、私はより多くの詳細をあなたに提供することができます。
高価な機能を使用していますか?それはメモリの割り当てを解除しない以外の理由かもしれません。 – Auriga