Solved

(60 pts) OpenGL question

Posted on 2000-04-22
10
231 Views
Last Modified: 2010-04-15
0
Comment
Question by:AzraSound
  • 6
  • 4
10 Comments
 

Expert Comment

by:mlaughton
ID: 2745226
It looks like you're making your lighting call right after you're swapping buffers.  I don't have access to an OpenGL development environment right now, but the first thing I'd try would be flip/flopping the lighting and swap buffer calls.  That might take care of your problem.
0
 
LVL 28

Author Comment

by:AzraSound
ID: 2745270
thanks for the input.  i had thought of that and tried it but it didnt seem to make any difference.  the change doesnt occur until later when i do stuff with the mouse which always updates it right away.  or even other keyboard events not related to lighting update right away.  it is when those events occur that the lighting is finally updated.
0
 

Expert Comment

by:mlaughton
ID: 2745319
Looking at the code again, I notice that your glLightf() call happens after all of the drawing you want to do takes place.  In order for your vertices/normals to reflect the changes in your lighting model, you have to make the glLightf() call first.  Try:

void display(void) {
   glClear(GL_COLOR_BUFFER_BIT |
         GL_DEPTH_BUFFER_BIT);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();

   /* moved here from bottom */
   glLightf(GL_LIGHT0,
      GL_SPOT_CUTOFF, angle);

   if(solid == TRUE)
      glutSolidSphere(r, 50, 40);
   else
      glutWireSphere(r, 50, 40);

   DrawVertices();
   glutSwapBuffers();
}

The reason your changes weren't taking immediate effect before is because the program had cycle through display() more than once in order to see the changes made at the end of the first time around (hence, you saw your changes after the next event).

I hope this works!
0
 
LVL 28

Author Comment

by:AzraSound
ID: 2746007
didnt seem to change anything...let me post all my code so maybe you can tinker with it.  its an assignment due in two days so if i dont get an answer by then it wont do me much good.  this is the only problem i have left.

#include <windows.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#define TRUE 1
#define FALSE 0
#define ON 1
#define OFF 0
#define POINTLIGHT 0.0
#define SPOTLIGHT 1.0
#define SIZE 40  /*total number of points allowed to be drawn*/


void chngDimensions(int value);
float getZval(float x, float y);
void DrawVertices(void);
int onSphere(float x, float y);

int increase = 10;
int decrease = -10;
float r = 1.0;
int solid = TRUE;
float points [SIZE][3];   /*store all current points drawn*/
int numPoints;  /*store the number of points drawn thus far*/
int winH, winW, light;
float angle = 45.0;
float prevangle = 45.0;
GLfloat lightType = SPOTLIGHT;
GLfloat spot_direction[] = {0.0, 0.0, 10.0};
GLfloat position[] = {0.0, 0.0, 3.0, 1.0};



void myinit(void)
{
       glLightfv(GL_LIGHT0,GL_POSITION,position);
       //glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,spot_direction);
    glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,angle);

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    //glDepthFunc(GL_LEQUAL);
    glEnable(GL_DEPTH_TEST);
}

void display(void){
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,angle);
   if(solid == TRUE)
         glutSolidSphere(r, 50, 40);
   else
         glutWireSphere(r, 50, 40);
   DrawVertices();
   glutSwapBuffers();
}

void myReshape(int w, int h){
      winW = w;
   winH = h;
   glViewport(0, 0, w, h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   if (w <= h)
         glOrtho (-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w,
            1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
   else
         glOrtho (-1.5*(GLfloat)w/(GLfloat)h,
            1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   display();
}


void mouse(int button, int state, int x, int y){
      int valid; float z; float x0, y0;
      if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN){
         y = winH - y;
      x0 = x;
      y0 = y;
      x0 = (x0 - winW/2)/165;
      y0 = (y0 - winH/2)/165;
      valid = onSphere(x0,y0);
      if(valid == TRUE){
            z = getZval(x0,y0);
            points[numPoints][0] = x0;
            points[numPoints][1] = y0;
         points[numPoints][2] = z;
            numPoints = numPoints + 1;
         glutPostRedisplay();
            }
      else
            return;
      }
   else if(button==GLUT_MIDDLE_BUTTON && state==GLUT_DOWN){
         chngDimensions(increase);
      glutPostRedisplay();
      }
   else if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN){
         chngDimensions(decrease);
      glutPostRedisplay();
      }
   }

void DrawVertices(void){
    int i;
    glPointSize(3.0);
    glBegin(GL_POINTS);
             for(i=0;i<numPoints;i++)
                glVertex3f(points[i][0],points[i][1],points[i][2]);
    glEnd();
}

void keyboard(unsigned char key,int x,int y){
      if(key == 's' || key == 'S'){
         solid = !solid;
         glutPostRedisplay();
      }
   else if(key == 't' || key == 'T'){
         if(light == OFF){
            angle = prevangle;
               light = ON;
         }
      else{
            angle = 0.0;
            light = OFF;
         }
      glutPostRedisplay();
      }
   else if(key == 'i' || key == 'I'){
         angle = angle * 1.1;
      glutPostRedisplay;
      }
   else if(key == 'd' || key == 'D'){
         angle = angle * .9;
      glutPostRedisplay;
      }
   else
         glutPostRedisplay;
   }

int onSphere(float x, float y){
      double temp;
   temp = x*x + y*y;
   if(temp <= (r*r))
         return TRUE;
   else{
         printf("That point is not on the sphere!\n");
         return FALSE;
      }
   }


void chngDimensions(int value){
      int i;
      if(value == increase){
         r = r * 1.1;
         for(i=0; i<=numPoints; i++){
            points[i][0] = points[i][0]*1.1;
         points[i][1] = points[i][1]*1.1;
         points[i][2] = points[i][2]*1.1;
         }
      }
   else{
         r = r * .9;
         for(i=0; i<=numPoints; i++){
            points[i][0] = points[i][0]*.9;
         points[i][1] = points[i][1]*.9;
         points[i][2] = points[i][2]*.9;
         }
      }
   glutPostRedisplay();
   }


float getZval(float x, float y){
      float z;
   z = r*r - x*x - y*y;
   z = sqrt(z);
   return z;
   }


int main(int argc, char** argv)
{
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(500,500);
    glutCreateWindow("Sphere Program");
    myinit();
    glutReshapeFunc(myReshape);
    glutMouseFunc(mouse);
    glutKeyboardFunc(keyboard);
    glutDisplayFunc(display);
    glutMainLoop();
}


0
 
LVL 28

Author Comment

by:AzraSound
ID: 2751889
can you believe it?  look at my keyboard function where i code glutPostRedisplay rather than glutPostRedisplay().  i am surprised that doesnt result in a compilation error but that was my problem all along.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Accepted Solution

by:
mlaughton earned 10 total points
ID: 2751921
Good job, Azra.  I can't believe I didn't catch that either.

What happens when you put the glLightf() call back at the bottom of your display() (where it was to begin with).  I'm curious to see if that makes a difference, and I can't do it myself since I don't have access to a GLUT-enabled machine right now.  Anyway, good luck on your assignment!
0
 
LVL 28

Author Comment

by:AzraSound
ID: 2751938
Adjusted points from 0 to 10
0
 
LVL 28

Author Comment

by:AzraSound
ID: 2751976
actually it seems to work just the same. pretty interesting.  just giving you something for looking at it for me.
0
 

Expert Comment

by:mlaughton
ID: 2752018
Thanks for the charity, Azra. ;)  By the way, if you are considering doing more development using OpenGL and a cross-platform Window/Event API (such as GLUT) you might want to check out SDL (www.devolution.com/~slouken/SDL/) as an alternative to GLUT.

By my understanding, GLUT was developed for demonstration purposes to make OpenGL easier to teach.  I've heard that development on it has stagnated somewhat, even though there were ambitious visions of grandeur earlier on.  SDL (Simple DirectMedia Layer), on the other hand, is very actively developed, and has hooks into audio, cdrom, events, OpenGL, and all kinds of good stuff.

I've been quite happy with SDL so far and highly recommend it.
0
 
LVL 28

Author Comment

by:AzraSound
ID: 2752030
to be quite honest i'm just taking a graphics class for school and this will most likely be the last i ever see of opengl. however i may consider in the future playing around with it in VB which is my area of expertise. thanks for the input and the link.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now