OpenGL Programming Guide

琐碎知识点:

glClearColor(): 指定窗口背景 被清除为什么颜色


glOrtho(): 指定OpenGL在绘制图像中所使用的坐标系统,决定了图像如何映射到屏幕上


glFlush(): 保证绘图命令将被执行 ,而非存储在缓冲区等待其他OpenGL命令


有些OpenGL函数名最后有一个字母v,他表示这个函数所接受的参数是个指向向量或数组的指针 ,而不是一系列的单独参数 ,如:glColor3fv (color_array);

代码讲解:  PART1

int main(int argc, char *argv[])               //argc:argument count,*argv[]指向argument数组的第一个值
{
    glutInit(&argc, argv);                          //glutInit,对GLUT进行初始化,这个函数必须在其它的GLUT使用之前调用一次。

    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);   //设置显示方式
    glutInitWindowPosition(100, 100);      //初始化窗口位置,距屏幕左边和距屏幕上边的像素位置

    glutInitWindowSize(400, 400);
    glutCreateWindow("第一个OpenGL程序");

  ... ...

 }

 

  //以上操作会得到一个空的控制台窗口,而没有OpenGL窗口。并且控制台窗口将很快消失。为了在OpenGL窗口中能够进行渲染,我们应该制定一个渲染函数,这里我们起名为void myDisplay:

void myDisplay(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
    glFlush();
}

 

//上面的函数的名字你可以自己取一个。现在你必须告诉GLUT使用我们上面的函数来进行渲染,这个叫寄存回调。让我们告诉GLUT这个函数 myDisplay应该被使用:

 int main(int argc, char *argv[])

{

    ... ...

    glutDisplayFunc(&myDisplay);             //寄存回调函数
    glutMainLoop();                                  //最后一件事是告诉GLUT我们准备进入应用程序事件处理循环。GLUT提供了一个函数让程序进入一

                                                                 个永不结束的循环。一直等待处理下一个事件。函数是 glutMainLoop()。
    return 0;
 }

 

-------------------------

代码(创建一个黑色为背景的窗口,在其中画一白色矩形):

#include <glut.h>

void myDisplay(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
    glFlush();
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(400, 400);
    glutCreateWindow("第一个OpenGL程序");
    glutDisplayFunc(&myDisplay);
    glutMainLoop();
    return 0;
}

PART2

通过part1,我们创建了一个控制台并且在OpenGL窗口中绘制了一个白色的矩形,但是当我们拉伸/缩小,最大化/最小化这个窗口时,我们会发现矩形的形状、长宽比例发生了改变,这是为什么捏?

 

这会发生是因为你没有正确设置投影矩阵 。默认的是透视投影矩阵且高宽比为1.因此高宽比改变了,投 影就会变形。因此只要高宽比改变了,投影就应该重新计算。

 

GLUT定义了当窗口大小改变时哪一个函数应该被调用。此外,这个函数还会在窗口初次被创建时调用,保证初始化窗口不是正方形的时候渲染也不会变形出错。



这个函数是glutReshapeFunc()。

void glutReshapeFunc(void(*func)(int width,int height));

参数:

func: 指负责设置正确投影的函数的名称。

因此我们必须做的第一件事是回到main()函数。在上一章的代码里加入对glutReshapeFunc()的调用,让我们把负责窗口尺寸的函数叫做changeSize。现在的代码如下。
----------------

#include <glut.h>

void myDisplay(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
    glFlush();
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(400, 400);
    glutCreateWindow("第一个OpenGL程序");
    glutDisplayFunc(&myDisplay);

    glutReshapeFunc( changeSize );
    glutMainLoop();
    return 0;
}

---------------------

下面我们需要做的就是定义函数changeSize()。从glutReshapeFunc()函数的声明可以看到,changSize()函数有两个形 参。这两个参数代表新的窗口高度和宽度。

 


void 

changeSize(int w, int h) {
 
         // 防止除数即高度为0
         // (你可以设置窗口宽度 为0).
         if(h == 0)
                 h = 1;
 
         float ratio = 1.0* w / h;
 
         // 单位化投影矩阵。
         glMatrixMode(GL_PROJECTION);
         glLoadIdentity();
         
         // 设置视口大小为增个窗 口大小
         glViewport(0, 0, w, h);
 
         // 设置正确的投影矩阵
         gluPerspective(45,ratio,1,1000);
        //下面是设置模型视图矩阵
         glMatrixMode(GL_MODELVIEW);
         glLoadIdentity();
         gluLookAt(0.0,0.0,5.0, 0.0,0.0,-1.0,0.0f,1.0f,0.0f);
}
我们在上一小段代码里引进了一些函数。下面让我们详细讲解,以免让你感到很迷茫。

第一步是计算高宽比(wight/height)。注意为了计算正确,我们必须保证高度不为0。

 

接着,我们设置当前矩阵为投影矩阵,这个矩阵定义了一个可视空间(viewing volume)。

我们调用一个单位矩阵来初始化投影矩阵。然后我们用函数glViewport把视口设置为整个窗口。你也可以设置不同的值。函数中(0,0)指定了视口的 左下角,(w,h)指定了视口矩形的大小。注意这个坐标是和客户区域有关的,而不是屏幕坐标。。

 

gluPerspective函数是其他OpenGL库(GLU库)里的一个函数。(gluPerspective函数的参数,设置请参见其他书籍。我并 不想在GLUT讲解里再加入其他一些OpenGL内容的讲解。)

 

最后就是设置模型观测矩阵。调用GL_MODELVIEW把当前矩阵设为模型观测矩阵。gluLookAt()也是GLU库里的一个函数,其参数详细设置 参见其他书籍。

From: http://www.lighthouse3d.com/opengl/glut/index.php?3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Includes Complete Coverage of the OpenGL® Shading Language! Today’s OpenGL software interface enables programmers to produce extraordinarily high-quality computer-generated images and interactive applications using 2D and 3D objects, color images, and programmable shaders. OpenGL® Programming Guide: The Official Guide to Learning OpenGL®, Version 4.3, Eighth Edition, has been almost completely rewritten and provides definitive, comprehensive information on OpenGL and the OpenGL Shading Language. This edition of the best-selling “Red Book” describes the features through OpenGL version 4.3. It also includes updated information and techniques formerly covered in OpenGL® Shading Language (the “Orange Book”). For the first time, this guide completely integrates shader techniques, alongside classic, functioncentric techniques. Extensive new text and code are presented, demonstrating the latest in OpenGL programming techniques. OpenGL® Programming Guide, Eighth Edition, provides clear explanations of OpenGL functionality and techniques, including processing geometric objects with vertex, tessellation, and geometry shaders using geometric transformations and viewing matrices; working with pixels and texture maps through fragment shaders; and advanced data techniques using framebuffer objects and compute shaders. New OpenGL features covered in this edition include Best practices and sample code for taking full advantage of shaders and the entire shading pipeline (including geometry and tessellation shaders) Integration of general computation into the rendering pipeline via compute shaders Techniques for binding multiple shader programs at once during application execution Latest GLSL features for doing advanced shading techniques Additional new techniques for optimizing graphics program performance
OpenGL开发指南英文第九版 What This Guide Contains This guide contains the following chapters: • Chapter 1 , “Introduction to OpenGL ,” provides a glimpse into what OpenGL can do. It also presents a simple OpenGL programand explains the essential programming details you need to know for the subsequent chapters. • Chapter 2 , “Shader Fundamentals ,” discusses the major features of the OpenGL Shading Language and SPIR-V, demonstrating how to initialize and use themwithin an application. • Chapter 3 , “Drawing with OpenGL ,” describes the various methods for rendering geometry using OpenGL, as well as some optimization techniques for making rendering more efficient. • Chapter 4 , “Color, Pixels, and Fragments ,” explains OpenGL’s processing of color, including how pixels are processed, how buffers are managed, and rendering techniques focused on pixel processing. • Chapter 5 , “Viewing Transformations, Culling, Clipping, and Feedback ,” details the operations for presenting a three-dimensional scene on a two-dimensional computer screen, including the mathematics and shader operations for the various types of geometric projection. • Chapter 6 , “Textures and Framebuffers ,” discusses combining geometric model s and imagery for creating realistic, highly detailed three-dimensional models. • Chapter 7 , “Light and Shadow ,” describes simulating illumination effects for computer graphics, focusing on implementing those techniques in programmable shaders. • Chapter 8 , “Procedural Texturing ,” details the generation of textures and other surface effects using programmable shaders for inc reased realismand other rendering effects. • Chapter 9 , “Tessellation Shaders ,” explains OpenGL’s shader facility for managing and tessellating geometric surfaces. • Chapter 10 , “Geometry Shaders ,” describe an additional technique for modifying geometric primitives within the OpenGL rendering pipeline using shaders. • Chapter 11 , “Memory ,” demonstrates techniques using OpenGL’s framebuffer and buffer memories for advanced rendering techniques and nongraphical uses. • Chapter 12 , “Compute Shaders ,” introduces the newest shader stage, which integrates general computation into the OpenGL rendering pipeline.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值