OpenGL纹理四边形

时间:2014-04-05 12:23:03

标签: opengl texture2d quad

我陷入了这个纹理问题。我创建了一个四边形作为地面,我想用草图填充其纹理。但是,当我尝试设置MultiTexCoord2f值时,我得到Segmentation Fault错误。我无法找到原因。我在这里做错了什么?

SetupRC(){
    floorBatch.Begin(GL_QUADS, 4);
    floorBatch.MultiTexCoord2f(0, 0.0f, 0.0f);
    floorBatch.Vertex3f(-50.0f, 0.0f, -50.0f);


    floorBatch.MultiTexCoord2f(0, 1.0f, 0.0f);
    floorBatch.Vertex3f(50.0f, 0.0f, -50.0f);


    floorBatch.MultiTexCoord2f(0, 0.0f, 1.0f);
    floorBatch.Vertex3f(-50.0f, 0.0f, 50.0f);


    floorBatch.MultiTexCoord2f(0, 1.0f, 1.0f);
    floorBatch.Vertex3f(50.0f, 0.0f, 50.0f);
    floorBatch.End();    

    glGenTextures(1, textures);

// Grass floor
    pBytes = gltReadTGABits("Grass.tga", &nWidth, &nHeight, &nComponents, &format);
    glBindTexture(GL_TEXTURE_2D, textures[0]);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexImage2D(GL_TEXTURE_2D,0,nComponents,nWidth, nHeight, 0,
    format, GL_UNSIGNED_BYTE, pBytes);
    glGenerateMipmap(GL_TEXTURE_2D);

    free(pBytes);
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
    RenderScene(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    static GLfloat vFloorColor[] = { 0.0f, 1.0f, 0.0f, 1.0f};
    M3DMatrix44f mCamera;
    cameraFrame.GetCameraMatrix(mCamera);
    mModelview.PushMatrix(mCamera);
    mModelview.Translate( 0.0f, -0.3f, -2.5f);
    mModelview.PushMatrix();
    mModelview.Translate( 0.0f, -0.3f, 0.0f);
    mModelview.PushMatrix();
    glBindTexture(GL_TEXTURE_2D, textures[0]);
    shaderManager.UseStockShader(GLT_SHADER_TEXTURE_REPLACE, transformPipeline.GetModelViewProjectionMatrix(), vFloorColor, 0);
    floorBatch.Draw();    
    mModelview.PopMatrix();
    mModelview.PopMatrix();
    mModelview.PushMatrix();
}
main()
{
            gltSetWorkingDirectory(argv[0]); // 

        glutInit(&argc, argv); //opengl initialization
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL); //display mode set
        glutInitWindowSize(800, 600); //window size
        glutCreateWindow("Neighbourhood"); // title ile pencereyi yarattik
        glutReshapeFunc(ChangeSize); // pencere boyutu degisince cagrilan fonksiyonun adi
        glutDisplayFunc(RenderScene); // her bir frame icin cagrilan fonksiyonun adi
            glutSpecialFunc(SpecialKeys);

// Add menu entries to change filter
        glutCreateMenu(ProcessMenu);
        glutAddMenuEntry("GL_NEAREST",0);
        glutAddMenuEntry("GL_LINEAR",1);
        glutAddMenuEntry("GL_NEAREST_MIPMAP_NEAREST",2);
        glutAddMenuEntry("GL_NEAREST_MIPMAP_LINEAR", 3);
        glutAddMenuEntry("GL_LINEAR_MIPMAP_NEAREST", 4);
        glutAddMenuEntry("GL_LINEAR_MIPMAP_LINEAR", 5);

        glutAttachMenu(GLUT_RIGHT_BUTTON);

        GLenum err = glewInit(); // bu ne a.q.
        if (GLEW_OK != err) {
            fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
            return 1;
            }

        SetupRC(); // opengle ozel kendimizce ayarlari initialize edecegimiz fonksiyon

        glutMainLoop(); // opengl calisiyor

        ShutdownRC();
        return 0;
}

2 个答案:

答案 0 :(得分:1)

在glBatch.Begin()函数中,您应该告诉函数您想要使用的纹理数量。这里的第三个参数是纹理数。

答案 1 :(得分:0)

我不知道为什么,但改变这一部分解决了这个问题;

floorBatch.Begin(GL_TRIANGLE_STRIP, 4, 2);

floorBatch.MultiTexCoord2f(0, 0.0f, 0.0f);
floorBatch.Vertex3f(-50.0f, 0.0f, -50.0f);

floorBatch.MultiTexCoord2f(0 , 1.0f, 0.0f);
floorBatch.Vertex3f(50.0f, 0.0f, -50.0f);

floorBatch.MultiTexCoord2f(0 , 0.0f, 1.0f);
floorBatch.Vertex3f(-50.0f, 0.0f, 50.0f);

floorBatch.MultiTexCoord2f(0 , 1.0f, 1.0f);
floorBatch.Vertex3f(50.0f, 0.0f, 50.0f);
floorBatch.End();