Thread: Help needed with opengl

  1. #1
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    Question Help needed with opengl

    Hello again, I need help from some opengl person around here. Well I'm using the GLut32.lib OpenGL32.lib GLu32.lib GLaux.lib libraries. My problem is, that one side comes in perfectly, but all of the others are somewhat transparent. Check it out for yourself, I've been working on this since yesturday, what do I do now?

    Code:
    #include <windows.h>
    #include <gl/glut.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    #include <gl\gl.h>               
    #include <gl\glu.h>               
    #include <gl\glaux.h>     
    
    int lp;
    
    int cran;
    
    static GLfloat rotatey = 0.0;
    
    float clvl[3][3]={{0.0,0.0,0.0},{0.0,0.0,0.0},{0.0,0.0,0.0}};
    
    void glutTimerFunc(int value)
    {
       rotatey = rotatey + 2.0;
       if (rotatey > 360.0)
       {
    		rotatey = rotatey - 360.0;
       }
       lp=0;
       while(lp!=3)
       {
    	    if(lp==3)
    		{
    			break;
    		}
    	    cran=rand()%3;
    		if(clvl[lp][cran]<1.0)
    		{
    			clvl[lp][cran]=clvl[lp][cran]+.1;
    		}
    		else
    		{
    			clvl[lp][cran]=0.0;
    		}
    		lp++;
       }
       glutPostRedisplay();
       glutTimerFunc(33,glutTimerFunc,1);
    }
    
    void RenderScene(void)
    {
    	glClear(GL_COLOR_BUFFER_BIT);
    	glPushMatrix();
    	gluLookAt(0.0, 0.0, -14.0, 0.0, 0.0, 0.0, 0.0, 0.1, 0.0);
    	
    	glRotatef(rotatey,0.0,0.5,0.0);
    	glBegin(GL_TRIANGLES);
    		//Front Side
    		glColor3f(1,0,0);
    		glVertex3f(0 ,5, 0);
    
    
    		glVertex3f(5,-5,0);
    
    
    		glVertex3f(-5,-5,0);
    
    		//Right Side
    		glColor3f(0,1,0);
    		glVertex3f(0,5,0);
    
    		glVertex3f(0,-5,-5);
    
    		glVertex3f(5,-5, 0);
    		//Left Side
    		glColor3f(0,0,1);
    		glVertex3f(0,5,0);
    
    		glVertex3f(-5,-5,0);
    
    		glVertex3f(0,-5,-5);
    	glEnd();
    	glFlush();
    	glPopMatrix();
    	glutSwapBuffers();
    }
    
    void init(void)
    {
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    	glShadeModel(GL_FLAT);
    }
    
    void reshape(int w, int h)
    {
    	glViewport(0, 0, (GLsizei) w, (GLsizei) h);
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
    	glFrustum (-1.0, 1.0, -0.5, 0.5, 1.0, 20.0);
    	glMatrixMode(GL_MODELVIEW);
    	glLoadIdentity();
    }
    
    void main(void)
    {
    	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    	glutCreateWindow("Multi Colors 3D");
    	glutDisplayFunc(RenderScene);
        glutReshapeFunc(reshape);
        glutTimerFunc(33, glutTimerFunc, 1);
    
    	init();
    	glutMainLoop();
    }

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    You're drawing the traingles in a static order, So sometimes it
    occurs that one triangle is drawn over another even though it's
    at the back of the traingle at that moment, The solution for this is
    either using a Depth Buffer or using Face Culling.

    Face Culling is the most speed efficient, Replace init(void) with
    this one:

    Code:
    void init(void)
    {
       glCullFace(GL_FRONT);
       glEnable(GL_CULL_FACE);
       glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
       glShadeModel(GL_FLAT);
    }
    Last edited by Travis Dane; 03-13-2003 at 10:24 AM.
    --

  3. #3
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    hm...

    Not really working, It worked for all of the sides, but now when it comes back to the front it is black space, I can't see through it and one of the other sides is still transparent.

    Code:
    void init(void)
    {
       glCullFace(GL_FRONT);
       glEnable(GL_CULL_FACE);
       glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
       glShadeModel(GL_SMOOTH);
    }

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Rename it to an .EXE and
    execute it and tell me what you see, You should see 3 triangle
    polygons forming a (weird) piramid shape. What side is
    transparant? There are only 3 polygons...
    --

  5. #5
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ...

    lol, sorry, i forgot to mention that I changed it back to my Gl_SMOOTH Color effect... here is the updated color affect.

    Code:
    		//Front Side
    		glColor3f(clvl[0][1],clvl[0][2],clvl[0][3]);
    		glVertex3f(0 ,5, 0);
    
    		glColor3f(clvl[1][1],clvl[1][2],clvl[1][3]);
    		glVertex3f(5,-5,0);
    
    		glColor3f(clvl[2][1],clvl[2][2],clvl[2][3]);
    		glVertex3f(-5,-5,0);
    
    		//Right Side
    		glColor3f(clvl[0][1],clvl[0][2],clvl[0][3]);
    		glVertex3f(0,5,0);
    
    		glColor3f(clvl[1][1],clvl[1][2],clvl[1][3]);
    		glVertex3f(5,-5, 0);
    
    		glColor3f(clvl[2][1],clvl[2][2],clvl[2][3]);
    		glVertex3f(0,-5,-5);
    		//Left Side
    		glColor3f(clvl[0][1],clvl[0][2],clvl[0][3]);
    		glVertex3f(0,5,0);
    
    		glColor3f(clvl[1][1],clvl[1][2],clvl[1][3]);
    		glVertex3f(-5,-5,0);
    
    		glColor3f(clvl[2][1],clvl[2][2],clvl[2][3]);
    		glVertex3f(0,-5,-5);
    EDIT: and it has the transparent problem.

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Okay, Repost your ENTIRE source and i'll have a look at it.
    --

  7. #7
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    code

    [/code]
    #include <windows.h>
    #include <gl/glut.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>

    #include <gl\gl.h>
    #include <gl\glu.h>
    #include <gl\glaux.h>

    int lp;

    int cran;

    static GLfloat rotatey = 0.0;

    float clvl[3][3]={{0.0,0.0,0.0},{0.0,0.0,0.0},{0.0,0.0,0.0}};

    void glutTimerFunc(int value)
    {
    rotatey = rotatey + 2.0;
    if (rotatey > 360.0)
    {
    rotatey = rotatey - 360.0;
    }
    lp=0;
    while(lp!=3)
    {
    if(lp==3)
    {
    break;
    }
    cran=rand()%3;
    if(clvl[lp][cran]<1.0)
    {
    clvl[lp][cran]=clvl[lp][cran]+.1;
    }
    else
    {
    clvl[lp][cran]=0.0;
    }
    lp++;
    }
    glutPostRedisplay();
    glutTimerFunc(33,glutTimerFunc,1);
    }

    void RenderScene(void)
    {
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
    gluLookAt(0.0, 0.0, -14.0, 0.0, 0.0, 0.0, 0.0, 0.1, 0.0);

    glRotatef(rotatey,0.0,0.5,0.0);
    glBegin(GL_POLYGON);
    //Front Side
    glColor3f(clvl[0][1],clvl[0][2],clvl[0][3]);
    glVertex3f(0 ,5, 0);

    glColor3f(clvl[1][1],clvl[1][2],clvl[1][3]);
    glVertex3f(5,-5,0);

    glColor3f(clvl[2][1],clvl[2][2],clvl[2][3]);
    glVertex3f(-5,-5,0);

    //Right Side
    glColor3f(clvl[0][1],clvl[0][2],clvl[0][3]);
    glVertex3f(0,5,0);

    glColor3f(clvl[1][1],clvl[1][2],clvl[1][3]);
    glVertex3f(5,-5, 0);

    glColor3f(clvl[2][1],clvl[2][2],clvl[2][3]);
    glVertex3f(0,-5,-5);
    //Left Side
    glColor3f(clvl[0][1],clvl[0][2],clvl[0][3]);
    glVertex3f(0,5,0);

    glColor3f(clvl[1][1],clvl[1][2],clvl[1][3]);
    glVertex3f(-5,-5,0);

    glColor3f(clvl[2][1],clvl[2][2],clvl[2][3]);
    glVertex3f(0,-5,-5);
    glEnd();
    glFlush();
    glPopMatrix();
    glutSwapBuffers();
    }

    void init(void)
    {
    glCullFace(GL_FRONT);
    glEnable(GL_CULL_FACE);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glShadeModel(GL_SMOOTH);
    }


    void reshape(int w, int h)
    {
    glViewport(0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum (-1.0, 1.0, -0.5, 0.5, 1.0, 20.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    }

    void main(void)
    {
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutCreateWindow("Multi Colors 3D");
    glutDisplayFunc(RenderScene);
    glutReshapeFunc(reshape);
    glutTimerFunc(33, glutTimerFunc, 1);

    init();
    glutMainLoop();
    }
    [/code]

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Okay, What Face Culling does is prevent polygons that are facing
    away from the user to be rendered, This way save time and
    polygons to render above eachother. However, For OpenGL
    to know when a polygon is facing away you must supply the
    Vertex coordinates in a counter-clock fasion (by default), Wich
    you din't do with your Triangles (You set it to render Polygons,
    I changed it because it can only render a single surface as
    Polygone not an entire model).

    I've just a Depth buffer instead wich is very easy to use, It
    bassically doesn't show Pixels that are drawn over another
    Pixel but have a higher Depth value (Further away), It isn't
    as fast as Culling because it still renders it internally but not
    in the final buffer.

    Your code.

    Code:
    #include <windows.h>
    #include <gl/glut.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    #include <gl\gl.h> 
    #include <gl\glu.h> 
    #include <gl\glaux.h> 
    
    int lp;
    
    int cran;
    
    static GLfloat rotatey = 0.0;
    
    float clvl[3][3]={{0.0,0.0,0.0},{0.0,0.0,0.0},{0.0,0.0,0.0}};
    
    void glutTimerFunc(int value)
    {
    	rotatey = rotatey + 2.0;
    	if (rotatey > 360.0)
    	{
    		rotatey = rotatey - 360.0;
    	}
    	lp=0;
    	while(lp!=3)
    	{
    		if(lp==3)
    		{
    			break;
    		}
    		cran=rand()%3;
    		if(clvl[lp][cran]<1.0)
    		{
    			clvl[lp][cran]=clvl[lp][cran]+.1;
    		}
    		else
    		{
    			clvl[lp][cran]=0.0;
    		}
    		lp++;
    	}
    	glutPostRedisplay();
    	glutTimerFunc(33,glutTimerFunc,1);
    }
    
    void RenderScene(void)
    {
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    	glPushMatrix();
    	gluLookAt(0.0, 0.0, -14.0, 0.0, 0.0, 0.0, 0.0, 0.1, 0.0);
    
    	glRotatef(rotatey,0.0,0.5,0.0);
    	
    	glBegin(GL_TRIANGLES);
    	
    		//Front Side
    		
    		glColor3f(clvl[0][1],clvl[0][2],clvl[0][3]);
    		glVertex3f(0,5,0);
    
    		glColor3f(clvl[1][1],clvl[1][2],clvl[1][3]);
    		glVertex3f(-5,-5,0);
    
    		glColor3f(clvl[2][1],clvl[2][2],clvl[2][3]);
    		glVertex3f(5,-5,0);
    
    		//Right Side
    		
    		glColor3f(clvl[0][1],clvl[0][2],clvl[0][3]);
    		glVertex3f(0,5,0);
    
    		glColor3f(clvl[1][1],clvl[1][2],clvl[1][3]);
    		glVertex3f(0,-5,-5);
    
    		glColor3f(clvl[2][1],clvl[2][2],clvl[2][3]);
    		glVertex3f(5,-5,0);
    		
    		//Left Side
    		
    		glColor3f(clvl[0][1],clvl[0][2],clvl[0][3]);
    		glVertex3f(0,5,0);
    
    		glColor3f(clvl[1][1],clvl[1][2],clvl[1][3]);
    		glVertex3f(0,-5,-5);
    
    		glColor3f(clvl[2][1],clvl[2][2],clvl[2][3]);
    		glVertex3f(-5,-5,0);
    	
    	glEnd();
    	glFlush();
    	glPopMatrix();
    	glutSwapBuffers();
    }
    
    void init(void)
    {
    	glEnable(GL_DEPTH_TEST);
    	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    	glShadeModel(GL_SMOOTH);
    }
    
    
    void reshape(int w, int h)
    {
    	glViewport(0, 0, (GLsizei) w, (GLsizei) h);
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
    	glFrustum (-1.0, 1.0, -0.5, 0.5, 1.0, 20.0);
    	glMatrixMode(GL_MODELVIEW);
    	glLoadIdentity();
    }
    
    void main(void)
    {
    	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    	glutCreateWindow("Multi Colors 3D");
    	glutDisplayFunc(RenderScene);
    	glutReshapeFunc(reshape);
    	glutTimerFunc(33, glutTimerFunc, 1);
    
    	init();
    	glutMainLoop();
    }
    --

  9. #9
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ...

    Thanks TD, that worked. So I guess in the future, if I plan on using 3d I'll have to use depth buffering. Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  2. Replies: 0
    Last Post: 12-08-2003, 11:40 PM
  3. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  4. OpenGL .dll vs video card dll
    By Silvercord in forum Game Programming
    Replies: 14
    Last Post: 02-12-2003, 07:57 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM