即使glew初始化,glGenBuffers也有访问冲突

时间:2013-11-04 12:42:09

标签: c++ visual-c++ opengl sdl vbo

即使我已经初始化了glew,我在渲染我的精灵时遇到了问题。

这是我的代码:

#include <glew.h>
#include <wglew.h>
#include <stdio.h>
#include <iostream>
#include <gl\GL.h>
#include <SDL.h>
#include "SDL_image.h"

GLuint _texturebufferID;
GLuint _vertexBufferID;
GLuint texturebufferID;

int SCREEN_WIDTH = 740;
int SCREEN_HEIGHT = 520;
int mode;

bool processing = true;

SDL_Surface* image;
SDL_Window* window;
SDL_Renderer* renderer;
SDL_GLContext context;
SDL_Surface* surface;
SDL_Event window_key;

typedef struct {

GLfloat positionCoordinates[3];
GLfloat textureCoordinates[2];

} Texture;

Texture vertices[] =
{ 
//  |        Pixels       |--|coords|
    {{1.0f,     0.0f, 0.0f}, {0.0f,   0.0f}},
    {{0.0f,    43.0f, 0.0f}, {0.0f,   1.0f}},
    {{168.0f,  43.0f, 0.0f}, {1.0f,   1.0f}},
    {{168.0f,   0.0f, 0.0f}, {1.0f,   0.0f}}

};

GLuint loadandbuffersprite(const char *filename)
{
image = IMG_Load(filename); 

if (image->format->BytesPerPixel == 3)
{
    mode = GL_RGB;
}

else if (image->format->BytesPerPixel == 4)
{
    mode = GL_RGBA;
}

else
{
    SDL_FreeSurface(image);
    return 0;
}

glGenTextures(1, &texturebufferID);
glBindTexture(GL_TEXTURE_2D, texturebufferID);
glTexImage2D(GL_TEXTURE_2D, 0, mode, image->w, image->h,
    0, mode, GL_UNSIGNED_BYTE, image->pixels);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

SDL_FreeSurface( image );

return texturebufferID;
}

void render()
{

    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glMatrixMode(GL_PROJECTION);
    glMatrixMode(GL_MODELVIEW);

    glGenBuffers(1, &_vertexBufferID);
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices,
        GL_STATIC_DRAW);

    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, sizeof(Texture), (GLvoid *) 
        offsetof(Texture, positionCoordinates));

    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glTexCoordPointer(2, GL_FLOAT, sizeof(Texture), (GLvoid *) 
        offsetof(Texture, textureCoordinates));

    glLoadIdentity();

    _texturebufferID = loadandbuffersprite("hane_stand.png");
}

// |----------------------------------------|
// |             Main function              |
// |----------------------------------------|

int main(int argc, char *argv[])
{

    SDL_Init(SDL_INIT_EVERYTHING);
    // Intialize everything.

    window = SDL_CreateWindow
        (
        "render example",
        SDL_WINDOWPOS_CENTERED,
        SDL_WINDOWPOS_CENTERED,
        SCREEN_WIDTH,
        SCREEN_HEIGHT,
        SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
        ); 
    // Create window for rendering.

    glewInit();

    renderer = SDL_CreateRenderer(window, -1, 0); 

    context = SDL_GL_CreateContext(window);

    SDL_GL_MakeCurrent(window, context);

    SDL_GL_LoadLibrary( NULL );

    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    // Clearing color

    glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    // Set viewport


// |----------------------------------------|
// |                Game loop               |
// |----------------------------------------|

    while (processing)
    {

    glClear(GL_COLOR_BUFFER_BIT);


    render();

    glDrawArrays(GL_QUADS, 0, 4);

    SDL_RenderPresent(renderer);
    SDL_GL_SwapWindow(window);

    }

    glDeleteTextures( 1, &texturebufferID);

    SDL_DestroyWindow(window); 

    SDL_Quit();

    return 0;
}

正是这个问题一直困扰着我的编码进程数月,真的需要帮助。任何人对我如何解决这个问题都有任何想法?

至于我的电脑资料,我使用带有AMD C-50处理器和2.0 GB RAM的东芝卫星。

0 个答案:

没有答案