opengl绘制图片
#include <GL/glew.h>
#include <glut.h>
#include "FreeImage.h"
#include <stdio.h>
#include <malloc.h>
#pragma comment(lib, "FreeImage.lib")
GLuint texture; // Storage For One Texture ( NEW )
/*void loadTexture()
{
FIBITMAP* img=FreeImage_Load(FIF_PNG, "asina.png");
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, FreeImage_GetWidth(img), FreeImage_GetHeight(img), 0, GL_RGBA, GL_BYTE, (const void*)FreeImage_GetBits(img));
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
FreeImage_Unload(img);
}*/
typedef struct
{
int w;
int h;
unsigned char *buf;
GLuint rgb_mode;
}GLBITMAP;
/*static GLuint S_png_tex = 0;
static GLuint S_jpg_tex = 0;
static GLuint S_bmp_tex = 0;
static GLuint S_tga_tex = 0;
static GLuint S_ico_tex = 0; */
GLBITMAP * FIBitmap2GLBitmap(FIBITMAP *fibmp)
{
int i, j, k;
int pitch = FreeImage_GetPitch(fibmp);
unsigned char *bits = FreeImage_GetBits(fibmp);
int bpp = FreeImage_GetBPP(fibmp);
GLBITMAP *glbmp = (GLBITMAP *)malloc(sizeof(GLBITMAP));
RGBQUAD *palette = NULL;
if ( !glbmp ) return NULL;
glbmp->w = FreeImage_GetWidth(fibmp);
glbmp->h = FreeImage_GetHeight(fibmp);
switch ( bpp ) {
case 8:
if ( !(palette = FreeImage_GetPalette(fibmp)) ) return NULL;
if ( !(glbmp->buf = (unsigned char *)malloc(glbmp->w*glbmp->h*3)) ) return NULL;
glbmp->rgb_mode = GL_RGB;
for ( i = 0; i < glbmp->h; ++i ) {
for ( j = 0; j < glbmp->w; ++j ) {
k = bits[i*pitch+j];
glbmp->buf[(i*glbmp->w+j)*3+0] = palette[k].rgbRed;
glbmp->buf[(i*glbmp->w+j)*3+1] = palette[k].rgbGreen;
glbmp->buf[(i*glbmp->w+j)*3+2] = palette[k].rgbBlue;
}
}
break;
case 24:
if ( !(glbmp->buf = (unsigned char *)malloc(glbmp->w*glbmp->h*3)) ) return NULL;
glbmp->rgb_mode = GL_RGB;
for ( i = 0; i < glbmp->h; ++i ) {
for ( j = 0; j < glbmp->w; ++j ) {
glbmp->buf[(i*glbmp->w+j)*3+0] = bits[i*pitch+j*3+2];
glbmp->buf[(i*glbmp->w+j)*3+1] = bits[i*pitch+j*3+1];
glbmp->buf[(i*glbmp->w+j)*3+2] = bits[i*pitch+j*3+0];
}
}
break;
case 32:
if ( !(glbmp->buf = (unsigned char *)malloc(glbmp->w*glbmp->h*4)) ) return NULL;
glbmp->rgb_mode = GL_RGBA;
for ( i = 0; i < glbmp->h; ++i ) {
for ( j = 0; j < glbmp->w; ++j ) {
glbmp->buf[(i*glbmp->w+j)*4+0] = bits[i*pitch+j*4+2];
glbmp->buf[(i*glbmp->w+j)*4+1] = bits[i*pitch+j*4+1];
glbmp->buf[(i*glbmp->w+j)*4+2] = bits[i*pitch+j*4+0];
glbmp->buf[(i*glbmp->w+j)*4+3] = bits[i*pitch+j*4+3];
}
}
break;
default: return NULL;
}
return glbmp;
}
void FreeGLBitmap(GLBITMAP *glbmp)
{
if ( glbmp ) {
if ( glbmp->buf ) free(glbmp->buf);
free(glbmp);
}
}
GLuint loadTexture(const char *filename)
{
GLuint tex = 0;
unsigned char *bits = NULL;
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
FIBITMAP *bitmap = NULL;
GLBITMAP *glbmp = NULL;
fif = FreeImage_GetFileType(filename, 0);
if ( FIF_UNKNOWN == fif ) {
fif = FreeImage_GetFIFFromFilename(filename);
if ( FIF_UNKNOWN == fif )
return 0;
}
if ( FreeImage_FIFSupportsReading(fif) )
bitmap = FreeImage_Load(fif, filename, 0);
if ( !bitmap )
return 0;
printf("bit: %d\n", FreeImage_GetBPP(bitmap));
glbmp = FIBitmap2GLBitmap(bitmap);
if ( !glbmp )
return 0;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, glbmp->rgb_mode, glbmp->w, glbmp->h, 0, glbmp->rgb_mode, GL_UNSIGNED_BYTE, glbmp->buf);
FreeGLBitmap(glbmp);
FreeImage_Unload(bitmap);
return tex;
}
void display()
{
glColor3f(1.0, 1.0, 1.0);
/*glBegin(GL_POLYGON);
glVertex3f(0.25, 0.25, 0.0);
glVertex3f(0.75, 0.25, 0.0);
glVertex3f(0.75, 0.75, 0.0);
glVertex3f(0.25, 0.75, 0.0);
glEnd();*/
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View
gluOrtho2D(0.0, 800.0,600.0,0.0);
//glTranslatef(0.0f,0.0f,-5.0f);
glBindTexture(GL_TEXTURE_2D, texture);
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
glBegin(GL_QUADS);
// Front Face
/*glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);*/
/*glTexCoord2f(0.0f, 0.0f); glVertex3f(-800.0f, -600.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 800.0f, -600.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 800.0f, 600.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-800.0f, 600.0f, 1.0f);*/
glTexCoord2f(0.0f, 0.0f); glVertex3f(10.0f, 0.0f, 0.4f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 74.0f, 0.0f, 0.4f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 74.0f, 64.0f, 0.4f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(10.0f, 64.0f, 0.4f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(0.0f, 0.0f, 0.5f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 64.0f, 0.0f, 0.5f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 64.0f, 64.0f, 0.5f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(0.0f, 64.0f, 0.5f);
/*// Back Face
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
// Top Face
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
// Bottom Face
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
// Left Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);*/
glEnd();
glFlush();
}
void init()
{
FreeImage_Initialise();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrtho(0.0, 1.0, 0.0,1.0,-1.0,1.0);
//glOrtho(0.0, 800.0, 0.0,600.0,-1.0,1.0);
gluOrtho2D(0.0, 800.0,600.0,0.0);
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
texture=loadTexture("asina.png");
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(800, 600);
glutInitWindowPosition(200, 100);
glutCreateWindow("hello opengl");
init();
glutDisplayFunc(display);
glutMainLoop();
}
opengl绘制图片的更多相关文章
- 基于OpenGL编写一个简易的2D渲染框架-04 绘制图片
阅读文章前需要了解的知识,纹理:https://learnopengl-cn.github.io/01%20Getting%20started/06%20Textures/ 过程简述:利用 FreeI ...
- [Modern OpenGL系列(三)]用OpenGL绘制一个三角形
本文已同步发表在CSDN:http://blog.csdn.net/wenxin2011/article/details/51347008 在上一篇文章中已经介绍了OpenGL窗口的创建.本文接着说如 ...
- UWP&WP8.1 重新绘制图片 WriteableBitmap用法 图片转byte[]数组,byte[]数组转图片
---恢复内容开始--- WriteableBitmap 是UWP和WP8.1绘制图片的,重组图片的最重要方法.方法较为简单,方法多样性. 通过查看文档,WriteableBitmap的继承性是 ...
- html5 绘制图片 drawImage
要在绘图上下文中绘制图片,可以使用 drawImage 方法.该方法有三种不同的参数: drawImage(image,dx,dy) drawImage(image,dx,dy,dw,dh) d ...
- 使用html5 canvas绘制图片
注意:本文属于<html5 Canvas绘制图形入门详解>系列文章中的一部分.如果你是html5初学者,仅仅阅读本文,可能无法较深入的理解canvas,甚至无法顺畅地通读本文.请点击上述链 ...
- opengl绘制正弦曲线
利用opengl绘制正弦曲线 ,见代码: #include <windows.h> //#include <GLUT/glut.h> #include <GL/glut. ...
- OpenGL绘制自由落体小球
OpenGL绘制自由落体小球 一. 程序运行的软硬件环境 本次设计在window10系统下进行,运用C++进行编写,在CodeBlocks环境下使用OpenGL进行设计. 所需环境配置分为2部分 ...
- Android Canvas使用drawBitmap绘制图片
1.基本的绘制图片方法 //Bitmap:图片对象,left:偏移左边的位置,top: 偏移顶部的位置 drawBitmap(Bitmap bitmap, float left, float top, ...
- android中Canvas使用drawBitmap绘制图片
1.主要的绘制图片方法 //Bitmap:图片对象,left:偏移左边的位置,top: 偏移顶部的位置 drawBitmap(Bitmap bitmap, float left, float ...
随机推荐
- 2017.7.18 linux下ELK环境搭建
参考来自:Linux日志分析ELK环境搭建 另一篇博文:2017.7.18 windows下ELK环境搭建 0 版本说明 因为ELK从5.0开始只支持jdk 1.8,但是项目中使用的是JDK 1 ...
- 转: java web demo的示例
http://quqtalk.iteye.com/blog/360699 从事Java开发已经两年了,但是由于工作的关系,对Java Web还是个freshman.今天做了一个Java Web的简单D ...
- C2:抽象工厂 Abstract Factory
提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类. 应用场景: 一系列相互依赖的对象有不同的具体实现.提供一种“封装机制”来避免客户程序和这种“多系列具体对象创建工作”的紧耦合 UM ...
- vue created 生命周期
在实例创建完成后被立即调用.在这一步,实例已完成以下的配置:数据观测 (data observer),属性和方法的运算,watch/event 事件回调.然而,挂载阶段还没开始,$el属性目前不可见. ...
- Android 创建Listener监听器形式选择:匿名内部类?外部类?
说到监听器,第一感觉就是直接写作匿名内部类来用,可是依据单一职责原则,好像又不应该作为匿名内部类来写(由于监听中有时要写较多的逻辑代码),所曾经段时间把有共性的listener单独创建放在glut.l ...
- Spark on Intellij IDEA
添加scala插件 如果网络有问题,可以手动下载插件安装包(http://plugins.jetbrains.com/plugin/?id=1347),在上面选择“Install plugin fro ...
- viewDidLayoutSubviews在ios7上导致应用崩溃
在ios8中使用viewDidLayoutSubviews,应用正常运行,没有问题,但是应用在ios7上运行的时候,报错,导致应用崩溃,错误信息类似: Cannot find executable f ...
- ios 使用gcd 显示倒计时
__block ;//倒计时时间 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, ...
- spark入门(helloworld插件)
1 http://www.cnblogs.com/openfire/archive/2013/04/26/3044722.html 2 在bulid文件夹下,(注意为主目录不是插件下的bulid.xm ...
- sparkStreaming的mapWithState函数【案例二】
sparkStreaming是以连续bathinterval为单位,进行bath计算,在流式计算中,如果我们想维护一段数据的状态,就需要持久化上一段的数据,sparkStreaming提供的Mapwi ...