GLFW GLSL OPENGL问题

这是使用GLFW构建简单的OpenGL渲染器的几次尝试之一 我使用GL_INFO_LOG_LENGTH和其他东西取出了错误检查 我基本上在代码中使用了所有ARB版本的东西, 但出于可读性,我将其取出。 我只有一个似乎可以正常工作一半的代码,但对我来说,那一个的语法是最混乱的。我真的只是想知道我缺少什么或做错了什么,或者我不知道。

我也不确定着色器文件中的#version。他们描述的是openGL版本还是GLSL版本?

我尝试了

layout(location=0) in vec4 position;

代替

in layout(location=0) vec4 position;

似乎没有什么不同! 有区别吗?是非法的吗?

但是我将这些着色器文本存储在文件的下面,并试图读取它们,而不是!


main.h

#include <GL/glew.h>
#include <GLFW/glfw3.h>

#include <time.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

int WIDTH = 800;
int HEIGHT = 640;
const char* WINDOW_TITLE = "Window Title";

/*  Attributes:  Position,Normal,Color  0,1,2 0,3,4  */

GLfloat vertex_array[] =
{
+0.0f,+0.0f,-1.0f,+1.0f,/*  Vertex 0,Attribute: Position   */
+1.0f,Attribute: Color      */

+1.0f,/*  Vertex 1,Attribute: Position   */
+0.0f,Attribute: Color      */

-1.0f,/*  Vertex 2,/*  Vertex 3,+1.0f   /*  Vertex 4,/*  Vertex 4,Attribute: Color      */
};

const char* vertex_shader_code =
"#version 410 core\n"
"in layout(location=0) vec4 position;\n"
"in layout(location=1) vec4 color;\n"
"out vec4 Color;\n"
"void main()\n"
"{\n"
"gl_Position = position;\n"
"Color = color;\n"
"}";

const char* fragment_shader_code =
"#version 410 core\n"
"in vec4 Color;\n"
"out vec4 color_out;\n"
"void main()\n"
"{\n"
"color_out = vec4(Color);\n"
"}";

unsigned int number_of_lines = 0;

char *vertex_shader_file_path = "/Users/sam/Documents/Xcode/GLSL_test 7/GLSL_test/vert.glsl";

char *fragment_shader_file_path = "/Users/sam/Documents/Xcode/GLSL_test 7/GLSL_test/frag.glsl";

void init(void);
extern void draw_graphics(void);

main.c

char** line_read(char* file_name,unsigned int* number_of_lines)
{
#if 0
printf("debug: read_lines()\n");
#endif

size_t number_of_lines_default = 1;

FILE *input_file = NULL;
input_file = fopen(file_name,"r");
if (input_file == NULL)
{
    fprintf(stderr,"Failed to open input file for reading!\n");
    exit(1);
}

char** lines = NULL;
#if 0
lines = (char**)malloc(sizeof(char*) * number_of_lines_default);        /*  malloc version  */
#endif

#if 1
lines = (char**)calloc(number_of_lines_default,sizeof(char*));         /*  calloc version  */
#endif

int i = 0;
while(1)
{
    if(i == number_of_lines_default)
    {
        char** lines_new = NULL;
        lines_new = (char**)realloc(lines,number_of_lines_default * 2 * sizeof(char*) );
        if(lines_new == NULL)
        {
            fprintf(stderr,"Failed to realloc more lines!\n");
            exit(1);
        }
        lines = lines_new;
        number_of_lines_default *= 2;
    }

    size_t line_length_default = 1;
#if 0
    lines[i] = malloc(line_length_default * sizeof(char));
#endif

#if 1
    lines[i] = calloc(line_length_default,sizeof(char));
#endif
    if(lines[i] == NULL)
    {
        fprintf(stderr,"Failed to alloc lines!\n");
        exit(1);
    }
    if(getline(&lines[i],&line_length_default,input_file) == EOF)
    {
        break;
    }

    i++;
}
*number_of_lines = i;
fclose(input_file);
return lines;
};

void draw_graphics(void)
{
glClearColor(1.0f,0.0f,1.0f,1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEpth_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

glViewport(0,WIDTH,HEIGHT);

glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0);
};

int main(int argc,char **argv)
{

glfwInit();

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,1);
glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT,GL_TRUE);
glfwWindowHint(GLFW_RESIZABLE,GL_TRUE);

GLFWwindow* window = NULL;
window = glfwCreateWindow(WIDTH,HEIGHT,WINDOW_TITLE,NULL,NULL);
if(window == NULL)
{
    fprintf(stderr,"failed to initialize a GLFW window");
}
glfwMakeContextCurrent(window);

glfwGetFramebufferSize(window,&WIDTH,&HEIGHT);

/*  GLEW  */

glewExperimental = GL_TRUE;
if (GLEW_OK != glewInit() )
{
    printf("Error: failed to init GLEW!\n");

    return -1;
}

/*  VERSION  */

#if 0
/* get version info */
const GLubyte* renderer = glGetString(GL_RENDERER);     /*  get renderer string */
const GLubyte* version = glGetString(GL_VERSION);       /*  version as a string */
printf("Renderer: %s\n",renderer);
printf("OpenGL version supported: %s\n\n",version);
#endif

/*  VERTEX BUFFER  */

#if 1
GLuint vertex_buffer_ID = 0;
glGenBuffers(1,&vertex_buffer_ID);
glBindBuffer(GL_ARRAY_BUFFER,vertex_buffer_ID);
glBufferData(GL_ARRAY_BUFFER,sizeof(vertex_array),vertex_array,GL_STATIC_DRAW);
/*  Position  */
glEnableVertexAttribArray(0);
glVertexAttribPointer(0,4,GL_FLOAT,GL_FALSE,sizeof(float) * 8,0);
/*  Color  */
glEnableVertexAttribArray(1);
glVertexAttribPointer(1,(char*)(sizeof(float) * 4));

/*  ELEMENT ARRAY / INDEX BUFFER  */

/*only use element or index!*/
GLuint element_array[] = {3,2,4};
GLuint index_buffer_ID = 0;
glGenBuffers(1,&index_buffer_ID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,index_buffer_ID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(element_array),element_array,GL_STATIC_DRAW);

/*  SHADER - SETUP  */

GLuint vert_shader_ID = glCreateShader(GL_VERTEX_SHADER);
GLuint frag_shader_ID = glCreateShader(GL_FRAGMENT_SHADER);

/*  const GLchar* adapter[1];  */

/*  VERTEX SHADER  */

#if 0
adapter[0] = vertex_shader_code;
glShaderSource(vert_shader_ID,adapter,0);
#endif

const char **vertex_shader_code_1 = NULL;
vertex_shader_code_1 = (const char**)line_read(vertex_shader_file_path,&number_of_lines);
glShaderSource(vert_shader_ID,vertex_shader_code_1,0);

glCompileShader(vert_shader_ID);

/*  FRAGMENT SHADER  */

#if 0
adapter[0] = fragment_shader_code;
glShaderSource(frag_shader_ID,0);
#endif

const char **fragment_shader_code_1 = NULL;
fragment_shader_code_1 = (const char**)line_read(fragment_shader_file_path,&number_of_lines);
glShaderSource(frag_shader_ID,fragment_shader_code_1,0);

glCompileShader(frag_shader_ID);

/*  SHADER PROGRAM  */

GLuint program_ID = glCreateProgram();
glAttachShader(program_ID,vert_shader_ID);
glAttachShader(program_ID,frag_shader_ID);

glBindAttribLocation(program_ID,"postion");

glLinkProgram(program_ID);

/*  PROGRAM VALIDATION  */

glValidateProgram(program_ID);

glUseProgram(program_ID);
#endif

glViewport(0,HEIGHT);
glfwSwapInterval(1);

while (!glfwWindowShouldClose(window))
{
    glClearColor(0.0f,0.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEpth_BUFFER_BIT);

    draw_graphics();

    glfwSwapBuffers(window);
    glfwPollEvents();
}

glDeleteProgram(program_ID);
glDeleteShader(frag_shader_ID);
glDeleteShader(vert_shader_ID);
glDeleteBuffers(1,&vertex_buffer_ID);
glDeleteVertexArrays(1,&index_buffer_ID);


glfwDestroyWindow(window);
glfwTerminate();
return 0;
}

顶点着色器

#version 410 core\r\n

in layout(location=0) vec4 position;
in layout(location=1) vec4 color;

out vec4 Color;

void main()
{
    gl_Position = position;
    Color = color;
}

片段着色器

#version 410 core\r\n

out vec4 color_out;
in vec4 Color;

void main()
{
    color_out = vec4(Color);
}
wangxin071 回答:GLFW GLSL OPENGL问题

如果您要使用兼容性配置文件OpenGL Context

glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_COMPAT_PROFILE);

然后您的代码将起作用。请注意,Forward compatibility对于OpenGL 3.3及更高版本没有意义。

但是由于您使用的是核心配置文件OpenGL上下文,

  
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,1);
glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);

您必须创建一个Vertex Array Object。请注意,默认的“顶点数组对象”(0)在cor配置文件中无效。
另请参见Vertex Specification

在规范通用顶点属性数据(请参见Vertex Buffer ObjectIndex buffers(在VAO中进行了说明)的数组的规范之前,创建并绑定命名的顶点数组对象,以解决此问题。 :

/* VERTEX ARRAY OBJECT */

GLuint vertex_arrray_ID = 0;
glGenVertexArrays(1,& vertex_arrray_ID);
glBindVertexArray(vertex_arrray_ID);

/*  VERTEX BUFFER  */

// [...]

/*  ELEMENT ARRAY / INDEX BUFFER  */

// [...]

旁注:

根据OpenGL Shading Language 4.60 Specification - 4.4. Layout Qualifiers(第62页),布局限定符必须在接口限定符之前:

  

[...]它们可以与使用接口限定符声明的单个变量一起出现:

     
layout-qualifier interface-qualifier declaration ;
本文链接:https://www.f2er.com/3130812.html

大家都在问