Thread: Simple OpenGL question

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    208

    Simple OpenGL question

    Hey guys,

    So I am using openGL to 3D plot some particle positions but I need to tag some particles blue and some red based on a flag read in from my data files.

    Here's what I attempted to do to no avail.

    Code:
    	  for (k=0;k<7031;k++)
    	  {
    		  if(color[k] == 1){glColor4f(0.0,0.0,1.0,0.6);}
    		  else{glColor4f(1.0,0.0,0.0,0.6);}
    		  glTranslatef(ox[k],oy[k],oz[k]);
    		  glutSolidSphere(0.1, 10, 10);
    		  glTranslatef(-ox[k],-oy[k],-oz[k]);
    	  }
    Anyone have any clue where I am going wrong. I presume it's something simple like clearing a buffer or something.

    Thanks,
    Paddon
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by kas2002 View Post
    Anyone have any clue where I am going wrong. I presume it's something simple like clearing a buffer or something.
    Nope. AFAIK that code is fine. If I do this:

    Code:
        glColor4f(1.0f,0.0f,0.0f,0.6f);
        glutSolidSphere(1.0f, 10, 10);
        glTranslatef(1.0f,0.5f,0.0f); 
        glColor4f(0.0f,1.0f,0.0f,0.6f);
        glutSolidSphere(1.0f, 10, 10);
    Altho the "0.6" alpha bit is irrelevant, I do see:
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    How exactly are things not working? Are all particles showing up in red, or all particles showing up in blue, or do they show up in a totally different color? Do the particles show up at all?

    What type is the 'color[]' array? Is it a bool or an integer type? If it is a floating point type (e.g. float or double) then you may only see red particles.

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    208
    I figured it out. I was setting the color and the material and I think only the material properties were being applied...

    This works, however, the alpha compoent of mat does nothing. I would like to be able to set the transparency of the particles as well. How can I make it so that I can set the translucency. Is it just a different argument to glMaterial then GL_AMBIENT_AND_DIFFUSE?

    Code:
     
    
    	  for (k=0;k<NUM_PARTICLES;k++){
    		  
    		  if(color[k] == 1 && blueswitch == 1){
    			  mat[0]=0.0; 
    			  mat[1]=0.0; 
    			  mat[2]=1.0; 
    			  mat[3]=0.6;
    			  glMaterialfv(GL_FRONT, GL_AMBIANT_AND_DIFFUSE, mat);
    			  glTranslatef(ox[k],oy[k],oz[k]);
    			  glutSolidSphere(0.2,10,10);
    			  glTranslatef(-ox[k],-oy[k],-oz[k]);
    		  }
    		  if(color[k] == 0 && redswitch == 1){
    			  mat[0]=1.0; 
    			  mat[1]=0.0; 
    			  mat[2]=0.0; 
    			  mat[3]=0.6;
    			  glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat);
    			  glTranslatef(ox[k],oy[k],oz[k]);
    			  glutSolidSphere(0.2,10,10);
    			  glTranslatef(-ox[k],-oy[k],-oz[k]);
    		  }
    		  
    	  }
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by kas2002 View Post
    I figured it out.
    No dice. That is way too long winded, meaning you are covering up some misunderstanding and/or mistake. Here is the entire program I used to create that read and green hydrogen nucleus*:
    Code:
    #include <GL/gl.h>
    #include <GL/glu.h>
    #include <GL/glut.h>
    #include <GL/glext.h>
    
    void change (int w, int h); 
    void init(float R, float G, float B); 
    void scene();
     
    int main(int argc, char *argv[]) {
    	glutInit(&argc,argv);
    	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
            glutInitWindowSize(1200,800);
    	glutCreateWindow("openGL test");
    	glutReshapeFunc(change); /* must be AFTER CreateWindow... */
    	glutDisplayFunc(scene);
    	
    	init(0.0,0.0,1.0);
    
    	glutMainLoop();
    }	
    
    void change (int w, int h) {
    	float aspect=(float)w/(float)h;
    	glViewport(0, 0, w, h);
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
    	gluPerspective(50.0f,aspect,1.0f,500.0f);
    	glMatrixMode(GL_MODELVIEW);
    	glLoadIdentity();
    }
    
    void init(float R, float G, float B) {
    	glEnable(GL_DEPTH_TEST);
    	glClearColor(R,G,B,1.0f);
    }
    	
    void scene() {
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    	glColor4f(1.0f,0.0f,0.0f,0.6f);
    	glutSolidSphere(1.0f, 10, 10);
    	glTranslatef(1.0f,0.5f,0.0f); 
    	glColor4f(0.0f,1.0f,0.0f,0.6f);
    	glutSolidSphere(1.0f, 10, 10);
    	gluLookAt(0.0f,0.0f,50.0f,0.0f,0.0f,0.0f,0.0f,1.0f,0.0f);
    	glutSwapBuffers();
    }
    Even that could get trimmed slightly, but close enough. Notice no glMaterial call at all; I don't think you need that unless you are using lighting with normals and/or textures. I would recommend you start small like this and build up from there rather than trying something more complicated without having much of a practical, applied understanding of OGL to work with.

    I'm also sure that creating transparency is not just as simple as using the alpha values. This is going to take you some time and research...

    *at least that's what it looks like to me
    Last edited by MK27; 06-30-2009 at 09:05 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by MK27 View Post
    hydrogen nucleus
    Since when do hydrogen nuclei contain two baryons?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question about the C programming tutorial #1
    By elsheepo in forum C Programming
    Replies: 13
    Last Post: 01-19-2009, 08:59 PM
  2. OpenGL Question
    By Halo3Master in forum Game Programming
    Replies: 16
    Last Post: 10-02-2007, 02:40 AM
  3. OpenGL question
    By sand_man in forum Game Programming
    Replies: 4
    Last Post: 11-08-2004, 07:05 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. OpenGL question
    By kas2002 in forum Game Programming
    Replies: 16
    Last Post: 08-02-2002, 12:29 PM