错误:“my_texture”没有命名类型
Posted
技术标签:
【中文标题】错误:“my_texture”没有命名类型【英文标题】:error: 'my_texture' does not name a type 【发布时间】:2013-10-15 14:59:31 【问题描述】:我试图制作简单的纹理,但出现了一个错误:
这里有它出现的地方(在LoadTexture
方法之后):
GLuint my_texture;
my_texture = LoadTexture( "grass.bmp" );
这是我的代码。怎么了?
#include <iostream>
#include <GL/gl.h>
#include <GL/glut.h>
#include <stdio.h> /* printf, scanf, NULL */
#include <stdlib.h> /* malloc, free, rand */
using namespace std;
float _angle = 0.5f;
GLuint LoadTexture( const char * filename )
GLuint texture;
int width, height;
unsigned char * data;
FILE * file;
file = fopen( filename, "rb" );
if ( file == NULL ) return 0;
width = 1024;
height = 512;
data = (unsigned char *)malloc( width * height * 3 );
fread( data, width * height * 3, 1, file );
fclose( file );
for(int i = 0; i < width * height ; ++i)
int index = i*3;
unsigned char B,R;
B = data[index];
R = data[index+2];
data[index] = R;
data[index+2] = B;
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT );
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,GL_RGB, GL_UNSIGNED_BYTE, data );
free( data );
return texture;
GLuint my_texture;
my_texture = LoadTexture( "grass.bmp" );
void render()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity ();
gluPerspective(90.0, 640.0f/480.0f, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity ();
gluLookAt(1, 1, 1, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
glPushMatrix();
glRotatef(_angle, 0.0f,0.0f,1.0f);
glColor3f(0.0f,0.7f,0.0f);
glBegin(GL_POLYGON);
glColor3f( 1.0, 0.0, 0.0 );
glVertex3f( -0.5, 0.5 , 0 );
glVertex3f( 0.5 , 0.5, 0 );
glVertex3f( 0.5, -0.5, 0 );
glVertex3f( -0.5, -0.5, 0 );
glEnd();
glPopMatrix();
_angle +=0.03f;
// check OpenGL error
GLenum err;
while ((err = glGetError()) != GL_NO_ERROR)
cerr << "OpenGL error: " << err << endl;
glutSwapBuffers();
glutPostRedisplay();
void init()
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(640, 480);
glutCreateWindow("test");
glClearColor(0.2,0.2,0.2,0.0);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glutDisplayFunc(render);
glutMainLoop();
int main(int argc, char * argv[])
glutInit(&argc, argv);
init();
return 0;
【问题讨论】:
错误是否指定了它在哪一行? @Paddyd 抱歉,我更新了帖子 仅供参考:您正在尝试读取.bmp
DIB 文件,但未正确解析它。 DIB 文件有一个必须处理的标头;它还告诉您图像的大小。不要只是盲目地读入二进制文件!
@datenwolf 哦,谢谢!我想这就是为什么某些.bmp
纹理没有正确显示的原因
【参考方案1】:
这两行出现在函数之外。您只能在此处声明变量,不能将语句直接放在此范围内。试试:
GLuint my_texture = LoadTexture( "grass.bmp" );
【讨论】:
以上是关于错误:“my_texture”没有命名类型的主要内容,如果未能解决你的问题,请参考以下文章