当我运行程序时,窗口只是黑色。我一直在使用这个教程https://open.gl/drawing。虽然我没有逐字复制。 glgeterror
出现了0.我的代码出了什么问题。我使用了9个数组元素而不是教程中建议的6个数组元素,但是它已经弥补了。
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
int main(){
//window initalization
if(!glfwInit()){
fprintf(stderr,"failed glfw initialization");
return -1;
}
GLFWwindow* window = glfwCreateWindow(800,800,"i hope this works",NULL,NULL);
if (!window){
fprintf(stderr,"window creation failed");
return -1;
}
glfwMakeContextCurrent(window);
//glew initialization
glewExperimental = 1;
if(glewInit() != GLEW_OK){
fprintf(stderr,"glew failed to initialize");
return -1;
//vertex specification(vs)
//vertex array object(vs)
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
//vertex buffer object(vs)
GLuint vbo;
glGenBuffers(1, &vbo);
//vertex data(vs)
const GLfloat tri_verts[] = {
-1.0f,1.0f,0.0f,
-1.0f,0.0f,0.0f,
1.0f,0.0f,0.0f};
//-west +east -south +north -farther +closer
//{{x,y,z}{x,y,z}{x,y,z}}
//just a nemonic to associate which direction each letter goes.
//bind the vertices to the buffer(vs)
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER,sizeof(tri_verts),tri_verts,GL_STATIC_DRAW);
//vertex processing(vp)
//vertex shader(vp)
const GLchar* Vshader_source = "\
version 330 \
in vec3 position; \
void main() \
\
gl_Position = vec4(position,1.0); \
\
";
//fragment shader(vp)
const GLchar* Fshader_source = "\
#version 330\
out vec4 outColor;\
void main()\
{\
outColor = vec4(1.0, 1.0, 1.0, 1.0);\
}";
//shader compilation(vp)
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1,&Vshader_source,NULL);
glCompileShader(vertexShader);
//vertex shader compilation status
GLint Vstatus;
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &Vstatus);
char Vbuffer[512];
glGetShaderInfoLog(vertexShader, 512, NULL, Vbuffer);
fprintf(stderr,"%s",Vbuffer);
fprintf(stderr,"\n\n");
//fragment shader compilation
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &Fshader_source, NULL);
glCompileShader(fragmentShader);
//Fragment shader compilation status
GLint Fstatus;
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &Fstatus);
char Fbuffer[512];
glGetShaderInfoLog(fragmentShader, 512, NULL, Fbuffer);
fprintf(stderr,"%s",Fbuffer);
//shader program creation
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram,vertexShader);
glAttachShader(shaderProgram,fragmentShader);
glBindFragDataLocation(shaderProgram,0,"outcolor");
glLinkProgram(shaderProgram);
glUseProgram(shaderProgram);
//tell opengl how to interpret and format the vertices data
GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
glVertexAttribPointer(posAttrib,3, GL_FLOAT,GL_FALSE,0,0);
glEnableVertexAttribArray(posAttrib );
//vertex post-processing(fixed)
//Primitive assembly(fixed)
//Rasterization(fixed)
//Fragment Processing(fixed)
//per-sample operations(fixed)
}
char error_num = 0;
while(!glfwWindowShouldClose(window)){
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (!error_num){
fprintf(stderr,"%d",glGetError());
error_num++;
}
glDrawArrays(GL_TRIANGLES,0,3);
glfwSwapBuffers(window);
glfwPollEvents();
}
}
答案 0 :(得分:1)
Vshader_source
:version 330
!= #version 330
。我强烈怀疑你的着色器没有按原样编译/链接。#version
指令需要实际的,大写 - \n
后面的换行符。线路延续不足。GL_COMPILE_STATUS
位执行某些操作(例如转储编译日志和退出),而不是盲目地向前执行程序。GL_LINK_STATUS
如果需要,转储链接日志。glfwWindowHint()
,那么现在您可以在窗口创建时明确请求(通过GLFW_CONTEXT_VERSION_MAJOR
GLFW_CONTEXT_VERSION_MINOR
和#version 330
)版本3.3上下文只是有点希望你会得到3.3。随意使用this answer中的着色器加载程序类。
答案 1 :(得分:1)
看起来像是一些错误的问题。首先,重要的一个:
if(glewInit() != GLEW_OK){
fprintf(stderr,"glew failed to initialize");
return -1;
此错误检查没有右括号。所以你的大部分程序实际上都没有执行,因为它 这个块。
你的顶点着色器:
version 330
in vec3 position;
void main()
gl_Position = vec4(position,1.0);
version 330
应为#version 330
。 main
应该有大括号。
更正版本:
#version 330
in vec3 position;
void main() {
gl_Position = vec4(position,1.0);
}
正如genpfault所提到的,你也没有包含换行符(\n
)。对于大多数事情来说,这很好(如果有点......丑陋) ,但不适用于像#version
这样的指令。你需要一个换行符。
将来,您应该真正省去一些麻烦并从文件中加载着色器,而不是将它们作为多行字符串嵌入。说到这一点,如果你真的 要使用这样的多行字符串,那么就这样做:
const GLchar* Vshader_source =
"#version 330\n"
"in vec3 position;"
"void main() {"
" gl_Position = vec4(position,1.0);"
"}";
当预处理器被空格分隔时,它们会自动连接常量字符串。比所有反斜杠更容易管理!
接下来,
glBindFragDataLocation(shaderProgram,0,"outcolor");
在您的frag shader中,您将其称为outColor
,而不是outcolor
。案件在这里很重要!
不,我并不是肯定的,我抓住了所有这些但是......那应该是一个开始?