我正在尝试使用Textures在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文件的代码是here
使用以下选项进行编译时: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}’
答案 0 :(得分:1)
您的num2.c
文件将gimp_image
声明为包含多个成员的结构,但在display
函数中,您创建的gimp_image
类型为GLuint
。此局部变量会影响您的全局变量,因此当您尝试访问gimp_image.bytes_per_pixel
时,它会查找局部变量(整数)而不是全局变量。由于整数没有成员,因此会出错。