Sign in to follow this  
Fennekin.hnnaik

My cubes faces see somewhat improper ,please tell solution.

Recommended Posts

i made this script to get a textured cube,but it returns what i have shown in picture.

 

#include "SOIL.h"
#include <gl/glut.h>
GLuint wall;

void Tex(int x,int y,GLuint tex){
    glBindTexture(GL_TEXTURE_2D,tex);
    glBegin(GL_QUADS);
        glTexCoord2f(0,0),glVertex2d(x,y);
        glTexCoord2f(1,0),glVertex2d(x+40,y);    
        glTexCoord2f(1,1),glVertex2d(x+40,y+40);    
        glTexCoord2f(0,1),glVertex2d(x,y+40);
    glEnd();    
}
void CubeTex(int l,int w,int h,GLuint texture){
    glBegin(GL_QUADS);
    //glColor3f(200,0,0);    
    GLfloat normals[6][3] ={
    {0,0,1},{1,0,0},{0,-1,0},{-1,0,0},{0,0,-1},{0,1,0}    
    };
glBindTexture(GL_TEXTURE,texture);
    
    glNormal3fv(&normals[0][0]);
    glTexCoord2f(0,0);
    glVertex3f(0,h,0);    glTexCoord2f(1,0);    glVertex3f(l,h,0);    glTexCoord2f(1,1);    glVertex3f(l,0,0);    glTexCoord2f(0,1);    glVertex3f(0,0,0);//F1
    
    glNormal3fv(&normals[1][0]);
    glTexCoord2f(0,0);
    glVertex3f(0,0,0);glTexCoord2f(1,0);    glVertex3f(0,0,w);glTexCoord2f(1,1);    glVertex3f(0,h,w);glTexCoord2f(0,1);    glVertex3f(0,h,0);//F2
    
    glNormal3fv(&normals[2][0]);
    glTexCoord2f(0,0);
    glVertex3f(0,0,0);glTexCoord2f(1,0);glVertex3f(l,0,0);glTexCoord2f(1,1);glVertex3f(l,0,w);glTexCoord2f(0,1);glVertex3f(0,0,w);//F3
    
    glNormal3fv(&normals[3][0]);
    glTexCoord2f(0,0);
    glVertex3f(l,h,0);glTexCoord2f(1,0);glVertex3f(l,h,w);glTexCoord2f(1,1);glVertex3f(l,0,w);glTexCoord2f(0,1); glVertex3f(l,0,0);//F4
        
    glNormal3fv(&normals[4][0]);
    glTexCoord2f(0,0);    
    glVertex3f(0,0,w);glTexCoord2f(1,0);glVertex3f(l,0,w);glTexCoord2f(1,1);glVertex3f(l,h,w);glTexCoord2f(0,1);glVertex3f(0,h,w);//F5
    
    glNormal3fv(&normals[5][0]);
    glTexCoord2f(0,0);
    glVertex3f(0,h,0);glTexCoord2f(1,0);glVertex3f(l,h,0);glTexCoord2f(1,1);glVertex3f(l,h,w);glTexCoord2f(0,1);glVertex3f(0,h,w);//F6
    glEnd();
}
void display(){
//    glLoadIdentity();
    glRotatef(0.05,0,1,0);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glColor3f(255,255,255);
//    Tex(20,20,wall);
    CubeTex(5,5,5,wall);
    glutSwapBuffers();    
}
void REanimation(){
    glutPostRedisplay();
}
int main(int argc, char** argv) {
    glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
    glutInitWindowSize(500,313);
    glutCreateWindow("Folder");
    glutDisplayFunc(display);
    glutIdleFunc(REanimation);
    
    glEnable(GL_STENCIL_TEST);
    glEnable(GL_DEPTH);
    glEnable(GL_TEXTURE_2D);
    wall=SOIL_load_OGL_texture("Wall.bmp",0,0,0);
    glCullFace(GL_FRONT_AND_BACK);
    glMatrixMode(GL_PROJECTION);
    gluPerspective(45,1.6,1,120);
    gluLookAt(20,30,20,2.5,2.5,2.5,0,1,0);
    glMatrixMode(GL_MODELVIEW);
    glutMainLoop();

    return 0;
}

Share this post


Link to post
Share on other sites

Backface culling issue.

You are calling

 

 


glCullFace(GL_FRONT_AND_BACK);

which tells GL to throw away all triangles (and you don't want this). But since culling is disabled by default, this call is meaningless.

What you need is

glEnable(GL_CULL_FACE);

Which enables culling. The default mode is GL_BACK, so once you enable culling you are ready to go.

 

[EDIT] - BTW, i see that depth test is disabled as well. If all you drawing is a single box, this will work, but for multiple objects you need depth-test to get correct visibility.

Edited by satanir

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this