1

I just got started trying to learn how to code graphics using C++. When compiling a linear interpolation code, the code does not run and sends VC++ to the xmemory file. No errors or warnings given, thus leaving me with nothing to work on. What did I do wrong? I suspect the problem is connected to the way I assign the vectors, yet none of my changes have worked.

Here is the code:

#include "SDL.h"
#include <iostream>
#include <glm/glm.hpp>
#include <vector>
#include "SDLauxiliary.h"

using namespace std;
using glm::vec3;
using std::vector;

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
SDL_Surface* screen;

void Draw();

void Interpolate( float a, float b, vector<float>& result ) {

    int i = 0;
    for ( float x=a;x < b+1; ++x )
    {
        result[i] = x;
        i = i + 1;
    }

}

void InterpolateVec( vec3 a, vec3 b, vector<vec3>& resultvec ) {
    int i = 0;    
    for (int add=0; add < 4; ++add) {
        float count1 = (b[add]-a[add])/resultvec.size() + a[add];
        float count2 = (b[add]-a[add])/resultvec.size() + a[add];
        float count3 = (b[add]-a[add])/resultvec.size() + a[add];

        resultvec[i].x = (count1, count2, count3);
        resultvec[i].y = (count1, count2, count3);
        resultvec[i].z = (count1, count2, count3);
        i = i + 1;
    }
}

int main( int argc, char* argv[] )
{
    vector<float> result(10); // Create a vector width 10 floats
    Interpolate(5, 14, result); // Fill it with interpolated values
    for( int i=0; i < result.size(); ++i )
        cout << result[i] << " "; // Print the result to the terminal

    vector<vec3> resultvec( 4 );
    vec3 a(1,4,9.2);
    vec3 b(4,1,9.8);
    InterpolateVec( a, b, resultvec );
    for( int i=0; i<resultvec.size(); ++i )
    {
        cout << "( "
        << resultvec[i].x << ", "
        << resultvec[i].y << ", "
        << resultvec[i].z << " ) ";
    }

    screen = InitializeSDL( SCREEN_WIDTH, SCREEN_HEIGHT );
    while( NoQuitMessageSDL() )
    {
        Draw();
    }
    SDL_SaveBMP( screen, "screenshot.bmp" );

    return 0;
}

void Draw()
{

    for( int y=0; y<SCREEN_HEIGHT; ++y )
    {

        for( int x=0; x<SCREEN_WIDTH; ++x )
        {
            vec3 color(1,0,1);
            PutPixelSDL( screen, x, y, color );
        }
    }

    if( SDL_MUSTLOCK(screen) )
        SDL_UnlockSurface(screen);

    SDL_UpdateRect( screen, 0, 0, 0, 0 );
}
4

3 回答 3

0

我不能对这个问题发表评论,所以我会写下我的想法作为答案。

    resultvec[i].x = (count1, count2, count3);
    resultvec[i].y = (count1, count2, count3);
    resultvec[i].z = (count1, count2, count3);

看起来您(或您的库之一)重载operator,floatto makevec2和 after vec3。不错的解决方案,但如果我是对的,那么没有理由将每个组件分配给该值,并且您的代码将类似于:

    resultvec[i] = (count1, count2, count3);

同样,这只是一个假设!我无法编译您的代码并查看错误。

另外我不明白你为什么使用i,这等于add.

于 2013-04-13T14:29:23.887 回答
0

奇怪的是你们中的一些人无法编译代码;可能是您尚未安装库(n00b 推测,是的......)。

因此,这是我为使其工作所做的工作(在这种情况下,代码越少越好,如第一条评论所述):

void InterpolateVec( vec3 a, vec3 b, vector<vec3>& resultvec ) {
resultvec[0].x = a[0];
resultvec[0].y = a[1];
resultvec[0].z = a[2];

float count1 = (b[0]-a[0])/(resultvec.size() - 1);
float count2 = (b[1]-a[1])/(resultvec.size() - 1);
float count3 = (b[2]-a[2])/(resultvec.size() - 1);

for (int add=1; add < 5; ++add) {
    a[0] = a[0] + count1; 
    a[1] = a[1] + count2; 
    a[2] = a[2] + count3; 
    resultvec[add].x = a[0];
    resultvec[add].y = a[1];
    resultvec[add].z = a[2];
}
 }

我发现(经过一个多小时......)是我不需要添加 count1、count2 和 count3;vec3 就是这样一种类型,添加 count1 可以达到我想要的效果;分配颜色(例如(0,0,1))。我是从那时起做的吗?我的词汇量不是我所知道的那种技术性的。

于 2013-04-14T14:01:31.753 回答
0

或者,您可以节省一些时间,让我们glm::vec3去做glm::vec3应该做的事情。

同时; 在这里,有一个 cookie ( cookie.png )

void Interpolate(vec3 a, vec3 b, vector<vec3>& result) {  
    vec3 diffStep = (b-a) * (1.0f / (result.size() - 1)); // Operator overloading
    result[0] = vec3(a);                                   
    for(int i = 1; i < result.size(); i++) {
        result[i] = result[i-1] + diffStep;
    }

}
于 2013-04-15T11:46:16.203 回答