glTexImage2D() 函数访问冲突错误
Posted
技术标签:
【中文标题】glTexImage2D() 函数访问冲突错误【英文标题】:glTexImage2D() function access violation errors 【发布时间】:2013-11-04 21:49:06 【问题描述】:我一直遇到 glTexImage2D 问题,它阻止我渲染到屏幕。
这是我的代码:
#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);
glGenTextures(1, &texturebufferID);
glBindTexture(GL_TEXTURE_2D, texturebufferID);
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);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->w, image->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); //Access Violation
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();
loadandbuffersprite("hane_stand.png");
我最近遇到了错误,但我不明白问题可能是什么。可以指出我正确的方向吗?
【问题讨论】:
【参考方案1】:唯一真正的问题是您从未启用 GL_TEXTURE_2D 进行 2D 渲染。
【讨论】:
以上是关于glTexImage2D() 函数访问冲突错误的主要内容,如果未能解决你的问题,请参考以下文章
glCreateShader(GL_VERTEX_SHADER);访问冲突执行位置