2013-10-15 98 views
0

我試圖讓簡單的紋理,但繼承人出現了一個錯誤:錯誤:「my_texture」沒有指定類型

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; 
} 
+0

錯誤是否指定它所在的行? – Paddyd

+0

@Paddyd我的道歉,我更新後 – DanilGholtsman

+1

只是FYI:您正在嘗試讀取'.bmp' DIB文件,但不要正確解析它。 DIB文件具有必須處理的標題;它也會告訴你圖像的大小。不要盲目地讀入二進制文件! – datenwolf

回答

6

這兩行出現在函數之外。你只能在那裏聲明變量,你不能把語句直接放在這個範圍內。請嘗試:

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

哦,似乎你的答案有幫助!謝謝! – DanilGholtsman