OpenGL: Colorful Line(CR)
PART ONE
codes.cpp
#include <stdio.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <iostream>
using namespace std;#define width 222
#define height 207
#define num 2 //计数器struct Vector3f {GLfloat x;GLfloat y;GLfloat z;
};struct Color {GLubyte r;GLubyte g;GLubyte b;GLubyte a;
};Color interp_color(Color c0, Color c1, float i)
{Color c;c.r = (1 - i) * c0.r + i * c1.r;c.g = (1 - i) * c0.g + i * c1.g;c.b = (1 - i) * c0.b + i * c1.b;c.a = (1 - i) * c0.a + i * c1.a;return c;
}GLubyte image[height][width][4];
void initLines()
{//定义NDC下的顶点数据Vector3f points[num]{{0.0f,0.5f,0.0f},{0.5f,0.0f,0.0f},};//定义颜色数据Color color[num]{{255,0,0,0},{0,255,0,0}};//将NDC坐标转换成屏幕坐标for (int i = 0; i < num; i++){points[i].x = (points[i].x + 1.0f) * (width / 2 - 1);points[i].y = (points[i].y + 1.0f) * (height / 2 - 1);points[i].z = 0; //zbuffer统一设置为0}for (int i = 0; i < num; i += 2) { //num=2 启用GL_LINES//定义线的起点int start = i;int start_x = (int)points[start].x;int start_y = (int)points[start].y;Color c0 = color[start];//定义线的终点 两两相邻取点int end;int end_x;int end_y;Color c1;if (i == num - 1) { end_x = (int)points[0].x;end_y = (int)points[0].y;c1 = color[0];}else {end = i + 1;end_x = (int)points[end].x;end_y = (int)points[end].y;c1 = color[end];}//对点进行x轴排序if (start_x > end_x) {swap(start_x, end_x);swap(start_y, end_y);swap(c0, c1);}//斜率法画线float delta_x = end_x - start_x;float delta_y = end_y - start_y;float lineWidth = 1.0f;//线宽设置为1//颜色插值画线 if (delta_x == 0) {if (delta_y < 0) {swap(start_y, end_y);swap(c0, c1);}for (int y = start_y, x = start_x; y <= end_y; y++) {//插值系数i根据y轴计算float i = (float)(y - start_y) / (end_y - start_y);Color icolor = interp_color(c0,c1,i);image[y][x][0] = icolor.r;image[y][x][1] = icolor.g;image[y][x][2] = icolor.b;image[y][x][3] = icolor.a;}}else {float gradient = (float)delta_y / delta_x;for (int x = start_x; x <= end_x; x++) {//插值系数i根据x轴计算float i = (float)(x-start_x) /(end_x - start_x);Color icolor = interp_color(c0, c1, i);//直线斜率公式int y = (int)(gradient * (x - start_x) + start_y); image[y][x][0] = icolor.r;image[y][x][1] = icolor.g;image[y][x][2] = icolor.b;image[y][x][3] = icolor.a;}}}
}void render()
{glClear(GL_COLOR_BUFFER_BIT);glDrawPixels(width, height, GL_RGBA, GL_UNSIGNED_BYTE, image);glutSwapBuffers();
}int main(int argc, char** argv)
{glutInit(&argc, argv);glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);glutInitWindowSize(width, height);glutInitWindowPosition(200, 100);int id = glutCreateWindow("彩色线段");GLenum err = glewInit();if (err != GLEW_OK) {fprintf(stderr, "Error: '%s'\n", glewGetErrorString(err));return 1;}initLines();glutDisplayFunc(render);glutMainLoop();return 0;
}
部分二
代码分析
1、在initLines()函数中,利用顶点索引坐标start和end在color[num]中进行顶点颜色的采样,得到点对{start:end}对应的颜色属性{c0:c1}。在根据x轴对顶点进行排序时,颜色属性同步处理。在"delta_x == 0"时,如果"delta_y < 0",颜色属性亦同步处理,否则根据y轴坐标获取插值系数"i"。在"delta_x != 0"时,利用x轴坐标信息完成对插值系数"i"的计算后,根据点对{start:end}和其对应的颜色属性{c0:c1}还有获得的插值系数"i"在函数interp_color( , , )中完成调用,有interp_color(c0, c1, i),即得到了不同像素点对应的颜色属性icolor。根据采样的点对{start:end}的坐标信息(x, y)和颜色信息icolor,在image[y][x][4]中写入icolor获得不同颜色的像素。
2、在render()中,利用glDrawPixels(, , , , image)完成对image[height][width][4]中像素信息的绘制,之后SwapBuffer显示到屏幕。
OpenGL: Colorful Line(CR)相关推荐
- (转)跨越Opengl和D3D的鸿沟
原帖地址: http://www.cnblogs.com/gongminmin/archive/2011/07/15/2107290.html 多年来,在论坛和各个网站上不断能看到拿OpenGL和D3 ...
- (转)利用libcurl和国内著名的两个物联网云端通讯的例程, ubuntu和openwrt下调试成功(四)...
1. libcurl 的参考文档如下 CURLOPT_HEADERFUNCTION Pass a pointer to a function that matches the following pr ...
- Oracle EBS SLA取值
-- 从GL总账追溯到 => 子分类账SLA => 子模块AP.AR等 SELECT xep.name, -- 法人主体xep.legal_entity_identifier, -- 法人 ...
- @程序员,不容错过的 Vim 实用技巧请查收!
Vim 是 Linux 系统上的最著名的文本/代码编辑器,也是早年的 Vi 编辑器的加强版.一直以来,Vim 普遍被推崇为类 Vi 编辑器中最好的一个,其拥有代码补全.编译及错误跳转等诸多丰富的功能, ...
- Cairo学习(一)
Cairo 介绍 最近在看Cairo,准备做ofd生成图片.技术选型选了几天,编译过Mesa3D,但是好像不太适合.最后选择Cairo 2D绘图库,因为是在Linux下跑的,特地安装了个CentOS7 ...
- python autoit打开软件_Python+AutoIt实现界面工具开发
前言 不同于Linux服务器上的命令行操作,在windows系统上用户的使用习惯还是倾向于使用有界面的工具.如果工具是命令行交互操作的方式,可能是有悖于在windows上使用的操作习惯,往往不容易推广 ...
- jpg图片无损放入PDF的程序,PDF文件格式分析,图像表达方式
wxleasyland@139.com 用打印的方式,一般是将JPG解码后的数据发给打印机,而不是JPG原始数据,所以PDF打印机会重新压缩. 不对JPG图像进行解压,直接将JPG图像传送到虚拟打印机 ...
- [倚天屠龙记] vim 复制与粘贴
对于任何一款文本编辑器而言,复制与粘贴都是最基本的功能,vim在此方面自然不甘示弱.事实上,得益于其丰富的移动命令,vim的复制与粘贴是相当快捷而高效的. vim的复制命令是y和Y,它是yank的简写 ...
- 在midjourney看到比较有趣的AI图并记录prompt(一)
a psychedelic retreat on top of the mountains, it's snowing, sunrise,a lot of people, lot of vegetat ...
最新文章
- pandas案例分析
- NS2中802.11代码深入理解—packet传输的流程 (转帖)
- 《Java多线程编程核心技术》读后感(七)
- SQL SERVER数据库修改是否区分大小写
- ARM 内核移植中常见的错误
- Android 之 LogDog
- cmd静默运行_【已解决】BAT批处理中如何静默执行,在完成后调用MSGBOX?
- VS 2019 16.11正式发布 | 新功能(Hot Reload 热重载)试用
- leetcode1046. 最后一块石头的重量(堆)
- python xpath定位元素方法_python--通过xpath相对节点位置查找元素(续)
- android 沙盒 ios,iOS中的沙盒机制
- Anybody = Nobody
- 线性规划 | 用实例展示Matlab和lingo求解线性规划问题的差异
- Java学习视频教程
- 微信小程序---点餐小程序左侧滑动菜单实现
- python二维数组的定义方式
- php怎么解析josn数据,用PHP解析JSON数据
- linux nano vim,修改ubuntu默认Nano编辑器为vim
- 问佛 (佛是過來人,人是未來佛)
- 解析G652,G657A,G655和G654光缆之间的区别
热门文章
- 区块链中Java基于WebSocket构建P2P网络
- msiexec安装参数详解
- Android手机锁代码研究---我的笔记
- php静态检测工具,PHP工具箱:PHPStan —— PHP 静态代码分析工具
- php多城市分站cms,织梦dedecms内核最新全国多城市分站地区插件
- e.preventDefault()与return false的区别
- 树莓派简单配置.txt
- 机器视觉基础笔记01
- php exif信息,php通过exif_read_data函数获取图片的exif信息 PHP
- 一只蒟蒻OIer的自我介绍