代码不编译 - 最终在xmemory中

时间:2013-04-13 13:39:55

标签: c++ vector

我刚开始尝试学习如何使用C ++编写图形。编译线性插值代码时,代码不会运行并将VC ++发送到xmemory文件。没有给出错误或警告,因此没有任何工作可做。我做错了什么?我怀疑问题与我分配向量的方式有关,但我的更改都没有奏效。

以下是代码:

#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 );
}

3 个答案:

答案 0 :(得分:0)

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

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

operator, float以及vec2之后,您(或您的某个图书馆)看起来超载vec3。很好的解决方案,但如果我是对的,那么没有理由将每个组件分配给该值,并且您的代码将类似于:

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

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

另外,我不明白您使用i的原因,等于add

答案 1 :(得分:0)

很奇怪你们有些人无法编译代码;可能是你没有安装库(n00b推测,yay ......)。

所以这就是我做的工作(在这种情况下代码越少越好,正如第一条评论所述):

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))。我从那以后做的?我的词汇不是我所知道的技术。

答案 2 :(得分: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;
    }

}