片段着色器似乎没有输出任何颜色。

huangapple go评论80阅读模式
英文:

Fragment Shader doesn't seem to output any colors

问题

我最近决定开始学习OpenGL,并且买了一本关于OpenGL核心3.3版本的书。这本书主要是关于C++的。

因此,在稍微搜索了一下后,我找到了一个我更熟悉的语言中的库,它提供了几乎相同的功能:lwjgl。
我成功地使用基本的顶点和片段着色器绘制了几个形状。然而,当我尝试让片段着色器输出除白色以外的颜色时,它似乎没有影响;这些形状仍然以白色渲染。

我的问题是着色器本身是否有问题,或者我没有正确加载它。
在我的Main.java中的代码(ShaderLoader只是一个加载.vs(顶点着色器)和.fs(片段着色器)文件的简单类):

 try {
			String vshaderSource, fshaderSource;
			vshaderSource = ShaderLoader.loadShaderSource(vertexShader, Path.of("main.vs"));
			glShaderSource(vertexShader, vshaderSource);
			fshaderSource = ShaderLoader.loadShaderSource(fragmentShader, Path.of("main.fs"));
			
			glShaderSource(fragmentShader, fshaderSource);
		} catch (IOException e) {
			log.severe(String.format("由于IOException加载着色器失败:%s", e.getMessage()));
			return;
		} 
		glCompileShader(vertexShader);
		int success = glGetShaderi(vertexShader, GL_COMPILE_STATUS);
		String infoLog;
		if(success == 0) {
			infoLog = glGetShaderInfoLog(vertexShader);
			log.severe("着色器编译失败:" + infoLog);
		}
		glCompileShader(fragmentShader);
		success = glGetShaderi(fragmentShader, GL_COMPILE_STATUS);
		if(success == 0) {
			infoLog = glGetShaderInfoLog(fragmentShader);
			log.severe("着色器编译失败:" + infoLog);
		}
		int shaderProgram = glCreateProgram();
		glAttachShader(shaderProgram, vertexShader);
		glAttachShader(shaderProgram, fragmentShader);
		glDeleteShader(vertexShader);
		glDeleteShader(fragmentShader);
        glUseProgram(shaderProgram);
        //在渲染循环中
        glUseProgram(shaderProgram);
		        
		glBindVertexArray(VAO); 

	    glUseProgram(shaderProgram);

		glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

顶点着色器 main.vs:

#version 330 core
layout (location = 0) in vec3 aPos;

out vec4 vertexColor;

void main()
{
    gl_Position = vec4(aPos, 1.0);
    vertexColor = vec4(0.5, 0.0, 0.0, 1);
}

片段着色器 main.fs:

#version 330 core
   in vec4 vertexColor;
   out vec4 FragColor;
   void main()
    {
       FragColor = vertexColor;
    };
英文:

I recently decided to start Learning OpenGL and got myself a book about OpenGL Core 3.3. The book is generally about C++.

So, after looking for a bit, I found a library in a language I was better in which provided almost the same functionality: lwjgl.
I managed to draw several shapes with basic vertex and fragment shaders. When I tried getting the Fragment shader to output a color other than white though, it didn't affect anything; The shapes are still getting rendered in white.

My question is if something is wrong with the shader itself or if i didn't load it up correctly.
The code in my Main.java(The ShaderLoader is simply a class that loads .vs(vertex shader) and .fs(fragment shader) files):

 try {
			String vshaderSource, fshaderSource;
			vshaderSource = ShaderLoader.loadShaderSource(vertexShader, Path.of("main.vs"));
			glShaderSource(vertexShader, vshaderSource);
			fshaderSource = ShaderLoader.loadShaderSource(fragmentShader, Path.of("main.fs"));
			
			glShaderSource(fragmentShader, fshaderSource);
		} catch (IOException e) {
			log.severe(String.format("Loading of Shaders failed due to IOException: %s", e.getMessage()));
			return;
		} 
		glCompileShader(vertexShader);
		int success = glGetShaderi(vertexShader, GL_COMPILE_STATUS);
		String infoLog;
		if(success == 0) {
			infoLog = glGetShaderInfoLog(vertexShader);
			log.severe("Shader Compilation failed: " + infoLog);
		}
		glCompileShader(fragmentShader);
		success = glGetShaderi(fragmentShader, GL_COMPILE_STATUS);
		if(success == 0) {
			infoLog = glGetShaderInfoLog(fragmentShader);
			log.severe("Shader Compilation failed:" + infoLog);
		}
		int shaderProgram = glCreateProgram();
		glAttachShader(shaderProgram, vertexShader);
		glAttachShader(shaderProgram, fragmentShader);
		glDeleteShader(vertexShader);
		glDeleteShader(fragmentShader);
        glUseProgram(shaderProgram);
        //In the rendering Loop
        glUseProgram(shaderProgram);
		        
		glBindVertexArray(VAO); 

	    glUseProgram(shaderProgram);

		glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

Vertex Shader main.vs:

#version 330 core
layout (location = 0) in vec3 aPos;

out vec4 vertexColor;

void main()
{
    gl_Position = vec4(aPos, 1.0);
    vertexColor = vec4(0.5, 0.0, 0.0, 1);
}

fragment shader main.fs:

#version 330 core
   in vec4 vertexColor;
   out vec4 FragColor;
   void main()
    {
       FragColor = vertexColor;
    };

答案1

得分: 2

你没有调用 glLinkProgram 函数来链接着色器程序。因此,在将着色器附加到程序之后、再次分离它们之前,你应该加入 glLinkProgram(shaderProgram)

英文:

You are not glLinkProgram the shader program. So, between attaching the shaders to the program and detaching them again, you should add glLinkProgram(shaderProgram).

huangapple
  • 本文由 发表于 2020年10月19日 22:06:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/64429062.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定