Sign in to follow this  
Garra

Grid of spheres

Recommended Posts

having some issues drawing a 20x20 grid of white spheres, if anyone has some advice please share.
#include <GL\glut.h>


//set values for context menu
/*
#define MENU_INC_ALPHA	100
#define MENU_DEC_ALPHA	101
#define MENU_ENA_DEPTH	102
#define MENU_DIS_DEPTH	103
#define MENU_WHITE		104
#define MENU_RED		105
#define MENU_YELLOW		106
#define MENU_BLUE		107
#define MENU_GREEN		108
*/

struct point3d
{
	GLfloat x,y,z;
};

struct color4
{
	GLfloat r,g,b,a;
};

GLint i = 0;
GLint j = 0;

point3d rainGrid[20][20];
float winWidth, winHeight;

//i'm not really sure where to put this
//rainGrid[i][j].x = i;
//rainGrid[i][j].z = j;
//rainGrid[i][j].y = 0;


void init(void)
{
	glClearColor(0.0, 0.0, 0.0, 0.0);

	glEnable(GL_BLEND);//enable blend
	glEnable(GL_DEPTH_TEST);//enable depth
	
	//function for alpha
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	
	glShadeModel(GL_SMOOTH);

}

void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	glColor4f(1,1,1,1);

	for (int i = 0; i < 20; i++)
		for (int j = 0; j < 20; j++)
		{
			glPushMatrix();
				glTranslatef(rainGrid[i][j].x, rainGrid[i][j].y, rainGrid[i][j].z);				
				glutSolidSphere(2, 16, 16); //draw sphere
			glPopMatrix();
		}

glutSwapBuffers();

}
void reshape(int w, int h)
{
	winWidth = w;
	winHeight = h;

	glViewport(0, 0, (GLsizei) w, (GLsizei) h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	//gluLookAt(0.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}


void keyboard(unsigned char key, int x, int y)
{
	float aspect;

	switch (key) 
	{
	case 'd':

	
	break;
	case 'D':
	
	
	break;
	case 'y':	

	
	break;
	case 'o':

		
	break;
	case 'p':
			
	break;
	default:

	break;
	}
}


int main(int argc, char** argv)
{
	GLint myMenu = 0;
	GLint lightSub = 0;

	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); //say we are using RGBA and DEPTH
	glutInitWindowSize(900, 700);
	glutInitWindowPosition(100, 100);
	glutCreateWindow("blah");
	init();
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutKeyboardFunc(keyboard);

	//lightSub = glutCreateMenu(menuCallback);
	//glutAddMenuEntry("White", 104);
	//glutAddMenuEntry("Red", 105);
	//glutAddMenuEntry("Yellow", 106);
	//glutAddMenuEntry("Blue", 107);
	//glutAddMenuEntry("Green", 108);

	////creates and populates context menu
	//myMenu = glutCreateMenu(menuCallback);
	//glutAddMenuEntry("Increase Earth Alpha", 100);
	//glutAddMenuEntry("Decrease Earth Alpha", 101);
	//glutAddMenuEntry("Enable Depth Test", 102);
	//glutAddMenuEntry("Disable Depth Test", 103);
	//glutAddSubMenu("Light Color", lightSub); 

	//glutAttachMenu(GLUT_RIGHT_BUTTON); //assigns context menu to right mouse button

	glutMainLoop();
	return 0;

}

Share this post


Link to post
Share on other sites
Put the array initialization in the init function:

for (int i=0; i<20 ; i++)
for (int j=0; j<20 ; j++)
{
rainGrid[i][j].x = i;
rainGrid[i][j].z = j;
rainGrid[i][j].y = 0;
}

Share this post


Link to post
Share on other sites
alright thanks, another question i have is for some reason the glColor4f(1,1,1,1); is setting the entire window to white instead of setting the sphere color to white and the background staying black. I tried just moving it inside the push/pop but that ended with the same results. So, even if it is drawing the spheres everything is white so i can't see them.

Share this post


Link to post
Share on other sites
Did you forget to uncomment glulookat? Your view matrix is identity, and you're drawing a sphere at 0,0. You view is white probably because your camera is inside one of the spheres.

Share this post


Link to post
Share on other sites
I'm having a few more issues trying to make a rotating spotlight. I have it so there is a cone rotating and I'm trying to get a spotlight to rotate with it using a Idle() function. Below is the code if anyone can help.


#include <GL\glut.h>

struct point3d
{
GLfloat x,y,z;
};


GLfloat mat_emission[] = {1.0, 1.0, 0.0, 1.0};
GLfloat no_mat[] = { 0, 0, 0, 0 };
GLfloat noAmbient[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat redAmbient[] = {0.20, 0.0, 0.0, 1.0 };
GLfloat whiteDiffuse[] = {1.0, 1.0, 1.0, 1.0};
GLfloat greenDiffuse[] = {0.0, 1.0, 0.0, 1.0};
GLfloat blackDiffuse[] = {0.0, 0.0, 0.0, 1.0};
//GLfloat light_pos[4] = {0.0, 0.0, 0.0, 1.0}; //array to set light pos at origin
GLfloat mat_specular[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
GLfloat lmodel_ambient[] = { 0.0, 0.1, 0.0, 1.0 };

//spotlight
//float noAmbient[] = {0.0f, 0.0f, 0.2f, 1.0f};//low ambient light
GLfloat diffuse[] = {0.0f, 0.0f, 1.0f, 1.0f};
GLfloat position[] = {0.0f, 0.0f, 0.0f, 1.0f};


//light attenuation (default values used here : no attenuation with the distance)
GLfloat light_dir[4] = {1.0f, -20.0f, 0.0f, 0.0f};
GLfloat green_light[] = { 0.0, 1.0, 0.0, 1.0 }; //green
GLfloat direction[] = {0, 0, 1};

GLint i = 0;
GLint j = 0;
GLdouble spin = 0;

point3d rainGrid[20][20];


//float winWidth, winHeight;


void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT2);
glEnable(GL_BLEND);//enable blend
glEnable(GL_DEPTH_TEST);//enable depth
//glEnable(GL_COLOR_MATERIAL);

//function for alpha
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glShadeModel(GL_SMOOTH);

glLightfv(GL_LIGHT0, GL_AMBIENT, redAmbient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, greenDiffuse);
glLightfv(GL_LIGHT0, GL_POSITION, light_dir);
glLightfv(GL_LIGHT0, GL_SPECULAR, whiteDiffuse);

//spot light
//properties of the light
glLightfv(GL_LIGHT2, GL_AMBIENT, noAmbient);
glLightfv(GL_LIGHT2, GL_DIFFUSE, diffuse); //blue diffuse color
glLightfv(GL_LIGHT2, GL_SPECULAR, whiteDiffuse);
glLightfv(GL_LIGHT2, GL_POSITION, position);
glLightf(GL_LIGHT2, GL_CONSTANT_ATTENUATION, 1.0f);
glLightf(GL_LIGHT2, GL_LINEAR_ATTENUATION, 0.0f);
glLightf(GL_LIGHT2, GL_QUADRATIC_ATTENUATION, 0.0f);
glLightf(GL_LIGHT2, GL_SPOT_CUTOFF, 20.0);
glLightf(GL_LIGHT2, GL_SPOT_EXPONENT, 15.0f);


//spot direction
// glLightfv(GL_LIGHT2, GL_SPOT_DIRECTION, direction);
//angle of the cone light emitted by the spot : value between 0 to 180


}

void Idle()
{
spin+=5;

}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


for (int i = 0; i < 20; i++)
for (int j = 0; j < 20; j++)
{
rainGrid[i][j].x = i*2;
rainGrid[i][j].z = j*2;
rainGrid[i][j].y = 0;

glPushMatrix();
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);

//glColor4f(1,1,1,1);
glTranslatef(rainGrid[i][j].x*2, rainGrid[i][j].y, rainGrid[i][j].z);
glutSolidSphere(1, 16, 16); //draw sphere
glPopMatrix();
}

glPushMatrix();

glColor4f(0,1,0,1);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
//glutIdleFunc();
//glColor4f(0,0,1,1);
Idle();
glRotatef(spin,0,1.0,0);
glLightfv(GL_LIGHT2, GL_SPOT_DIRECTION, direction);
//glColor4f(1,0,0,1);
//glTranslatef(5,0,0);
glutSolidCone(3,5,15,15);
glPopMatrix();

glutPostRedisplay();
glutSwapBuffers();

}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70.0, (GLfloat) w/(GLfloat) h, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 50.0, 50.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

}

int main(int argc, char** argv)
{

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); //say we are using RGBA and DEPTH
glutInitWindowSize(900, 700);
glutInitWindowPosition(100, 100);
glutCreateWindow("blah");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
//glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;

}




[Edited by - Garra on March 9, 2010 2:39:28 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this