OpenGL partial transparency

Started by
1 comment, last by madsravn 17 years, 1 month ago
I've made a simple OpenGL with some help from the nehe tutorials. But when I look at the triangle, it has some kinda partial transparency, but I do not recall that any of the functions I've used, will activate this kinda behaviour... Could anyone help me? Here's my code #include <windows.h> #include <iostream> #include<GL/glut.h> // #pragma comment (lib, "opengl32.lib") /* link with Microsoft OpenGL lib */ // #pragma comment (lib, "glu32.lib") /* link with OpenGL Utility lib */ // #pragma comment (lib, "glut32.lib") /* link with Win32 GLUT lib */ using namespace std; float angle = 0.0; float factor = 0.0; float diff = 0.010; float cblue = 0.0; float cgreen = 1.0; float ups = 0.005; void changeSize(int w, int h) { if(h == 0) h = 1; float ratio = 1.0* w / h; glMatrixMode(GL_PROJECTION); glLoadIdentity(); glViewport(0, 0, w, h); gluPerspective(45,ratio,1,1000); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0,4.0,8.0, 0.0,0.0,-1.0, 0.0f,1.0f,0.0f); } void processNormalKeys(unsigned char key, int x, int y) { if (key == 27) exit(0); } void renderScene(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if (factor>4) diff=-0.010; if (factor<2) diff=0.010; if (cgreen>0.95) ups=-0.005; if (cgreen<0.05) ups=0.005; glPushMatrix(); glRotatef(angle,0.0,1.0,0.0); glColor3f(0.0f,cgreen,cblue); glBegin(GL_TRIANGLES); glColor3f(1.0f,0.0f,0.0f); glVertex3f( 0.0f, 1.0f, 0.0f); glColor3f(0.0f,1.0f,0.0f); glVertex3f(-1.0f,-1.0f, 1.0f); glColor3f(0.0f,0.0f,1.0f); glVertex3f( 1.0f,-1.0f, 1.0f); glColor3f(1.0f,0.0f,0.0f); glVertex3f( 0.0f, 1.0f, 0.0f); glColor3f(0.0f,0.0f,1.0f); glVertex3f( 1.0f,-1.0f, 1.0f); glColor3f(0.0f,1.0f,0.0f); glVertex3f( 1.0f,-1.0f, -1.0f); glColor3f(1.0f,0.0f,0.0f); glVertex3f( 0.0f, 1.0f, 0.0f); glColor3f(0.0f,1.0f,0.0f); glVertex3f( 1.0f,-1.0f, -1.0f); glColor3f(0.0f,0.0f,1.0f); glVertex3f(-1.0f,-1.0f, -1.0f); glColor3f(1.0f,0.0f,0.0f); glVertex3f( 0.0f, 1.0f, 0.0f); glColor3f(0.0f,0.0f,1.0f); glVertex3f(-1.0f,-1.0f,-1.0f); glColor3f(0.0f,1.0f,0.0f); glVertex3f(-1.0f,-1.0f, 1.0f); glEnd(); glPopMatrix(); angle++; factor+=diff; cgreen+=ups; cblue+=-ups; glutSwapBuffers(); Sleep(10); } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB); glutInitWindowPosition(100,100); glutInitWindowSize(320,320); glutCreateWindow("Mads er nice ;)"); glutDisplayFunc(renderScene); glutIdleFunc(renderScene); glutReshapeFunc(changeSize); glutKeyboardFunc(processNormalKeys); glutMainLoop(); return 0; } and screenshot is at http://fb.meganice.dk/example.jpg . It is only from some sides you can see through the triangle. Thanks
Advertisement
Since you are running in perspective mode, you need to set depth testing on so OpenGL can properly render things in 3D space. Add

glEnable(GL_DEPTH_TEST);

after the glClear call in the renderScene() routine.

Also, change angle++ to angle+=0.03f; and remove the Sleep(10). That sleep command is killing your framerate and increases the chances of the rotation being less smooth that it should.

hth
F451
That just did the trick... Thanks a bunch ;)

This topic is closed to new replies.

Advertisement