c语言opengl代码
⑴ C语言如何用OpenGL
OpenGL就是基于C语言的,只需要下载OpenGL的SDK库安装即可,在编写源码时:
1、添加头文件glut.h。
注意glut.h文件中已经包含gl.h,glu.h在实际编译中可以只加入头文件glut.h,很多相关的例子都是这样的,但是在mingwstudio上编译发现,在glut.h前还是需要加入glu.h, gl.h.如:
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>
2、在工程中添加OpenGL的库,有关命令行加入,glu32 opengl32 glut32库就可以编译了。
⑵ 利用C语言实现二维图形的变换
你先看看吧,思路大概就是这样,不懂的问我。
#include<stdio.h>
#include<math.h>
#include<iostream>
using namespace std;
struct point
{
float x;
float y;
};
void translation(point*pt, float xp,float yp,int num)//num代表点的个数
{
for(int i=0;i<num;i++)
{
(pt+i)->x+=xp;
(pt+i)->y+=yp;
}
}
void scale(point *pt,float xs,float ys,int num)
{
for(int i=0;i<num;i++)
{
(pt+i)->x*=xs;
(pt+i)->y*=ys;
}
}
void rotation(point *pt,float angle,int num)
{
int a[2][2];
angle=angle/180*3.141592657;
a[0][0]=cos(angle);
a[0][1]=-sin(angle);
a[1][0]=sin(angle);
a[1][1]=cos(angle);
point* temp;
for(int i=0;i<num;i++)
{
temp->x=(pt+i)->x;
temp->y=(pt+i)->y;
(pt+i)->x=temp->x*a[0][0]+a[0][1]*temp->y;
(pt+i)->y*=temp->x*a[1][0]+a[1][1]*temp->y;
}
}
int main()
{
int i=0,N,mode,angle,xp,yp,xk,yk,num;
cout<<"please input the number of point "<<endl;
scanf("%d",&N);
num=N;
point pt[10];
while(N--)
{
printf("please input points(x,y):\n");
scanf("%f%f",&pt[i].x,&pt[i].y);
i++;
}
printf("please input motions\n");
printf("0 stand for translation:\n");
printf("1 stand for scale:\n");
printf("2 stand for rotation:\n");
scanf("%d",&mode);
switch(mode)
{
case 0:
printf("please input the translation in x and y direction respectivly:\n");
cin>>xp>>yp;
translation(pt, xp,yp,num);
break;
case 1:
printf("please input the scale in x and y direction respectivly:\n");
scanf("%f%f",&xk,&yk);
scale(pt, xk,yk,num);
break;
case 2:
printf("please input the angle:\n");
scanf("%f",&angle);
rotation(pt, angle,num);
break;
}
printf("after translatiton or scale or rotation:\n");
for(int i=0;i<num;i++)
printf("%f %f\n",pt[i].x,pt[i].y);
}
⑶ C语言windows,OpenGL编程
查找 MSDN 可以得知,MSG 里面的 pt 坐标是相对于窗口的左上角的;
2. 至于声音控制和播放,可以使用 Windows 自带的 MCI API,或者使用 DirectSound 来播放,我推荐你使用 un4seen 的 BASS,简单实用强大,一两个函数就可以播放音效了;
3. 你要使用 alpha blend 与桌面进行镂空运算,就必须首先获得桌面的窗体句柄,OpenGL 的 alppha 运算我不是很懂,不过 Direct3D 的话就简单多了;
4. 屏幕常亮,其实就是阻止系统进入休眠状态,每当系统要进入休眠状态之前,都会向系统的所有窗口发送一条消息,你拦截这条消息,进行特别的处理就可以防止系统进入休眠了,至于是什么消息,请查看 MSDN,我也好久没用过这条消息了;
5.bmp 文件可以保存 alpha 通道,使用 32bit 色深的 bmp 文件就可以了,RGB 分别 8bit,alpha 通道 8bit,不过说到 alpha 通道,tga 或者 png 图片更加合适,因为他们可以进行无损压缩;
6.用GetPocAddress导出函数,只能用类型强制转换,这个是 windows 的原则,我们只能去迎合它了 ...
7. 执行 NULL 指针的话,不同的系统会有不同的反应,XP 是直接程序崩溃,Vista 或者以上的系统,就会提示无响应
8. 如果你建立的工程是 Win32 窗口程序,那么就不会有 DOS 窗口,如果你建立的是 Win32 控制台程序,那么就会有 DOS 窗口;如果你使用 OpenGL 实用库来创建 OpenGL 程序,那个 DOS 窗口是无法消除的,它可以帮助你进行错误排查
9. 不要用 Dev C++ 了,用 VS2010 吧,这是行业规范
最后,祝楼主学习愉快