我的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
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
相关文章推荐
- openGL学习笔记一:环境搭建
- openGL学习笔记二:看看上一篇我们写的代码
- OpenGL学习笔记0(综合整理)
- 【OpenGL 学习笔记03】点画多边形
- OpenGL学习笔记——纹理贴图
- OpenGL学习笔记——Blending
- OpenGL ES学习笔记之四
- OpenGL学习笔记(10)抗锯齿
- OpenGL 学习笔记(2)创建第一个图形
- OPENGL学习笔记整理(二):纹理知多少?
- opengl学习笔记2-1-基本的运行框架
- OpenGL学习笔记-OpenGL的变换和矩阵
- OpenGL 学习笔记 -- Mac 上环境搭建
- OpenGL 学习笔记一
- OpenGL2.0学习笔记【二】——(第二章)矩形的绘制
- OpenGL学习笔记(六)
- 《OpenGL游戏程序设计》学习笔记---第五章坐标变换和OpenGL矩阵
- OpenGL学习笔记(三):颜色与光照
- opengl学习笔记(一)---获取opengl版本等信息
- OpenGL 学习笔记