当我尝试使用glsl绘制一个带有opengl和glew的三角形时出错了

时间:2016-11-06 17:53:51

标签: c macos opengl glsl

我使用OpenGL 3和Glew来绘制一个三角形,我有一个窗口(改变背景颜色工作正常)但我不能把我的着色器放在上面。我做了一些测试,如:

glGetProgramiv(shader_programme, GL_LINK_STATUS, &isLinked);

printf("\nProg : %i",isLinked);

它没事; print为程序,顶点和frag返回1。

我想我错过了一个清楚的地方,但我不确定,也很丢失......

这是我的代码:

#include    "../include/scop.h"
#include <OpenGL/gl.h>
#include ".../lfw3/3.2.1/include/GLFW/glfw3.h"


t_scop      *ft_init_window(t_scop *scop, t_parse parse)
{
if (!glfwInit())
    ft_putstr("error init");
else
{
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
WIN = glfwCreateWindow(WIN_X, WIN_Y, "Scop", NULL, NULL);
glfwMakeContextCurrent(WIN);

glfwSetInputMode(WIN, GLFW_STICKY_KEYS, GL_TRUE);
glfwSetInputMode(WIN, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwPollEvents();
glfwSetCursorPos(WIN, WIN_X / 2.0, WIN_Y / 2.0);
glClearColor(0.0f, 0.5f, 0.4f, 0.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
}

    float points[] = {
  -1.0f,  -1.0f,  0.0f,
  1.0f, -1.0f,  0.0f,
  0.0f, 1.0f,  0.0f
    };

//init buffer and fill it
    GLuint vbo = 0;
    glGenBuffers (1, &vbo);
    glBindBuffer (GL_ARRAY_BUFFER, vbo);
    glBufferData (GL_ARRAY_BUFFER, 9 * sizeof (float), points, GL_STATIC_DRAW);

//init VertexArray
    GLuint vao = 0;
    glGenVertexArraysAPPLE (1, &vao);
    glBindVertexArrayAPPLE (vao);

    glEnableVertexAttribArray (0);
    glBindBuffer (GL_ARRAY_BUFFER, vbo);
    glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glDrawArrays(GL_TRIANGLES, 0, 3);

const char* vertex_shader =
  "#version 330 core\n"
  "layout (location = 0) in vec3 position;\n"
"void main () {"
"gl_Position.xyz = position;"
"gl_Position.w = 1.0;"
  "}\0";

const char* fragment_shader =
  "#version 330 core\n"
"out vec3 color;"
"void main () {"
"color = vec3(1,0,0);"
"}\0";
//create vertex
GLuint vs = glCreateShader (GL_VERTEX_SHADER);
glShaderSource (vs, 1, &vertex_shader, NULL);
glCompileShader (vs);
//tests
GLint success = 0;
glGetShaderiv(vs, GL_COMPILE_STATUS, &success);
printf ("Taille du source:%i\n", success);
if (GL_FALSE == success)
printf("false");
else printf("true");

//create frag
GLuint fs = glCreateShader (GL_FRAGMENT_SHADER);
glShaderSource (fs, 1, &fragment_shader, NULL);
glCompileShader (fs);
//tests
success = 0;
glGetShaderiv(fs, GL_COMPILE_STATUS, &success);
printf("Taille fs : %i",success);

// GLuint shader_programme = LoadShaders (vs,fs);
GLint shader_programme = glCreateProgram ();
glAttachShader (shader_programme, vs);
glAttachShader (shader_programme, fs);
glLinkProgram (shader_programme);
//tests
GLint isLinked = 0;
glGetProgramiv(shader_programme, GL_LINK_STATUS, &isLinked);
printf("\nProg : %i",isLinked);

//idk if i need to do this now
glDetachShader(shader_programme, vs);
glDetachShader(shader_programme, fs);
glDeleteShader(vs);
glDeleteShader(fs);
glGetError();

while (!glfwWindowShouldClose(WIN))
{

  glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glClearDepth(GL_DEPTH_TEST);

  glUseProgram (shader_programme);
  glBindVertexArrayAPPLE (vao);
  glDrawArrays (GL_TRIANGLES, 0, 3);
  //glUseProgram(0); ???
  glfwPollEvents ();
  glBindVertexArrayAPPLE (0);
  glfwSwapBuffers(WIN);
}

//  glfwTerminate();

return (scop);
}

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

问题出在这一行:

glClearDepth(GL_DEPTH_TEST);

glClearDepthdoc)指定应该清除深度缓冲区的值,并期望浮点值介于0和1之间。它与glClearColor类似,仅用于深度

此外,您应该使用核心配置文件VAO功能而不是APPLE扩展功能。 apple扩展名只能在OpenGL上下文中使用&lt; = 2.1。