PDA

View Full Version : error compiling shader



rodyfeghaly
09-10-2017, 11:24 AM
Hi I am a beginner to opengl try to learn the language and i am having the following error i believe it is related to compiling the shader. here 's the error below and the code. the error says unexpected qualifier error 12, and error 132 syntax error, and error(#273) 2 compilation errors. and i dont seem to figure out how to fix it.



File "C:\Users\valuedcustomer\Anaconda3\lib\site-packages\OpenGL\GL\shaders.py", line 236, in compileShader
shaderType,

RuntimeError: ('Shader compile failure (0): b\'Vertex shader failed to compile with the following errors:\\nERROR: 0:3: error(#12) Unexpected qualifier\\nERROR: 0:4: error(#132) Syntax error: "in" parse error\\nERROR: error(#273) 2 compilation errors. No code generated\\n\\n\'', [b'\n #version 330\n in layout(location = 0) vec3 position\n in layout(location = 1) vec2 textureCoords\n uniform mat4 transform\n uniform mat4 view\n uniform mat4 model\n uniform mat4 projection\n out vec2 newTexture\n void main()\n {\n gl_Position = projection * view * model * transform * vec4(position, 1.0f)\n newTexture = textureCoords\n }\n '], GL_VERTEX_SHADER)



import glfw
from OpenGL.GL import *
import OpenGL.GL.shaders
import numpy
import pyrr
from PIL import Image
from ObjLoader import *

def window_resize(window, width, height):
glViewport(0, 0, width, height)

def main():

# initialize glfw
if not glfw.init():
return

w_width, w_height = 800, 600

#glfw.window_hint(glfw.RESIZABLE, GL_FALSE)

window = glfw.create_window(w_width, w_height, "My OpenGL window", None, None)

if not window:
glfw.terminate()
return

glfw.make_context_current(window)
glfw.set_window_size_callback(window, window_resize)

obj = ObjLoader()
obj.load_model("cube.obj")

texture_offset = len(obj.vertex_index)*12


# shader = ShaderLoader.compile_shader("video_17_vert.vs", "video_17_frag.fs")

vertex_shader = """
#version 330
in layout(location = 0) vec3 position
in layout(location = 1) vec2 textureCoords;
uniform mat4 transform;
uniform mat4 view;
uniform mat4 model;
uniform mat4 projection;
out vec2 newTexture;
void main()
{
gl_Position = projection * view * model * transform * vec4(position, 1.0f);
newTexture = textureCoords;
}
"""

fragment_shader = """
#version 330
in vec2 newTexture;
out vec4 outColor;
uniform sampler2D samplerTexture;
void main()
{
outColor = texture(samplerTexture, newTexture);
}
"""

shader = OpenGL.GL.shaders.compileProgram(OpenGL.GL.shaders .compileShader(vertex_shader, GL_VERTEX_SHADER),
OpenGL.GL.shaders.compileShader(fragment_shader, GL_FRAGMENT_SHADER))






VBO = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
glBufferData(GL_ARRAY_BUFFER, obj.model.itemsize * len(obj.model), obj.model, GL_STATIC_DRAW)

#position
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, obj.model.itemsize * 3, ctypes.c_void_p(0))
glEnableVertexAttribArray(0)
#texture
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, obj.model.itemsize * 2, ctypes.c_void_p(texture_offset))
glEnableVertexAttribArray(1)


texture = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, texture)
# Set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
# Set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)

# load image
image = Image.open("cube_texture.jpg")
flipped_image = image.transpose(Image.FLIP_TOP_BOTTOM)
img_data = numpy.array(list(flipped_image.getdata()), numpy.uint8)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width, image.height, 0, GL_RGB, GL_UNSIGNED_BYTE, img_data)
glEnable(GL_TEXTURE_2D)


glUseProgram(shader)

glClearColor(0.2, 0.3, 0.2, 1.0)
glEnable(GL_DEPTH_TEST)
#glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)

view = pyrr.matrix44.create_from_translation(pyrr.Vector3 ([0.0, 0.0, -3.0]))
projection = pyrr.matrix44.create_perspective_projection_matrix (65.0, w_width / w_height, 0.1, 100.0)
model = pyrr.matrix44.create_from_translation(pyrr.Vector3 ([0.0, 0.0, 0.0]))

view_loc = glGetUniformLocation(shader, "view")
proj_loc = glGetUniformLocation(shader, "projection")
model_loc = glGetUniformLocation(shader, "model")

glUniformMatrix4fv(view_loc, 1, GL_FALSE, view)
glUniformMatrix4fv(proj_loc, 1, GL_FALSE, projection)
glUniformMatrix4fv(model_loc, 1, GL_FALSE, model)



while not glfw.window_should_close(window):
glfw.poll_events()

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

rot_x = pyrr.Matrix44.from_x_rotation(0.5 * glfw.get_time() )
rot_y = pyrr.Matrix44.from_y_rotation(0.8 * glfw.get_time() )

transformLoc = glGetUniformLocation(shader, "transform")
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, rot_x * rot_y)

glDrawArrays(GL_TRIANGLES, 0, len(obj.vertex_index))

glfw.swap_buffers(window)

glfw.terminate()

if __name__ == "__main__":
main()

GClements
09-10-2017, 02:23 PM
vertex_shader = """
#version 330
in layout(location = 0) vec3 position
in layout(location = 1) vec2 textureCoords;


There should be a semicolon after "position".

arekkusu
09-10-2017, 02:57 PM
in layout(location = 1) vec2 textureCoords


The GLSL grammar says that layout-qualifier must precede interface-qualifier, so "in layout" is invalid, you must write "layout(location=1) in".

rodyfeghaly
09-10-2017, 03:09 PM
arekkusu and GClements

thank you both adding the semicolon after "position" and fixing in layout(location = 1) to "layout(location=1) in" solved it.

Thank you alot