Home Database Mysql Tutorial opengl编程学习笔记(三)(2D绘图)

opengl编程学习笔记(三)(2D绘图)

Jun 07, 2016 pm 03:09 PM
opengl Can study notes Drawing programming

在opengl中可以绘制二维图像,分为两种,BITMAP和image 1.bitmap和image的区别有: bitmap中对图像的每个像素只存储一位(0或1),用来表示用当前颜色绘制还是不绘制,而image是图像的每个像素存储可能很多位,存储它的各个通道的颜色信息; bitmap是绘制在

在opengl中可以绘制二维图像,分为两种,BITMAP和image

1.bitmap和image的区别有:

bitmap中对图像的每个像素只存储一位(0或1),用来表示用当前颜色绘制还是不绘制,而image是图像的每个像素存储可能很多位,存储它的各个通道的颜色信息;

bitmap是绘制在三位空间中的一群点,其中每个点都受各变换矩阵的影响,等同于一群Gl_begin(GL_POINTS)操作联合在一起 ,而image是直接绘制在帧缓存上,在绘制原点所投影到的屏幕坐标处开始绘制,就是直接在二维屏幕上绘制,就像普通的二维绘制一样,他只有那个绘制的原点受变换矩阵的影响,比如当原点的位置被裁剪掉之后,整张图片也将消失。

bitmap是绘制在三位场景的一些点,也就是每个点都会有法向等信息,回收光照等影响,拉近看还会发现点间的距离,image是直接写入帧缓存的,绘制的颜色就是最终看到的颜色, 像素点间不会有距离

2.bitmap的绘制步骤

glColor3f制定当前绘制的点的颜色

glRasterPos2f定义当前的绘制原点(OPENGL绘制的原点是在左下角的,即定义之后,都是从这点开始按照从左至右,从下至上绘制的)

只有当调用了 glRasterPos2f之后,他前面的glColor3f才会起作用,所以要先设置颜色,再定义原点

void glBitmap( GLsizei width,  绘制宽度的点数
  GLsizei height, 绘制高度 的点数
  GLfloat xorig, 在以原点的二维坐标系中的X坐标 
  GLfloat yorig, 在以原点的二维坐标系中的Y坐标 
  GLfloat xmove,绘制后原点的横向位移
  GLfloat ymove,绘制后原点的纵向位移
  const GLubyte * bitmap 存储的数据);
绘制存储在内存中的数据

 其中bitmap中存储的数据按照从左下角起8位对其(一个GLUBYTE),例如下图的数据就应该因该存储为

opengl编程学习笔记(三)(2D绘图) 

0xc0, 0x00, 0xc0, 0x00, 0xc0,0x00, 0xc0, 0x00, 0xc0, 0x00,0xff, 0x00, 0xff, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0xc0, 0x00,0xff, 0xc0, 0xff, 0xc0。如果width不是八的倍数,比如是30个点,那么这一行仍然要占8的倍数位,应占用4个字节,最后面的两位将被自动舍弃。

 

 bitmap更适合绘制颜色较单一,且需要能进行三维观察的图

他也可以绘制彩色图,只是要每个点绘制前改变当前绘制颜色,如图

opengl编程学习笔记(三)(2D绘图) 

2.image的绘制步骤

 2.1glPixelStorei()设置数据的存储和解读方式,这里可以进行的常用设置有

   GL_PACK_SWAP_BYTES改变一个像素各个成分(不是颜色通道的顺序)的顺序,比如原先的存储顺序为(颜色、深度、索引),颠倒后为(索引、深度、颜色),而一个颜色的通常都存储为RGB。这个设置通常不用改变

  剪裁图像:比如我们只要绘制在内存中图像数据中的一个矩形区域,原图为640*480,我们需要绘制从(10,10) 开始长100,宽100的区域到屏幕。我们需要先用glPixelStorei(GL_UNPACK_ROW_LENGTH,640)设置图像的原宽为640,再用glPixelStorei(GL_UNPACK_SKIP_ROWS,10)设置我们跳过了10行,然后用glPixelStorei(GL_UNPACK_SKIP_PIXELS,10)设置我们又把起点跳过了10个像素(列),最后调用glDrawPixels(100,100...)来绘图。

  2.2glPixelTransferf设置像素的传递和映射,用这个操作,可以在存储或读取时将特定的颜色映射到一种颜色,或者将特定颜色的值扩大缩小等

 2.3glPixelZoom(x,y)设置几何变换,这将设置最后图像的一个像素在X/Y方向上被放大的倍数,都是2则放大四倍,如果为负数在读取时进行镜像

 2.4调用glDrawPixels绘制存储在内存中的图像到帧缓存上

      或调用glReadPixels读取帧缓存上的数据到内存中(可以做截图用) 

      或调用glCopyPixels拷贝帧缓存的一处数据到帧缓存的另一处(觉得很适合做赛车游戏的后视镜 ^^)

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Remove duplicate values ​​from PHP array using regular expressions Remove duplicate values ​​from PHP array using regular expressions Apr 26, 2024 pm 04:33 PM

How to remove duplicate values ​​from PHP array using regular expressions: Use regular expression /(.*)(.+)/i to match and replace duplicates. Iterate through the array elements and check for matches using preg_match. If it matches, skip the value; otherwise, add it to a new array with no duplicate values.

What is programming for and what is the use of learning it? What is programming for and what is the use of learning it? Apr 28, 2024 pm 01:34 PM

1. Programming can be used to develop various software and applications, including websites, mobile applications, games, and data analysis tools. Its application fields are very wide, covering almost all industries, including scientific research, health care, finance, education, entertainment, etc. 2. Learning programming can help us improve our problem-solving skills and logical thinking skills. During programming, we need to analyze and understand problems, find solutions, and translate them into code. This way of thinking can cultivate our analytical and abstract abilities and improve our ability to solve practical problems.

Build browser-based applications with Golang Build browser-based applications with Golang Apr 08, 2024 am 09:24 AM

Build browser-based applications with Golang Golang combines with JavaScript to build dynamic front-end experiences. Install Golang: Visit https://golang.org/doc/install. Set up a Golang project: Create a file called main.go. Using GorillaWebToolkit: Add GorillaWebToolkit code to handle HTTP requests. Create HTML template: Create index.html in the templates subdirectory, which is the main template.

Collection of C++ programming puzzles: stimulate thinking and improve programming skills Collection of C++ programming puzzles: stimulate thinking and improve programming skills Jun 01, 2024 pm 10:26 PM

C++ programming puzzles cover algorithm and data structure concepts such as Fibonacci sequence, factorial, Hamming distance, maximum and minimum values ​​of arrays, etc. By solving these puzzles, you can consolidate C++ knowledge and improve algorithm understanding and programming skills.

Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Oct 11, 2024 pm 08:58 PM

Pythonempowersbeginnersinproblem-solving.Itsuser-friendlysyntax,extensivelibrary,andfeaturessuchasvariables,conditionalstatements,andloopsenableefficientcodedevelopment.Frommanagingdatatocontrollingprogramflowandperformingrepetitivetasks,Pythonprovid

The Key to Coding: Unlocking the Power of Python for Beginners The Key to Coding: Unlocking the Power of Python for Beginners Oct 11, 2024 pm 12:17 PM

Python is an ideal programming introduction language for beginners through its ease of learning and powerful features. Its basics include: Variables: used to store data (numbers, strings, lists, etc.). Data type: Defines the type of data in the variable (integer, floating point, etc.). Operators: used for mathematical operations and comparisons. Control flow: Control the flow of code execution (conditional statements, loops).

Unleash Your Inner Programmer: C for Absolute Beginners Unleash Your Inner Programmer: C for Absolute Beginners Oct 11, 2024 pm 03:50 PM

C is an ideal language for beginners to learn programming, and its advantages include efficiency, versatility, and portability. Learning C language requires: Installing a C compiler (such as MinGW or Cygwin) Understanding variables, data types, conditional statements and loop statements Writing the first program containing the main function and printf() function Practicing through practical cases (such as calculating averages) C language knowledge

Use golang's error wrapping and unwinding mechanism for error handling Use golang's error wrapping and unwinding mechanism for error handling Apr 25, 2024 am 08:15 AM

Error handling in Go includes wrapping errors and unwrapping errors. Wrapping errors allows one error type to be wrapped with another, providing a richer context for the error. Expand errors and traverse the nested error chain to find the lowest-level error for easy debugging. By combining these two technologies, error conditions can be effectively handled, providing richer error context and better debugging capabilities.

See all articles