创建顶点缓冲
创建纹理
步骤:创建一个纹理、绑定、填充、配置。
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
利用GLFW加载纹理
GLuint loadTGA_glfw(const char * imagepath){
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glfwLoadTexture2D(imagepath, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
return textureID;
}
渲染到纹理
创建渲染纹理对象
要渲染的对象叫帧缓存。帧缓存是一个包含纹理和深度缓存(可选择)区的容器。 其创建方式与其余的在OpenGL创建对象的方式一致。
GLuint FramebufferName = 0;
glGenFramebuffers(1, &FramebufferName);
glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);
GLuint renderedTexture;
glGenTextures(1, &renderedTexture);
glBindTexture(GL_TEXTURE_2D, renderedTexture);
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, 1024, 768, 0,GL_RGB, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
GLuint depthrenderbuffer;
glGenRenderbuffers(1, &depthrenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthrenderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, 1024, 768);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthrenderbuffer);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, renderedTexture, 0);
GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0};
glDrawBuffers(1, DrawBuffers);
把图像渲染到纹理上
渲染纹理很简单。简单地绑定你的帧缓冲区,并像往常一样绘制场景。
glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);
glViewport(0,0,1024,768);
CUDA和OpenGL互操作
简要说一下如何使CUDA和OpenGL互操作来实现GPU中数据的交互传输,而不用通过主机的CPU。
在OpenGL中声明一个buffer
cudaGraphicsResource_t cudaBuffer;
cudaGraphicsGLRegisterImage(&cudaBuffer, texture, GL_TEXTURE_2D, cudaGraphicsRegisterFlagsWriteDiscard);
把CUDA计算出来的数据写入cudaBuffer中
cudaError_t err;
err = cudaGraphicsMapResources(1, cudaBuffer, 0);
err = cudaGraphicsSubResourceGetMappedArray(&cudaArray, cudaBuffer, 0, 0);
uchar* cudaArray=NULL;
cudaChannelFormatDesc cuDesc = cudaCreateChannelDesc<uchar4>();
cudaMallocArray(&cudaArray, &cuDesc, imgWidth, imgHeight);
err=cudaMemcpyToArray(cudaArray, 0, 0, pResult, imgWidth*imgHeight * sizeof(uchar4), cudaMemcpyDeviceToDevice);
cudaGraphicsUnmapResources(1, &cudaBuffer, 0);
|