Thread: OpenGL question

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    2

    OpenGL question

    First, lets get the problem out of the way.

    Code:
    Draw an octahedron [eight triangles] in space with corners at (+/-1.0, 0.0, +/-1.0) and (0.0, +/-1.0, 0.0). 
    Use a 3D perspective frame (viewing volume) with boundaries (+/-5.0, +/-5.0, +/-5.0). 
    Temporarily assign each face different color, display and rotate around the y-axis.
    Now, I am new to OpenGL, and really, new to C++ all together, so I am learning at a slow pace. Disclaimer aside, I am still tweaking the octahedron because it isn't coming out the way I am expecting and I still have to figure out what I am missing, but that is not the issue right now. You will see that I am using an old program I did which turned out to be a simple box with all its sides, and a cut open area revealing the inner sides as well. I have removed the code I didn't feel needed to be used in this assignment and have tried moving on, while hitting some roadblocks.

    The main issue that I am having, is that I have no idea how I am supposed to be able to display the octahedron, and have it rotate around the y-axis. I can't really conceive anyway of how I am going to do this nor do I truly have the knowledge of going about doing it. Any helpful tips or hints would be greatly appreciated. I will show you what I have so far (the busted octahedron as of now).

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <GL/glut.h>
    
    typedef float Point2f [2];
    
    GLfloat v [6][3] = {{-1.0, 0.0, -1.0}, {1.0, 0.0, -1.0},
    				{-1.0, 0.0, 1.0}, {1.0, 0.0, 1.0},
    				{0.0, 1.0, 0.0}, {0.0, -1.0, 0.0}};
    
    GLfloat colors [6][3] = {{0.0, 0.0, 0.0}, {1.0, 0.0, 0.0}, 
    					{0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}, 
    					{1.0, 0.0, 1.0}, {0.0, 1.0, 1.0}};
    
    void side (GLfloat * a, GLfloat * b, GLfloat * c, GLfloat * d) {
    	glVertex3fv (a);
    	glVertex3fv (b);
    	glVertex3fv (c);
    //	glVertex3fv (d);
    }
    
    void octo (GLfloat * a, GLfloat * b, GLfloat * c, GLfloat * d,
    		   GLfloat * e, GLfloat * f, GLfloat * g, GLfloat * h) {
    	glColor3fv (colors [0]);
    	side (a, b, d, c);
    	glColor3fv (colors [1]);
    	side (c, d, f, e);	
    	glColor3fv (colors [2]);
    	side (e, f, h, g);
    	glColor3fv (colors [3]);
    	side (g, h, b, a);
    	glColor3fv (colors [4]);
    	side (a, c, e, g);
    	glColor3fv (colors [5]);
    	side (b, d, f, h);
    }
    
    void display ( ) {		// need one such function per callback method
    	char * name = "octahedron";
    
    	glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    	
    	glPushMatrix ( );
    	glBegin (GL_TRIANGLE_STRIP);
    		octo (v [0], v [1], v [2], v [3], v [4], v[5], v[6], v[7]);
    	glEnd ( );
    	glPopMatrix ( );
    
    	glFlush ( );
    }
    
    void myReshape (int w, int h) {
    	GLfloat aspect;
    
    	glViewport (0, 0, w, h);
    	glMatrixMode (GL_PROJECTION);
    	glLoadIdentity ( );
    	aspect = (GLfloat) h / (GLfloat) w;
    	glOrtho (-5.0, 5.0, -5.0, 5.0, -5.0, 5.0);
    
    	gluPerspective (10.0, aspect, 10.0, -10.0);							
    
    	gluLookAt (5.0, 5.0, 10.0, 0.5, 1.0, 1.0, 0.0, 1.0, 0.0);			
    	glMatrixMode (GL_MODELVIEW);
    	glutPostRedisplay ( );
    }
    
    int main (int argc, char ** argv) {
    
    	/* 3D initialization stuff */
    	glutInit (&argc, argv);
    	glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);	
    	glutInitWindowSize (500, 500);
    	glutInitWindowPosition (100,100);
    	glutCreateWindow ("Octahedron");
    	glutReshapeFunc (myReshape);
    	glutDisplayFunc (display);		/* display callback invoked when window opened */
    	glEnable (GL_DEPTH_TEST);
    	glClearColor (1.0, 1.0, 1.0, 1.0);	/* white background */
    
    	glutMainLoop ( );
    	return 0;
    }
    Thanks again.

  2. #2

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    2
    thanks for this site, some very informative stuff

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    If you are still stuck after going through this, you may want to go ask your question at an OpenGL forum (OpenGL.org Discussion and Help Forums - Powered by UBB.threads™ is an example).

  5. #5
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Have a look at glRotatef() for rotation. You'll need to keep track of a rotation amount and increment it before each rendering.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGL Question
    By Halo3Master in forum Game Programming
    Replies: 16
    Last Post: 10-02-2007, 02:40 AM
  2. Opengl question
    By Shadowwoelf in forum Windows Programming
    Replies: 3
    Last Post: 06-03-2007, 01:21 PM
  3. OpenGL Camera Question
    By Astra in forum Game Programming
    Replies: 2
    Last Post: 03-25-2007, 01:53 PM
  4. OpenGL Question
    By Shamino in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2005, 02:35 PM
  5. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM

Tags for this Thread