最近在看超级宝典(第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();
}
着色器都没有效果,请问,这是什么原因。举个简单例子:输出一个有颜色的三角形,但结果却输出一个无任何颜色的三角形。有错误提示:
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();
}
在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大神。
过几天来结贴,要接分的回复下就行
移动或更改窗口尺寸的时候opengl视口里的图形会暂停,松开鼠标图形就会继续运动……
我的贴:
http://topic.csdn.net/u/20120801/01/6d2d63b2-13ce-4cef-8f69-253dc51931cb.html