1. 下载freeglut并使用cmake配置,编译安装
https://github.com/FreeGLUTProject/freeglut
git clone https://github.com/FreeGLUTProject/freeglut.git
并配置进VC++包含目录中去
库目录,连接器输入项
2.下载nvidia vendor-neutral dispatch layer for arbitrating OpenGL API calls
Files · master · glvnd / libglvnd · GitLab
git clone https://github.com/NVIDIA/open-gpu-doc.git
并配置进vc++包含目录中去
//#include <GL/gl.h>
#include <GL/freeglut.h>
#include <GL/glext.h>
#include <stdlib.h>
#include <stdio.h>
#ifdef GL_VERSION_1_2
#define iWidth 16
#define iHeight 16
#define iDepth 16
static GLubyte image[iDepth][iHeight][iWidth][3]; //深度,高度,宽度+3个颜色通道
static GLuint texName;
/* Create a 16x16x16x3 array with different color values in
* each array element [r, g, b]. Values range from 0 to 255.
*/
PFNGLTEXIMAGE3DPROC glTexImage3D_ptr; //定义一个glTexImage3D指针
void makeImage(void) //造纹理
{
int s, t, r;
for (s = 0; s < 16; s++)
for (t = 0; t < 16; t++)
for (r = 0; r < 16; r++) {
image[r][t][s][0] = (GLubyte)(s * 17);
image[r][t][s][1] = (GLubyte)(t * 17);
image[r][t][s][2] = (GLubyte)(r * 17);
}
}
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT); //两点之间的着色模式为片状,另一个参数可以写为,GL_SMOOTH这样绘制出来的就是渐变效果
//glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST); //启用深度测试
makeImage(); //产生纹理
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &texName);
glBindTexture(GL_TEXTURE_3D, texName);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
//q是一个缩放因子,相当于顶点坐标中的w。实际应用在纹理读取中的坐标应该是s/q,t/q,r/q。默认情况下,q是1.0。通常情况下貌似没什么用,但是在一些产生纹理坐标的高级算法比如阴影贴图中
//s、t、r分别相当于普通坐标系中的x、y、z三个方向。分别对应glTexImage3D中的参数width、height、depth。
glTexImage3D_ptr = reinterpret_cast<PFNGLTEXIMAGE3DPROC>(::wglGetProcAddress("glTexImage3D")); //使能glTexImage3D指针
glTexImage3D_ptr(GL_TEXTURE_3D, 0, GL_RGB, iWidth, iHeight, iDepth, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glEnable(GL_TEXTURE_3D);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
glTexCoord3f(0.0, 0.0, 0.0); glVertex3f(-2.25, -1.0, 0.0);
glTexCoord3f(0.0, 1.0, 0.0); glVertex3f(-2.25, 1.0, 0.0);
glTexCoord3f(1.0, 1.0, 1.0); glVertex3f(-0.25, 1.0, 0.0);
glTexCoord3f(1.0, 0.0, 1.0); glVertex3f(-0.25, -1.0, 0.0);
glTexCoord3f(0.0, 0.0, 1.0); glVertex3f(0.25, -1.0, 0.0);
glTexCoord3f(0.0, 1.0, 1.0); glVertex3f(0.25, 1.0, 0.0);
glTexCoord3f(1.0, 1.0, 0.0); glVertex3f(2.25, 1.0, 0.0);
glTexCoord3f(1.0, 0.0, 0.0); glVertex3f(2.25, -1.0, 0.0);
glEnd();
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 1.0, 30.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -4.0);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(250, 250);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
#else
int main(int argc, char** argv)
{
fprintf(stderr, "This program demonstrates a feature which is not in OpenGL Version 1.0 or 1.1.\n");
fprintf(stderr, "If your implementation of OpenGL has the right extensions,\n");
fprintf(stderr, "you may be able to modify this program to make it run.\n");
return 0;
}
#endif