在opengl中绘制一个可穿透的对象

时间:2013-11-20 06:44:13

标签: c++ opengl graphics glut

如何使用opengl绘制可穿透的立方体?

#include <GL/glut.h>  // GLUT, include glu.h and gl.h

double rotate_y; 
double rotate_x;
double zoom;

void solidSphere(double r)
{

    glutSolidSphere(r, 50, 50);
}

float ALPHA = 0.6;
float trans = 0.1;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);

    glDisable(GL_TEXTURE_2D);
    glLoadIdentity();
    glTranslatef(-2.2f, 0.31f, -7.0f);
    glScalef(1.0, 1.0, 1.0);
    glRotatef(rotate_x, 1.0, 0.0, 0.0 );
    glRotatef(rotate_y, 0.0, 1.0, 0.0 );
    glColor4f(1,0,0, ALPHA);
    glutSolidCube(2);

    glLoadIdentity();
    glTranslatef(2.2f - trans, 0.31f, -7.0f);
    glScalef(1.0, 1.0, 1.0);
    glColor3f(0,1,0);
    glutSolidCube(1);




    glutSwapBuffers();
}

// ----------------------------------------------------------
// specialKeys() Callback Function
// ----------------------------------------------------------
void specialKeys( int key, int x, int y ) {

    //  Right arrow - increase rotation by 5 degree
    if (key == GLUT_KEY_RIGHT)
    trans -= 0.1;

    //  Left arrow - decrease rotation by 5 degree
    else if (key == GLUT_KEY_LEFT)
    trans += 0.1;

    else if (key == GLUT_KEY_UP)
    rotate_x += 5;

    else if (key == GLUT_KEY_DOWN)
    rotate_x -= 5;

    else if (key == GLUT_KEY_INSERT)
        zoom *= (1.0 + 0.01);
    else if (key == GLUT_KEY_END)
        zoom *= (1.0 - 0.01);

    //  Request display update
    glutPostRedisplay();

}

void reshape(GLsizei width, GLsizei height) 
{
    if (height == 0)
    {
        height = 1;
    }

    GLfloat aspect = (GLfloat)width / (GLfloat)height; 
    glViewport(0, 0, width, height);    
    glOrtho(0.0, width, 0.0, height, 0, 50000);    
    glMatrixMode(GL_PROJECTION);    
    glLoadIdentity();
    gluPerspective(90.0f, aspect, 0.1f, 100.0f);
}

void initGL() 
{
    GLfloat sun_direction[] = { 0.0, 2.0, -1.0, 1.0 };
    GLfloat sun_intensity[] = { 0.7, 0.7, 0.7, 1.0 };
    GLfloat ambient_intensity[] = { 0.3, 0.3, 0.3, 1.0 };

    glClearColor(0.1, 0.39, 0.88, 1.0);
    glColor3f(1.0, 1.0, 1.0);

    glEnable (GL_BLEND); 
    glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glShadeModel(GL_SMOOTH);
    glEnable(GL_LIGHTING);
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient_intensity);

    glEnable(GL_LIGHT0);
    glLightfv(GL_LIGHT0, GL_POSITION, sun_direction);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, sun_intensity);

    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluPerspective(90.0f, 2.0, 0.1f, 100.0f);

}

int main(int argc, char** argv) 
{
    rotate_y = 0; 
    rotate_x = 0;
    zoom = 1.0;

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE| GLUT_RGB | GLUT_DEPTH);    
    glutInitWindowSize(640, 480);
    glutInitWindowPosition(50, 50);
    glutCreateWindow("cube and sphere");
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutSpecialFunc(specialKeys);
    initGL();
    glutMainLoop();
    return 0;
}

此代码将绘制两个立方体,一个部分透明,一个完全不透明 通过左右箭头键,您可以移动绿色立方体。 我需要当绿色立方体进入红色立方体(透明)时,立方体里面的部分也是可见的

可行吗?

0 个答案:

没有答案