在前言中,我使用Derelict在D中编写OpenGL。然而,它应该与OpenGL和C++几乎相同,因为函数调用是相同的。无论如何,我不知道为什么我的代码不会在屏幕上打印任何内容,因为我的着色器程序没有错误。很抱歉代码转储太大,但我无法隔离问题。我的着色器类如下:
module shader;
import std.stdio;
import std.string;
import derelict.sdl2.sdl;
import derelict.opengl3.gl3;
class Shader
{
this(string file_name)
{
// Load OpenGL versions 1.0 and 1.1.
DerelictGL3.load();
// Load versions 1.2+ and all supported ARB and EXT extensions.
DerelictGL3.reload();
program = glCreateProgram();
shaders[0] = create_shader(load_shader(file_name ~ ".vs"), GL_VERTEX_SHADER);
shaders[1] = create_shader(load_shader(file_name ~ ".fs"), GL_FRAGMENT_SHADER);
for (int i = 0; i < NUM_SHADERS; i++)
glAttachShader(program, shaders[i]);
glBindAttribLocation(program, 0, "position");
glLinkProgram(program);
check_shader_error(program, GL_LINK_STATUS, true, "Error linking shader!");
glValidateProgram(program);
check_shader_error(program, GL_VALIDATE_STATUS, true, "Error invalid shader!");
}
void bind()
{
glUseProgram(program);
}
string load_shader(string shader_name) {
string output;
auto f = File(shader_name); // open file for reading,
scope(exit) f.close(); // and close the file when we're done.
// (optional)
foreach (str; f.byLine) // read every line in the file,
{
output ~= str;
output ~= "n";
}
///writeln(output);
return output; // and return it
}
GLuint create_shader(string text, GLenum shader_type)
{
GLuint shader = glCreateShader(shader_type);
GLchar* shader_source_strings[1];
GLint shader_source_strings_lengths[1];
shader_source_strings[0] = cast(char*)(text);
shader_source_strings_lengths[0] = (cast(int)text.length);
glShaderSource(shader, 1, shader_source_strings.ptr, shader_source_strings_lengths.ptr);
glCompileShader(shader);
check_shader_error(shader, GL_COMPILE_STATUS, false, "Error compiling shader!");
return shader;
}
void check_shader_error(GLuint shader, GLuint flag, bool isProgram, string error_message)
{
GLint success = 0;
GLchar[1024] error;
if (isProgram)
glGetProgramiv(shader, flag, &success);
else
glGetShaderiv(shader, flag, &success);
if (success == GL_FALSE)
{
if(isProgram)
glGetProgramInfoLog(shader, error.sizeof, null, error.ptr);
else
glGetProgramInfoLog(shader, error.sizeof, null, error.ptr);
writeln(error_message ~ error ~ "n");
}
}
private:
static const int NUM_SHADERS = 2;
GLuint program;
GLuint shaders[NUM_SHADERS];
}
我的Mesh类是:
module mesh;
import std.stdio;
import derelict.opengl3.gl3;
import vertex;
class Mesh
{
this(Vertex* vertices, int num_vertices)
{
// Load OpenGL versions 1.0 and 1.1.
DerelictGL3.load();
// Load versions 1.2+ and all supported ARB and EXT extensions.
DerelictGL3.reload();
draw_count = num_vertices;
glGenVertexArrays(1, &vertex_array_object);
glBindVertexArray(vertex_array_object);
glGenBuffers(NUM_BUFFERS, vertex_array_buffers.ptr);
glBindBuffer(GL_ARRAY_BUFFER, vertex_array_buffers[POSITION_VB]);
glBufferData(GL_ARRAY_BUFFER, num_vertices * vertices[0].sizeof, vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(cast(GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, cast(void*)0);
glBindVertexArray(0);
}
void draw()
{
glBindVertexArray(vertex_array_object);
glDrawArrays(GL_TRIANGLES, 0, draw_count);
glBindVertexArray(0);
}
private:
enum
{
POSITION_VB,
NUM_BUFFERS
};
GLuint vertex_array_object;
GLuint vertex_array_buffers[NUM_BUFFERS];
int draw_count;
}
我找到了答案,当我使用函数加载器(Derelict)加载OpenGL库时,我有一些多余的调用,并且我错误地表示了我的顶点数组。我在这里的数字火星论坛上找到了帮助。