【问题标题】:Access violations while trying to get a texture to work in OpenGL using C++尝试使用 C++ 使纹理在 OpenGL 中工作时访问冲突
【发布时间】:2011-12-22 02:36:47
【问题描述】:

好的.. 对 C++ 来说是全新的(我的意思是超级新的)。我了解很多 PHP 和 javascript,所以我了解基本的代码结构等。现在,我只是想掌握 C++ 概念。我可以使用我在网上找到的教程制作一个正方形并移动它。不要介意正方形大小与纹理(它的 175x55 并且它位于根目录 [texture.bmp] 中)。但是我试图获得纹理并且我得到一个错误。我正在使用 SDL 和 openGL。我得到的错误是:

“Test1.exe 中 0x0020fee9 处未处理的异常:0x0000005:访问冲突读取位置 0x00139000”

我在网上查看过,据我了解,有些东西被乱序调用(根据我在网上找到的所有结果)。我觉得..

我的代码如下:

//The headers
#include "SDL.h"
#include "SDL_opengl.h"
#include "stdio.h"
#pragma comment( lib, "opengl32.lib" )
#pragma comment( lib, "glu32.lib" )

//Screen attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

//The frame rate
const int FRAMES_PER_SECOND = 60;

//The attributes of the square
const int SQUARE_WIDTH = 20;
const int SQUARE_HEIGHT = 20;

//Event handler
SDL_Event event;

//The square
class Square
{
    private:
    //The offsets
    int x, y;

    //The velocity of the square
    int xVel, yVel;

    public:
    //Initializes
    Square();

    //Handles key presses
    void handle_input();

    //Moves the square
    void move();

    //Shows the square on the screen
    void show();

    void getTexture();
};

//The timer
class Timer
{
    private:
    //The clock time when the timer started
    int startTicks;

    //The ticks stored when the timer was paused
    int pausedTicks;

    //The timer status
    bool paused;
    bool started;

    public:
    //Initializes variables
    Timer();

    //The various clock actions
    void start();
    void stop();
    void pause();
    void unpause();

    //Gets the timer's time
    int get_ticks();

    //Checks the status of the timer
    bool is_started();
    bool is_paused();
};

bool init_GL()
{
    //Set clear color
    glClearColor( 0, 0, 0, 0 );

    //Set projection
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    glOrtho( 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1 );

    //Initialize modelview matrix
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    //If there was any errors
    if( glGetError() != GL_NO_ERROR )
    {
        return false;
    }

    //If everything initialized
    return true;
}

bool init()
{
    //Initialize SDL
    if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
    {
        return false;
    }

    //Create Window
    if( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL ) == NULL )
    {
        return false;
    }

    //Initialize OpenGL
    if( init_GL() == false )
    {
        return false;
    }

    //Set caption
    SDL_WM_SetCaption( "OpenGL Test", NULL );

    return true;
}

void clean_up()
{
    //Quit SDL
    SDL_Quit();
}

Square::Square()
{
    //Initialize offsets
    x = 0;
    y = 0;

    //Initialize velocity
    xVel = 0;
    yVel = 0;
}

void Square::handle_input()
{
    //If a key was pressed
    if( event.type == SDL_KEYDOWN )
    {
        //Adjust the velocity
        switch( event.key.keysym.sym )
        {
            case SDLK_UP: yVel -= SQUARE_HEIGHT / 2; break;
            case SDLK_DOWN: yVel += SQUARE_HEIGHT / 2; break;
            case SDLK_LEFT: xVel -= SQUARE_WIDTH / 2; break;
            case SDLK_RIGHT: xVel += SQUARE_WIDTH / 2; break;
        }
    }
    //If a key was released
    else if( event.type == SDL_KEYUP )
    {
        //Adjust the velocity
        switch( event.key.keysym.sym )
        {
            case SDLK_UP: yVel += SQUARE_HEIGHT / 2; break;
            case SDLK_DOWN: yVel -= SQUARE_HEIGHT / 2; break;
            case SDLK_LEFT: xVel += SQUARE_WIDTH / 2; break;
            case SDLK_RIGHT: xVel -= SQUARE_WIDTH / 2; break;
        }
    }
}

void Square::move()
{
    //Move the square left or right
    x += xVel;

    //If the square went too far
    if( ( x < 0 ) || ( x + SQUARE_WIDTH > SCREEN_WIDTH ) )
    {
        //Move back
        x -= xVel;
    }

    //Move the square up or down
    y += yVel;

    //If the square went too far
    if( ( y < 0 ) || ( y + SQUARE_HEIGHT > SCREEN_HEIGHT ) )
    {
        //Move back
        y -= yVel;
    }
}

void getTexture()
{
    GLuint texture; // Texture object handle

    SDL_Surface *surface; // Gives us the information to make the texture

    if ( (surface = SDL_LoadBMP("image.bmp")) ) { 

        // Check that the image's width is a power of 2
        if ( (surface->w & (surface->w - 1)) != 0 ) {
            printf("warning: image.bmp's width is not a power of 2\n");
        }

        // Also check if the height is a power of 2
        if ( (surface->h & (surface->h - 1)) != 0 ) {
            printf("warning: image.bmp's height is not a power of 2\n");
        }

        // Have OpenGL generate a texture object handle for us
        glGenTextures( 1, &texture );

        // Bind the texture object
        glBindTexture( GL_TEXTURE_2D, texture );

        // Set the texture's stretching properties
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

        // Edit the texture object's image data using the information SDL_Surface gives us
        glTexImage2D( GL_TEXTURE_2D, 0, 3, surface->w, surface->h, 0, 
                      GL_BGR, GL_UNSIGNED_BYTE, surface->pixels );
    }     

    // Free the SDL_Surface only if it was successfully created
    if ( surface ) { 
        SDL_FreeSurface( surface );
    }

    // Bind the texture to which subsequent calls refer to
    glBindTexture( GL_TEXTURE_2D, texture );

}

void Square::show()
{
    //Move to offset
    glTranslatef( x, y, 0 );

    //Start quad
    glBegin( GL_QUADS );
        // Top-left vertex (corner)
        glTexCoord2i( 0, 0 );
        glVertex3f( 100, 100, 0 );

        // Bottom-left vertex (corner)
        glTexCoord2i( 1, 0 );
        glVertex3f( 228, 100, 0 );

        // Bottom-right vertex (corner)
        glTexCoord2i( 1, 1 );
        glVertex3f( 228, 228, 0 );

        // Top-right vertex (corner)
        glTexCoord2i( 0, 1 );
        glVertex3f( 100, 228, 0 );
    glEnd();

    //Reset
    glLoadIdentity();
}

Timer::Timer()
{
    //Initialize the variables
    startTicks = 0;
    pausedTicks = 0;
    paused = false;
    started = false;
}

void Timer::start()
{
    //Start the timer
    started = true;

    //Unpause the timer
    paused = false;

    //Get the current clock time
    startTicks = SDL_GetTicks();
}

void Timer::stop()
{
    //Stop the timer
    started = false;

    //Unpause the timer
    paused = false;
}

void Timer::pause()
{
    //If the timer is running and isn't already paused
    if( ( started == true ) && ( paused == false ) )
    {
        //Pause the timer
        paused = true;

        //Calculate the paused ticks
        pausedTicks = SDL_GetTicks() - startTicks;
    }
}

void Timer::unpause()
{
    //If the timer is paused
    if( paused == true )
    {
        //Unpause the timer
        paused = false;

        //Reset the starting ticks
        startTicks = SDL_GetTicks() - pausedTicks;

        //Reset the paused ticks
        pausedTicks = 0;
    }
}

int Timer::get_ticks()
{
    //If the timer is running
    if( started == true )
    {
        //If the timer is paused
        if( paused == true )
        {
            //Return the number of ticks when the timer was paused
            return pausedTicks;
        }
        else
        {
            //Return the current time minus the start time
            return SDL_GetTicks() - startTicks;
        }
    }

    //If the timer isn't running
    return 0;
}

bool Timer::is_started()
{
    return started;
}

bool Timer::is_paused()
{
    return paused;
}

int main( int argc, char *argv[] )
{
    //Quit flag
    bool quit = false;

    //Initialize
    if( init() == false )
    {
        return 1;
    }

    //Our square object
    Square square;

    getTexture();

    //The frame rate regulator
    Timer fps;

    //Wait for user exit
    while( quit == false )
    {
        //Start the frame timer
        fps.start();

        //While there are events to handle
        while( SDL_PollEvent( &event ) )
        {
            //Handle key presses
            square.handle_input();

            if( event.type == SDL_QUIT )
            {
                quit = true;
            }
        }

        //Move the square
        square.move();

        //Clear the screen
        glClear( GL_COLOR_BUFFER_BIT );

        //Show the square
        square.show();

        //Update screen
        SDL_GL_SwapBuffers();

        //Cap the frame rate
        if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
        {
            SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
        }
    }

    //Clean up
    clean_up();

    return 0;
}

您可以将其粘贴到 MS Visual C++ 2010(我正在使用的)中。请给我建议,如果它更好地以不同的方式做,或者您可能有任何其他建议。谢谢。

【问题讨论】:

  • C++ 新手?我有义务推荐a good introductory C++ book。 :)
  • 您是否尝试过使用调试器来确定异常的确切位置?
  • Visual C 中的“反汇编”说:0020FEE9 movzx ebx,byte ptr [esi+2]。不确定这是否有帮助。
  • @ALF: 这对应于哪个源代码行?
  • 如果您是 C++ 新手,那么您需要做的最后件事就是图形编程。 first 尝试了解 C++ 的工作原理。图形不是初级程序员的任务。

标签: c++ opengl sdl


【解决方案1】:

我无法真正重现您的问题。该程序执行得很好,并且添加了 glEnable(GL_TEXTURE_2D) 它甚至显示了一个纹理正方形。请注意,您不应在对象的类中进行输入处理,而应通过专用的输入处理程序将输入事件的结果委托给对象。这不是Java,你不必把所有东西都放在一个类中,尤其是像SDL这样与类无关的库。 getTextures 在您的代码中被声明为成员函数,但未定义。

进行这 4 项更改后,http://pastebin.com/c58Hmw9Z 中的代码按预期执行。顺便说一句:由于 OpenGL-2 纹理可能具有任意分辨率。

【讨论】:

  • 好的,从您帮助我的情况来看,似乎某些配置不正确。当我按下 f10(运行到光标)并将我带到 WinMainCRTStartup(void)?我尝试禁用 /GS,但仍然遇到完全相同的错误。
  • 谢谢。必须重新启动项目配置所有内容并重新下载所有 dll。在某些时候,我一定改变了一些东西..虽然不确定。
【解决方案2】:

看来你在同一个纹理上调用了glBindTexture()两次;我不能说这是问题的根源,但我认为您可能应该删除第二个实例(如果未采用 if() 分支,则 texture 可能未初始化)。

【讨论】:

  • 不错的收获。在同一个纹理上调用它两次没有问题(它是多余的,但不应该成为问题)。可悲的是,用不确定的值调用它也可以。在 OpenGL 3 之前(就像这里的情况),使用未生成的纹理对象句柄会自动生成它。所以,它可能会导致一些问题,但我认为它不会导致异常。无论如何 +1。
  • 因为它只是一个整数,所以进行 real “indeterminate”检查无论如何都太过分了,但它至少可以确保该值是被传递的值早点出来。 (映射纹理多久会真正发生一次,以消除对性能的错误检查才有意义?)感谢您的警告。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-22
  • 2020-04-11
  • 2021-02-13
  • 1970-01-01
  • 2021-10-16
相关资源
最近更新 更多