opengl的helloworld

阅读更多

1.我提供一个不需要配置环境就可运行的源码。
glut.h放在项目上一层include/gl目录。
glut.lib和glut32.lib放在上一层lib目录。
glut.dll和glut32.dll放exe同目录。
2.不需要配置环境变量。
3.头文件glut.h和库文件glut.lib与glut32.lib
这是include和lib
opengl的helloworld_第1张图片
设计
#include 
#include "Shell.h"
#include "Box.h"
#include "Matrix.h"

void myDisplay(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
    glFlush();
}

int main(int argc, char *argv[])
{
    //炮弹发射
    Shell(argc,argv);
    //方块世界
    //Box(argc,argv);
    //矩阵变换
    //Matrix(argc,argv);

    return 0;

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(400, 400);
    glutCreateWindow("第一个OpenGL程序");
    glutDisplayFunc(&myDisplay);
    glutMainLoop();
    return 0;
}


Shell.h
#include "Shell.h"

GLfloat f_Speed = 80.0f;
GLfloat f_X = -70;
GLfloat f_Y = -70;
GLfloat f_Agle = 45;

void Shell_Reshape(GLsizei w,GLsizei h)//重绘回调
{
	GLfloat aspectRatio;
	//防止被0所除
	if (h==0)
	{
		h=1;
	}
	//把视口设置为窗口的大小
	glViewport(0,0,w,h);
	//重置坐标系统
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	//建立裁剪区域(左,右,底,顶,近,远)
	aspectRatio = (GLfloat)w / (GLfloat)h;
	if(w <= h)
		glOrtho(-100.0, 100.0, -100 / aspectRatio, 100.0 / aspectRatio, 1.0, -1.0);
	else
		glOrtho(-100.0 * aspectRatio, 100.0*aspectRatio, -100.0, 100.0, 1.0, -1.0);
	//
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}
//设置渲染状态
void Shell_SetupRC(void)
{
	glClearColor(0.0f,0.0f,0.0f,1.0f);
}
//绘制场景
void Shell_RenderScene(void)
{
	//用当前清除颜色清除窗口
	glClear(GL_COLOR_BUFFER_BIT);

	//画速度指示条
	//绿色
	glColor3f(0.0f,1.0f,0.0f);
	//平移
	glPushMatrix();
	glTranslatef(0.0f,80.0f,0.0f);
	glTranslatef(-90.0f,0.0f,0.0f);
	//指示条
	glRectf(0.0f, 0.0f, f_Speed, 10.0f);
	//还原
	glPopMatrix();
	
	//画炮台
	//灰色
	glColor3f(1.0f,1.0f,1.0f);
	//平移
	glPushMatrix();
	glTranslatef(-77.0f,-86.0f,0.0f);
	//炮台
	glBegin(GL_POLYGON);
	glVertex2f(0.0f, 0.0f);
	glVertex2f(13.0f, 0.0f);
	glVertex2f(10.0f, 16.0f);
	glVertex2f(3.0f, 16.0f);
	glVertex2f(0.0f, 0.0f);
	glEnd();
	//还原
	glPopMatrix();

	//画炮管
	//灰色
	glColor3f(1.0f,0.0f,1.0f);
	//平移
	glPushMatrix();
	glTranslatef(-70.0f,-70.0f,0.0f);
	glRotatef(f_Agle,0.0,0.0,1.0);
	//炮管
	glBegin(GL_POLYGON);
	glVertex2f(-5.0f, 5.0f);
	glVertex2f(45.0f, 5.0f);
	glVertex2f(45.0f, -5.0f);
	glVertex2f(-5.0f, -5.0f);
	glVertex2f(-5.0f, 5.0f);
	glEnd();
	//还原
	glPopMatrix();


	//画炮弹
	//红色
	glColor3f(1.0f,0.0f,0.0f);  
	//平移
	glPushMatrix();
	glTranslatef(f_X,0,0.0f);
	glTranslatef(0,f_Y,0.0f);
	glRotatef(f_Agle-45,0.0,0.0,1.0);
	//炮弹
	glBegin(GL_TRIANGLES);
	glVertex3f(-5.0f,5.0f, 0.0f);
	glVertex3f(5.0f,5.0f, 0.0f);
	glVertex3f(5.0f,-5.0f, 0.0f);
	glEnd();
	//还原
	glPopMatrix();

	//刷新绘图命令
	glFlush();
}
void Shell_SpecialKeys(int key, int x, int y)//特殊按键F1之类
{
	GLfloat fOld = f_Speed;
	switch(key) 
	{
	case GLUT_KEY_DOWN:
		f_Agle -= 5;
		break;
	case GLUT_KEY_UP:
		f_Agle += 5;
		break;
	case GLUT_KEY_LEFT:
		f_Speed -= 10.0f;
		break;
	case GLUT_KEY_RIGHT:
		f_Speed += 10.0f;
		break;
	}
	if (f_Speed < 10.0f || f_Speed > 180.0f)
	{
		f_Speed = fOld;
		return;
	}
	if (f_Agle < 0)
	{
		f_Agle = 0;
		return;
	}
	if (f_Agle > 90)
	{
		f_Agle = 90;
		return;
	}
	glutPostRedisplay();//重绘消息
}
void Shell_timer(int id)//定时器
{
	f_X += 5.0f * cos((f_Agle/180)*Pi);
	f_Y += 5.0f * sin((f_Agle/180)*Pi);
	glutPostRedisplay();
	glutTimerFunc((190-f_Speed)*5,Shell_timer,5);//需要在函数中再次调用,才能完成循环。
}
void Shell_key(unsigned char key,int x,int y)//键盘鼠标事件
{
	if (key == ' ')
	{
		f_X = -70;
		f_Y = -70;
	}

	glutPostRedisplay();//重绘消息
}
//main调用
int Shell(int argc, char* argv[])
{
	glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
	glutInitWindowSize(500,500);//窗体大小
	glutInitWindowPosition(500,100);//起始位置
	glutCreateWindow("炮弹发射");//创建窗体

	glutDisplayFunc(Shell_RenderScene);//类OnDraw()
	glutReshapeFunc(Shell_Reshape);//重绘回调
	glutKeyboardFunc(Shell_key);//键盘事件
	glutSpecialFunc(Shell_SpecialKeys);//特殊按键F1之类
	glutTimerFunc((190-f_Speed)*5,Shell_timer,5);

	Shell_SetupRC();//类Init()
	cout<<"上下键控制角度,左右键控制发射速度。"< 
 
Box.cpp
#include "Box.h"

double rotatex=0,rotatey=0,rotatez=0; 
double movex=0,movey=0,movez=0;
double zoomx=0.5,zoomy=0.5,zoomz=0.5;
vector vecCube;

void Box_Init(void)
{
	glClearColor(0.0,0.0,0.0,0.0);//黑色清屏
	AddCube(1,1,1,0,0,1);
	AddCube(0,0,0,1,0,1);
	AddCube(-1,-1,0,0,1,1);
}
void AddCube(int x,int y,GLfloat fR,GLfloat fG,GLfloat fB,GLfloat fWidth)
{
	structCube cube;
	cube.x = x;
	cube.y = y;
	cube.fR = fR;
	cube.fG = fG;
	cube.fB = fB;
	cube.fWidth = fWidth;
	vecCube.push_back(cube);
}
void Box_Display(void)
{
	//
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(-2.0*64/48.0,2.0*64/48.0,-2.0,2.0,0.1,100);
	//
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(2.0,2.0,2.0,0.7,0.7,0.0,0.0,1.0,0.0);//设置相机
	glClear(GL_COLOR_BUFFER_BIT);

	//变换
	glPushMatrix();
	//平移
	glTranslatef (movex + 2,0.0, 0.0);//x轴平移
	glTranslatef (0.0, movey + 2, 0.0);//y轴平移
	//旋转
	glRotatef (rotatez, 0.0, 0.0, 1.0);//z轴旋转
	glRotatef (rotatex, 1.0, 0.0, 0.0);//x轴旋转
	glRotatef (rotatey, 0.0, 1.0, 0.0);//y轴旋转
	//缩放
	glScalef(zoomx,zoomy,zoomz);//缩放

	for(vector::iterator iter=vecCube.begin();iter!=vecCube.end();iter++)
	{
		DrawCube(iter->x,iter->y,iter->fR,iter->fG,iter->fB,iter->fWidth);
	}

	//还原
	glPopMatrix();

	glFlush();//绘图
}
void DrawCube(int x,int y,GLfloat fR,GLfloat fG,GLfloat fB,GLfloat fWidth)
{
	glColor3d(fR,fG,fB);//设置立方体边颜色
	GLfloat fX = (GLfloat)x;
	GLfloat fY = (GLfloat)y;
	fX = (fX - 400) / 400;
	fY = (fY - 400) / 400;
	//cout< 
 
Matrix.cpp
#include "Matrix.h"

void Matrix_Reshape(GLsizei w,GLsizei h)//重绘回调
{
	GLfloat aspectRatio;
	//防止被0所除
	if (h==0)
	{
		h=1;
	}
	//把视口设置为窗口的大小
	glViewport(0,0,w,h);
	//重置坐标系统
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	//建立裁剪区域(左,右,底,顶,近,远)
	aspectRatio = (GLfloat)w / (GLfloat)h;
	if(w <= h)
		glOrtho(-100.0, 100.0, -100 / aspectRatio, 100.0 / aspectRatio, 1.0, -1.0);
	else
		glOrtho(-100.0 * aspectRatio, 100.0*aspectRatio, -100.0, 100.0, 1.0, -1.0);
	//
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}
//设置渲染状态
void Matrix_Init(void)
{
	glClearColor(0.0f,0.0f,0.0f,1.0f);
}
//绘制场景
void Matrix_Display(void)
{
	//用当前清除颜色清除窗口
	glClear(GL_COLOR_BUFFER_BIT);


	//把当前矩阵设置为模型视力矩阵,并进行重置。
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	//画球体
	glutSolidSphere(10,25,25);
	/*
	//沿y轴正方向移动10个单位
	glTranslatef(0,50,0);
	//绘制第一个球体
	glutSolidSphere(10,25,25);

	//再次重置模型视力矩阵
	glLoadIdentity();
	//沿x轴正方向移动10个单位
	glTranslatef(50,0,0);
	//绘制第二个球体
	glutSolidSphere(10,25,25);
	*/

	glPushMatrix();

	//沿y轴正方向移动10个单位
	glTranslatef(0,50,0);
	//绘制第一个球体
	glutSolidSphere(10,25,25);

	glPopMatrix();

	//

	glPushMatrix();

	//沿x轴正方向移动10个单位
	glTranslatef(50,0,0);
	//绘制第二个球体
	glutSolidSphere(10,25,25);

	glPopMatrix();

	//刷新绘图命令
	glFlush();
}
//main调用
int Matrix(int argc, char* argv[])
{
	glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
	glutInitWindowSize(500,500);//窗体大小
	glutInitWindowPosition(500,100);//起始位置
	glutCreateWindow("矩形转换");//创建窗体

	glutDisplayFunc(Matrix_Display);//类OnDraw()
	glutReshapeFunc(Matrix_Reshape);//重绘回调

	Matrix_Init();//类Init()
	cout<<"提示信息。"< 
 

参考
http://www.cnblogs.com/greatverve/archive/2013/01/02/openGL-HelloWorld.html
  • glut.zip (145.6 KB)
  • 下载次数: 0
  • glHelloWord.rar (623.6 KB)
  • 下载次数: 0
  • opengl的helloworld_第2张图片
  • 大小: 103.6 KB
  • 查看图片附件

你可能感兴趣的:(c,opengl)