版权声明:本文为博主原创文章,未经博主允许不得转载。
目录(?)[+]
本文是笔者自己在配置中遇到问题并解决后将原资料和自己的经验结合整理而成
可以在CSDN上搜索
可以参考我的另一篇博文《使用cmake和visual studio编译freeglut和glew源代码并配置的流程》,但可能并不是必须的步骤。
如果发现大片大片的红线,则需要添加include和lib:
包含目录–编辑,添加红宝书源码目录中的include文件夹
库目录–编辑,添加红宝书源码目录中的lib文件夹
_sscanf、 ___iob_func
等。这个问题很好解决,主要是VS2015相对于之前的版本有所改动,具体可以自行百度。解决方法如下:legacy_stdio_definitions.lib
#if _MSC_VER>=1900
#include "stdio.h"
_ACRTIMP_ALT FILE* __cdecl __acrt_iob_func(unsigned);
#ifdef __cplusplus
extern "C"
#endif
FILE* __cdecl __iob_func(unsigned i) {
return __acrt_iob_func(i);
}
#endif /* _MSC_VER>=1900 */
原因是glewInit()没有完成所有的初始化,所以在glewInit()前面一行加上:
glewExperimental = GL_TRUE;
详见代码部分。
glewExperimental 相当一个总开关,如果将它设置成 GL_TRUE 就可以让glew支持所有的拓展,glewInit()也可以顺利完成所有的初始化。
一、新建一个Win32 Console Application
修改后的完整代码如下:
///////////////////////////////////////////////////////////////////////
//
// triangles.cpp
//
///////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include
using namespace std;
#include "vgl.h"
#include "LoadShaders.h"
enum VAO_IDs { Triangles, NumVAOs };
enum Buffer_IDs { ArrayBuffer, NumBuffers };
enum Attrib_IDs { vPosition = 0 };
GLuint VAOs[NumVAOs];
GLuint Buffers[NumBuffers];
const GLuint NumVertices = 6;
//---------------------------------------------------------------------
//
// init
//
void init(void)
{
glGenVertexArrays(NumVAOs, VAOs);
glBindVertexArray(VAOs[Triangles]);
GLfloat vertices[NumVertices][2] = {
{ -0.90, -0.90 }, // Triangle 1
{ 0.85, -0.90 },
{ -0.90, 0.85 },
{ 0.90, -0.85 }, // Triangle 2
{ 0.90, 0.90 },
{ -0.85, 0.90 }
};
glGenBuffers(NumBuffers, Buffers);
glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices),
vertices, GL_STATIC_DRAW);
ShaderInfo shaders[] = {
{ GL_VERTEX_SHADER, "triangles.vert" },
{ GL_FRAGMENT_SHADER, "triangles.frag" },
{ GL_NONE, NULL }
};
GLuint program = LoadShaders(shaders);
glUseProgram(program);
glVertexAttribPointer(vPosition, 2, GL_FLOAT,
GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(vPosition);
}
//---------------------------------------------------------------------
//
// display
//
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(VAOs[Triangles]);
glDrawArrays(GL_TRIANGLES, 0, NumVertices);
glFlush();
}
//---------------------------------------------------------------------
//
// main
//
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(512, 512);
glutInitContextVersion(4, 3);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutCreateWindow(argv[0]);
glewExperimental = GL_TRUE;
if (glewInit()) {
cerr << "Unable to initialize GLEW ... exiting" << endl;
exit(EXIT_FAILURE);
}
init();
glutDisplayFunc(display);
glutMainLoop();
}
二、常见问题
(1)关于”stdafx.h”的错
代码首部添加或删除 #include “stdafx.h”
(2)编译一下发现不通过,报无法解析外部命令的错误
这可能是因为找不到LoadShaders.cpp
在红宝书源代码目录中有个lib文件夹,里面有LoadShaders.cpp
在工程的源文件右键添加现有项,把它添加进来
(3)
现在再编译一次,发现还是报错,说有个libcmtd.lib库跟其他库有冲突,我们可以去忽略它
点击项目右键–属性–链接器–输入,在忽略特定默认库中添加它
原因是glewInit()没有完成所有的初始化,所以在glewInit()前面一行加上:
glewExperimental = GL_TRUE;
详见代码部分。
glewExperimental 相当一个总开关,如果将它设置成 GL_TRUE 就可以让glew支持所有的拓展,glewInit()也可以顺利完成所有的初始化。
(5)
现在在编译一次发现可以通过了出来
但是它是白色的三角形不是蓝色的,需要在工程目录中新建两个文本
更名为triangles.vert和triangles.frag
代码如下:
triangles.vert
layout(location = 0) in vec4 vPosition;
void
main()
{
gl_Position = vPosition;
}
triangles.frag
out vec4 fColor;
void
main()
{
fColor = vec4(0.0, 0.0, 1.0, 1.0);
}
(1)修改后的完整代码:
/* $URL$
$Rev$
$Author$
$Date$
$Id$
*/
#include "vapp.h"
#include "vutils.h"
#include "vmath.h"
#include "LoadShaders.h"
#include
using namespace vmath;
// Define USE_PRIMITIVE_RESTART to 0 to use two separate draw commands
#define USE_PRIMITIVE_RESTART 1
BEGIN_APP_DECLARATION(DrawCommandExample)
// Override functions from base class
virtual void Initialize(const char * title);
virtual void Display(bool auto_redraw);
virtual void Finalize(void);
virtual void Reshape(int width, int height);
// Member variables
float aspect;
GLuint render_prog;
GLuint vao[1];
GLuint vbo[1];
GLuint ebo[1];
GLint render_model_matrix_loc;
GLint render_projection_matrix_loc;
END_APP_DECLARATION()
DEFINE_APP(DrawCommandExample, "Drawing Commands Example")
void DrawCommandExample::Initialize(const char * title)
{
base::Initialize(title);
static ShaderInfo shader_info[] =
{
{ GL_VERTEX_SHADER, "../../03/ch03_primitive_restart/primitive_restart.vs.glsl" },
{ GL_FRAGMENT_SHADER, "../../03/ch03_primitive_restart/primitive_restart.fs.glsl" },
{ GL_NONE, NULL }
};
render_prog = LoadShaders(shader_info);
glUseProgram(render_prog);
// "model_matrix" is actually an array of 4 matrices
render_model_matrix_loc = glGetUniformLocation(render_prog, "model_matrix");
render_projection_matrix_loc = glGetUniformLocation(render_prog, "projection_matrix");
// A single triangle
static const GLfloat vertex_positions[] =
{
-1.0f, -1.0f, 0.0f, 1.0f,
1.0f, -1.0f, 0.0f, 1.0f,
-1.0f, 1.0f, 0.0f, 1.0f,
-1.0f, -1.0f, 0.0f, 1.0f,
};
// Color for each vertex
static const GLfloat vertex_colors[] =
{
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f, 1.0f,
0.0f, 1.0f, 1.0f, 1.0f
};
// Indices for the triangle strips
static const GLushort vertex_indices[] =
{
0, 1, 2
};
// Set up the element array buffer
glGenBuffers(1, ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo[0]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(vertex_indices), vertex_indices, GL_STATIC_DRAW);
// Set up the vertex attributes
glGenVertexArrays(1, vao);
glBindVertexArray(vao[0]);
glGenBuffers(1, vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_positions) + sizeof(vertex_colors), NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertex_positions), vertex_positions);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertex_positions), sizeof(vertex_colors), vertex_colors);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, NULL);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (const GLvoid *)sizeof(vertex_positions));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
void DrawCommandExample::Display(bool auto_redraw)
{
float t = float(GetTickCount() & 0x1FFF) / float(0x1FFF);
static float q = 0.0f;
static const vmath::vec3 X(1.0f, 0.0f, 0.0f);
static const vmath::vec3 Y(0.0f, 1.0f, 0.0f);
static const vmath::vec3 Z(0.0f, 0.0f, 1.0f);
vmath::mat4 model_matrix;
// Setup
glEnable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Activate simple shading program
glUseProgram(render_prog);
// Set up the model and projection matrix
vmath::mat4 projection_matrix(vmath::frustum(-1.0f, 1.0f, -aspect, aspect, 1.0f, 500.0f));
glUniformMatrix4fv(render_projection_matrix_loc, 1, GL_FALSE, projection_matrix);
// Set up for a glDrawElements call
glBindVertexArray(vao[0]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo[0]);
// Draw Arrays...
model_matrix = vmath::translate(-3.0f, 0.0f, -5.0f);
glUniformMatrix4fv(render_model_matrix_loc,1, GL_FALSE, model_matrix);
glDrawArrays(GL_TRIANGLES, 0, 3);
// DrawElements
model_matrix = vmath::translate(-1.0f, 0.0f, -5.0f);
glUniformMatrix4fv(render_model_matrix_loc, 1, GL_FALSE, model_matrix);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, NULL);
// DrawElementsBaseVertex
model_matrix = vmath::translate(1.0f, 0.0f, -5.0f);
glUniformMatrix4fv(render_model_matrix_loc, 1, GL_FALSE, model_matrix);
glDrawElementsBaseVertex(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, NULL, 1);
// DrawArraysInstanced
model_matrix = vmath::translate(3.0f, 0.0f, -5.0f);
glUniformMatrix4fv(render_model_matrix_loc, 1, GL_FALSE, model_matrix);
glDrawArraysInstanced(GL_TRIANGLES, 0, 3, 1);
base::Display();
}
void DrawCommandExample::Finalize(void)
{
glUseProgram(0);
glDeleteProgram(render_prog);
glDeleteVertexArrays(1, vao);
glDeleteBuffers(1, vbo);
}
void DrawCommandExample::Reshape(int width, int height)
{
glViewport(0, 0 , width, height);
aspect = float(height) / float(width);
}
(2)常见问题:
line 46: static const ShaderInfo shader_info[] = 改成 static ShaderInfo shader_info[] =
line 116: mat4 model_matrix; 改成 vmath::mat4 model_matrix;
line 136/141/146/151: translation 改成 translate
line 137/142/147/152: 把参数4改成1 ,即
glUniformMatrix4fv(render_model_matrix_loc, 4, GL_FALSE, model_matrix); 改成 glUniformMatrix4fv(render_model_matrix_loc, 1, GL_FALSE, model_matrix);
以下是原文地址或参考地址: