openGL with c?

hey there,

I'm thinking of solving a math problem in C using openGL graphics.
I tried and it didn't work the way I planned it.
It turned out to be,the answers being displayed in DOS window,and the graphics in openGL window.

I wanna put both of them together in an openGL window.

If this is possible,can anyone please help?

thank you..

Comments

  • SephirothSephiroth Fayetteville, NC, USA
    First off, it sounds like you're writing a console (DOS) program but trying to create an OpenGL window, which would fall under Win32. There should be no "DOS window" when using OpenGL. If you are getting one of those windows, you need to make your project a Win32 or Windows project. That will get rid of the DOS window.

    Next, if you do not know how to write a Windows program, I would suggest picking that up. If your main loop uses the "main()" function, it is a DOS program. If it uses the "WinMain()" or "wWinMain()" functions, it is a Windows program. If you are writing Windows code and not using the "main()" function, then I can't tell you why a DOS window is opening to display results.

    Finally, if you know Win32 and are writing Windows code already, the best place for OpenGL tutorials including full sources, would be NeHe's site. The site is linked below and covers everything you should need to know about programming OpenGL on modern systems. Note that his tutorials pick on on Win9X systems and use only Windows code. He does not offer tutorials for OpenGL in DOS, since DOS is no longer used.

    If you are using DOS code and need to do this in DOS for some reason, I don't know what to tell you. I'd recommend using Google to find some ancient DOS OpenGL guides. I personally did not start programming OpenGL until 98SE was out, so I cannot offer you any DOS advice myself.

    http://nehe.gamedev.net/

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
  • So,emm..I don't quite get that.

    let say,this is the C code:

    #include
    int main()
    {int a;
    printf("enter value for a
    ");
    scanf("%d",&a);

    printf("%d",&a);
    return 0;}

    and this is the openGL:

    #include
    #include
    #include
    #include

    #include

    #define TORUS 0
    #define TEAPOT 1
    #define DOD 2
    #define TET 3
    #define ISO 4
    #define QUIT 5

    static int spin = 0;
    static int obj = TORUS;
    static int begin;

    void
    output(GLfloat x, GLfloat y, char *format,...)
    {
    va_list args;
    char buffer[200], *p;

    va_start(args, format);
    vsprintf(buffer, format, args);
    va_end(args);
    glPushMatrix();
    glTranslatef(x, y, 0);
    for (p = buffer; *p; p++)
    glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
    glPopMatrix();
    }

    void
    menu_select(int item)
    {
    if (item == QUIT)
    exit(0);
    obj = item;
    glutPostRedisplay();
    }

    /* ARGSUSED2 */
    void
    movelight(int button, int state, int x, int y)
    {
    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
    begin = x;
    }
    }

    /* ARGSUSED1 */
    void
    motion(int x, int y)
    {
    spin = (spin + (x - begin)) % 360;
    begin = x;
    glutPostRedisplay();
    }

    void
    myinit(void)
    {
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);

    glDepthFunc(GL_LESS);
    glEnable(GL_DEPTH_TEST);
    }

    /* Here is where the light position is reset after the modeling
    * transformation (glRotated) is called. This places the
    * light at a new position in world coordinates. The cube
    * represents the position of the light.
    */
    void
    display(void)
    {
    GLfloat position[] =
    {0.0, 0.0, 1.5, 1.0};

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glTranslatef(0.0, 0.0, -5.0);

    glPushMatrix();
    glRotated((GLdouble) spin, 0.0, 1.0, 0.0);
    glRotated(0.0, 1.0, 0.0, 0.0);
    glLightfv(GL_LIGHT0, GL_POSITION, position);

    glTranslated(0.0, 0.0, 1.5);
    glDisable(GL_LIGHTING);
    glColor3f(0.0, 1.0, 1.0);
    glutWireCube(0.1);
    glEnable(GL_LIGHTING);
    glPopMatrix();

    switch (obj) {
    case TORUS:
    glutSolidTorus(0.275, 0.85, 20, 20);
    break;
    case TEAPOT:
    glutSolidTeapot(1.0);
    break;
    case DOD:
    glPushMatrix();
    glScalef(.5, .5, .5);
    glutSolidDodecahedron();
    glPopMatrix();
    break;
    case TET:
    glutSolidTetrahedron();
    break;
    case ISO:
    glutSolidIcosahedron();
    break;
    }

    glPopMatrix();
    glPushAttrib(GL_ENABLE_BIT);
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_LIGHTING);
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    gluOrtho2D(0, 3000, 0, 3000);
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();
    output(80, 2800, "Welcome to movelight.");
    output(80, 2650, "Right mouse button for menu.");
    output(80, 400, "Hold down the left mouse button");
    output(80, 250, "and move the mouse horizontally");
    output(80, 100, "to change the light position.");
    glPopMatrix();
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glPopAttrib();
    glutSwapBuffers();
    }

    void
    myReshape(int w, int h)
    {
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(40.0, (GLfloat) w / (GLfloat) h, 1.0, 20.0);
    glMatrixMode(GL_MODELVIEW);
    }

    void
    tmotion(int x, int y)
    {
    printf("Tablet motion x = %d, y = %d
    ", x, y);
    }

    void
    tbutton(int b, int s, int x, int y)
    {
    printf("b = %d, s = %d, x = %d, y = %d
    ", b, s, x, y);
    }

    void
    smotion(int x, int y, int z)
    {
    fprintf(stderr, "Spaceball motion %d %d %d
    ", x, y, z);
    }

    void
    rmotion(int x, int y, int z)
    {
    fprintf(stderr, "Spaceball rotate %d %d %d
    ", x, y, z);
    }

    void
    sbutton(int button, int state)
    {
    fprintf(stderr, "Spaceball button %d is %s
    ",
    button, state == GLUT_UP ? "up" : "down");
    }

    void
    dials(int dial, int value)
    {
    fprintf(stderr, "Dial %d is %d
    ", dial, value);
    spin = value % 360;
    glutPostRedisplay();
    }

    void
    buttons(int button, int state)
    {
    fprintf(stderr, "Button %d is %s
    ", button,
    state == GLUT_UP ? "up" : "down");
    }

    /* Main Loop
    * Open window with initial window size, title bar,
    * RGBA display mode, and handle input events.
    */

    int
    main(int argc, char **argv)
    {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(1280, 800);
    glutCreateWindow("First Project");
    myinit();
    glutMouseFunc(movelight);
    glutMotionFunc(motion);
    glutReshapeFunc(myReshape);
    glutDisplayFunc(display);
    glutTabletMotionFunc(tmotion);
    glutTabletButtonFunc(tbutton);
    glutSpaceballMotionFunc(smotion);
    glutSpaceballRotateFunc(rmotion);
    glutSpaceballButtonFunc(sbutton);
    glutDialsFunc(dials);
    glutButtonBoxFunc(buttons);
    glutCreateMenu(menu_select);
    glutAddMenuEntry("Torus", TORUS);
    glutAddMenuEntry("Teapot", TEAPOT);
    glutAddMenuEntry("Dodecahedron", DOD);
    glutAddMenuEntry("Tetrahedron", TET);
    glutAddMenuEntry("Icosahedron", ISO);
    glutAddMenuEntry("Quit", QUIT);
    glutAttachMenu(GLUT_RIGHT_BUTTON);
    glutMainLoop();

    return 0; /* ANSI C requires main to return int. */
    }

    How do I mix both codes together,so that,the program will only execute in OpenGl window?
  • SephirothSephiroth Fayetteville, NC, USA
    I just glanced over your code, but for one I see you using the additional GLUT library. I don't use it and don't advocate its usage. I write my GL apps using plain old GL and Win32 code.

    I do see "output()" being called. That is a shell (DOS) function. It will not work in Windows code. You have to map your characters to polygons in GL to display them as far as I know. If that is over your head, you might be able to use "TextOut()", but I have never used that function on a GL window before, only normal application windows. Can't hurt to try it though!

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories