Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get OpenGL code to work properly

I'm very new to OpenGL, GLSL and WebGL. I'm trying to get this sample code to work in a tool like http://shdr.bkcore.com/ but I can't get it to work.

Vertex Shader:

void main()
{ 
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    gl_TexCoord[0] = gl_MultiTexCoord0;
}

Fragment Shader:

precision highp float;
uniform float time;
uniform vec2 resolution;
varying vec3 fPosition;
varying vec3 fNormal;

uniform sampler2D tex0;

void main()
{
  float border = 0.01;
  float circle_radius = 0.5;
  vec4 circle_color = vec4(1.0, 1.0, 1.0, 1.0);
  vec2 circle_center = vec2(0.5, 0.5);

  vec2 uv = gl_TexCoord[0].xy;
  vec4 bkg_color = texture2D(tex0,uv * vec2(1.0, -1.0));

  // Offset uv with the center of the circle.
  uv -= circle_center;
  float dist = sqrt(dot(uv, uv));

  if ( (dist > (circle_radius+border)) || (dist < (circle_radius-border)) )
gl_FragColor = bkg_color;
else
gl_FragColor = circle_color;
}

I figured that this code must be from an outdated version of the language, so I changed the vertex shader to:

precision highp float;
attribute vec2 position;
attribute vec3 normal;

varying vec2 TextCoord;
attribute vec2 textCoord;

uniform mat3 normalMatrix;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
varying vec3 fNormal;
varying vec3 fPosition;

void main()
{

  gl_Position = vec4(position, 0.0, 1.0);
  TextCoord = vec2(textCoord);
}

That seemed to fix the error messages about undeclared identifiers and not being able to "convert from 'float' to highp 4-component something-or-other", but I have no idea if, functionally, this will do the same thing as the original intended.

Also, when I convert to this version of the Vertex Shader I have no idea what I'm supposed to do with this line in the Fragment Shader:

vec2 uv = gl_TexCoord[0].xy;

How do I convert this line to fit in with the converted vertex shader and how can I be sure that the vertex shader is even converted correctly?

like image 337
SuperSonic68 Avatar asked Nov 28 '25 21:11

SuperSonic68


1 Answers

gl_TexCoord is from desktop OpenGL, and not part of OpenGL ES. You'll need to create a new user-defined vec2 varying to hold the coordinate value.

like image 162
solidpixel Avatar answered Dec 01 '25 21:12

solidpixel