LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-24-2005, 08:19 AM   #1
leadazide
Member
 
Registered: Apr 2004
Location: Germany
Distribution: SuSE 11.0, Ubuntu 7.10
Posts: 390

Rep: Reputation: 30
What's wrong with this OpenGL code?


The following piece of code should draw a sphere in an SDL window, which is opened from a Qt window. The SDL window pops up properly, but the sphere doesn't get displayed. I have little experience with OpenGL, and even less experience with GLU functions. I can't find my error. Can anyone help me?

(The constructor is being called from the main function)

Code:
Simulationwindow::Simulationwindow()
{
  //SDL-Initialisierung
  if(SDL_Init(SDL_INIT_VIDEO) < 0)
  {
    cout << "Could not initialize SDL:" << SDL_GetError() << endl;
    atexit(SDL_Quit);
  }
  //Video-Initialisierung
  SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
  SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
  SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
  SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
  SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );  
  screen = SDL_SetVideoMode(width,height,16, SDL_HWSURFACE | SDL_ANYFORMAT | SDL_OPENGL);
  
  if ( screen == NULL )
  {
     cout << "Couldn't set 640x480x16 video mode: " << SDL_GetError();
     atexit(SDL_Quit);
  }
  
  SDL_WM_SetCaption("Simulation", NULL);
  
  InitGL();
  
  InitDrawScene();
  DrawKugel(300, 200, 10);
  FinalizeDrawGLScene();

}
void Simulationwindow::InitGL()
{

  
  

  glShadeModel(GL_SMOOTH);
   
  glCullFace( GL_BACK );
  glFrontFace( GL_CCW );
  glEnable( GL_CULL_FACE );
  
  
  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  
  glViewport(0, 0, width, height);         
  //glEnable(GL_DEPTH_TEST);            
  
  
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(60.0f, (GLfloat)width/(GLfloat)height, 1.0f, 1024.0f);
  
  
  
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

void Simulationwindow::DrawKugel(int x, int y, int r)
{
  
  glPushMatrix();

  GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8f, 1.0f };
  GLfloat specularLight[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  GLfloat LightPosition[] = { 0.4f, 0.0f, 8.0f, 1.0f };
  

  
  glTranslatef(GLfloat(x), GLfloat(y), -5.0f);
  
  glRotatef(0.0f, 0.0f, 0.0f, 0.0f);



  GLUquadric* quad = gluNewQuadric();
  gluQuadricDrawStyle(quad, GLU_FILL);
  
  glEnable(GL_LIGHTING);
               
  glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
  glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
  glLightfv(GL_LIGHT0, GL_POSITION, LightPosition);
  glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 10.0f);
  glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 75.0f);
  
  glEnable(GL_LIGHT0);

  
  glEnable(GL_COLOR_MATERIAL);
  glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
  glMaterialfv(GL_FRONT, GL_SPECULAR, specularLight);
  glMateriali(GL_FRONT, GL_SHININESS, 128);
  
  
  gluSphere(quad, r, 20, 20);
  gluDeleteQuadric(quad);

  glPopMatrix();
}

void Simulationwindow::InitDrawScene()
{  
  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
}

void Simulationwindow::FinalizeDrawGLScene()
{   
  SDL_GL_SwapBuffers();
}
P. S. Thanks for reading the whole piece of code

Last edited by leadazide; 02-24-2005 at 08:20 AM.
 
Old 02-25-2005, 08:06 AM   #2
leadazide
Member
 
Registered: Apr 2004
Location: Germany
Distribution: SuSE 11.0, Ubuntu 7.10
Posts: 390

Original Poster
Rep: Reputation: 30
BUMP

I took the lighting out and tried to draw some vertices with glBegin(GL_TRIANGLES); glColor3f(), glVertex3f() and glEnd(); and this worked. But why doesn't gluSphere work?
 
Old 02-25-2005, 08:58 AM   #3
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
I didn't have time to mess with this much, but some interesting lines that are probably the source of the problem are the following (taken from different functions):

Code:
gluPerspective(60.0f, (GLfloat)width/(GLfloat)height, 1.0f, 1024.0f);

DrawKugel(300, 200, 10);
  
glTranslatef(GLfloat(x), GLfloat(y), -5.0f);
You are using a perspective projection, so your viewing volume is kind of like a pyramid shape with the top cut off. Anything within this volume will be visible, anything not in it will not be visible.

It appears that you are trying to draw your circle at 300, 200, -5. I haven't done the calculations, but I am pretty confident that this location will not fall within your viewing volume. (It is likely clipped by both the top and right clip planes). Try Change the values you pass to DrawKugel to something like 0,0 initially and go from there.
 
Old 02-25-2005, 10:24 AM   #4
leadazide
Member
 
Registered: Apr 2004
Location: Germany
Distribution: SuSE 11.0, Ubuntu 7.10
Posts: 390

Original Poster
Rep: Reputation: 30
Thanks

Thank you for your reply, I´m in the library now, will try this when i come home.
 
Old 02-26-2005, 09:31 AM   #5
leadazide
Member
 
Registered: Apr 2004
Location: Germany
Distribution: SuSE 11.0, Ubuntu 7.10
Posts: 390

Original Poster
Rep: Reputation: 30
Thanks, changing the coordinates in DrawKugel(300, 200, 10); worked and I could see a sphere (althought it looked like a circle) Now I only have to adjust the lights. Although it could be better if the sphere stayed at 300.0f, 200.0f, 10.0f and I could change the gluPerspective to see it (or replace gluPerspective with gluLookAt)
 
Old 02-27-2005, 09:09 AM   #6
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
You wouldn't replace gluPerspective with gluLookAt as they do different things. Think of gluPerspective as setting the properties of your camera lens, and gluLookAt as a means pointing your camera in the direction you want. If you want to use gluLookAt, you should use it in the MODELVIEW matrix, and make sure it is the first thing applied after a glLoadIdentity().
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What is wrong with the following C code? indian Programming 17 12-04-2005 09:01 AM
What is wrong in this code hhegab Programming 12 08-13-2005 11:23 AM
What is wrong with this C code ? indian Programming 8 04-16-2005 04:42 AM
errors with simple C++ Code... openGL problem? poeta_boy Programming 4 11-08-2004 12:34 PM
What is wrong with this code? qanopus Programming 4 03-12-2004 07:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration