您的位置:首页 > 运维架构

我的OpenGL学习笔记二

2009-10-29 15:51 399 查看
I am a fresh man in CSDN, so I have to start with nothing.

I am a programmer. what I have learned are C/C++ ASP,SQL

I have learned OPENGL for several weeks.

The following is my complied program

#include "stdafx.h"
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "windows.h"
#include "math.h"

static GLfloat spin=0.0;
static GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
GLdouble g_eye;
GLdouble g_look;

void init(void)
{
g_eye=0.0;
g_look=0.0;
g_eye=0.0;
glClearColor(1.0,1.0,1.0,1.0);
glShadeModel(GL_SMOOTH);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);

}
void idle(void)
{

spin=spin+0.25;
if(spin>360.0)
spin=spin-360.0;
glutPostRedisplay();

}

void display(void)
{

glClear(GL_COLOR_BUFFER_BIT);

glPushMatrix();
glTranslatef(0.0,g_look,g_eye);
glRotatef(view_rotx, 1.0, 0.0, 0.0);
glRotatef(view_roty, 0.0, 1.0, 0.0);
glRotatef(view_rotz, 0.0, 0.0, 1.0);
glRotated(spin, 1.0, 0.0, 0.0);
glRotated(spin, 0.0, 1.0, 0.0);
glBegin(GL_TRIANGLE_STRIP);
glColor4f(1.0, 0.0, 0.0, 0.3);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(1.0, -1.0, 1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, 1.0);
glColor4f(0.0, 1.0, 0.0, 0.3);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(1.0, 1.0, -1.0);
glColor4f(0.0, 0.0, 1.0, 0.3);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(1.0, -1.0, -1.0);
glEnd();
glBegin(GL_TRIANGLE_STRIP);
glColor4f(1.0, 1.0, 0.0, 0.3);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, 1.0);
glColor4f(0.0, 1.0, 1.0, 0.3);
glVertex3f(1.0, -1.0, -1.0);
glVertex3f(1.0, -1.0, 1.0);
glColor4f(1.0, 0.0, 1.0, 0.3);
glVertex3f(1.0, 1.0, -1.0);
glVertex3f(1.0, 1.0, 1.0);
glEnd();

glPopMatrix();

glutSwapBuffers();
}

void reshape(int w,int h)
{

glViewport(0,0,(GLsizei) w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 1.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 4.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

// gluLookAt(0,0,4.0,0.0,g_eye[2],g_look[2],0.0,1.0,0.0);

}
void mouse(int button,int state,int x,int y)
{

switch(button)
{
case GLUT_LEFT_BUTTON:
if (state==GLUT_DOWN)
glutIdleFunc(idle);
break;
case GLUT_RIGHT_BUTTON:
if (state==GLUT_DOWN)
glutIdleFunc(NULL);
break;
default :break;
}
}
void key(unsigned char k, int x, int y)
{

switch (k)
{
case 'w':
view_rotx += 5.0;
break;
case 's':
view_rotx -= 5.0;
break;
case 'a':
view_roty += 5.0;
break;
case 'd':view_roty -= 5.0;break;

case 'l': g_look+=0.05;break;
case 'j': g_look-=0.05;break;
case 'i':
g_eye+=0.05;break;
case 'k':
g_eye-=0.05;break;
case'v':view_rotx = 1.0;view_roty = 2.0;view_roty = 5.0;break;
case'b':view_rotx = 5.0;view_roty = 1.0;view_roty = 5.0;break;
case'n':view_rotx = 15.0;view_roty = 8.0;view_roty = 4.0;break;
default:
return;
}

}

int main(int argc,char* argv[])
{
// TODO: Place code here.
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow(argv[0]);
init();
glutKeyboardFunc(key);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutIdleFunc(idle);
// glutSpecialFunc(special);
glutMainLoop();
return 0;
}

"w""a""s""d" are the charging the rotat e

press "i""j""k""l" to control eye position
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: