记录一下GLFW的2D贴图
2016-04-01 17:15
316 查看
// // New.c // Gmaedemo // // #include <GLFW/glfw3.h> #include <stdio.h> #include <stdlib.h> #include "SOIL.h" #define TITLE "Test" #define WIDTH 1024 #define HEIGHT 768 GLFWwindow* window; #define UPDATE_INTERVAL 1 /* 10ms */ GLuint texture[5]; GLuint texture1; GLuint Loadbmpfile(const char* Image) { GLuint Tex; printf("Reading image %s\n", Image); FILE* img = NULL; img = fopen(Image,"rb"); unsigned long bWidth = 0; unsigned long bHeight = 0; unsigned int size = 0; fseek(img,18,SEEK_SET); fread(&bWidth,4,1,img); fread(&bHeight,4,1,img); fseek(img,0,SEEK_END); size = ftell(img) - 54; unsigned char *data = (unsigned char*)malloc(size); fseek(img,54,SEEK_SET); // image data fread(data,size,1,img); fclose(img); glGenTextures(1, &Tex); glBindTexture(GL_TEXTURE_2D, Tex); gluBuild2DMipmaps(GL_TEXTURE_2D, 3, bWidth, bHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, data); //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bWidth, bHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); if (data) free(data); return Tex; } //取时间 static uint32_t _gettime(void) { uint32_t t; struct timeval tv; gettimeofday(&tv, NULL); t = (uint32_t)(tv.tv_sec & 0xffffff) * 100; t += tv.tv_usec / 10000; return t; } //取按键 static void _glfw_key_cb(GLFWwindow* window, int key, int scancode, int action, int mods) { if(key == GLFW_KEY_Q && action == GLFW_PRESS) { glfwSetWindowShouldClose(window, GL_TRUE); } } void init(){ if (glfwInit() != GL_TRUE){ printf("Unable to initialize GLFW"); } glfwDefaultWindowHints(); glfwWindowHint(GLFW_VISIBLE, GL_FALSE); // the window will stay hidden after creation glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); // the window will be resizable window = glfwCreateWindow(WIDTH, HEIGHT, TITLE, NULL, NULL); if (window == NULL) { glfwTerminate(); printf("Failed to create the GLFW window"); } glfwSetKeyCallback(window,_glfw_key_cb); glfwMakeContextCurrent(window); // Enable v-sync glfwSwapInterval(1); // Make the window visible glfwShowWindow(window); } //显示图片 void drawTex2D(GLuint textureId, int x, int y, int width, int height) { glTranslatef(x, y, 0); glBindTexture(GL_TEXTURE_2D, textureId); glBegin(GL_QUADS); glTexCoord2f(0, 0); glVertex2f(0, 0); glTexCoord2f(1, 0); glVertex2f(width, 0); glTexCoord2f(1, 1); glVertex2f(width, height); glTexCoord2f(0, 1); glVertex2f(0, height); glEnd(); glBindTexture(GL_TEXTURE_2D, 0); glLoadIdentity(); } void update(){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer for(int si=0;si<=5;si++){ drawTex2D(texture[si], 470+(si)*48, 0, 48, 160); } drawTex2D(texture1, 0, 0, 470, 436); glFlush(); } void setupgl(){ // 操作投影矩阵 glMatrixMode(GL_PROJECTION); glLoadIdentity(); // 复位 // 将坐标系置为左上角(0,0),右下角(width,height) glOrtho(0, WIDTH, HEIGHT, 0, 1, -1); // 操作模型矩阵 glMatrixMode(GL_MODELVIEW); // 开启2D纹理支持 glEnable(GL_TEXTURE_2D); // 开启混合渲染 glEnable(GL_BLEND); } void settex(){ for(int i=0;i<=5;i++){ texture[i] = Loadbmpfile("2.bmp"); } texture1 = Loadbmpfile("uvtemplate.bmp"); } void loop(){ glViewport(0, 0, WIDTH*2, HEIGHT*2); uint32_t timestamp = 0; glClearColor(0.0f, 0.0f, 0.4f, 0.0f); while (glfwWindowShouldClose(window) == GL_FALSE) { uint32_t current = _gettime(); if(current - timestamp >= UPDATE_INTERVAL) { timestamp = current; update(); GLenum error = glGetError(); if(error != GL_NO_ERROR) printf("error: %d\n",error); } glfwSwapBuffers(window); // swap the color buffers // Poll for window events. The key callback above will only be // invoked during this call. glfwPollEvents(); } glfwDestroyWindow(window); glfwTerminate(); } int main(){ init(); setupgl(); settex(); loop(); return 0; }
相关文章推荐
- Gson 将list封装成json
- Android静默安装实现方案,仿360手机助手秒装和智能安装功能
- 关注CSDN开发高手的blog主页链接
- 股票输入软键盘,android初探自定义软键盘
- 解决maillog文件删除后无法重建的问题
- 设计模式——职责链模式
- 5周学习杂谈(数学基础)
- c++作业2
- Mysql 删除重复记录,只保留最小的一条
- 页面跨域传参 nginx iframe
- python依赖包numpy、scipy、scikit-learn运行冲突解决方案
- [Gradle]org.gradle.tooling.GradleConnectionException: Could not install Gradle distribution from 'ht
- ios 添加pch文件
- CentOS6.6安装cobbler
- 极验高并发验证服务背后的技术实现
- 修改framework/base下面的api要注意要修改的地方
- zookeeper原理(转)
- Myeclipse tomcat debug(调试模式)启动过慢的问题
- “cannot resolve symbol R” in Android Studio
- 教你如何在Play框架中加入Spark