opengl 学习第一日

#include "stdafx.h"

#include <windows.h>

#include <GL/glu.h>

#include <GL/gl.h>

#include <GL/glut.h>

#include <GL/glaux.h> 

#include <gl\glut.h>

#include <string> 

using namespace std;

//http://space.itpub.net/17267437/viewspace-545635

//http://www.cnblogs.com/CodeBlove/articles/1669945.html



/////////////////////////////////////init



float x1 = 100;

float y1 = 150;

int size = 50;



float setp = 1; 

float ww,wh;



void SetupRC()

{

    glClearColor(0.0f,0.0f,1.0f,1.0f);

}



void RenderSence()

{

    glClear(GL_COLOR_BUFFER_BIT);

        glColor3f(1.5,0.5,0.5);

        glRectf(x1,y1,x1+size,y1+size);

    glutSwapBuffers();

    //glFlush();

}

/*

修剪视图两个重要方法

glViewport

glOrtho

*/

void ChangeSize(int w , GLsizei h)

{

    if(h==0)

    {

        h=1;

    }

    glViewport(0,0,w,h);        //窗口可视范围...

    glMatrixMode(GL_PROJECTION); //投射模式

    glLoadIdentity();            //观察。。。



    //char to int

    char t[20];

    itoa(w,t,10);

    //MessageBox(NULL,t,NULL,NULL);



    itoa(h,t,10);

    //MessageBox(NULL,t,NULL,NULL);



    /*

     下面这句很重要     //剪载视图

    */

    if(w<=h)    

        glOrtho(0,250,0,250,1,-1);

    else

        glOrtho(0,250,0,250,1,-1);

    // left,right,buttom,top,near,far

    /*

     left,right x 轴最少最大值

     然后到,y,z

    */

    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();

}







void eventKeys(unsigned char key,int x,int y)

{



    switch(key)

    {

        case 'a':

            MessageBox(NULL,"a",NULL,NULL);

            break;

        case 'b':

                MessageBox(NULL,"b",NULL,NULL);

            break;

        default:

            //char to string 

            string s=""; 

            s.insert(0,key);

            MessageBox(NULL,s.c_str(),NULL,NULL);

            break;

    }

}

void eventMouses(int e , int type,int x, inty)

{

switch (e)
{
case GLUT_LEFT_BUTTON:
if(type==GLUT_DOWN)
{
  MessageBox(NULL,"MOUSE DOWN",NULL,NULL);
}
  break;
}



}

void MyTimer(int v)

{

    x1+=setp;

    y1+=setp;

    glutPostRedisplay(); //刷新 glutDisplayFunc

    glutTimerFunc(33,MyTimer,1);

}

int main(int argc, char* argv[])

{

    //GLUT_SINGLE

    //GLUT_DOUBLE 双缓冲技术

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);

    

    glutCreateWindow("Simple");        //窗口名为



    glutDisplayFunc(RenderSence);    //绘制场景

    glutReshapeFunc(ChangeSize);    //监听窗口更改 size 事件

    glutTimerFunc(33,MyTimer,1);    //监听计时器



    glutKeyboardFunc(eventKeys);    //监听 keyboard 事件

    glutMouseFunc(eventMouses);        //监听 mouse 事件

    //glutWireCube(50);                //画一个 cube

     

    SetupRC();

    glutMainLoop();

    return 0;

}

你可能感兴趣的:(OpenGL)