Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6586639
  • 博文数量: 227
  • 博客积分: 10047
  • 博客等级: 上将
  • 技术积分: 6678
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-11 10:33
个人简介

网上的蜘蛛

文章分类

全部博文(227)

文章存档

2010年(19)

2009年(29)

2008年(179)

分类: C/C++

2008-11-14 19:09:35

虽然已经是好几年前写的了。本来以为从那以后都不会再接触OpenGL,可是现在居然还在修这门课。令人头大的是project真的不好做...现在这个"An interactive 3d terrain game"估计我是没有办法全部搞定了。所以为了那些跟我一样迷茫的人,把我当年写的分享下吧。也许不是很perfect,但是我想参考下还是不错的。第一个我记得是用线条画一座简单的房子~
要求我就不写了。b/B->开始画,m/M开始移动,f/F->新增一条不连续的线条,d/D删除指定的线条等,看代码吧。

#ifndef POINTARRAY_H
#define POINTARRAY_H
#include "point.h"
class GLintPointArray
{
public:
    
    GLintPoint pt[60]; //initiate the array with the index 60

    GLintPoint CP; //the current point

    //GLintPoint CP1;//another point


};
#endif

Array用来保存点啊~

#ifndef POINT_H
#define POINT_H
class GLintPoint
{
public:
    //its constructure functions

    GLintPoint(){x=y=0.0f;}
    GLintPoint(int xx,int yy){x=xx,y=yy;}
   //set the value of x

    void setX(int xx){x=xx;}
     //set the value of y

    void setY(int yy){y=yy;}
    //get the value of x

    float getX(){return x;}
    //get the value of y

    float getY(){return y;}

    //set the arribute of the point

    void setFlag(int f){flag=f;}
    //get the attribut of the point,1 or 0

    int getFlag(){return flag;}
private:
    int x,y;
    int flag;
    /*1 stands for
    starting another point of the image,0,stands
    for continuity of this point*/

};
#endif

接下来是实现~:

/*
Author's name: Leyond Lin
Author's Blog: http://yexin218.cublog.cn
Student's ID:04009853F-I011-0032
declaration: These codes just for study. If you want to use my codes
in your own program, please keep the information of author, thanks.
Data:2005-3-5
*/


#include "pointArray.h"
#include "stdlib.h"
#include <GL/glut.h>
#include <GL/glu.h>
#include <iostream>
using namespace std;

int operation=0; //operation of begin, delete,move ,refresh, quit

int selection; //selecton of GL_LINE_LOOP¡¡or GL_LINER_STRIP

GLint WinHeight=480,WinWidth=480;//set the value of the Windows

static GLintPointArray poly;//creat an instantiation of GLintPointArray

int current=-1; //the current number of the points

int deleteIndex,moveIndex; //the index of deleting and moving point

int NUM=0;
int F=0,flag[20]={0};    // an array stores the number of Starting points



void drawPolyLine(GLintPointArray p,int selection)
{
    int v=0;//parameter of the number of the strating points

    //using the array flag to store the index of the starting points

    for(int i=0;i<=current;i++)
    {
        if(poly.pt[i].getFlag()==1)
            flag[++v]=i;
    }

    //print the indexs

    /*for(int i=0;i<20;i++)
        cout<<"the index of the strating points"<     cout<
    //draw the integrated image with the beginning of the starting point

     for(int k=0;k<NUM;k++){
     glBegin(selection==5? GL_LINE_LOOP:GL_LINE_STRIP);
     for(int m=flag[k];m<flag[k+1];m++)
         glVertex2i(p.pt[m].getX(),p.pt[m].getY());
     glEnd();
        
    }
    //draw the image of the rest part

    glBegin(selection==5? GL_LINE_LOOP:GL_LINE_STRIP);
     for(int n= flag[NUM];n<=current;n++)
     glVertex2i(p.pt[n].getX(),p.pt[n].getY());
     glEnd();

    //glFlush();

}
void mykey(unsigned char key, int mouseX, int mouseY)
{
   //GLint x= mouseX;

  // GLint y = mouseY;


   switch(key)
   {
   case 'b': // presents for creating a new polyLine

   case 'B':
     operation = 1;
     cout<< "operation is Beginning"<<endl;
     break;
   case 'd': //presents for deleting the next point pointed to

   case 'D':
     operation = 2;
     cout<< "operation is Deleting"<<endl;
     break;
   case 'm': //presents for moveing the point

   case 'M':
     operation = 3;
     cout<< "operation is Moving"<<endl;
     break;
   case 'r'://refresh the screen and redraw the polylines

   case 'R':
     glClear(GL_COLOR_BUFFER_BIT);
        current=-1;
        operation=0;
        F=0;
        NUM=0;
         cout<< "operation is erasing "<<endl;
        glFlush();
     break;
   case 'l': //do the action of GL_LINE_LOOP

   case 'L':
     selection = 5;
     cout<<"selecton is LOOP"<<endl;
     break;
   case 's'://do the actions of GL_LINE_STRIP

   case 'S':
     selection = 6;
     cout<<"selecton is STRIP"<<endl;
     break;
   case 'f'://presents for a new staring point

   case 'F':
     F=1;
     NUM++;
     cout<<"NUM is"<<NUM<<endl;
     break;
   case 'g'://reset the value of NUM to 0

   case 'G':
     F=0;
     cout<<"NUM is"<<NUM<<endl;
     break;
   case 'q'://quit the programming

   case 'Q':
     exit(-1);
   default:
     break;
   }
   //glFlush();

}

void mymouse(int button,int state,int mouseX,int mouseY)
{
   
    //to creat a new polyline

    if(button == GLUT_LEFT_BUTTON && state ==GLUT_DOWN && current< 60 && operation == 1)
     {
         if(current == 59 || NUM == 19){
         cout<<"YOU COULD NOT ADD MORE POINTS!!!"<<endl;
         exit(-1);
     }
    
         poly.pt[++current].setX(mouseX);
         poly.pt[current].setY(WinHeight-mouseY);
         poly.pt[current].setFlag(F);
         cout<<"point begin ("<<poly.pt[current].getX()<<","<<
             poly.pt[current].getY()<<" ) with the flag of "<<poly.pt[current].getFlag()<<endl;
           glClear(GL_COLOR_BUFFER_BIT);
         drawPolyLine(poly,selection);
    }
   
    //to delete the point

    if(button==GLUT_LEFT_BUTTON && state ==GLUT_DOWN && operation==2)
    {
        poly.CP.setX(mouseX);
        poly.CP.setY(WinHeight-mouseY);
        //cout<<"point to be deleted is:"<
        for(int i= 0;i<=current;i++)
        { int subX = poly.pt[i].getX() - poly.CP.getX();
         int subY = poly.pt[i].getY() - poly.CP.getY();
            if((subX < 8 && subX > -8)&&(subY < 8 && subY > -8))
            {
                deleteIndex =i;
                break;
            
            }
        }
        cout<<"point ("<<poly.pt[deleteIndex].getX()<<","<<poly.pt[deleteIndex].getY()<<") be deleted"<<endl;
       //set the next point's attribute if current's flag =1

        if(poly.pt[deleteIndex].getFlag()==1)
            poly.pt[deleteIndex+1].setFlag(1);
     //refresh the array

        for(int k=deleteIndex; k<=current;k++)
        { poly.pt[k].setX(poly.pt[k+1].getX());
         poly.pt[k].setY(poly.pt[k+1].getY());
         poly.pt[k].setFlag(poly.pt[k+1].getFlag());
        
        }
         current--;
        
           glClear(GL_COLOR_BUFFER_BIT);
         drawPolyLine(poly,selection);
    }

    //to move the point

    if(button==GLUT_LEFT_BUTTON && state ==GLUT_DOWN && operation==3)
    {
        poly.CP.setX(mouseX);
        poly.CP.setY(WinHeight-mouseY);

        //cout<<"current to moving is"<
        for(int i= 0;i<=current;i++)
          { int subX =poly.pt[i].getX()-poly.CP.getX();
         int subY = poly.pt[i].getY() - poly.CP.getY();
             if((subX< 8 && subX> -8)&&(subY < 8 &&subY > -8))
             {
                 moveIndex =i;
             break;
             }
        }
        // to move a new one when click again

     if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN && operation==3)
     {
             poly.CP.setX(mouseX);
         poly.CP.setY(WinHeight-mouseY);
             poly.pt[moveIndex].setX(poly.CP.getX());
     poly.pt[moveIndex].setY(poly.CP.getY());
             //poly.pt[moveIndex].setFlag();

             cout<<"destinaton to moving is ("<<poly.CP.getX()<<","<<poly.CP.getY()<<")"<<endl;

             /*show a new polylines
             for(int i=0;i<=current;i++)
         {
             cout<         }*/

     }
           glClear(GL_COLOR_BUFFER_BIT);
         drawPolyLine(poly,selection);
    }

    
    glFlush();
}

void reshape(GLsizei w, GLsizei h)
{
  glViewport(0, 0, w, h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   gluOrtho2D(0, w , 0, h);
   glMatrixMode(GL_PROJECTION);
   //drawPolyLine(poly,selection);

  glFlush();

    
}
void display(void)
{
   glClear(GL_COLOR_BUFFER_BIT);
   glColor3f(1.0, 1.0, 0.0);
   drawPolyLine(poly,selection);
   glFlush();
}

void init(void)
{
   glClearColor(0.0, 0.0, 1.0, 1.0);
   glColor3f(0.0f,0.0f,0.0f);
  glPointSize(2.0);

}


int main(int argc, char** argv)
{
    
   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
   glutInitWindowSize(480,480);
   glutInitWindowPosition(0, 0);
   glutCreateWindow("Drawing a House,0409853F-I011-0032");
   glutReshapeFunc(reshape);
   glutDisplayFunc(display);
   glutKeyboardFunc(mykey);
   glutMouseFunc(mymouse);
   init();
   glutMainLoop();
   return 0;
}

运行效果:

注:自作参考只用~

阅读(5876) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

zhaorongsu2008-11-14 22:43:17

呼呼 老大 一定都要是代码呀 ,地球人都看不懂啊