2 Replies - 2233 Views - Last Post: 08 October 2008 - 12:55 PM Rate Topic: -----

#1 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

C++ with glut

Posted 08 October 2008 - 11:13 AM

Hi, i am tottally new to c++ and i have to use it with the glut library. I am way more comfortable in java but this is becoming quite a nice challenge. My first program is very simple. I basically have to draw my initials NP. I am trying to follow an example program which just draws four different straight lines. Here it is
// wk2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include "glut.h"
#include <stdlib.h>

#define drawOneLine(x1,y1,x2,y2)  glBegin(GL_LINES);  \
   glVertex2f ((x1),(y1)); glVertex2f ((x2),(y2)); glEnd(); 
//A small function (or a macro) to draw a line with ending points fo (x1,y1) and (x2,y2).

void init(void) 
{
   glClearColor (0.0, 1.0, 0.0, 0.0);  
	glMatrixMode (GL_PROJECTION);		//Viewport
	gluOrtho2D( 0.0,400.0,0.0,400.0);	//Viewport, i.e., camera position.

}

void display(void)
{
   int i;

   glClear (GL_COLOR_BUFFER_BIT);

/* select white for all lines  */
   glColor3f (1.0, 0.0, 0.0);

/* in 1st row, 3 lines, each with a different stipple  */
   glEnable (GL_LINE_STIPPLE);
   
   glLineStipple (1, 0x0101);  /*  dotted  */
   drawOneLine (50.0, 125.0, 150.0, 125.0);
   glLineStipple (1, 0x00FF);  /*  dashed  */
   drawOneLine (150.0, 125.0, 250.0, 125.0);
   glLineStipple (1, 0x1C47);  /*  dash/dot/dash  */
   drawOneLine (250.0, 125.0, 350.0, 125.0);

/* in 2nd row, 3 wide lines, each with different stipple */
   glLineWidth (5.0);
   glLineStipple (1, 0x0101);  /*  dotted  */
   drawOneLine (50.0, 100.0, 150.0, 100.0);
   glLineStipple (1, 0x00FF);  /*  dashed  */
   drawOneLine (150.0, 100.0, 250.0, 100.0);
   glLineStipple (1, 0x1C47);  /*  dash/dot/dash  */
   drawOneLine (250.0, 100.0, 350.0, 100.0);
   glLineWidth (1.0);

/* in 3rd row, 6 lines, with dash/dot/dash stipple  */
/* as part of a single connected line strip		 */
   glLineStipple (1, 0x1C47);  /*  dash/dot/dash  */
   glBegin (GL_LINE_STRIP);
   for (i = 0; i < 7; i++)
	  glVertex2f (50.0 + ((GLfloat) i * 50.0), 75.0);
   glEnd ();

/* in 4th row, 6 independent lines with same stipple  */
   for (i = 0; i < 6; i++) {
	  drawOneLine (50.0 + ((GLfloat) i * 50.0), 50.0,
		 50.0 + ((GLfloat)(i+1) * 50.0), 50.0);
   }

/* in 5th row, 1 line, with dash/dot/dash stipple	*/
/* and a stipple repeat factor of 5				  */
   glLineStipple (5, 0x1C47);  /*  dash/dot/dash  */
   drawOneLine (50.0, 25.0, 350.0, 25.0);


   glDisable (GL_LINE_STIPPLE);
   glFlush ();
}



int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
   glutInitWindowSize (400, 150); 
   glutInitWindowPosition (100, 100);
   glutCreateWindow (argv[0]);
   init ();
   glutDisplayFunc(display); 

   glutMainLoop();
   return 0;
}



I just basically want to try and understand a few things. First of all, what is this line doing
#define drawOneLine(x1,y1,x2,y2)  glBegin(GL_LINES);  \
   glVertex2f ((x1),(y1)); glVertex2f ((x2),(y2)); glEnd(); 


Is #define somewhere i can define my own functions?
My second question is whether the display is where i do all my display work? Or is it like java where i can create seperate methods and then just call them up within display?

My last question is too do with there being 5 lines displayed (although some lines look like they are supposed to be made up of multiple lines) with spaces between them all. Now i would have imagined that one line would be 2 points x1, y1. So why is the define function and all the lines created out of four points? x1,y1,x2,y2

Sorry for the basic of basics questions. As i say, i am a lot more comfortable with java but only cos this is new. Once i get used to it i should find it a lot easier than it currently is. Any help in answering these questions would be such a help.
cheers

Is This A Good Question/Topic? 0
  • +

Replies To: C++ with glut

#2 Psionics  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 158
  • Joined: 06-September 08

Re: C++ with glut

Posted 08 October 2008 - 12:25 PM

Well for starters one can think of #define as a simple find and replace:
// simple find and replace
// this will replace any instances of VARIABLE with 10
#define VARIABLE 10;



But yes, you can create a function with a #define in C++. Please be careful after you use '\' which simply tells the #define to keep reading (otherwise, the #define stops at the newline). But be careful because if you have white space after the '\' you'll get an error. If all else fails, just type everything on one line and you'll be fine.


As a side note, if it's a data type you'd like to make generic, you can use typedef:
// make a generic data type
typedef int Item;



This is again a simple find and replace, but good for generic programming!

Hope this helps :)
Was This Post Helpful? 0
  • +
  • -

#3 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: C++ with glut

Posted 08 October 2008 - 12:55 PM

cheers, get you. I am working on writing the letter N, and being stubern i dont want to write it with just 3 lines, so i am trying to create some graffeti style text. Now say if i make 4 lines which creates a box, is there anyway of filling in the colour in the area these lines make?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1