最近在看超级宝典(第5版),第6章GLSL里面基本上所有的例子在我自己的电脑(VS2005)上运行,
着色器都没有效果,请问,这是什么原因。举个简单例子:输出一个有颜色的三角形,但结果却输出一个无任何颜色的三角形。有错误提示:
   The shader at ShadedIdentity.fp failed to compile with the following error:
Fragment shader failed to compile with the following errors:
ERROR: 0:3: '' : Declaration must include a precision qualifier or the default p
recision must have been previously declared.
ERROR: 0:4: '' : Declaration must include a precision qualifier or the default p
recision must have been previously declared.
ERROR:  compilation errors.  No code generated.找了半天也没发现哪错了,其他有些列子没有提示错误,但是也没有效果。望高手能解惑,万分感谢!
 
该例子的相关源码:
ShadedIdentity.fp: #version 130out vec4 vFragColor;
in vec4 vVaryingColor;void main(void)
   { 
   vFragColor = vVaryingColor;
   }
ShadedIdentity.vp:#version 130in vec4 vColor;
in vec4 vVertex;out vec4 vVaryingColor;void main(void) 
    { 
    vVaryingColor = vColor;
    gl_Position = vVertex;
    }  
#include <GLTools.h>
#include <GLShaderManager.h>
#include "LoadShader.hpp"#ifdef __APPLE__
#include <glut/glut.h>          // OS X version of GLUT
#else
#define FREEGLUT_STATIC
#include <GL/glut.h>            // Windows FreeGlut equivalent
#endifGLBatch triangleBatch;
GLShaderManager shaderManager;GLint myIdentityShader;void ChangeSize(int w, int h);
void SetupRC();
void ShutdownRC();
void RenderScene();int main(int argc, char* argv[])
{
gltSetWorkingDirectory(argv[0]); glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
glutInitWindowSize(800, 600);
glutCreateWindow("Shaded Triangle");
glutReshapeFunc(ChangeSize);
glutDisplayFunc(RenderScene); GLenum err = glewInit();
if (GLEW_OK != err) {
fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
return 1;
} SetupRC(); glutMainLoop(); ShutdownRC();
return 0;
}void ChangeSize(int w, int h)
{
glViewport(0, 0, w, h);
}void SetupRC()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
shaderManager.InitializeStockShaders(); GLfloat vVerts[] = { -0.5f, 0.0f, 0.0f,
  0.5f, 0.0f, 0.0f,
  0.0f, 0.5f, 0.0f }; GLfloat vColors[] = { 1.0f, 0.0f, 0.0f, 1.0f,
  0.0f, 1.0f, 0.0f, 1.0f,
  0.0f, 0.0f, 1.0f, 1.0f }; triangleBatch.Begin(GL_TRIANGLES, 3);
triangleBatch.CopyVertexData3f(vVerts);
triangleBatch.CopyColorData4f(vColors);
triangleBatch.End(); myIdentityShader = gltLoadShaderPairWithAttributes("ShadedIdentity.vp",
"ShadedIdentity.fp", 2, GLT_ATTRIBUTE_VERTEX, "vVertex",
GLT_ATTRIBUTE_COLOR, "vColor"); /*myIdentityShader = LoadShaderPairWithAttributes("ShadedIdentity.vp",
"ShadedIdentity.fp", 2, GLT_ATTRIBUTE_VERTEX, "vVertex",
GLT_ATTRIBUTE_COLOR, "vColor");*/}void ShutdownRC()
{
glDeleteProgram(myIdentityShader);
}void RenderScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glUseProgram(myIdentityShader);
triangleBatch.Draw(); glutSwapBuffers();
}

解决方案 »

  1.   

    竟然没回复,不过在别的地方碰到到个大神帮咱解决了。分享下,给那些碰到同样问题的人,希望会有帮助。to 147楼:
    在GLSL 4.20规范中有如下内容:
    The vertex, tessellation, and geometry languages have the following predeclared globally scoped defaultprecision statements:
    precision highp float;
    precision highp int;The fragment language has the following predeclared globally scoped default precision statements:
    precision mediump int;
    precision highp float;看起来1.30版本似乎没有默认值,你可以自己添加,应该可以解决问题。具体做法: wuwenye: 回复 @chen16501560 : 把precision mediump int;和precision highp float;添加到#version 130下面。 
    再次感谢wuwenye大神。
    过几天来结贴,要接分的回复下就行
      

  2.   

    既然lz的问题解决了,小白我遇到个问题不知lz有没有答案,就是nehe的那个教程。
    移动或更改窗口尺寸的时候opengl视口里的图形会暂停,松开鼠标图形就会继续运动……
    我的贴:
    http://topic.csdn.net/u/20120801/01/6d2d63b2-13ce-4cef-8f69-253dc51931cb.html
      

  3.   

    搭车请教,同样是opengl编程宝典中的程序问题,我下载的程序中,第四章SphereWorld2以后的程序(包括后续章节和这个程序相关的程序),在编译链接的时候都没有错,但运行的时候报错,通过调试,错误如下:SphereWorld2.exe 中的 0x0276e640 处未处理的异常: 0xC0000005: 读取位置 0x04e5e000 时发生访问冲突,反汇编提示错误是:0276E640  movzx       eax,word ptr [edx+ebp*2] ,搞不懂了,求大侠指教!
      

  4.   

    楼主,我在mac系统的,运行例子,precision mediump int;和precision highp float;添加到#version 130下面,结果还是一样。。有解决方案!