c++ - 错误 : 'my_texture' does not name a type

标签 c++ opengl textures glut

我试图制作简单的纹理,但出现了错误:

error: 'my_texture' does not name a type

这里是它出现的地方(紧接在 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;
}

最佳答案

这两行出现在函数之外。你只能在那里声明变量,你不能直接在这个范围内放置语句。尝试:

GLuint my_texture = LoadTexture( "grass.bmp" );

关于c++ - 错误 : 'my_texture' does not name a type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19384516/

相关文章:

iphone - 异步纹理加载 iPhone OpenGL ES 2

ios - 在 iOS 中创建 RGB CVOpenGLESTexture

c++ - VertexBuffer 混合顶点

php - 无法从套接字读取(挂起)

c++ - 如果 catch 不执行任何操作,编译器优化会删除 try/catch block 吗?

opengl - 学习游戏开发,有什么书推荐吗?

java - 如何实现 FPS 相机?

windows - 无法在 Netbeans(Win 和 cygwin)中运行 OpenGL 代码,未设置显示

ios - ASyncDisplayKit - 一个容器中的多个节点

c++ - 模板化类构造函数的模板实例化