0

我正在尝试使用纹理在 openGL 中插入图像。我正在使用 Ubuntu Linux。这是我的主要代码:

#include <iostream>
#include <GL/gl.h>
#include <GL/glut.h>
#include <math.h>
#include "Images/num2.c"

using namespace std;

void display() {
glClear (GL_COLOR_BUFFER_BIT);
/* create the image variable */
GLuint gimp_image;

/* assign it a reference. You can use any number */
glGenTextures(1, &gimp_image);

/* load the image into memory. Replace gimp_image with whatever the array is called in the .c file */
gluBuild2DMipmaps(GL_TEXTURE_2D, gimp_image.bytes_per_pixel, gimp_image.width, gimp_image.height, GL_RGBA, GL_UNSIGNED_BYTE, gimp_image.pixel_data);

/* enable texturing. If you don't do this, you won't get any image displayed */
glEnable(GL_TEXTURE_2D);

/* draw the texture to the screen, on a white box from (0,0) to (1, 1). Other shapes may be used. */
glColor3f(1.0, 1.0, 1.0);

/* you need to put a glTexCoord and glVertex call , one after the other, for each point */
glBegin(GL_QUADS);
glTexCoord2d(0.0, 1.0); glVertex2d(0.0, 0.0);
glTexCoord2d(0.0, 0.0); glVertex2d(0.0, 1.0);
glTexCoord2d(1.0, 0.0); glVertex2d(1.0, 1.0);
glTexCoord2d(1.0, 1.0); glVertex2d(1.0, 0.0);
glEnd();

/* clean up */
glDisable(GL_TEXTURE_2D);
glDeleteTextures(1, &gimp_image);
glFlush();
 }


 void init (void) {
/*  select clearing (background) color       */
    glClearColor (1.0, 1.0, 1.0, 0.0);

/*  initialize viewing values  */
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize (500, 500);
    glutInitWindowPosition (400, 300);
    glutCreateWindow ("MineSweeper");
    init ();

    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}

我使用的 num2.c 文件的代码在这里

在使用以下选项进行编译时:g++ temp.cpp -lGL -lGLU -lglut我收到以下错误:

temp.cpp:23:46: error: request for member ‘bytes_per_pixel’ in ‘gimp_image’, which is   of non-class type ‘GLuint {aka unsigned int}’
temp.cpp:23:74: error: request for member ‘width’ in ‘gimp_image’, which is of non-class type ‘GLuint {aka unsigned int}’
temp.cpp:23:92: error: request for member ‘height’ in ‘gimp_image’, which is of non-class type ‘GLuint {aka unsigned int}’
temp.cpp:23:138: error: request for member ‘pixel_data’ in ‘gimp_image’, which is of non-class type ‘GLuint {aka unsigned int}’
4

1 回答 1

1

您的num2.c文件声明gimp_image为具有多个成员的结构,但在您的display函数中,您创建了一个gimp_imagetype GLuint。此局部变量会隐藏您的全局变量,因此当您尝试访问 时gimp_image.bytes_per_pixel,它会查看局部变量(整数)而不是全局变量。由于整数没有成员,它会给你一个错误。

于 2013-09-26T03:11:21.500 回答