2 Replies - 1821 Views - Last Post: 12 August 2012 - 12:03 PM Rate Topic: -----

#1 dragstang86  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 28-September 09

OpenGl lighting effect problem

Posted 11 August 2012 - 11:12 AM

I am trying to learn all the basics of OpenGL and am working on a random scene using different functions. I am now trying to figure out how to put some sort of lighting effect to the circle (moon) to make it look more realistic and then possibly enable a lighting effect that shows some sort of light emitting from the moon. Right now everything I try just places a gray scale to all the objects in the scene. The code below has the lighting code commented out to show what the scene looks light before the light. What am I missing here? How do I isolate just the sphere vs. the entire scene and how do I get some sort of faded/textured effect on the moon? Thanks!

#include <GL/glut.h>

//This will vary depending upon your application
#include <stdlib.h>
#include<iostream>
#include<windows.h>
#include<math.h>
#include <GL/glut.h>



// Global Variable Section
// List all of your global variables, stuctures and other definition here
class screenPt;

// Window display size
GLsizei winWidth = 1600, winHeight = 1100;   


static float GL_PI = 3.1415926f;
class screenPt
{
    private:
       GLint x, y;

    public:
       /*  Default Constructor: initializes coordinate position to (0, 0).  */
       screenPt ( )  {
           x = y = 0;
       }
       void setCoords (GLint xCoordValue, GLint yCoordValue)  {
           x = xCoordValue;
           y = yCoordValue;
       }

       GLint getx ( ) const  {
           return x;
       }

       GLint gety ( ) const  {
           return y;
       }
       void incrementx ( )  {
           x++;
       }
       void decrementy ( )  {
           y--;
       }
};

// Initialize method
void init (void)
{
	// Get and display your OpenGL version
	const GLubyte *Vstr;
	Vstr = glGetString (GL_VERSION);
	fprintf(stderr, "Your OpenGL version is %s\n", Vstr);

	// White color window
   	glClearColor (0.0, 0.0, 0.0, 0.0);	
   
   // Projection on World Coordinates 
   	glMatrixMode (GL_PROJECTION);  

   GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
   GLfloat mat_shininess[] = { 50.0 };
   GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };

   glClearColor (0.0, 0.0, 0.0, 0.0);

   //glEnable(GL_LIGHTING);
   //glEnable(GL_LIGHT0);

	// Create light components
	//GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
	//GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8, 1.0f };
	//GLfloat specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };
	//GLfloat position[] = { -1.5f, 1.0f, -4.0f, 1.0f };

	// Assign created components to GL_LIGHT0
	//glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
	//glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
	//glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
	//glLightfv(GL_LIGHT0, GL_POSITION, position);
	
}


void displayFcn (void)
{
	 // Clear display window.
   glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
   glMatrixMode (GL_MODELVIEW);
   glLoadIdentity;
        
 
	//Draw Road
    glColor3f (0.2, 0.2, 0.2); 
	glBegin(GL_POLYGON);
		glVertex2f(-500,-1200);
		glVertex2f(900,-1200);
		glVertex2f(230, 600);
		glVertex2f(170, 600);
	glEnd();
	

	glColor3f (1.0, 1.0, 0.0);
	glBegin(GL_LINES);
		glVertex2f(180.0f, -1200.0f); // origin of the line
		glVertex2f(200.0f, 600.0f); // ending point of the line
	glEnd( );

	glBegin(GL_LINES);
		glVertex2f(220.0f, -1200.0f); // origin of the line
		glVertex2f(200.0f, 600.0f); // ending point of the line
	glEnd( );

	//Light Pole #1
	glColor3f (0.2, 0.2, 0.2); 
	glBegin(GL_POLYGON);
		glVertex2f(700, 0);
		glVertex2f(720, 0);
		glVertex2f(720, 650);
		glVertex2f(700, 650);
	glEnd();

	glColor3f (0.2, 0.2, 0.2); 
	glBegin(GL_POLYGON);
		glVertex2f(700, 650);
		glVertex2f(700, 640);
		glVertex2f(680, 640);
		glVertex2f(680, 650);
	glEnd();

	glColor3f (1.0, 1.0, 1.0); 
	glBegin(GL_POLYGON);
		glVertex2f(700, 640);
		glVertex2f(700, 630);
		glVertex2f(680, 630);
		glVertex2f(680, 640);
	glEnd();

	//Light Pole #2
	glColor3f (0.2, 0.2, 0.2); 
	glBegin(GL_POLYGON);
		glVertex2f(430, 400);
		glVertex2f(445, 400);
		glVertex2f(445, 750);
		glVertex2f(430, 750);
	glEnd();

	glColor3f (0.2, 0.2, 0.2); 
	glBegin(GL_POLYGON);
		glVertex2f(430, 750);
		glVertex2f(430, 740);
		glVertex2f(410, 740);
		glVertex2f(410, 750);
	glEnd();

	glColor3f (1.0, 1.0, 1.0); 
	glBegin(GL_POLYGON);
		glVertex2f(430, 740);
		glVertex2f(430, 730);
		glVertex2f(410, 730);
		glVertex2f(410, 740);
	glEnd();

		//Light Pole #3
	glColor3f (0.2, 0.2, 0.2); 
	glPushMatrix();
	glTranslatef(600, -900,0); //translate pole
	glBegin(GL_POLYGON);
		glVertex2f(430, 400);
		glVertex2f(450, 400);
		glVertex2f(450, 1400);
		glVertex2f(430, 1400);
	glEnd();
	glPopMatrix();

	glColor3f (0.2, 0.2, 0.2); 
	glPushMatrix();
	glTranslatef(600, -250,0); //translate light cover
	glBegin(GL_POLYGON);
		glVertex2f(430, 750);
		glVertex2f(430, 740);
		glVertex2f(410, 740);
		glVertex2f(410, 750);
	glEnd();
	glPopMatrix();

	glColor3f (1.0, 1.0, 1.0); 
	glPushMatrix();
	glTranslatef(600, -250,0); //translate light on pole
	glBegin(GL_POLYGON);
		glVertex2f(430, 740);
		glVertex2f(430, 730);
		glVertex2f(410, 730);
		glVertex2f(410, 740);
	glEnd();
	glPopMatrix();

	//Light Pole #4
	glColor3f (0.2, 0.2, 0.2); 
	glPushMatrix();
	glTranslatef(-475, 0,0); //translate pole
	glBegin(GL_POLYGON);
		glVertex2f(430, 400);
		glVertex2f(445, 400);
		glVertex2f(445, 750);
		glVertex2f(430, 750);
	glEnd();
	glPopMatrix();

	glColor3f (0.2, 0.2, 0.2); 
	glPushMatrix();
	glTranslatef(-440, 0,0); //translate light cover
	glBegin(GL_POLYGON);
		glVertex2f(430, 750);
		glVertex2f(430, 740);
		glVertex2f(410, 740);
		glVertex2f(410, 750);
	glEnd();
	glPopMatrix();

	glColor3f (1.0, 1.0, 1.0); 
	glPushMatrix();
	glTranslatef(-440, 0,0); //translate light
	glBegin(GL_POLYGON);
		glVertex2f(430, 740);
		glVertex2f(430, 730);
		glVertex2f(410, 730);
		glVertex2f(410, 740);
	glEnd();
	glPopMatrix();

	//Street Sign
	glColor3f (0.0, 1.0, 0.0); 
	glBegin(GL_POLYGON);
		glVertex2f(150, 700);
		glVertex2f(250, 700);
		glVertex2f(250, 750);
		glVertex2f(150, 750);
	glEnd();

	glColor3f (0.2, 0.2, 0.2); 
	glBegin(GL_POLYGON);
		glVertex2f(150, 600);
		glVertex2f(160, 600);
		glVertex2f(160, 700);
		glVertex2f(150, 700);
	glEnd();

		glBegin(GL_POLYGON);
		glVertex2f(250, 600);
		glVertex2f(240, 600);
		glVertex2f(240, 700);
		glVertex2f(250, 700);
	glEnd();


	//Building #1
	glColor3f (0.4, 0.4, 0.4); 
	glBegin(GL_POLYGON);
		glVertex2f(-1200,0);
		glVertex2f(-600,0);
		glVertex2f(-600, 1000);
		glVertex2f(-1200, 1000);
	glEnd();

	glColor3f (0.8, 0.8, 0.8);
	glBegin(GL_POLYGON);
		glVertex2f(-600,0);
		glVertex2f(-600,1000);
		glVertex2f(-530, 1100);
		glVertex2f(-530, 100);
	glEnd();

	glColor3f (0.35, 0.16, 0.14);
	glBegin(GL_POLYGON);
		glVertex2f(-600,1000);
		glVertex2f(-1200,1000);
		glVertex2f(-1100, 1100);
		glVertex2f(-530, 1100);
	glEnd();

	//Windows
	glColor3f(0.0,0.0,0.0);
   
   glRecti(-800,800,-725,900);
   glRecti(-800,600,-725,700);
   glRecti(-800,400,-725,500);
   glRecti(-800,200,-725,300);
   glRecti(-1000,800,-925,900);


	//Building #2
	glColor3f (0.4, 0.4, 0.4); 
	glBegin(GL_POLYGON);
		glVertex2f(-1500,-500);
		glVertex2f(-900,-500);
		glVertex2f(-900, 500);
		glVertex2f(-1500, 500);
	glEnd();

	glColor3f (0.8, 0.8, 0.8);
	glBegin(GL_POLYGON);
		glVertex2f(-900,-500);
		glVertex2f(-900,500);
		glVertex2f(-830, 600);
		glVertex2f(-830, -400);
	glEnd();

	glColor3f (0.35, 0.16, 0.14);
	glBegin(GL_POLYGON);
		glVertex2f(-900, 500);
		glVertex2f(-1500,500);
		glVertex2f(-1400, 600);
		glVertex2f(-830, 600);
	glEnd();

	//Windows
	glColor3f(0.0,0.0,0.0);
   glRecti(-1100,300,-1025,400);
   glRecti(-1100,100,-1025,200);
   glRecti(-1100,-100,-1025,0);
   glRecti(-1100,-300,-1025,-200); 
   glRecti(-1300,300,-1225,400);
   glRecti(-1300,100,-1225,200);

	//Building #3
	glColor3f (0.4, 0.4, 0.4); 
	glPushMatrix();
	glScalef(0.85f,0.85f,1.0);
	glTranslatef(-450, -600,0);
	glBegin(GL_POLYGON);
		glVertex2f(-1500,-500);
		glVertex2f(-900,-500);
		glVertex2f(-900, 500);
		glVertex2f(-1500, 500);
	glEnd();
	glPopMatrix();

	glColor3f (0.8, 0.8, 0.8);
	glPushMatrix();
	glScalef(0.85f,0.85f,1.0);
	glTranslatef(-450, -600,0);
	glBegin(GL_POLYGON);
		glVertex2f(-900,-500);
		glVertex2f(-900,500);
		glVertex2f(-830, 600);
		glVertex2f(-830, -400);
	glEnd();
	glPopMatrix();

	glColor3f (0.35, 0.16, 0.14);
	glPushMatrix();
	glScalef(0.85f,0.85f,1.0);
	glTranslatef(-450, -600,0);
	glBegin(GL_POLYGON);
		glVertex2f(-900, 500);
		glVertex2f(-1500,500);
		glVertex2f(-1400, 600);
		glVertex2f(-830, 600);
	glEnd();
	glPopMatrix();
 
	//Windows
	glColor3f(0.0,0.0,0.0);
   glRecti(-1350,-275,-1275,-175);
   glRecti(-1350,-475,-1275,-375);
   glRecti(-1350,-675,-1275,-575);
   glRecti(-1350,-875,-1275,-775); 
   glRecti(-1550,-275,-1475,-175);
   glRecti(-1550,-475,-1475,-375);
   glRecti(-1550,-675,-1475,-575);
   glRecti(-1550,-875,-1475,-775); 
 
   

   float delta_theta = 0.01;
   int r = 150;

   glColor3f(1.0,1.0,1.0);
   	glPushMatrix();
	glTranslatef(1100, 850,0);
	glBegin( GL_POLYGON ); // OR GL_LINE_LOOP

		for( float angle = 0; angle < 2*GL_PI; angle += delta_theta )
			glVertex3f( r*cos(angle), r*sin(angle), 0 );

	glEnd();
	glPopMatrix();

	// Excecute OpenGL functions
   glFlush ( );   
}

// Windows redraw function
void winReshapeFcn (GLint newWidth, GLint newHeight)
{
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ( );
    gluOrtho2D ((GLdouble) -newWidth, (GLdouble) newWidth, (GLdouble) -newHeight, (GLdouble) newHeight);
	
    glClear (GL_COLOR_BUFFER_BIT);  
}



// Main function
void main (int argc, char** argv)
{
    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
    // Set initial Window position
    glutInitWindowPosition (100, 100);
    // Set Window Size
    glutInitWindowSize (winWidth, winHeight);
    // Set Window Title
    glutCreateWindow ("CMSC405 Template");

    // Initialize
    init ( );
    // Display function call
    glutDisplayFunc (displayFcn);
    // Window reshape call
    glutReshapeFunc (winReshapeFcn);

    glutMainLoop ( );
}



Is This A Good Question/Topic? 0
  • +

Replies To: OpenGl lighting effect problem

#2 BBeck  Icon User is online

  • Here to help.
  • member icon


Reputation: 536
  • View blog
  • Posts: 1,191
  • Joined: 24-April 12

Re: OpenGl lighting effect problem

Posted 11 August 2012 - 03:04 PM

Let me start by saying that I haven't played with OpenGl for many many years and even then it was less complex than what you are doing. So, everything I'm saying here is based on computer graphics knowledge in general.

But the first thing I would check to see is if you are drawing your moon, or whatever it is, with the lighting rig you want then changing the lighting rig and drawing every thing else. You might think about that.

But I think what you "may" be wanting is more the advanced lighting of High Level Shader Language (I think it's called GLSL or something like that in OpenGL). For example, you may be wanting point lights where light radiates outward from a point like a lightbulb. Or you may be wanting a spot light where you have a very defined falloff around the spot being lit rather than an ambient, or directional, light that lights everything equally.

In this case, it sounds like you want "emmisive" lighting, which is probably a HLSL form of lighting, but you may be able to simulate it by changing your lighting rig right before drawing that object then changing it back for everything else you draw. So, that one object can be lit in an entirely different way than everything else. The downside, of course, is that the light of that object won't light up anything else. For that you probably need spot lighting or a point light, and that's probably going to require HLSL (or GLSL or whatever they call it in OpenGL). (I've got what looks like a pretty good book on the subject written for OpenGL, but haven't read it yet.)

For a true "glowing" effect you definately need HLSL to create the blur/glow.

Hopefully, that gives you some ideas to google. :-)

This post has been edited by BBeck: 11 August 2012 - 03:10 PM

Was This Post Helpful? 0
  • +
  • -

#3 dragstang86  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 28-September 09

Re: OpenGl lighting effect problem

Posted 12 August 2012 - 12:03 PM

Thanks, this did give me some ideas. Now time for the research!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1