⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 molecule.c

📁 本文件是ni公司的labwindows/cvi软件的部分例子和源程序
💻 C
字号:
//----------------------------------------------------------------------------// Module to display a "molecule" as spheres//----------------------------------------------------------------------------#include <windows.h>#include <GL\gl.h>#include <GL\glu.h>#include <userint.h>#include <ansi_c.h>#include "toolbox.h"#include "ogldemo.h"#define C_COLOR glColor3f(0.0f, 0.0f, 1.0f)#define H_COLOR glColor3f(1.0f, 0.0f, 0.0f)#define O_COLOR glColor3f(0.0f, 1.0f, 0.0f)#define N_COLOR glColor3f(1.0f, 1.0f, 1.0f)#define HN_COLOR glColor3f(1.0f, 0.0f, 1.0f)#define NA_COLOR glColor3f(1.0f, 1.0f, 0.0f)typedef struct tag_Molecule{    char AtomName[10];    float X;    float Y;    float Z;}MOLECULE;  struct {    float X;     float Y;     float Z;}gAngle={0.0,0.0,0.0};// Global variablesstatic ListType List=NULL;     static float        gZoomZ=0.0;           static int molecule_list=0;static int reload = 0;static int prevFastFlag = 0;// Local functionsvoid RenderMolecule(int fastFlag);            static void SetupRC(int fastFlag);    static void ReadMolecule(ListType *List);static void DrawMolecule(ListType *List,int fastFlag);void ConvertString(char *strBuff,ListType localList,int *Val){    register int i;    MOLECULE        Mol;    char                c;    RemoveSurroundingWhiteSpace (strBuff);    if(strlen(strBuff)==0)        return;    else{           // the line can be a comment, the number of atom or an atom        if(!*Val){            *Val = atoi(strBuff);            if(!*Val && strBuff[0]!='#'){                sscanf(strBuff, "%s %f %f %f", Mol.AtomName, &Mol.X, &Mol.Y, &Mol.Z);                   ListInsertItem (localList, &Mol, END_OF_LIST);                return;            }               else                return;        }           if(strBuff[0]!='#'){            sscanf(strBuff, "%s %f %f %f", Mol.AtomName, &Mol.X, &Mol.Y, &Mol.Z);               for(i=0; i<strlen(Mol.AtomName); i++)                Mol.AtomName[i] = toupper(Mol.AtomName[i]);                            ListInsertItem (localList, &Mol, END_OF_LIST);        }    }   }int LoadFile(char *Path){    char                strBuff[255];    FILE                *Stream;    ListType localList = NULL;    int Val = 0;    Stream = fopen (Path, "r");    if(Stream==NULL)        return -1;        localList = ListCreate (sizeof(MOLECULE));    if (!localList)        return -1;    while(fgets (strBuff, 255, Stream))        ConvertString(strBuff,localList,&Val);    fclose(Stream);    if(Val && Val!=ListNumItems (localList))    {        ListDispose(localList);        return -1;    }    else        if (List)            ListDispose(List);            List = localList;    reload = 1;    return 0;}// Read a molecule in a .xyz file and draw itint LoadMoleculeFromArray(char **molecule){    char **ptr = molecule;    int Val = 0;    ListType localList = NULL;    localList = ListCreate (sizeof(MOLECULE));    if (!localList)        return -1;    for(;*ptr;ptr++)        ConvertString(*ptr,localList,&Val);            if(Val && Val!=ListNumItems (localList))    {        ListDispose(localList);        return -1;    }    else        if (List)            ListDispose(List);            List = localList;    reload = 1;    return 0;}       // Read a molecule in a .xyz file and draw itint LoadMolecule(void){        static char Path[MAX_PATHNAME_LEN];    int                 Status;        Status = FileSelectPopup ("", "*.xyz", "", "Select a File", VAL_SELECT_BUTTON, 0, 1, 1, 0, Path);    if(Status==VAL_NO_FILE_SELECTED)        return -1;    return LoadFile(Path);}// bUpdate indicates if the Rendering context need to be refreshed or not// this occurs only when a new Molecule is drawn for the first timevoid RenderMolecule(int fastFlag){    if (prevFastFlag != fastFlag)    {        prevFastFlag = fastFlag;        reload = 1;    }        // Draw the molecule    if(glIsList(molecule_list) && !reload)        glCallList(molecule_list);         else    {        SetupRC(fastFlag);        if(glIsList(molecule_list))            glCallList(molecule_list);       }}// Set up the rendering context AND teach Opengl how to draw// the current molecule// A list is involved to speed up the drawing processstatic void SetupRC(int fastFlag){    reload = 0;     if(!glIsList(molecule_list))        molecule_list = glGenLists(1);            if(glIsList(molecule_list))    {        glNewList(molecule_list, GL_COMPILE);        if (glGetError()  != GL_NO_ERROR)            printf("Error Found\n");    }    DrawMolecule(&List,fastFlag);    if(glIsList(molecule_list))        glEndList();    return;}// This function is only call once per molecule// to let OpenGL learn how to fraw the molecule// See the use of glCallList(molecule_list) in// RenderScene functionstatic void DrawMolecule(ListType *List,int fastFlag){        int                     NbItem=0;    int                     i;    MOLECULE            *Ptr=NULL;    GLUquadricObj   *obj;        obj = gluNewQuadric();    if (fastFlag)        gluQuadricDrawStyle(obj,GLU_POINT);            NbItem = ListNumItems(*List);    for(i=1; i<=NbItem; i++){        Ptr = ListGetPtrToItem (*List, i);                // This part really need more work ...        // the aim of the game is to select the color         // corresponding to the atom name        // In first approximation we make a difference between 2 letters         // atom name and one letter atom name                // One letter atom name        if(strlen(Ptr->AtomName)==1){            switch(Ptr->AtomName[0]){                case 'C':                    C_COLOR;                break;                case 'H':                    H_COLOR;                break;                case 'O':                    O_COLOR;                break;                case 'N':                    N_COLOR;                break;                // The program found a one letter atom name for which no color has been defined                default:                    assert(FALSE);            }           // Two letters atom name            }else{            if(!strcmp(Ptr->AtomName, "HN"))                HN_COLOR;            else if(!strcmp(Ptr->AtomName, "NA"))                NA_COLOR;            else                // The program found a two letter atom name for which no color has been defined                assert(FALSE);        }                glPushMatrix();            glTranslatef(Ptr->X/4, Ptr->Y/4, Ptr->Z/4);            gluSphere(obj, 0.125, 10, 10);          glPopMatrix();      }    gluDeleteQuadric(obj);    return;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -