最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to send multiple parameters to the vertex shader on WebGL? - Stack Overflow

programmeradmin0浏览0评论

I'm following a tutorial on OpenGL (because WebGL tutorials are rare), where it uses the following syntax for multiple parameteres:

#version 330

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

smooth out vec4 theColor;

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

But the layout (location = #) syntax doesn't work on WebGL. What is the substitute for this?

I'm following a tutorial on OpenGL (because WebGL tutorials are rare), where it uses the following syntax for multiple parameteres:

#version 330

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

smooth out vec4 theColor;

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

But the layout (location = #) syntax doesn't work on WebGL. What is the substitute for this?

Share Improve this question edited Sep 14, 2013 at 7:49 fen 10.1k6 gold badges36 silver badges57 bronze badges asked Sep 14, 2013 at 1:14 MaiaVictorMaiaVictor 53.1k47 gold badges157 silver badges301 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 11

There are a number of things wrong with this shader if you intend to use it in WebGL.

For starters, WebGL is based on OpenGL ES 2.0, which uses a version of GLSL derived from 120.

Your #version directive is invalid for WebGL; you cannot use in, out, or smooth for vertex attributes or varying variables; there is no layout qualifier.


This will get you part of the way to fixing your shader:

#version 100

attribute vec4 position;
attribute vec4 color;

varying vec4 theColor;

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

But you will also need to bind the attribute locations for position and color in your code (before linking your shaders - see glBindAttribLocation (...)). If you are having difficulty finding tutorials for WebGL / ESSL, you can re-use many OpenGL tutorials that were written for GLSL version 120 or older.

You can read the offical specification for OpenGL ES 2.0's GLSL (ESSL) here. At the very least, have a look at the introductory section because it contains a lot of useful information.

发布评论

评论列表(0)

  1. 暂无评论