OpenGL开发环境配置-Windows/MinGW/Clion/CMake

因为某些原因,不想用过于臃肿的VS了,转而使用常用的jetbrains的CLion,Clion沿袭了jetbrans的优良传统,基本代码提示功能还是比较好的,不过就是对于windows不熟悉cmake(像我这样)的朋友可能不是太友好,经过了2个小时的查资料,终于正常运行了一个简单示例。

下面谈谈如何在Windows下配置这个开发环境。

起始,我是参考了我的前一篇OpenGL+VS开发环境的搭建,实际上除了freeglut重新下载的是MinGW版本之外,其他的文件并无区别,当然为了方便引用,我把所有相关文件都保存到了一个文件目录下,按照dll(bin),头文件(include),lib(lib)存放。

下面是我的文件目录结构

C:.
│ re.txt

├─bin
│ │ freeglut.dll
│ │ glu32.dll
│ │ opengl32.dll
│ │
│ ├─Release
│ │ ├─Win32
│ │ │ glew32.dll
│ │ │ glewinfo.exe
│ │ │ visualinfo.exe
│ │ │
│ │ └─x64
│ │ glew32.dll
│ │ glewinfo.exe
│ │ visualinfo.exe
│ │
│ ├─ReleaseMX
│ │ ├─Win32
│ │ │ glew32mx.dll
│ │ │
│ │ └─x64
│ │ glew32mx.dll
│ │
│ └─x64
│ freeglut.dll

├─include
│ │ GL.h
│ │ GLU.h
│ │
│ └─GL
│ freeglut.h
│ freeglut_ext.h
│ freeglut_std.h
│ glew.h
│ glut.h
│ glxew.h
│ wglew.h

└─lib
│ GlU32.Lib
│ libfreeglut.a
│ libfreeglut_static.a
│ OpenGL32.Lib

├─Release
│ ├─Win32
│ │ glew32.lib
│ │ glew32s.lib
│ │
│ └─x64
│ glew32.lib
│ glew32s.lib

├─ReleaseMX
│ ├─Win32
│ │ glew32mx.lib
│ │ glew32mxs.lib
│ │
│ └─x64
│ glew32mx.lib
│ glew32mxs.lib

└─x64
libfreeglut.a
libfreeglut_static.a

文件怎么放其实很随意,就是http://www.cnblogs.com/lhyz/p/4178004.html中第一步的文件按照类型放到上述三个文件夹下,下载freeglut的mingw版本后解压缩,同样将相应文件夹下的东西转移到上述文件夹下,同样也适用于glew(经过测试暂时用不到它)。

然后是最重要的,将dll的那个bin目录加入到系统的PATH环境变量下,否则程序会因为运行时检测不到依赖而退出(诡异的是build会成功,所以---呵呵)

接下来的问题就是怎么编写CMakeList.txt了

在Clion下新建项目之后会自动帮你建立CMakeList.txt和一个源文件,

OpenGL开发环境配置-Windows/MinGW/Clion/CMake

我编写CMakeList.txt暂时如下:

cmake_minimum_required(VERSION 3.2)
project(opengl) include_directories(C:\\\\opengl\\\\include)
add_executable(opengl dinoshader.c) set(TARGET_LIB
"C:\\\\opengl\\\\lib\\\\GlU32.Lib"
"C:\\\\opengl\\\\lib\\\\OpenGL32.Lib"
"C:\\\\opengl\\\\lib\\\\libfreeglut.a"
"C:\\\\opengl\\\\lib\\\\libfreeglut_static.a"
) target_link_libraries(opengl ${TARGET_LIB})

TARGET_LIB设置的就是所有需要引用的lib和a库文件的绝对地址了,如果还有缺少的只需要再添加就好了。

下面测试文件dinoshader.c

 /* Copyright (c) Mark J. Kilgard, 1994, 1997.  */

 /* This program is freely distributable without licensing fees
and is provided without guarantee or warrantee expressed or
implied. This program is -not- in the public domain. */ /* Example for PC game developers to show how to *combine* texturing,
reflections, and projected shadows all in real-time with OpenGL.
Robust reflections use stenciling. Robust projected shadows
use both stenciling and polygon offset. PC game programmers
should realize that neither stenciling nor polygon offset are
supported by Direct3D, so these real-time rendering algorithms
are only really viable with OpenGL. The program has modes for disabling the stenciling and polygon
offset uses. It is worth running this example with these features
toggled off so you can see the sort of artifacts that result. Notice that the floor texturing, reflections, and shadowing
all co-exist properly. */ /* When you run this program: Left mouse button controls the
view. Middle mouse button controls light position (left &
right rotates light around dino; up & down moves light
position up and down). Right mouse button pops up menu. */ /* Check out the comments in the "redraw" routine to see how the
reflection blending and surface stenciling is done. You can
also see in "redraw" how the projected shadows are rendered, including the use of stenciling and polygon offset. */ /* This program is derived from glutdino.c */ /* Compile: cc -o dinoshade dinoshade.c -lglut -lGLU -lGL -lXmu -lXext -lX11 -lm */ #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h> /* for cos(), sin(), and sqrt() */
#include <GL/freeglut.h> /* OpenGL Utility Toolkit header */ /* Some <math.h> files do not define M_PI... */
#ifndef M_PI
#define M_PI 3.14159265
#endif /* Variable controlling various rendering modes. */
static int stencilReflection = , stencilShadow = , offsetShadow = ;
static int renderShadow = , renderDinosaur = , renderReflection = ;
static int linearFiltering = , useMipmaps = , useTexture = ;
static int reportSpeed = ;
static int animation = ;
static GLboolean lightSwitch = GL_TRUE;
static int directionalLight = ;
static int forceExtension = ; /* Time varying or user-controled variables. */
static float jump = 0.0;
static float lightAngle = 0.0, lightHeight = ;
GLfloat angle = -;
/* in degrees */
GLfloat angle2 = ;
/* in degrees */ int moving, startx, starty;
int lightMoving = , lightStartX, lightStartY; enum {
MISSING, EXTENSION, ONE_DOT_ONE
};
int polygonOffsetVersion; static GLdouble bodyWidth = 3.0;
/* *INDENT-OFF* */
static GLfloat body[][] = {{, },
{, },
{, },
{, },
{, },
{, },
{, 11.5},
{, },
{, },
{, 13.5},
{, },
{, },
{, },
{, },
{, },
{, },
{, },
{, },
{, },
{, },
{, },
{, }};
static GLfloat arm[][] = {{, },
{, },
{, },
{, },
{, },
{, },
{, 9.5},
{, },
{, },
{15.5, },
{14.5, },
{, },
{, },
{, },
{, },
{, }};
static GLfloat leg[][] = {{, },
{, },
{, },
{, },
{, },
{, 0.5},
{, },
{, },
{, },
{, },
{, },
{, },
{, },
{, }};
static GLfloat eye[][] = {{8.75, },
{, 14.7},
{9.6, 14.7},
{10.1, },
{9.6, 15.25},
{, 15.25}};
static GLfloat lightPosition[];
static GLfloat lightColor[] = {0.8, 1.0, 0.8, 1.0};
/* green-tinted */
static GLfloat skinColor[] = {0.1, 1.0, 0.1, 1.0}, eyeColor[] = {1.0, 0.2, 0.2, 1.0};
/* *INDENT-ON* */ /* Nice floor texture tiling pattern. */
static char *circles[] = {
"....xxxx........",
"..xxxxxxxx......",
".xxxxxxxxxx.....",
".xxx....xxx.....",
"xxx......xxx....",
"xxx......xxx....",
"xxx......xxx....",
"xxx......xxx....",
".xxx....xxx.....",
".xxxxxxxxxx.....",
"..xxxxxxxx......",
"....xxxx........",
"................",
"................",
"................",
"................",
}; static void
makeFloorTexture(void) {
GLubyte floorTexture[][][];
GLubyte *loc;
int s, t; /* Setup RGB image for the texture. */
loc = (GLubyte *) floorTexture;
for (t = ; t < ; t++) {
for (s = ; s < ; s++) {
if (circles[t][s] == 'x') {
/* Nice green. */
loc[] = 0x1f;
loc[] = 0x8f;
loc[] = 0x1f;
} else {
/* Light gray. */
loc[] = 0xaa;
loc[] = 0xaa;
loc[] = 0xaa;
}
loc += ;
}
} glPixelStorei(GL_UNPACK_ALIGNMENT, ); if (useMipmaps) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_LINEAR);
gluBuild2DMipmaps(GL_TEXTURE_2D, , , ,
GL_RGB, GL_UNSIGNED_BYTE, floorTexture);
} else {
if (linearFiltering) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
} else {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
}
glTexImage2D(GL_TEXTURE_2D, , , , , ,
GL_RGB, GL_UNSIGNED_BYTE, floorTexture);
}
} enum {
X, Y, Z, W
};
enum {
A, B, C, D
}; /* Create a matrix that will project the desired shadow. */
void
shadowMatrix(GLfloat shadowMat[][],
GLfloat groundplane[],
GLfloat lightpos[]) {
GLfloat dot; /* Find dot product between light position vector and ground plane normal. */
dot = groundplane[X] * lightpos[X] +
groundplane[Y] * lightpos[Y] +
groundplane[Z] * lightpos[Z] +
groundplane[W] * lightpos[W]; shadowMat[][] = dot - lightpos[X] * groundplane[X];
shadowMat[][] = .f - lightpos[X] * groundplane[Y];
shadowMat[][] = .f - lightpos[X] * groundplane[Z];
shadowMat[][] = .f - lightpos[X] * groundplane[W]; shadowMat[X][] = .f - lightpos[Y] * groundplane[X];
shadowMat[][] = dot - lightpos[Y] * groundplane[Y];
shadowMat[][] = .f - lightpos[Y] * groundplane[Z];
shadowMat[][] = .f - lightpos[Y] * groundplane[W]; shadowMat[X][] = .f - lightpos[Z] * groundplane[X];
shadowMat[][] = .f - lightpos[Z] * groundplane[Y];
shadowMat[][] = dot - lightpos[Z] * groundplane[Z];
shadowMat[][] = .f - lightpos[Z] * groundplane[W]; shadowMat[X][] = .f - lightpos[W] * groundplane[X];
shadowMat[][] = .f - lightpos[W] * groundplane[Y];
shadowMat[][] = .f - lightpos[W] * groundplane[Z];
shadowMat[][] = dot - lightpos[W] * groundplane[W]; } /* Find the plane equation given 3 points. */
void
findPlane(GLfloat plane[],
GLfloat v0[], GLfloat v1[], GLfloat v2[]) {
GLfloat vec0[], vec1[]; /* Need 2 vectors to find cross product. */
vec0[X] = v1[X] - v0[X];
vec0[Y] = v1[Y] - v0[Y];
vec0[Z] = v1[Z] - v0[Z]; vec1[X] = v2[X] - v0[X];
vec1[Y] = v2[Y] - v0[Y];
vec1[Z] = v2[Z] - v0[Z]; /* find cross product to get A, B, and C of plane equation */
plane[A] = vec0[Y] * vec1[Z] - vec0[Z] * vec1[Y];
plane[B] = -(vec0[X] * vec1[Z] - vec0[Z] * vec1[X]);
plane[C] = vec0[X] * vec1[Y] - vec0[Y] * vec1[X]; plane[D] = -(plane[A] * v0[X] + plane[B] * v0[Y] + plane[C] * v0[Z]);
} void
extrudeSolidFromPolygon(GLfloat data[][], unsigned int dataSize,
GLdouble thickness, GLuint side, GLuint edge, GLuint whole) {
static GLUtriangulatorObj *tobj = NULL;
GLdouble vertex[], dx, dy, len;
int i;
int count = (int) (dataSize / ( * sizeof(GLfloat))); if (tobj == NULL) {
tobj = gluNewTess(); /* create and initialize a GLU
polygon tesselation object */
gluTessCallback(tobj, GLU_BEGIN, glBegin);
gluTessCallback(tobj, GLU_VERTEX, glVertex2fv); /* semi-tricky */
gluTessCallback(tobj, GLU_END, glEnd);
}
glNewList(side, GL_COMPILE);
glShadeModel(GL_SMOOTH); /* smooth minimizes seeing
tessellation */
gluBeginPolygon(tobj);
for (i = ; i < count; i++) {
vertex[] = data[i][];
vertex[] = data[i][];
vertex[] = ;
gluTessVertex(tobj, vertex, data[i]);
}
gluEndPolygon(tobj);
glEndList();
glNewList(edge, GL_COMPILE);
glShadeModel(GL_FLAT); /* flat shade keeps angular hands
from being "smoothed" */
glBegin(GL_QUAD_STRIP);
for (i = ; i <= count; i++) {
/* mod function handles closing the edge */
glVertex3f(data[i % count][], data[i % count][], 0.0);
glVertex3f(data[i % count][], data[i % count][], thickness);
/* Calculate a unit normal by dividing by Euclidean
distance. We * could be lazy and use
glEnable(GL_NORMALIZE) so we could pass in * arbitrary
normals for a very slight performance hit. */
dx = data[(i + ) % count][] - data[i % count][];
dy = data[i % count][] - data[(i + ) % count][];
len = sqrt(dx * dx + dy * dy);
glNormal3f(dx / len, dy / len, 0.0);
}
glEnd();
glEndList();
glNewList(whole, GL_COMPILE);
glFrontFace(GL_CW);
glCallList(edge);
glNormal3f(0.0, 0.0, -1.0); /* constant normal for side */
glCallList(side);
glPushMatrix();
glTranslatef(0.0, 0.0, thickness);
glFrontFace(GL_CCW);
glNormal3f(0.0, 0.0, 1.0); /* opposite normal for other side */
glCallList(side);
glPopMatrix();
glEndList();
} /* Enumerants for refering to display lists. */
typedef enum {
RESERVED, BODY_SIDE, BODY_EDGE, BODY_WHOLE, ARM_SIDE, ARM_EDGE, ARM_WHOLE,
LEG_SIDE, LEG_EDGE, LEG_WHOLE, EYE_SIDE, EYE_EDGE, EYE_WHOLE
} displayLists; static void
makeDinosaur(void) {
extrudeSolidFromPolygon(body, sizeof(body), bodyWidth,
BODY_SIDE, BODY_EDGE, BODY_WHOLE);
extrudeSolidFromPolygon(arm, sizeof(arm), bodyWidth / ,
ARM_SIDE, ARM_EDGE, ARM_WHOLE);
extrudeSolidFromPolygon(leg, sizeof(leg), bodyWidth / ,
LEG_SIDE, LEG_EDGE, LEG_WHOLE);
extrudeSolidFromPolygon(eye, sizeof(eye), bodyWidth + 0.2,
EYE_SIDE, EYE_EDGE, EYE_WHOLE);
} static void
drawDinosaur(void) {
glPushMatrix();
/* Translate the dinosaur to be at (0,8,0). */
glTranslatef(-, , -bodyWidth / );
glTranslatef(0.0, jump, 0.0);
glMaterialfv(GL_FRONT, GL_DIFFUSE, skinColor);
glCallList(BODY_WHOLE);
glTranslatef(0.0, 0.0, bodyWidth);
glCallList(ARM_WHOLE);
glCallList(LEG_WHOLE);
glTranslatef(0.0, 0.0, -bodyWidth - bodyWidth / );
glCallList(ARM_WHOLE);
glTranslatef(0.0, 0.0, -bodyWidth / );
glCallList(LEG_WHOLE);
glTranslatef(0.0, 0.0, bodyWidth / - 0.1);
glMaterialfv(GL_FRONT, GL_DIFFUSE, eyeColor);
glCallList(EYE_WHOLE);
glPopMatrix();
} static GLfloat floorVertices[][] = {
{-20.0, 0.0, 20.0},
{20.0, 0.0, 20.0},
{20.0, 0.0, -20.0},
{-20.0, 0.0, -20.0},
}; /* Draw a floor (possibly textured). */
static void
drawFloor(void) {
glDisable(GL_LIGHTING); if (useTexture) {
glEnable(GL_TEXTURE_2D);
} glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex3fv(floorVertices[]);
glTexCoord2f(0.0, 16.0);
glVertex3fv(floorVertices[]);
glTexCoord2f(16.0, 16.0);
glVertex3fv(floorVertices[]);
glTexCoord2f(16.0, 0.0);
glVertex3fv(floorVertices[]);
glEnd(); if (useTexture) {
glDisable(GL_TEXTURE_2D);
} glEnable(GL_LIGHTING);
} static GLfloat floorPlane[];
static GLfloat floorShadow[][]; static void
redraw(void) {
int start, end; if (reportSpeed) {
start = glutGet(GLUT_ELAPSED_TIME);
} /* Clear; default stencil clears to zero. */
if ((stencilReflection && renderReflection) || (stencilShadow && renderShadow)) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
} else {
/* Avoid clearing stencil when not using it. */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
} /* Reposition the light source. */
lightPosition[] = * cos(lightAngle);
lightPosition[] = lightHeight;
lightPosition[] = * sin(lightAngle);
if (directionalLight) {
lightPosition[] = 0.0;
} else {
lightPosition[] = 1.0;
} shadowMatrix(floorShadow, floorPlane, lightPosition); glPushMatrix();
/* Perform scene rotations based on user mouse input. */
glRotatef(angle2, 1.0, 0.0, 0.0);
glRotatef(angle, 0.0, 1.0, 0.0); /* Tell GL new light source position. */
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); if (renderReflection) {
if (stencilReflection) {
/* We can eliminate the visual "artifact" of seeing the "flipped"
dinosaur underneath the floor by using stencil. The idea is
draw the floor without color or depth update but so that
a stencil value of one is where the floor will be. Later when
rendering the dinosaur reflection, we will only update pixels
with a stencil value of 1 to make sure the reflection only
lives on the floor, not below the floor. */ /* Don't update color or depth. */
glDisable(GL_DEPTH_TEST);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); /* Draw 1 into the stencil buffer. */
glEnable(GL_STENCIL_TEST);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
glStencilFunc(GL_ALWAYS, , 0xffffffff); /* Now render floor; floor pixels just get their stencil set to 1. */
drawFloor(); /* Re-enable update of color and depth. */
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glEnable(GL_DEPTH_TEST); /* Now, only render where stencil is set to 1. */
glStencilFunc(GL_EQUAL, , 0xffffffff); /* draw if ==1 */
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
} glPushMatrix(); /* The critical reflection step: Reflect dinosaur through the floor
(the Y=0 plane) to make a relection. */
glScalef(1.0, -1.0, 1.0); /* Reflect the light position. */
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); /* To avoid our normals getting reversed and hence botched lighting
on the reflection, turn on normalize. */
glEnable(GL_NORMALIZE);
glCullFace(GL_FRONT); /* Draw the reflected dinosaur. */
drawDinosaur(); /* Disable noramlize again and re-enable back face culling. */
glDisable(GL_NORMALIZE);
glCullFace(GL_BACK); glPopMatrix(); /* Switch back to the unreflected light position. */
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); if (stencilReflection) {
glDisable(GL_STENCIL_TEST);
}
} /* Back face culling will get used to only draw either the top or the
bottom floor. This let's us get a floor with two distinct
appearances. The top floor surface is reflective and kind of red.
The bottom floor surface is not reflective and blue. */ /* Draw "bottom" of floor in blue. */
glFrontFace(GL_CW); /* Switch face orientation. */
glColor4f(0.1, 0.1, 0.7, 1.0);
drawFloor();
glFrontFace(GL_CCW); if (renderShadow) {
if (stencilShadow) {
/* Draw the floor with stencil value 3. This helps us only
draw the shadow once per floor pixel (and only on the
floor pixels). */
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, , 0xffffffff);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
}
} /* Draw "top" of floor. Use blending to blend in reflection. */
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4f(0.7, 0.0, 0.0, 0.3);
glColor4f(1.0, 1.0, 1.0, 0.3);
drawFloor();
glDisable(GL_BLEND); if (renderDinosaur) {
/* Draw "actual" dinosaur, not its reflection. */
drawDinosaur();
} if (renderShadow) { /* Render the projected shadow. */ if (stencilShadow) { /* Now, only render where stencil is set above 2 (ie, 3 where
the top floor is). Update stencil with 2 where the shadow
gets drawn so we don't redraw (and accidently reblend) the
shadow). */
glStencilFunc(GL_LESS, , 0xffffffff); /* draw if ==1 */
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
} /* To eliminate depth buffer artifacts, we use polygon offset
to raise the depth of the projected shadow slightly so
that it does not depth buffer alias with the floor. */
if (offsetShadow) {
switch (polygonOffsetVersion) {
case EXTENSION:
#ifdef GL_EXT_polygon_offset
glEnable(GL_POLYGON_OFFSET_EXT);
break;
#endif
#ifdef GL_VERSION_1_1
case ONE_DOT_ONE:
glEnable(GL_POLYGON_OFFSET_FILL);
break;
#endif
case MISSING:
/* Oh well. */
break;
}
} /* Render 50% black shadow color on top of whatever the
floor appareance is. */
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_LIGHTING); /* Force the 50% black. */
glColor4f(0.0, 0.0, 0.0, 0.5); glPushMatrix();
/* Project the shadow. */
glMultMatrixf((GLfloat *) floorShadow);
drawDinosaur();
glPopMatrix(); glDisable(GL_BLEND);
glEnable(GL_LIGHTING); if (offsetShadow) {
switch (polygonOffsetVersion) {
#ifdef GL_EXT_polygon_offset
case EXTENSION:
glDisable(GL_POLYGON_OFFSET_EXT);
break;
#endif
#ifdef GL_VERSION_1_1
case ONE_DOT_ONE:
glDisable(GL_POLYGON_OFFSET_FILL);
break;
#endif
case MISSING:
/* Oh well. */
break;
}
}
if (stencilShadow) {
glDisable(GL_STENCIL_TEST);
}
} glPushMatrix();
glDisable(GL_LIGHTING);
glColor3f(1.0, 1.0, 0.0);
if (directionalLight) {
/* Draw an arrowhead. */
glDisable(GL_CULL_FACE);
glTranslatef(lightPosition[], lightPosition[], lightPosition[]);
glRotatef(lightAngle * -180.0 / M_PI, , , );
glRotatef(atan(lightHeight / ) * 180.0 / M_PI, , , );
glBegin(GL_TRIANGLE_FAN);
glVertex3f(, , );
glVertex3f(, , );
glVertex3f(, -, );
glVertex3f(, -, -);
glVertex3f(, , -);
glVertex3f(, , );
glEnd();
/* Draw a white line from light direction. */
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES);
glVertex3f(, , );
glVertex3f(, , );
glEnd();
glEnable(GL_CULL_FACE);
} else {
/* Draw a yellow ball at the light source. */
glTranslatef(lightPosition[], lightPosition[], lightPosition[]);
glutSolidSphere(1.0, , );
}
glEnable(GL_LIGHTING);
glPopMatrix(); glPopMatrix(); if (reportSpeed) {
glFinish();
end = glutGet(GLUT_ELAPSED_TIME);
printf("Speed %.3g frames/sec (%d ms)\n", 1000.0 / (end - start), end - start);
} glutSwapBuffers();
} /* ARGSUSED2 */
static void
mouse(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON) {
if (state == GLUT_DOWN) {
moving = ;
startx = x;
starty = y;
}
if (state == GLUT_UP) {
moving = ;
}
}
if (button == GLUT_MIDDLE_BUTTON) {
if (state == GLUT_DOWN) {
lightMoving = ;
lightStartX = x;
lightStartY = y;
}
if (state == GLUT_UP) {
lightMoving = ;
}
}
} /* ARGSUSED1 */
static void
motion(int x, int y) {
if (moving) {
angle = angle + (x - startx);
angle2 = angle2 + (y - starty);
startx = x;
starty = y;
glutPostRedisplay();
}
if (lightMoving) {
lightAngle += (x - lightStartX) / 40.0;
lightHeight += (lightStartY - y) / 20.0;
lightStartX = x;
lightStartY = y;
glutPostRedisplay();
}
} /* Advance time varying state when idle callback registered. */
static void
idle(void) {
static float time = 0.0; time = glutGet(GLUT_ELAPSED_TIME) / 500.0; jump = 4.0 * fabs(sin(time) * 0.5);
if (!lightMoving) {
lightAngle += 0.03;
}
glutPostRedisplay();
} enum {
M_NONE, M_MOTION, M_LIGHT, M_TEXTURE, M_SHADOWS, M_REFLECTION, M_DINOSAUR,
M_STENCIL_REFLECTION, M_STENCIL_SHADOW, M_OFFSET_SHADOW,
M_POSITIONAL, M_DIRECTIONAL, M_PERFORMANCE
}; static void
controlLights(int value) {
switch (value) {
case M_NONE:
return;
case M_MOTION:
animation = - animation;
if (animation) {
glutIdleFunc(idle);
} else {
glutIdleFunc(NULL);
}
break;
case M_LIGHT:
lightSwitch = !lightSwitch;
if (lightSwitch) {
glEnable(GL_LIGHT0);
} else {
glDisable(GL_LIGHT0);
}
break;
case M_TEXTURE:
useTexture = !useTexture;
break;
case M_SHADOWS:
renderShadow = - renderShadow;
break;
case M_REFLECTION:
renderReflection = - renderReflection;
break;
case M_DINOSAUR:
renderDinosaur = - renderDinosaur;
break;
case M_STENCIL_REFLECTION:
stencilReflection = - stencilReflection;
break;
case M_STENCIL_SHADOW:
stencilShadow = - stencilShadow;
break;
case M_OFFSET_SHADOW:
offsetShadow = - offsetShadow;
break;
case M_POSITIONAL:
directionalLight = ;
break;
case M_DIRECTIONAL:
directionalLight = ;
break;
case M_PERFORMANCE:
reportSpeed = - reportSpeed;
break;
}
glutPostRedisplay();
} /* When not visible, stop animating. Restart when visible again. */
static void
visible(int vis) {
if (vis == GLUT_VISIBLE) {
if (animation)
glutIdleFunc(idle);
} else {
if (!animation)
glutIdleFunc(NULL);
}
} /* Press any key to redraw; good when motion stopped and
performance reporting on. */
/* ARGSUSED */
static void
key(unsigned char c, int x, int y) {
if (c == ) {
exit(); /* IRIS GLism, Escape quits. */
}
glutPostRedisplay();
} /* Press any key to redraw; good when motion stopped and
performance reporting on. */
/* ARGSUSED */
static void
special(int k, int x, int y) {
glutPostRedisplay();
} static int
supportsOneDotOne(void) {
const char *version;
int major, minor; version = (char *) glGetString(GL_VERSION);
if (sscanf(version, "%d.%d", &major, &minor) == )
return major >= && minor >= ;
return ; /* OpenGL version string malformed! */
} int
main(int argc, char **argv) {
int i; glutInit(&argc, argv); for (i = ; i < argc; i++) {
if (!strcmp("-linear", argv[i])) {
linearFiltering = ;
} else if (!strcmp("-mipmap", argv[i])) {
useMipmaps = ;
} else if (!strcmp("-ext", argv[i])) {
forceExtension = ;
}
} glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_STENCIL | GLUT_MULTISAMPLE); #if 1
/* In GLUT 4.0, you'll be able to do this an be sure to
get 2 bits of stencil if the machine has it for you. */
glutInitDisplayString("samples stencil>=2 rgb double depth");
#endif glutCreateWindow("Shadowy Leapin' Lizards"); if (glutGet(GLUT_WINDOW_STENCIL_SIZE) <= ) {
printf("dinoshade: Sorry, I need at least 2 bits of stencil.\n");
exit();
} /* Register GLUT callbacks. */
glutDisplayFunc(redraw);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutVisibilityFunc(visible);
glutKeyboardFunc(key);
glutSpecialFunc(special); glutCreateMenu(controlLights); glutAddMenuEntry("Toggle motion", M_MOTION);
glutAddMenuEntry("-----------------------", M_NONE);
glutAddMenuEntry("Toggle light", M_LIGHT);
glutAddMenuEntry("Toggle texture", M_TEXTURE);
glutAddMenuEntry("Toggle shadows", M_SHADOWS);
glutAddMenuEntry("Toggle reflection", M_REFLECTION);
glutAddMenuEntry("Toggle dinosaur", M_DINOSAUR);
glutAddMenuEntry("-----------------------", M_NONE);
glutAddMenuEntry("Toggle reflection stenciling", M_STENCIL_REFLECTION);
glutAddMenuEntry("Toggle shadow stenciling", M_STENCIL_SHADOW);
glutAddMenuEntry("Toggle shadow offset", M_OFFSET_SHADOW);
glutAddMenuEntry("----------------------", M_NONE);
glutAddMenuEntry("Positional light", M_POSITIONAL);
glutAddMenuEntry("Directional light", M_DIRECTIONAL);
glutAddMenuEntry("-----------------------", M_NONE);
glutAddMenuEntry("Toggle performance", M_PERFORMANCE);
glutAttachMenu(GLUT_RIGHT_BUTTON);
makeDinosaur(); #ifdef GL_VERSION_1_1
if (supportsOneDotOne() && !forceExtension) {
polygonOffsetVersion = ONE_DOT_ONE;
glPolygonOffset(-2.0, -1.0);
} else
#endif
{
#ifdef GL_EXT_polygon_offset
/* check for the polygon offset extension */
if (glutExtensionSupported("GL_EXT_polygon_offset")) {
polygonOffsetVersion = EXTENSION;
glPolygonOffsetEXT(-0.1, -0.002);
} else
#endif
{
polygonOffsetVersion = MISSING;
printf("\ndinoshine: Missing polygon offset.\n");
printf(" Expect shadow depth aliasing artifacts.\n\n");
}
} glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glLineWidth(3.0); glMatrixMode(GL_PROJECTION);
gluPerspective( /* field of view in degree */ 40.0,
/* aspect ratio */ 1.0,
/* Z near */ 20.0, /* Z far */ 100.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0.0, 8.0, 60.0, /* eye is at (0,8,60) */
0.0, 8.0, 0.0, /* center is at (0,8,0) */
0.0, 1.0, .); /* up is in postivie Y direction */ glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, );
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor);
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.1);
glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING); makeFloorTexture(); /* Setup floor plane for projected shadow calculations. */
findPlane(floorPlane, floorVertices[], floorVertices[], floorVertices[]); glutMainLoop();
return ; /* ANSI C requires main to return int. */
}

不得不说我找的sample还是太老了,凑合看吧,至少能够测试就好了,因为暂时没有继续学习opengl的打算,所以只能配置到这里了,想要我的opengl目录下文件的可以用QQ找我,我可以把压缩包传给你。

好了,看看结果就知道是否完成了。

OpenGL开发环境配置-Windows/MinGW/Clion/CMake

OK,暂时就是这样。稍后几天有空的话我会试着重新建立opencv的环境。

上一篇:WIN10 多用户登录


下一篇:Future Maker | 领跑亚太 进击的阿里云数据库