[openGl][glut.h] Bekomm nur grun und braune farb tone hilfe



  • Hallo,

    hab in internet code gefunden das fractal baume zeichnet zimlich gut sogar. Hab den code irgentwie am laufen bekomen aber das broplemm ich bekomm nur braun - grun farb tone. Das ist der code. Ich erkler speter problem.

    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <math.h>
    #include <glut.h>
    
    #ifndef M_PI
    #define M_PI 3.1415926
    #endif
    
    #define WIDTH           300 
    #define HEIGHT          200 
    #define MAX_DEPTH 4
    int height,width;
    enum clist {LEVEL0 = 1, LEVEL1, LEVEL2, LEVEL3, LEAFE, LAWN, BOGUS};
    
    typedef struct figure figure;
    struct figure
    {
      GLdouble rot_x_angle;
      GLdouble rot_z_angle;
      GLdouble trans_x;
      GLdouble trans_y;
      GLdouble trans_z;
      enum clist branch_type;
      figure *child;
      figure *sibling;
    };
    
    figure *fig_tree;
    
    GLdouble vXZ = 0.0, vY = M_PI/4;
    int oldX, oldY;
    double zoom = 30.0;
    
    /* Light definition */
    const GLfloat l0amb[4] = { (GLfloat)0.5, (GLfloat)0.5, (GLfloat)0.5, (GLfloat)1.0 };
    const GLfloat l0diff[4] = { (GLfloat)0.8, (GLfloat)0.8, (GLfloat)0.8, (GLfloat)1.0 };
    const GLfloat l0spec[4] = { (GLfloat)0.8, (GLfloat)0.8, (GLfloat)0.8, (GLfloat)1.0 };
    GLfloat l0pos[4] = { (GLfloat)10.0, (GLfloat)4.0, (GLfloat)5.0, (GLfloat)1.0 };
    
    /* Material definitions */
    const GLfloat m0spec[] = { (GLfloat)0.0, (GLfloat)0.0, (GLfloat)0.0 };
    const GLfloat m0amb[] = { (GLfloat)0.2, (GLfloat)0.10, (GLfloat)0.0 };
    const GLfloat m0diff[] = { (GLfloat)0.2, (GLfloat)0.10, (GLfloat)0.0 };
    const GLfloat m0shine = (GLfloat)0.0;
    
    const GLfloat m1spec[] = { (GLfloat)0.0, (GLfloat)0.3, (GLfloat)0.0 };
    const GLfloat m1amb[] = { (GLfloat)0.2, (GLfloat)0.4, (GLfloat)0.1 };
    const GLfloat m1diff[] = { (GLfloat)0.2, (GLfloat)0.4, (GLfloat)0.1 };
    const GLfloat m1shine = (GLfloat)40;
    
    GLvoid resize(GLsizei, GLsizei); 
    GLvoid initializeGL();
    GLvoid drawScene(GLvoid); 
    
    /* Funktion som anropas när något av fönstren ändrar storlek */
    GLvoid resize (GLsizei width, GLsizei height) {
    
      /* Fixar till perspektivet  på världen */
      glViewport(0, 0, width, height);
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      gluPerspective(120.0, ((GLdouble) width) / ((GLdouble) height), 1.0, 180.0);
    }
    
    void drawFigTree(figure *fig)
    {
      glPushMatrix();
    
      glTranslated(fig->trans_x, fig->trans_y, fig->trans_z);
      glRotated(fig->rot_z_angle, 0.0, 0.0, 1.0);
      glRotated(fig->rot_x_angle, 1.0, 0.0, 0.0);
    
      glCallList(fig->branch_type);
    
      if (fig->child != NULL)
        drawFigTree(fig->child);
    
      glPopMatrix();
    
      if (fig->sibling != NULL)
        drawFigTree(fig->sibling);
    }
    
    /* Display the scene */
    void drawScene(void)
    {
    
      glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
    
      gluLookAt(sin(vXZ) * cos(vY) * zoom, sin(vY) * zoom, cos(vXZ) * cos(vY) * zoom, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
      l0pos[0] = (GLfloat) (sin(vXZ) * cos(vY) * zoom);
      l0pos[1] = (GLfloat) (sin(vY) * zoom);
      l0pos[2] = (GLfloat) (cos(vXZ) * cos(vY) * zoom);
    
     	glColor4f(0.5, 0, 0, 0);
    	glutSolidSphere(3, 20, 20); 
      glLightfv(GL_LIGHT0, GL_POSITION, l0pos);
    
      glCallList(LAWN);
    
      drawFigTree(fig_tree);
      glutSwapBuffers();
    }
    
    figure *buildTreeStructure(figure *base, int depth) {
      int i, no_of_children;
      figure *cursor;
    
      base->trans_x = 0.0;
      base->trans_z = 0.0;
      base->rot_x_angle = -50 + 100*((double) rand()/RAND_MAX);
      base->rot_z_angle = -50 + 100*((double) rand()/RAND_MAX);
      base->sibling = NULL;
      switch(depth) {	
      case 0:
        base->branch_type = LEVEL0;
        base->trans_y = 0.0;
        break;
      case 1:
        base->branch_type = LEVEL1;
        base->trans_y = 6.0;
        break;
      case 2:
        base->branch_type = LEVEL2;
        base->trans_y = 4.0;
        break;
      case 3:
        base->branch_type = LEVEL3;
        base->trans_y = 3.0;
        break;
      case 4:
        base->rot_x_angle = -180 + 360*((double) rand()/RAND_MAX);
    	base->rot_z_angle = -180 + 360*((double) rand()/RAND_MAX);
    
        base->branch_type = LEAFE;
        base->trans_y = 2.0;
      }
    
      if (depth == MAX_DEPTH) {
        base->child = NULL;
        return base + 1;
      } else {
        base->child = base + 1;
    
        no_of_children = 5;
    
        cursor = base->child;
        base = base->child;
    
        for (i = 0; i < no_of_children; i++) {
          cursor = buildTreeStructure(base, depth + 1);
          if (i != (no_of_children - 1))
    		  base->sibling = cursor;
          else
    		  base->sibling = NULL;
          base = cursor;
    
        }
        return cursor;
      }
    }
    
    void buildForest()
    {
      figure *cursor, *next;
      int i, no_of_trees;
    
      no_of_trees = 1;
    
      /* Generate tree */
      fig_tree = malloc(sizeof(figure) * (size_t)no_of_trees * (size_t)pow(5, MAX_DEPTH+1));
      srand((unsigned) time (NULL));
    
      cursor = fig_tree + 1;
      fig_tree->branch_type = BOGUS;
      fig_tree->rot_x_angle = 0.0;
      fig_tree->rot_z_angle = 0.0;
      fig_tree->trans_x = 0.0;
      fig_tree->trans_y = 0.0;
      fig_tree->trans_z = 0.0;
    
      fig_tree->child = cursor;
      fig_tree->sibling = NULL;
    
      for (i = 0; i < no_of_trees; i++) {
        next = buildTreeStructure(cursor, 0);
        cursor->rot_x_angle = 0;
        cursor->rot_z_angle = 0;
        cursor->trans_x = 0; //-20 + 40*((double) rand()/RAND_MAX);
        cursor->trans_y = 0; //0.0;
        cursor->trans_z = 0; //-20 + 40*((double) rand()/RAND_MAX);
        if (i != (no_of_trees - 1))
          cursor->sibling = next;
        else
          cursor->sibling = NULL;
        cursor = next;
      }
    }
    
    /* Create objects used in the model (tree and lawn) */
    GLvoid defineCLists()
    { 
      GLUquadricObj *quadObj; 
    
      /* Lawn */
      glNewList(LAWN, GL_COMPILE);
      glEnable(GL_COLOR_MATERIAL);
      glColor4d(0.0, 0.65, 0.30, 0.8);
      glBegin(GL_QUADS);
      glTexCoord2d (0.0, 0.0);
      glVertex3d(-50.0, 0.0, -50.0);
      glTexCoord2d(5.0, 0.0);
      glVertex3d(50.0, 0.0, -50.0);
      glTexCoord2d (5.0, 5.0);
      glVertex3d(50.0, 0.0, 50.0);
      glTexCoord2d (0.0, 5.0);
      glVertex3d(-50.0, 0.0, 50.0);
      glEnd();
      glDisable(GL_COLOR_MATERIAL);
      glEndList();
    
      glNewList(LEVEL0, GL_COMPILE);
      glPushMatrix();
      glMaterialfv(GL_FRONT, GL_AMBIENT, m0amb);
      glMaterialfv(GL_FRONT, GL_DIFFUSE, m0diff); 
      glMaterialfv(GL_FRONT, GL_SPECULAR, m0spec);
      glMaterialf(GL_FRONT, GL_SHININESS, m0shine);
      glRotated(-90.0, 1.0, 0.0, 0.0);
      quadObj = gluNewQuadric ();
      gluQuadricDrawStyle (quadObj, GLU_FILL); 
      gluQuadricNormals (quadObj, GLU_SMOOTH); 
      gluCylinder(quadObj, 0.5, 0.4, 6.0, 5, 5);
      glPopMatrix();
      glEndList();
    
      glNewList(LEVEL1, GL_COMPILE);
      glPushMatrix();
      glMaterialfv(GL_FRONT, GL_AMBIENT, m0amb);
      glMaterialfv(GL_FRONT, GL_DIFFUSE, m0diff); 
      glMaterialfv(GL_FRONT, GL_SPECULAR, m0spec);
      glMaterialf(GL_FRONT, GL_SHININESS, m0shine);
      glRotated(-90.0, 1.0, 0.0, 0.0);
      quadObj = gluNewQuadric ();
      gluQuadricDrawStyle (quadObj, GLU_FILL); 
      gluQuadricNormals (quadObj, GLU_SMOOTH); 
      gluCylinder(quadObj, 0.35, 0.20, 4.0, 5, 5);
      glPopMatrix();
      glEndList();
    
      glNewList(LEVEL2, GL_COMPILE);
      glPushMatrix();
      glMaterialfv(GL_FRONT, GL_AMBIENT, m0amb);
      glMaterialfv(GL_FRONT, GL_DIFFUSE, m0diff); 
      glMaterialfv(GL_FRONT, GL_SPECULAR, m0spec);
      glMaterialf(GL_FRONT, GL_SHININESS, m0shine);
      glRotated(-90.0, 1.0, 0.0, 0.0);
      quadObj = gluNewQuadric ();
      gluQuadricDrawStyle (quadObj, GLU_FILL); 
      gluQuadricNormals (quadObj, GLU_SMOOTH);
      gluCylinder(quadObj, 0.17, 0.10, 3.0, 3, 3);
      glPopMatrix();
      glEndList();
    
      glNewList(LEVEL3, GL_COMPILE);
      glPushMatrix();
      glMaterialfv(GL_FRONT, GL_AMBIENT, m0amb);
      glMaterialfv(GL_FRONT, GL_DIFFUSE, m0diff); 
      glMaterialfv(GL_FRONT, GL_SPECULAR, m0spec);
      glMaterialf(GL_FRONT, GL_SHININESS, m0shine);
      glRotatef(-90.0, 1.0, 0.0, 0.0);
      quadObj = gluNewQuadric ();
      gluQuadricDrawStyle (quadObj, GLU_FILL); 
      gluQuadricNormals (quadObj, GLU_SMOOTH); 
      gluCylinder(quadObj, 0.09, 0.05, 2.0, 3, 3);
      glPopMatrix();
      glEndList();
    
      glNewList(LEAFE, GL_COMPILE);
      glMaterialfv(GL_FRONT, GL_AMBIENT, m1amb);
      glMaterialfv(GL_FRONT, GL_DIFFUSE, m1diff); 
      glMaterialfv(GL_FRONT, GL_SPECULAR, m1spec);
      glMaterialf(GL_FRONT, GL_SHININESS, m1shine);
      glBegin(GL_QUADS);
      glNormal3d(0.0, 1.0, 0.0);
      glVertex3d(0.0, 0.0, 0.0);
      glVertex3d(0.4, -0.5, 0.0);
      glVertex3d(1.5, 0.0, 0.0);
      glVertex3d(0.4, 0.5, 0.0);
      glEnd();
      glEndList();
    
      glNewList(BOGUS, GL_COMPILE);
      glEndList();
    
    }
    
    void init(void) 
    {
    
      /* Make some initiations */
      glClearColor((GLfloat)0.4, (GLfloat)0.4, (GLfloat)0.5, (GLfloat)0.0);
      glClearDepth( 50.0 );
      glEnable(GL_DEPTH_TEST);
      defineCLists();
    
      /* Enable light */
      glEnable(GL_LIGHTING);
      glEnable(GL_LIGHT0);
      glEnable(GL_NORMALIZE);
      glLightfv(GL_LIGHT0, GL_AMBIENT, l0amb);
      glLightfv(GL_LIGHT0, GL_DIFFUSE, l0diff);
      glLightfv(GL_LIGHT0, GL_SPECULAR, l0spec);
    
      buildForest();
    
    }
    
    void reshape(int w, int h)
    {
      height=h;
      width=w;
      // define the visible area of the window ( in pixels )
      if (h==0) h=1;
      glViewport(0,0,w,h); 
    
      // Setup viewing volume
    
      glMatrixMode(GL_PROJECTION); 
      glLoadIdentity();
    
      //          L	     R 	     B 		T		 N	   F
      glOrtho (-150.0f, 150.0f, -150.0f, 150.0f, 500.0f, -500.0f);
    
    }
    int main(int argc, char** argv)
    {
       //Hier werden die elementaren Dinge eingestellt
    
       glutInit(&argc, argv); 
    
       //wie z.B. das Fenster
       //hier die Größe
       glutInitWindowSize (1024, 800); 
    
       //der Ort wo das Fenster erscheinen soll
       glutInitWindowPosition (0, 0);
    
       //und der Titel des Fensters
       glutCreateWindow ("Master's Golem");
    
       init ();
     // buildForest();
    
       //Hier wird angegeben welche Funktion die Befehlen fürs Zeichnen enthält
       //wir haben sie "zeichenfunktion" genannt
       glutDisplayFunc(drawScene);
    
       glutReshapeFunc(resize);
    
        //glutTimerFunc (MILLISEC_PRO_FRAME, timer_func, 1);
    
       glutMainLoop();
       return 0;
    }
    

    Am anfang hab bekomm ich ales code gezeichnet also ein baum und ne kugel. Wie der code hier sagt

    glColor4f(0.5, 0, 0, 0);
    	glutSolidSphere(3, 20, 20); 
      glLightfv(GL_LIGHT0, GL_POSITION, l0pos);
    
      glCallList(LAWN);
    
      drawFigTree(fig_tree);
      glutSwapBuffers();
    

    Speter aber also wen alles wieder neu gezeichnet wird. ( also entweder lange warten oder fenster versteken und wieder in vordergrund tuhen). Bekomm ich die kugel in grun!!!!!!!!!!. Irgentwas geschied in der funktion glCallList(LAWN); hab in nett gesucht und gefunden das die funktion defineCLists() in den gpu speicher geladen wird also ist die funktion problematisch. Bin aber noch ein n00b in opengl und das ist mir alles zu hoch kann mir einer den code repariren plz :'(?.



  • ich bekomm nur braun - grun farb tone

    const GLfloat m0diff[] = { (GLfloat)0.2, (GLfloat)0.10, (GLfloat)0.0 };
    

    braun.



  • Ok und wie kann ich das ruckengich machen 😕 hab kein plan der code is net von mier :/. Da wo die Kugel ist will ich mein kramm hin zeichnen aber dann wir alles grun.



  • du kannst die auswirkungen der drei farbkomponenten leicht selbst durch ausprobieren ermitteln.
    ohne derartige grundlagen verstanden zu haben, solltest du dich nicht mit opengl beschaeftigen.



  • pffffff, ok danke fur deine hilfe, ja ich spiel jetzt mit meinen farben das lost sicher mein problem :P. Vielen dank fur deine prezisen andword n4p!.


Anmelden zum Antworten