glGenTextures(1, &texture);报错cannot convert from 'unsigned int *' to 'int'
时间: 2024-01-01 09:06:06 浏览: 22
这个错误是因为 `glGenTextures` 函数的第二个参数需要传入一个 `GLuint` 类型的指针,而你传入了一个 `unsigned int` 类型的指针。虽然 `GLuint` 和 `unsigned int` 都是无符号整数类型,但是它们在 C++ 中被认为是不同的类型,所以编译器会报错。
要解决这个错误,可以将第二个参数的类型改为 `GLuint*`,并将 `&texture` 改为 `reinterpret_cast<GLuint*>(&texture)`,即:
```
GLuint texture;
glGenTextures(1, reinterpret_cast<GLuint*>(&texture));
```
这样就能够正确地调用 `glGenTextures` 函数了。不过需要注意的是,使用 `reinterpret_cast` 进行类型转换可能会带来一些潜在的风险,因此应该谨慎使用。
相关问题
opengl es 如何将图片的RGB格式转换成unsigned char*并且保存成jpg完整例子
下面是一个完整的示例代码,它使用OpenGL ES将纹理转换为RGB格式的像素数据,并使用libjpeg-turbo库将像素数据保存为JPG格式的图像文件。
```
#include <EGL/egl.h>
#include <GLES2/gl2.h>
#include <jpeglib.h>
#include <turbojpeg.h>
#include <stdio.h>
#include <stdlib.h>
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
#define TEXTURE_FILE "texture.jpg"
#define IMAGE_FILE "image.jpg"
static const char *vertexShaderSource =
"attribute vec4 a_position;\n"
"attribute vec2 a_texcoord;\n"
"varying vec2 v_texcoord;\n"
"void main() {\n"
" gl_Position = a_position;\n"
" v_texcoord = a_texcoord;\n"
"}\n";
static const char *fragmentShaderSource =
"precision mediump float;\n"
"uniform sampler2D u_texture;\n"
"varying vec2 v_texcoord;\n"
"void main() {\n"
" gl_FragColor = texture2D(u_texture, v_texcoord);\n"
"}\n";
static const GLfloat vertices[] = {
-1.0f, 1.0f, 0.0f, // top left
-1.0f, -1.0f, 0.0f, // bottom left
1.0f, -1.0f, 0.0f, // bottom right
1.0f, 1.0f, 0.0f, // top right
};
static const GLfloat texcoords[] = {
0.0f, 0.0f, // top left
0.0f, 1.0f, // bottom left
1.0f, 1.0f, // bottom right
1.0f, 0.0f, // top right
};
static const GLushort indices[] = {
0, 1, 2,
0, 2, 3,
};
int main(int argc, char *argv[])
{
// Initialize EGL display and context
EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
EGLint major, minor;
eglInitialize(display, &major, &minor);
EGLint attributes[] = {
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_NONE
};
EGLConfig config;
EGLint num_configs;
eglChooseConfig(display, attributes, &config, 1, &num_configs);
EGLint context_attributes[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
EGLContext context = eglCreateContext(display, config, EGL_NO_CONTEXT, context_attributes);
// Create window surface
EGLSurface surface;
EGLint surface_attributes[] = {
EGL_WIDTH, WINDOW_WIDTH,
EGL_HEIGHT, WINDOW_HEIGHT,
EGL_NONE
};
surface = eglCreateWindowSurface(display, config, NULL, surface_attributes);
// Make the context current
eglMakeCurrent(display, surface, surface, context);
// Load texture from file
unsigned char *texture_data;
int texture_width, texture_height;
tjhandle tj = tjInitDecompress();
FILE *fp = fopen(TEXTURE_FILE, "rb");
fseek(fp, 0, SEEK_END);
long file_size = ftell(fp);
fseek(fp, 0, SEEK_SET);
texture_data = (unsigned char *)malloc(file_size);
fread(texture_data, 1, file_size, fp);
fclose(fp);
int tj_width, tj_height, tj_subsamp;
tjDecompressHeader2(tj, texture_data, file_size, &tj_width, &tj_height, &tj_subsamp);
texture_width = tj_width;
texture_height = tj_height;
unsigned char *texture_pixels = (unsigned char *)malloc(texture_width * texture_height * 3);
tjDecompress2(tj, texture_data, file_size, texture_pixels, texture_width, 0, texture_height, TJPF_RGB, TJFLAG_FASTDCT);
tjDestroy(tj);
// Create texture
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texture_width, texture_height, 0, GL_RGB, GL_UNSIGNED_BYTE, texture_pixels);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Create program
GLuint program;
GLint success;
char info_log[512];
GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &vertexShaderSource, NULL);
glCompileShader(vertex_shader);
glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(vertex_shader, 512, NULL, info_log);
fprintf(stderr, "Failed to compile vertex shader: %s\n", info_log);
exit(1);
}
GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragment_shader);
glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(fragment_shader, 512, NULL, info_log);
fprintf(stderr, "Failed to compile fragment shader: %s\n", info_log);
exit(1);
}
program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(program, 512, NULL, info_log);
fprintf(stderr, "Failed to link program: %s\n", info_log);
exit(1);
}
glUseProgram(program);
// Set up vertex attributes
GLint position_attrib = glGetAttribLocation(program, "a_position");
glVertexAttribPointer(position_attrib, 3, GL_FLOAT, GL_FALSE, 0, vertices);
glEnableVertexAttribArray(position_attrib);
GLint texcoord_attrib = glGetAttribLocation(program, "a_texcoord");
glVertexAttribPointer(texcoord_attrib, 2, GL_FLOAT, GL_FALSE, 0, texcoords);
glEnableVertexAttribArray(texcoord_attrib);
// Set up texture uniform
GLint texture_uniform = glGetUniformLocation(program, "u_texture");
glUniform1i(texture_uniform, 0);
// Render to framebuffer
GLuint framebuffer, renderbuffer;
glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glGenRenderbuffers(1, &renderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, WINDOW_WIDTH, WINDOW_HEIGHT);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderbuffer);
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glClear(GL_COLOR_BUFFER_BIT);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
// Read framebuffer pixels
unsigned char *pixels = (unsigned char *)malloc(WINDOW_WIDTH * WINDOW_HEIGHT * 3);
glReadPixels(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, pixels);
// Save image to file
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
FILE *fp2 = fopen(IMAGE_FILE, "wb");
jpeg_stdio_dest(&cinfo, fp2);
cinfo.image_width = WINDOW_WIDTH;
cinfo.image_height = WINDOW_HEIGHT;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
jpeg_start_compress(&cinfo, TRUE);
JSAMPROW row_pointer[1];
while (cinfo.next_scanline < cinfo.image_height) {
row_pointer[0] = &pixels[(cinfo.image_height - cinfo.next_scanline - 1) * cinfo.image_width * 3];
jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
jpeg_finish_compress(&cinfo);
fclose(fp2);
// Clean up
free(texture_data);
free(texture_pixels);
free(pixels);
glDeleteProgram(program);
glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);
glDeleteTextures(1, &texture);
glDeleteFramebuffers(1, &framebuffer);
glDeleteRenderbuffers(1, &renderbuffer);
eglDestroySurface(display, surface);
eglDestroyContext(display, context);
eglTerminate(display);
return 0;
}
```
注意:此示例代码仅支持RGB格式的图像文件,如果要处理其他格式的图像文件,请根据需要进行适当修改。
我写了这样一个函数 int LoadGLTextures() { int Status=FALSE; AUX_RGBImageRec *TextureImage[1]; memset(TextureImage,0,sizeof(void *)*1); if (TextureImage[0]=LoadBMP("rose.bmp")) { Status=TRUE; glGenTextures(1, &texture[0]); glBindTexture(GL_TEXTURE_2D, texture[0]); glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); } if (TextureImage[0]) { if (TextureImage[0]->data) { free(TextureImage[0]->data); } free(TextureImage[0]); } return Status; },请问接下来怎么写代码才能完成OpenGL纹理贴图呢?
完成 OpenGL 纹理贴图需要在绘制图形时启用纹理功能并设置纹理坐标,在片元着色器中使用纹理坐标获取纹理颜色。
下面是一个简单的例子,假设你已经有一个能够绘制一个矩形的程序:
```
void drawRect()
{
// 定义矩形的四个顶点坐标和纹理坐标
GLfloat vertices[] = {
// 顶点坐标 纹理坐标
-0.5f, 0.5f, 0.0f, 0.0f, 1.0f, // 左上角
0.5f, 0.5f, 0.0f, 1.0f, 1.0f, // 右上角
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, // 右下角
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f // 左下角
};
// 定义矩形的索引
GLuint indices[] = {
0, 1, 2,
0, 2, 3
};
// 创建 VBO 和 EBO 并绑定数据
GLuint VBO, EBO;
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// 设置顶点属性指针
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (void*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
// 创建纹理并绑定纹理数据
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
int width, height, nrChannels;
stbi_set_flip_vertically_on_load(true); // 加载图片时翻转 y 轴
unsigned char* data = stbi_load("rose.bmp", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(data);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
// 绘制矩形
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
// 解除绑定
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0);
}
```
在这个例子中,我们先定义了矩形的四个顶点坐标和纹理坐标,然后创建了 VBO 和 EBO 并绑定了数据。接着设置了顶点属性指针,使得顶点着色器能够正确地获取顶点数据。然后创建了纹理并绑定纹理数据,最后在绘制矩形时启用纹理功能并设置纹理坐标。
注意,在这个例子中我们使用了 stb_image 库来加载纹理文件,需要提前下载和安装。另外,这个例子中的顶点着色器和片元着色器都比较简单,只是简单地将顶点坐标和纹理坐标传递给片元着色器,并在片元着色器中使用纹理坐标获取纹理颜色。你可以根据实际情况进行修改。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)