我有一个纹理,我想要在内存中存储。然后我想将这个纹理的一部分渲染到OpenGL上下文中。我想知道如何做到这一点。如何在内存中存储纹理并渲染它的一小部分
到目前为止,我已经设置了我的顶点数据。
GLfloat texture[] =
{
-0.5f, 0.5f, 0.0f, 0.1f, 0.0f, 0.0f, 1.0f, 1.0f, // top rght
0.5f, 0.5f, 0.0f, 0.0f, 0.1f, 0.0f, 1.0f, 0.0f, // bottom right
0.5f, -0.5f, 0.0, 0.0f, 0.0f, 0.1f, 0.0f, 0.0f, // bottom left
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, // top left
};
然后我去完成创建VBO,并使用OpenGL函数
GLuint indices[] =
{
0, 1, 3, //First triangle
1, 2, 3 //Second triangle
};
GLuint VBO, VAO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1,& VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VB);
glBufferData(GL_ARRAY_BUFFER, sizeof(texture), texture, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
//position attributes
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
//colour attributes
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
//TexCoord attribute
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
glEnableVertexAttribArray(2);
glBindVertexArray(0); //unbind VAO
负荷通常的步骤,并创建一个质地:
GLuint texture0;
int width, height;
unsigned char* image[1];
glGenTextures(1, &texture0);
glBindTexture(GL_TEXTURE_2D, texture0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);//set texture wrapping to GL_REPEAT (usually basic wrapping method)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
image[0] = SOIL_load_image("Textures/texture.png", &width, &height, 0, SOIL_LOAD_RGB);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image[0]);
glGenerateMipmap(GL_TEXTURE_2D);
SOIL_free_image_data(image[0]);
glBindTexture(GL_TEXTURE_2D, texture0);
然后我用一个while循环激活着色器,然后绘制纹理。不过,现在我想要做的就是将此纹理存储在内存中,并只渲染它的一小部分。我是否使用Renderbuffer对象或PBO,还是有其他方法来实现这一点?
非常感谢!这是我需要的答案:] –