GIDForums  

Go Back   GIDForums > Computer Programming Forums > OpenGL Programming
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Aug-2005, 00:18
StiRaSpecC StiRaSpecC is offline
New Member
 
Join Date: Aug 2005
Posts: 11
StiRaSpecC is on a distinguished road

Newbie OpenGL Programmer!


Hi Guys!! This is my first post on the forum. I'm trying to learn how to program in open gl. I recently bought a book on open gl game programming and am having trouble with one of the exercises. I'm not sure if it is ok to post the source code on here, but here goes.

CPP / C++ / C Code:
#include <gl\glut.h>

#include <gl\glut.h>

void Initialize();
void MouseHandler(int button, int state, int x, int y);
void KeyboardHandler(unsigned char key, int x, int y);
void MainMenuHandler(int option);
void Animate();
void Reshape(int width, int height);
void Display();

/****************************************************************************
 main()

 Setup GLUT and OpenGL, drop into the event loop
*****************************************************************************/
int main(int argc, char **argv)
{
  // Setup the basic GLUT stuff
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

  // Create the window
  glutInitWindowSize(1024, 768);
  glutInitWindowPosition(100, 150);
  glutCreateWindow("BOGLGP Chapter 1");

  Initialize();

  // Register the event callback functions
  glutDisplayFunc(Display); 
  glutReshapeFunc(Reshape);
  glutMouseFunc(MouseHandler);
  glutKeyboardFunc(KeyboardHandler);
  glutIdleFunc(Animate);

  // At this point, control is relinquished to the GLUT event handler.
  // Control is returned as events occur, via the callback functions.
  glutMainLoop();   
   
  return 0;
} // end main()


/****************************************************************************
 Initialize()

 One time setup, including creating menus, creating a light, setting the
 shading mode and clear color, and loading textures.
*****************************************************************************/
void Initialize()
{
  // set up the only meny
  int mainMenu;

  mainMenu = glutCreateMenu(MainMenuHandler);

  glutSetMenu(mainMenu);
  glutAddMenuEntry("Exit", 0);
  glutAttachMenu(GLUT_RIGHT_BUTTON);

  glEnable(GL_DEPTH_TEST);
} // end Initialize()


/****************************************************************************
 MouseHandler()
 
 Handle mouse events. For this simple demo, just exit on a left click.
*****************************************************************************/
void MouseHandler(int button, int state, int x, int y)
{
  switch (button)
  {
  case GLUT_LEFT_BUTTON:
    {
      exit(0);
    } break;
  default:
    break;
  }

  // force a screen redraw
  glutPostRedisplay();
} // end MouseHandler()


/****************************************************************************
 KeyboardHandler()

 Keyboard handler. Again, we'll just exit when q is pressed.
*****************************************************************************/
void KeyboardHandler(unsigned char key, int x, int y)
{
  switch (key)
  {
  case 'q':  // exit
    {
      exit(0);
    } break;
  default:
    {
    } break;
  }
  glutPostRedisplay();
} // end KeyboardHandler()


/****************************************************************************
 MainMenuHandler()

 Main menu callback.
*****************************************************************************/
void MainMenuHandler(int option)
{
  switch(option)
  {
  case 0:
    {
      exit(0);
    } break;
  default:
    break;
  }
  glutPostRedisplay();
} // end MainMenuHandler()


/****************************************************************************
 Animate()

 Rotate the cube by 4 degrees and force a redisplay.
*****************************************************************************/
void Animate()
{
  glutPostRedisplay();
} // end Animate()


/****************************************************************************
 Reshape()

 Reset the viewport for window changes
*****************************************************************************/
void Reshape(int width, int height)
{
  if (height == 0)
    return;

  glViewport(0, 0, (GLsizei) width, (GLsizei) height);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(90.0, width/height, 1.0, 100.0);

  glMatrixMode(GL_MODELVIEW);
} // end Reshape


/****************************************************************************
 Display()

 Clear and redraw the scene.
*****************************************************************************/
void Display()
{
  // set up the camera
  glLoadIdentity();
  gluLookAt(0.0, 1.0, 6.0,
            0.0, 0.0, 0.0,
            0.0, 1.0, 0.0);

  // clear the screen
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  // draw a triangle
  glBegin(GL_TRIANGLES);
    glColor3f(1.0, 0.0, 0.0);
    glVertex3f(2.0, 2.5, -1.0);
    glColor3f(1.0, 0.0, 0.0);
    glVertex3f(-3.5, -2.5, -1.0);
    glColor3f(1.0, 0.0, 0.0);
    glVertex3f(2.0, -4.0, 0.0);
  glEnd();

  // draw a polygon
  glBegin(GL_POLYGON);
    glColor3f(1.0, 1.0, 1.0);
    glVertex3f(-1.0, 2.0, 0.0);
    glColor3f(1.0, 1.0, 0.0);
    glVertex3f(-3.0, -0.5, 0.0);
    glColor3f(0.0, 1.0, 1.0);
    glVertex3f(-1.5, -3.0, 0.0);
    glColor3f(0.0, 0.0, 0.0);
    glVertex3f(1.0, -2.0, 0.0);
    glColor3f(1.0, 0.0, 1.0);
    glVertex3f(1.0, 1.0, 0.0);
  glEnd();

  // draw everything and swap the display buffer
  glutSwapBuffers();
} // end Display()




This code creates a triangle and polygon. The exercise wants you to modify the triangle to be all red and the polygon to be all blue. This seems simple, I tried modifying the values in the glColor3f function. For the triangle I have

CPP / C++ / C Code:
glBegin(GL_TRIANGLES);
    glColor3f(1.0, 0.0, 0.0);
    glVertex3f(2.0, 2.5, -1.0);
    glColor3f(1.0, 0.0, 0.0);
    glVertex3f(-3.5, -2.5, -1.0);
    glColor3f(1.0, 0.0, 0.0);
    glVertex3f(2.0, -4.0, 0.0);
  glEnd();

For the polygon I have:

CPP / C++ / C Code:
glBegin(GL_POLYGON);
    glColor3f(0.0, 0.0, 1.0);
    glVertex3f(-1.0, 2.0, 0.0);
    glColor3f(0.0, 0.0, 0.0);
    glVertex3f(-3.0, -0.5, 0.0);
    glColor3f(0.0, 0.0, 1.0);
    glVertex3f(-1.5, -3.0, 0.0);
    glColor3f(0.0, 0.0, 1.0);
    glVertex3f(1.0, -2.0, 0.0);
    glColor3f(0.0, 0.0, 1.0);
    glVertex3f(1.0, 1.0, 0.0);
  glEnd();

When I modify these values in Visual C++, they don't have any effect on the triangle or polygon. I'm a struggling programmer that needs help!! Any help would be greatly appreciated.
Last edited by LuciWiz : 23-Aug-2005 at 00:53. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 23-Aug-2005, 09:51
pixienick pixienick is offline
New Member
 
Join Date: Aug 2005
Posts: 23
pixienick is on a distinguished road
Hello, i love opengl.
I copied just your draw triangle and polygon (the bits that start, glBegin() and end glEnd()), parts into my prog and the two versions you got do have different effects. The first ones create gradual colors and your new ones do create a total color, though on the blue quad one of the glcolor3f is black (0 0 0). However if you want to color something all the same color just call glColor3f once at the start and opengl will subsequently draw everything in that color till it comes across another glColor function.
Try

CPP / C++ / C Code:
    glColor3f(1.0, 0.0, 0.0);
glBegin(GL_TRIANGLES);
    glVertex3f(2.0, 2.5, -1.0);
    glVertex3f(-3.5, -2.5, -1.0);
    glVertex3f(2.0, -4.0, 0.0);
  glEnd();

Changing the color values is having an effect in mine so you must be not compiling it right or summink?
Last edited by LuciWiz : 31-Aug-2005 at 04:25. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #3  
Old 23-Aug-2005, 16:34
StiRaSpecC StiRaSpecC is offline
New Member
 
Join Date: Aug 2005
Posts: 11
StiRaSpecC is on a distinguished road
Hi!! Thanks for the reply!!! I had no idea that if you call:
glColor3f(1.0, 0.0, 0.0);

before the glbegin call that it would change the color to solid. I will definetly try that.

I have another question. I got this code from cd from a book I just purchase. I just copied all the files from the cd and saved this into my VS C++ folder. Now when I change the values in my prog, I save it, it won't let me compile it. When you entered this code into your prog, did you create a new project and choose win32 project under templates? After this, did you just copy and paste my code into VS C++ and compile it? When I try to compile the whole big thing of code, i get build errors.

If you wouldn't mind Pixienick, would it be too much of me to ask you to please go step by step on how you compiled this code would be great, Thanks!!!
  #4  
Old 23-Aug-2005, 18:35
pixienick pixienick is offline
New Member
 
Join Date: Aug 2005
Posts: 23
pixienick is on a distinguished road
I just copied yor drawing of poly and triangles into the draw function of one of my progs and they worked. If your having trouble creating new projects have you maybe not added the opengl libraries into the project settings (alt F7) link tab? I'm not going to rebuild all your code but if you send me your email to me, i'll send you a skeleton project of my own that will do the same job, can't say fairer than that!
  #5  
Old 24-Aug-2005, 00:44
StiRaSpecC StiRaSpecC is offline
New Member
 
Join Date: Aug 2005
Posts: 11
StiRaSpecC is on a distinguished road
for some reason it wont let me shoot you a pm. maybe i need to have more posts? when i get enough i'll give you my email addy. Thanks again for the help!!

oh btw, do you have any recommendations on a great c++ book? I have a little programming experience, but I really feel like I need to learn a lot more.
  #6  
Old 24-Aug-2005, 04:26
pixienick pixienick is offline
New Member
 
Join Date: Aug 2005
Posts: 23
pixienick is on a distinguished road
There's loads of books about. I learnt a bit of c then started going through the tutorials at http://nehe.gamedev.net/, they got a real nice step by step guide taking you through all the opengl basics and some nice source code to play with. You'll be making games in no time!
  #7  
Old 25-Aug-2005, 11:24
StiRaSpecC StiRaSpecC is offline
New Member
 
Join Date: Aug 2005
Posts: 11
StiRaSpecC is on a distinguished road
Quote:
Originally Posted by pixienick
There's loads of books about. I learnt a bit of c then started going through the tutorials at http://nehe.gamedev.net/, they got a real nice step by step guide taking you through all the opengl basics and some nice source code to play with. You'll be making games in no time!

i will definetly check that site out. im trying to get my posts up so i can pm you my email addy.
  #8  
Old 27-Aug-2005, 00:31
StiRaSpecC StiRaSpecC is offline
New Member
 
Join Date: Aug 2005
Posts: 11
StiRaSpecC is on a distinguished road
I pm'ed you my email address. Hope you got it. Thanks again!!
  #9  
Old 30-Aug-2005, 07:28
Janakiraman Janakiraman is offline
Junior Member
 
Join Date: Apr 2005
Location: Chennai - India
Posts: 47
Janakiraman is on a distinguished road

use directX instead of OpenGL


Hi,

since u r at the begining level of OpenGL... my suggestion is why dont u go for the advanced version of OpenGL that is DirectX

(Just clarify it with some other regrding this)

Best Regards,
Janakiraman
  #10  
Old 31-Aug-2005, 04:23
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by Janakiraman
my suggestion is why dont u go for the advanced version of OpenGL that is DirectX

DirectX is NOT a version of OpenGL, it is a competing standard. The difference is that while OpenGL is an open, cross-platform standard, DirectX is a locked in standard maintained by Microsoft. It only works on Windows as far as I know, but you can use certain emulators to fake it on various Operating Systems.
D3D is very popular with the gaming crowd, but some cool games (like Quake) used OpenGL, and some others offered support for both of them.
However OpenGL only deals with the graphics part involved in creating a game, while D3D offers support for sound too, and I guess that's why they (game developers) tend to use it...
Anyway, don't use DirectX if you don't strictly program for the Windows platform. Also, if you aren't into games, maybe OpenGL would be a better way to go.
If you are interested into further reading, I suggest you google "directx vs opengl" for some interesting articles.

Best regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
new programmer wendy C++ Forum 4 14-Jan-2005 00:59
hello all!! newbie to the GIDforums usmsci Sports Forum 20 16-Dec-2004 08:40
nVidia & OpenGL Problems in windows XP marjasin Computer Hardware Forum 21 30-Aug-2004 01:57
linked lists, newbie needs help moltarim C Programming Language 4 06-May-2004 12:32
C++ books for newbie mak90thug C++ Forum 4 04-Feb-2004 15:58

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:55.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.