Question

I am stuck in this texturing problem. I have a created a quad as the ground and I want to fill its texture with a grass image. But, when I try to set MultiTexCoord2f values, I get Segmentation Fault error. I couldn't find the reason. What am I doing wrong here?

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;
}
Was it helpful?

Solution

In glBatch.Begin() function, you should tell the function the number of textures you want to use. The third parameter here is the number of textures.

OTHER TIPS

I don't know why but changing that part to this solved the problem;

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top