本文是笔者自己在配置中遇到问题并解决后将原资料和自己的经验结合整理而成
代码下载
可以在CSDN上搜索
环境配置问题(glut 和 glew)
可以参考我的另一篇博文《使用cmake和visual studio编译freeglut和glew源代码并配置的流程》,但可能并不是必须的步骤。
基本头文件和库文件配置
如果发现大片大片的红线,则需要添加include和lib:
点项目右键属性,点击vc++目录
![这里写图片描述](https://img-blog.csdn.net/20151219215707846)
包含目录–编辑,添加红宝书源码目录中的include文件夹
库目录–编辑,添加红宝书源码目录中的lib文件夹
![这里写图片描述](https://img-blog.csdn.net/20151219215731023)
- 错误主要是:无法解析的外部符号
_sscanf、 ___iob_func
等。这个问题很好解决,主要是VS2015相对于之前的版本有所改动,具体可以自行百度。解决方法如下: - 1.右键项目–>属性–>链接器–>输入–>附加依赖项:
legacy_stdio_definitions.lib
- 2.在源文件中添加以下代码
#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 */
编译通过了,但是VAOs出现访问冲突:
![这里写图片描述](https://img-blog.csdn.net/20151219215851943)
原因是glewInit()没有完成所有的初始化,所以在glewInit()前面一行加上:
glewExperimental = GL_TRUE;
详见代码部分。
glewExperimental 相当一个总开关,如果将它设置成 GL_TRUE 就可以让glew支持所有的拓展,glewInit()也可以顺利完成所有的初始化。
以下是正文内容,不断添加中:
第一个渲染程序Triangles
一、新建一个Win32 Console Application
修改后的完整代码如下:
#include "stdafx.h"
#include <iostream>
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;
void init(void)
{
glGenVertexArrays(NumVAOs, VAOs);
glBindVertexArray(VAOs[Triangles]);
GLfloat vertices[NumVertices][2] = {
{ -0.90, -0.90 },
{ 0.85, -0.90 },
{ -0.90, 0.85 },
{ 0.90, -0.85 },
{ 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);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(VAOs[Triangles]);
glDrawArrays(GL_TRIANGLES, 0, NumVertices);
glFlush();
}
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
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
二、常见问题
(1)关于”stdafx.h”的错
代码首部添加或删除 #include “stdafx.h”
(2)编译一下发现不通过,报无法解析外部命令的错误
这可能是因为找不到LoadShaders.cpp
在红宝书源代码目录中有个lib文件夹,里面有LoadShaders.cpp
在工程的源文件右键添加现有项,把它添加进来
![这里写图片描述](https://img-blog.csdn.net/20151219215800866)
(3)
现在再编译一次,发现还是报错,说有个libcmtd.lib库跟其他库有冲突,我们可以去忽略它
点击项目右键–属性–链接器–输入,在忽略特定默认库中添加它
![这里写图片描述](https://img-blog.csdn.net/20151219215828907)
(4)
编译通过了,但是VAOs出现访问冲突:
![这里写图片描述](https://img-blog.csdn.net/20151219215851943)
原因是glewInit()没有完成所有的初始化,所以在glewInit()前面一行加上:
glewExperimental = GL_TRUE;
详见代码部分。
glewExperimental 相当一个总开关,如果将它设置成 GL_TRUE 就可以让glew支持所有的拓展,glewInit()也可以顺利完成所有的初始化。
(5)
现在在编译一次发现可以通过了出来
但是它是白色的三角形不是蓝色的,需要在工程目录中新建两个文本
更名为triangles.vert和triangles.frag
![这里写图片描述](https://img-blog.csdn.net/20151219215919382)
代码如下:
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);
}
然后再编译运行,出现蓝色三角形
![这里写图片描述](https://img-blog.csdn.net/20151219215936008)
第三章 03\ch03_drawcommands
(1)修改后的完整代码:
#include "vapp.h"
#include "vutils.h"
#include "vmath.h"
#include "LoadShaders.h"
#include <stdio.h>
using namespace vmath;
#define USE_PRIMITIVE_RESTART 1
BEGIN_APP_DECLARATION(DrawCommandExample)
virtual void Initialize(const char * title);
virtual void Display(bool auto_redraw);
virtual void Finalize(void);
virtual void Reshape(int width, int height);
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);
render_model_matrix_loc = glGetUniformLocation(render_prog, "model_matrix");
render_projection_matrix_loc = glGetUniformLocation(render_prog, "projection_matrix");
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,
};
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
};
static const GLushort vertex_indices[] =
{
0, 1, 2
};
glGenBuffers(1, ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo[0]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(vertex_indices), vertex_indices, GL_STATIC_DRAW);
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;
glEnable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(render_prog);
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);
glBindVertexArray(vao[0]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo[0]);
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);
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);
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);
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);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
(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);
结果如下:
![这里写图片描述](https://img-blog.csdn.net/20151219215952319)
以下是原文地址或参考地址: