# C++ OpenGL zeichnen



## hury (22. Mai 2008)

Hallo,

ich wollte ein Testprogramm von einem Kumpel in VC++ 9.0 einbinden und es testen - allerdings klappt es nicht. Ich bekomme eine Menge Fehlermeldungen wie zb:


```
c:\programme\microsoft sdks\windows\v6.0a\include\gl\gl.h(1152) : error C2144: Syntaxfehler: 'void' sollte auf ';' folgen

c:\programme\microsoft sdks\windows\v6.0a\include\gl\gl.h(1152) : error C4430: Fehlender Typspezifizierer - int wird angenommen. Hinweis: "default-int" wird von C++ nicht unterstützt.

c:\programme\microsoft sdks\windows\v6.0a\include\gl\gl.h(1152) : error C2146: Syntaxfehler: Fehlendes ';' vor Bezeichner 'glAccum'
```

usw.
Das Programm läuft aber, der Kumpel hat mir die .exe mitgeschickt.

Was mache ich falsch? Hier der Quellcode:


```
#include <math.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <gl/glaux.h>

float angle = 0.0, deltaAngle = 0.0, ratio;
float angleUp = 0.0, deltaAngleUp = 0.0;
float x = 0.0f, y = 1.75f, z = 5.0f;
float lx = 0.0f, ly = 0.0f, lz = -1.0f;
int deltaMove = 0, deltaStrafe = 0;
int xAxis = 0, yAxis = 0, zAxis = 0, deltaXAxis = 0, deltaYAxis = 0, deltaZAxis = 0;
int item = 1;

void changeSize(int w, int h) {
     
	// Prevent a divide by zero, when window is too short
	// (you cant make a window of zero width).
	if(h == 0)
		h = 1;

	ratio = 1.0f * w / h;
	// Reset the coordinate system before modifying
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	
	// Set the viewport to be the entire window
	glViewport(0, 0, w, h);

	// Set the clipping volume
	gluPerspective(45, ratio, 1, 1000);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(x, y, z, 
		      x + lx, y + ly, z + lz,
			  0.0f, 1.0f, 0.0f);
}

// Draw Quad
void drawQuad() {

	glBegin(GL_QUADS);									// Draw A Quad
		glColor3f(0.0f,1.0f,0.0f);						// Set The Color To Blue
		glVertex3f( 1.0f, 1.0f,-1.0f);					// Top Right Of The Quad (Top)
		glVertex3f(-1.0f, 1.0f,-1.0f);					// Top Left Of The Quad (Top)
		glVertex3f(-1.0f, 1.0f, 1.0f);					// Bottom Left Of The Quad (Top)
		glVertex3f( 1.0f, 1.0f, 1.0f);					// Bottom Right Of The Quad (Top)
		glColor3f(1.0f,0.5f,0.0f);						// Set The Color To Orange
		glVertex3f( 1.0f,-1.0f, 1.0f);					// Top Right Of The Quad (Bottom)
		glVertex3f(-1.0f,-1.0f, 1.0f);					// Top Left Of The Quad (Bottom)
		glVertex3f(-1.0f,-1.0f,-1.0f);					// Bottom Left Of The Quad (Bottom)
		glVertex3f( 1.0f,-1.0f,-1.0f);					// Bottom Right Of The Quad (Bottom)
		glColor3f(1.0f,0.0f,0.0f);						// Set The Color To Red
		glVertex3f( 1.0f, 1.0f, 1.0f);					// Top Right Of The Quad (Front)
		glVertex3f(-1.0f, 1.0f, 1.0f);					// Top Left Of The Quad (Front)
		glVertex3f(-1.0f,-1.0f, 1.0f);					// Bottom Left Of The Quad (Front)
		glVertex3f( 1.0f,-1.0f, 1.0f);					// Bottom Right Of The Quad (Front)
		glColor3f(1.0f,1.0f,0.0f);						// Set The Color To Yellow
		glVertex3f( 1.0f,-1.0f,-1.0f);					// Top Right Of The Quad (Back)
		glVertex3f(-1.0f,-1.0f,-1.0f);					// Top Left Of The Quad (Back)
		glVertex3f(-1.0f, 1.0f,-1.0f);					// Bottom Left Of The Quad (Back)
		glVertex3f( 1.0f, 1.0f,-1.0f);					// Bottom Right Of The Quad (Back)
		glColor3f(0.0f,0.0f,1.0f);						// Set The Color To Blue
		glVertex3f(-1.0f, 1.0f, 1.0f);					// Top Right Of The Quad (Left)
		glVertex3f(-1.0f, 1.0f,-1.0f);					// Top Left Of The Quad (Left)
		glVertex3f(-1.0f,-1.0f,-1.0f);					// Bottom Left Of The Quad (Left)
		glVertex3f(-1.0f,-1.0f, 1.0f);					// Bottom Right Of The Quad (Left)
		glColor3f(1.0f,0.0f,1.0f);						// Set The Color To Violet
		glVertex3f( 1.0f, 1.0f,-1.0f);					// Top Right Of The Quad (Right)
		glVertex3f( 1.0f, 1.0f, 1.0f);					// Top Left Of The Quad (Right)
		glVertex3f( 1.0f,-1.0f, 1.0f);					// Bottom Left Of The Quad (Right)
		glVertex3f( 1.0f,-1.0f,-1.0f);					// Bottom Right Of The Quad (Right)
	glEnd();											// Done Drawing The Quad     
}

// Draw Pyramid
void drawPyramid() {

	glBegin(GL_TRIANGLES);								// Start Drawing A Triangle
		glColor3f(1.0f,0.0f,0.0f);						// Red
		glVertex3f( 0.0f, 1.0f, 0.0f);					// Top Of Triangle (Front)
		glColor3f(0.0f,1.0f,0.0f);						// Green
		glVertex3f(-1.0f,-1.0f, 1.0f);					// Left Of Triangle (Front)
		glColor3f(0.0f,0.0f,1.0f);						// Blue
		glVertex3f( 1.0f,-1.0f, 1.0f);					// Right Of Triangle (Front)
		glColor3f(1.0f,0.0f,0.0f);						// Red
		glVertex3f( 0.0f, 1.0f, 0.0f);					// Top Of Triangle (Right)
		glColor3f(0.0f,0.0f,1.0f);						// Blue
		glVertex3f( 1.0f,-1.0f, 1.0f);					// Left Of Triangle (Right)
		glColor3f(0.0f,1.0f,0.0f);						// Green
		glVertex3f( 1.0f,-1.0f, -1.0f);					// Right Of Triangle (Right)
		glColor3f(1.0f,0.0f,0.0f);						// Red
		glVertex3f( 0.0f, 1.0f, 0.0f);					// Top Of Triangle (Back)
		glColor3f(0.0f,1.0f,0.0f);						// Green
		glVertex3f( 1.0f,-1.0f, -1.0f);					// Left Of Triangle (Back)
		glColor3f(0.0f,0.0f,1.0f);						// Blue
		glVertex3f(-1.0f,-1.0f, -1.0f);					// Right Of Triangle (Back)
		glColor3f(1.0f,0.0f,0.0f);						// Red
		glVertex3f( 0.0f, 1.0f, 0.0f);					// Top Of Triangle (Left)
		glColor3f(0.0f,0.0f,1.0f);						// Blue
		glVertex3f(-1.0f,-1.0f,-1.0f);					// Left Of Triangle (Left)
		glColor3f(0.0f,1.0f,0.0f);						// Green
		glVertex3f(-1.0f,-1.0f, 1.0f);					// Right Of Triangle (Left)
	glEnd();											// Done Drawing The Pyramid
	glBegin(GL_QUADS);									
        glColor3f(0.5f,0.0f,1.0f);						// Set The Color To Dark Violet
		glVertex3f( 1.0f,-1.0f, 1.0f);					// Top Right Of The Quad (Bottom)
		glVertex3f(-1.0f,-1.0f, 1.0f);					// Top Left Of The Quad (Bottom)
		glVertex3f(-1.0f,-1.0f,-1.0f);					// Bottom Left Of The Quad (Bottom)
		glVertex3f( 1.0f,-1.0f,-1.0f);					// Bottom Right Of The Quad (Bottom)    
	glEnd();
}

void drawObjects() {
  
    // Place object over the ground
    glTranslatef(0.0f , 1.75f, 0.0f);
    
    // Rotate directions
    glRotatef ((GLfloat) xAxis, 1.0, 0.0, 0.0);
    glRotatef ((GLfloat) yAxis, 0.0, 1.0, 0.0);
    glRotatef ((GLfloat) zAxis, 0.0, 0.0, 1.0);
    
    // choose object to be shown
    if (item == 1)
    	drawQuad();
   	if (item == 2)
   	   	drawPyramid();
   	if (item == 3) {
        glColor3f(1.0f, 0.0f, 0.0f);
        glutSolidSphere(1.0f, 20, 20);
    }
    if (item == 4) {
        glColor3f(1.0f, 0.0f, 0.0f);
        glutWireSphere(1.0f, 20, 20);
    }
   	if (item == 5) {
        glColor3f(0.0f, 1.0f, 0.0f);
   	    glutSolidCone(0.75f, 1.5f, 50, 12);
    }
    if (item == 6) {
        glColor3f(0.0f, 1.0f, 0.0f);
   	    glutWireCone(0.75f, 1.5f, 50, 12);
    }
	

// Hier wollte ich alle Objekte gleichzeitig darstellen & diese immer um die selbe Achse drehen lassen.
// Jedoch funktionier GLLoadIdentity() nicht richtig zum zurücksetzen von glRotateF & glTranslatef

    /*glLoadIdentity();
	
    glTranslatef(5.0f, 1.75f, 5.0f);
    glRotatef ((GLfloat) xAxis, 1.0, 0.0, 0.0);
    glRotatef ((GLfloat) yAxis, 0.0, 1.0, 0.0);
    glRotatef ((GLfloat) zAxis, 0.0, 0.0, 1.0);
    glutWireSphere(0.75f, 20, 20);
    
    glLoadIdentity();
   	
    glTranslatef(5.0f, 1.75f, 10.0f);
    glRotatef ((GLfloat) xAxis, 1.0, 0.0, 0.0);
    glRotatef ((GLfloat) yAxis, 0.0, 1.0, 0.0);
    glRotatef ((GLfloat) zAxis, 0.0, 0.0, 1.0);
   	glutWireCone(0.75f, 1.5f, 4, 12);
   	
    glLoadIdentity();
    
    glTranslatef(0.0f, 1.75f, 15.0f);
    glRotatef ((GLfloat) xAxis, 1.0, 0.0, 0.0);
    glRotatef ((GLfloat) yAxis, 0.0, 1.0, 0.0);
    glRotatef ((GLfloat) zAxis, 0.0, 0.0, 1.0);
    glutWireCone(0.75f, 1.5f, 50, 12);
*/
}

// Turning left and right
void orientMe(float ang) {

/* Camera is only moving in the xz-plane
   lx & lz are mapped onto a unitary circle on the xz-plane */
	lx = sin(ang);
	lz = -cos(ang);
	
	glLoadIdentity();
/* First line: camera position
   Second line: line of sight
   Third line: up vector */
	gluLookAt(x, y, z, 
		      x + lx,y + ly,z + lz,
			  0.0f, 1.0f, 0.0f);
}

// Looking up and down
void lookUpDown(float ang) {

// If sin(ang) = 0 then the line of sight is straight forward.
	ly = sin(ang);
	glLoadIdentity();
	gluLookAt(x, y, z, 
		      x + lx,y + ly,z + lz,
			  0.0f, 1.0f, 0.0f);
}

// Moving forward and backward
void moveMe(int i) {

/* Camera position must be along the line of sight while we move forward or backward.
   "0.1" is the speed value of the movement. To implement running and walking just change it. */
	x = x + i*(lx)*0.1;
	z = z + i*(lz)*0.1;
	glLoadIdentity();
	gluLookAt(x, y, z, 
		      x + lx, y + ly, z + lz,
			  0.0f, 1.0f, 0.0f);
}

// Moving left and right
void strafeMe(int i) {

/* Camera position must be against the line of sight while we move forward or backward.
   Therefore lx & lz are changed to the other coordinate. */
	x = x + i*(lz)*0.1;
	z = z + i*(lx)*0.1;
	glLoadIdentity();
	gluLookAt(x, y, z, 
		      x + lx, y + ly, z + lz,
			  0.0f, 1.0f, 0.0f);
}

// Rendering the scene from the actual point of view 
void renderScene() {

	if (deltaMove)
		moveMe(deltaMove);
	if (deltaStrafe)
	   strafeMe(deltaStrafe);
	if (deltaAngle) {
		angle += deltaAngle;
		orientMe(angle);
	}
	if (deltaAngleUp) {
       angleUp += deltaAngleUp;
       lookUpDown(angleUp);
    }                     
	if (deltaXAxis)
	   xAxis += deltaXAxis % 360;
	if (deltaYAxis)
	   yAxis += deltaYAxis % 360;
	if (deltaZAxis)
	   zAxis += deltaZAxis % 360;
         	
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
// Draw ground
	glColor3f(0.7f, 0.7f, 0.7f);
	glBegin(GL_QUADS);
		glVertex3f(-50.0f, 0.0f, -50.0f);
		glVertex3f(-50.0f, 0.0f,  50.0f);
		glVertex3f( 50.0f, 0.0f,  50.0f);
		glVertex3f( 50.0f, 0.0f, -50.0f);
	glEnd();
	
// Draw sky
   	glColor3f(0.0f, 0.0f, 1.0f);
    glBegin(GL_QUADS);
        glVertex3f(-1000.0f, 200.0f, -1000.0f);
		glVertex3f(-1000.0f, 200.0f,  1000.0f);
		glVertex3f( 1000.0f, 200.0f,  1000.0f);
		glVertex3f( 1000.0f, 200.0f, -1000.0f);
    glEnd();
    
// Draw objects
	glPushMatrix();
    drawObjects();
	glPopMatrix();	
	glutSwapBuffers();
}

void initScene() {

	glEnable(GL_DEPTH_TEST);
    renderScene();
}

// Controls the objects, "w" resets its position to default, "n" & "m" let the camera strafe
void keyDown(unsigned char key, int x, int y) {

	switch (key) {
		case 'w' : 
			xAxis = 0; yAxis = 0; zAxis=0; break;
		case 'a': 
			deltaXAxis = 1; break;
		case 's' : 
			deltaYAxis = 1; break;
		case 'd' : 
			deltaZAxis = 1; break;
       	case '1' : 
			item = 1; break;
       	case '2' : 
			item = 2; break;
	   	case '3' : 
			item = 3; break;
        case '4' : 
			item = 4; break;
	   	case '5' : 
			item = 5; break;
        case '6' : 
			item = 6; break;
        case 'n' :
            deltaStrafe = 1; break;
        case 'm' :
            deltaStrafe = -1; break;
    }
}

// Controls the key release and resets the delta* variables.
void keyUp(unsigned char key, int x, int y) {
     
	switch (key) {
		case 'a' : 
             deltaXAxis = 0; break;
		case 's' : 
             deltaYAxis = 0; break;
		case 'd' : 
             deltaZAxis = 0; break;
        case 'n' :
        case 'm' :
            deltaStrafe = 0; break;
	}    
}

/* Controls the camera and its point of view.
   The delta* variables let the camera continue to move while the key is pressed.
   The "home" key resets the camera position in the yAxis */
void specialKeyDown(int key, int x, int y) {

	switch (key) {
		case GLUT_KEY_LEFT : 
			deltaAngle = -0.01f; break;
		case GLUT_KEY_RIGHT : 
			deltaAngle = 0.01f; break;
		case GLUT_KEY_UP : 
			deltaMove = 1; break;
		case GLUT_KEY_DOWN : 
			deltaMove = -1; break;
		case GLUT_KEY_HOME :
             angleUp = 0.0f; deltaAngleUp = 0.0f; lookUpDown(deltaAngleUp); break;
		case GLUT_KEY_PAGE_UP :
                 deltaAngleUp = 0.005f; break;
        case GLUT_KEY_PAGE_DOWN :
                 deltaAngleUp = -0.005f; break;
    }
}

// Controls the key release and resets the delta* variables.
void specialKeyUp(int key, int x, int y) {

	switch (key) {
		case GLUT_KEY_LEFT : 
		case GLUT_KEY_RIGHT : 
			deltaAngle = 0.0f; break;
		case GLUT_KEY_UP : 
		case GLUT_KEY_DOWN : 
			deltaMove = 0; break;
		case GLUT_KEY_PAGE_UP :
        case GLUT_KEY_PAGE_DOWN :
             deltaAngleUp = 0.0f; break;
	}
}
   
// Glut main function
int main(int argc, char **argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowPosition(100,100);
	glutInitWindowSize(640,480);
	glutCreateWindow("Wii_PP Uebungssgruppe 4");

	initScene();

	glutIgnoreKeyRepeat(1);
	
	glutKeyboardFunc(keyDown);
    glutKeyboardUpFunc(keyUp);
	glutSpecialFunc(specialKeyDown);
	glutSpecialUpFunc(specialKeyUp);

	glutDisplayFunc(renderScene);
	glutIdleFunc(renderScene);

	glutReshapeFunc(changeSize);

	glutMainLoop();

	return(0);
}
```


----------



## Kahino (23. Mai 2008)

Ich hab zwar noch nie openGl gearbeitet, doch da ich die Fehlermeldungen etwas seltsam fand, habe ich mir die Datei die diesen Fehler verursacht etwas näher angeschaut.
Und wenn du unter windows programmierst musst du VOR dem einfügen der OpenGl header <windows.h> einfügen (oder was entsprechendes das WINGDIAPI definiert)

Wenn du das machst kommen jedoch wieder andere Fehler.
Denn auch wenn OpenGl mit den Headern gl.h und glaux.h standardmäßig mit dem Visual Studio mitgeliefert werden, trifft das nicht auf glut zu. 
Glut musst du selbstständig runterladen.

Falls du Windows verwendest, kannst du das hier machen
http://www.xmission.com/~nate/glut.html

Die libdatei musst du dann natürlich ins Libverzeichnis kopieren, die die glut.h datei in include/gl/
und die dll dann in dein projekt.
Dann geht das auch.
Zumindest klappte das dann bei mir (und bei mir kamen beim einfachen kopieren des Quellcodes die gleichen Fehler wie bei dir)


----------

