Robotics C++ Physics II AP Physics B Electronics Java Astronomy Other Courses Summer Session  

Examples

 

Example 2a

 

 

Example 1: Windows Without OpenGL - Using Win32 Application Project

 

Note use of WinMain ( )

 

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    MessageBox (NULL, "\tHello, world!", "My Firswt Windows Application", NULL);
    return 0;
}

 

  

Example 2: OpenGL Using Win32 Console Application Project

 

//Note use of main ( )

 

#include <gl/glut.h>

void renderScene(void)

{
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_TRIANGLES);
        glVertex3f(-0.5,-0.5,0.0);
        glVertex3f(0.5,0.0,0.0);
        glVertex3f(0.0,0.5,0.0);
    glEnd();
    glFlush();
}

void main(int argc, char **argv)

{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(320,320);
    glutCreateWindow("First Program Using OpenGL");
    glutDisplayFunc(renderScene);
    glutMainLoop();
}
 

 

Example 2a:

Duplicate the above triangle, except make the base parallel to the x axis

 

Example 3: OpenGL Using Win32 Cosole Application Project

 

//Note use of main ( )

 

#include <windows.h>
#include <gl/Gl.h>       //superfluous, included in glut.h
#include <gl/Glu.h>    //superfluous, included in glut.h
#include <gl/glut.h>
#include <math.h>


void myInit()

{
    glClearColor(1.0,1.0,1.0,0.0);
    glColor3f(1.0f, 0.0f, 0.0f);
    glPointSize(4.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}

void myDisplay()

{
    glClear(GL_COLOR_BUFFER_BIT);
   
    glBegin(GL_POINTS);
    glVertex2i(100, 50);
    glVertex2i(100, 130);
    glVertex2i(150, 130);
    glEnd();
    glFlush();
}

int main()

{
    char* argv[1];
    char dummyString[8];
    argv[0] = dummyString;
    int argc= 1;

    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(640, 480);
    glutInitWindowPosition(100, 150);
    glutCreateWindow("My Window");

    glutDisplayFunc(myDisplay);

    myInit();
    glutMainLoop();

    return 0;
}