- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我遇到了 glCreateShaders 的问题。它总是返回 0。我将 Glew 与 SDL 结合使用,每当我运行该程序时,它都会显示:
0(1) : error C0000: syntax error, unexpected '}' at token "}"
Shader Shaders/colorShading.vert failed to compile!
主要.cpp:
#include <iostream>
#include "MainGame.h"
int main(int argc, char** argv)
{
MainGame maingame;
maingame.run();
return 0;
}
主游戏.h:
//Core -> initializing glew, sdl etc...
//Just ignore the sprite class
#pragma once
#include <SDL/SDL.h>
#include <GL/glew.h>
#include <iostream>
#include <string>
#include "Errors.h"
#include "GLSLProgram.h"
enum class GameState {PLAY, EXIT};
#include "Sprite.h"
class MainGame
{
public:
MainGame(void);
~MainGame(void);
void run();
private:
void initSystems();
void initShaders();
void gameLoop();
void processInput();
void drawGame();
SDL_Window* _window;
int _screenWidth;
int _screenHeight;
GameState _gameState;
Sprite _sprite;
GLSLProgram _colorProgram;
};
主游戏.cpp:
#include "MainGame.h"
MainGame::MainGame(void)
{
_window = nullptr;
_screenWidth = 1024;
_screenHeight = 700;
_gameState = GameState::PLAY;
}
MainGame::~MainGame(void)
{
}
void MainGame::run()
{
initSystems();
_sprite.init(-1.0f, -1.0f, 1.0f, 1.0f);
gameLoop();
}
void MainGame::initSystems()
{
//Initialize SDL
SDL_Init(SDL_INIT_EVERYTHING);
_window = SDL_CreateWindow("Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, _screenWidth, _screenHeight, SDL_WINDOW_OPENGL);
if (_window == nullptr)
{
fatalError("SDL Window could not be created!");
}
SDL_GLContext glContext = SDL_GL_CreateContext(_window);
if (glContext == nullptr)
fatalError("SDL_GL context could not be created!");
GLenum error = glewInit();
if (error != GLEW_OK)
fatalError("Could not initialize glew!");
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
initShaders();
}
void MainGame::initShaders()
{
_colorProgram.compileShaders("Shaders/colorShading.vert", "Shaders/colorShading.frag");
_colorProgram.addAttribute("vertexPosition");
_colorProgram.linkShaders();
}
void MainGame::gameLoop()
{
while (_gameState != GameState::EXIT)
{
processInput();
drawGame();
}
}
void MainGame::processInput()
{
SDL_Event evnt;
while (SDL_PollEvent(&evnt))
{
switch (evnt.type)
{
case SDL_QUIT:
_gameState = GameState::EXIT;
break;
case SDL_MOUSEMOTION:
std::cout << "New Coords: " << evnt.motion.x << " " << evnt.motion.y << std::endl;
break;
}
}
}
void MainGame::drawGame()
{
glClearDepth(1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
_colorProgram.use();
_sprite.draw();
_colorProgram.unUse();
SDL_GL_SwapWindow(_window);
}
GLSL程序.h:
//Used for
#pragma once
#include <string>
#include <GL/glew.h>
//#include <SDL/SDL.h>
#include "Errors.h"
class GLSLProgram
{
public:
GLSLProgram();
~GLSLProgram();
void compileShaders(const std::string& vertextShaderFilePath, const std::string& fragmentShaderFilePath);
void linkShaders();
void addAttribute(const std::string&);
void use();
void unUse();
private:
int _numAttributes;
void _compileShader(const std::string&, GLuint);
GLuint _programID;
GLuint _vertexShaderID;
GLuint _fragmentShaderID;
};
GLSL程序.cpp:
//Used for compiling shaders
#include "GLSLProgram.h"
#include <fstream>
#include <vector>
GLSLProgram::GLSLProgram() : _numAttributes(0), _programID(0), _vertexShaderID(0), _fragmentShaderID(0)
{
}
GLSLProgram::~GLSLProgram()
{
}
void GLSLProgram::compileShaders(const std::string& vertexShaderFilePath, const std::string& fragmentShaderFilePath)
{
_vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
if (_vertexShaderID == 0)
{
fatalError("Vertex shader failed to be created!");
SDL_Quit();
}
_fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
if (_fragmentShaderID == 0)
{
fatalError("Fragment shader failed to be created!");
SDL_Quit();
}
_compileShader(vertexShaderFilePath, _vertexShaderID);
_compileShader(fragmentShaderFilePath, _fragmentShaderID);
}
void GLSLProgram::addAttribute(const std::string& attributeName)
{
glBindAttribLocation(_programID, _numAttributes++, attributeName.c_str());
}
void GLSLProgram::use()
{
glUseProgram(_programID);
for (int x = 0; x < _numAttributes; x++)
{
glEnableVertexAttribArray(x);
}
}
void GLSLProgram::unUse()
{
glUseProgram(0);
for (int x = 0; x < _numAttributes; x++)
{
glDisableVertexAttribArray(x);
}
}
void GLSLProgram::linkShaders()
{
//Vertex and fragment shaders are successfully compiled.
//Now time to link them together into a program.
//Get a program object.
_programID = glCreateProgram();
//Attach our shaders to our program
glAttachShader(_programID, _vertexShaderID);
glAttachShader(_programID, _fragmentShaderID);
//Link our program
glLinkProgram(_programID);
//Note the different functions here: glGetProgram* instead of glGetShader*.
GLint isLinked = 0;
glGetProgramiv(_programID, GL_LINK_STATUS, (int *)&isLinked);
if (isLinked == GL_FALSE)
{
GLint maxLength = 0;
glGetProgramiv(_programID, GL_INFO_LOG_LENGTH, &maxLength);
//The maxLength includes the NULL character
std::vector<char> infoLog(maxLength);
glGetProgramInfoLog(_programID, maxLength, &maxLength, &infoLog[0]);
//We don't need the program anymore.
glDeleteProgram(_programID);
//Don't leak shaders either.
glDeleteShader(_vertexShaderID);
glDeleteShader(_fragmentShaderID);
printf("%s\n", &(infoLog[0]));
fatalError("Shaders failed to link!");
}
//Always detach shaders after a successful link.
glDetachShader(_programID, _vertexShaderID);
glDetachShader(_programID, _fragmentShaderID);
glDeleteProgram(_programID);
glDeleteShader(_vertexShaderID);
glDeleteShader(_fragmentShaderID);
}
void GLSLProgram::_compileShader(const std::string &filePath, GLuint id)
{
std::ifstream vertexFile(filePath);
if (vertexFile.fail())
{
perror(filePath.c_str());
fatalError("Failed to open " + filePath);
}
std::string fileContents;
std::string line;
while (std::getline(vertexFile, line));
{
fileContents += line + "\n";
}
vertexFile.close();
const char *contentsPtr = fileContents.c_str();
glShaderSource(id, 1, &contentsPtr, nullptr);
glCompileShader(id);
GLint isCompiled = 0;
glGetShaderiv(id, GL_COMPILE_STATUS, &isCompiled);
if (isCompiled == GL_FALSE)
{
GLint maxLength = 0;
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &maxLength);
//The maxLength includes the NULL character
std::vector<char> errorLog(maxLength);
glGetShaderInfoLog(id, maxLength, &maxLength, &errorLog[0]);
//Provide the infolog in whatever manor you deem best.
//Exit with failure.
glDeleteShader(id); //Don't leak the shader.
printf("%s\n", &(errorLog[0]));
fatalError("Shader" + filePath + "failed to compile!");
}
}
顶点着色器:
#version 130
//The vertex shader operates on each vertex
//input data from the VBO. Each vertex is 2 floats
in vec2 vertexPosition;
void main()
{
//Set the x,y position on the screen
gl_Position.xy = vertexPosition;
//the z position is zero since we are in 2D
gl_Position.z = 0.0;
//Indicate that the coordinates are normalized
gl_Position.w = 1.0;
}
和片段着色器:
#version 130
//The fragment shader operates on each pixel in a given polygon
//This is the 3 component float vector that gets outputted to the screen
//for each pixel.
out vec3 color;
void main() {
//Just hardcode the color to red
color = vec3(1.0, 0.0, 1.0);
}
我不知道为什么会这样。 :(
PS:我是 glew 的初学者,所以请不要回答一些高级的东西:D
编辑 1:02/11/2014(美国人为 11/02/2014)我从我正在使用的教程中下载了源代码,它正在运行。所以我的代码有些问题。当我发现问题时,我会编辑帖子。
最佳答案
删除文件 GLSLProgram.cpp 中的分号:
while (std::getline(vertexFile, line));
您浏览文件,然后将着色器代码的最后一行添加到空字符串 fileContents 中,即“}”。
关于c++ - glCreateShader 失败并返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26698908/
当我尝试使用 glCreateShader 创建着色器并运行我的程序时,我刚刚创建的窗口变为白色并给出错误: OpenGl.exe 中的 0x03D312F0 (atioglxx.dll) 引发异常:
为什么会出现这些错误? error: 'GL_VERTEX_SHADER' was not declared in this scope error: 'glCreateShader' was not
我应该有最新版本的 Glew 和 Glut,所以这应该不是问题。一切都应该链接起来,我正在使用 MS visual studio 2010。我的程序可以编译,但是当我到达 glCreateShader
我有一个使用 cygwin 和 GCC 的 Windows 构建环境,并且正在链接到 GLEE、GLUT 和 opengl32 的库。这是 Win32 版本。 对 glCreateShader 的所有
背景: 我的 c++/OpenGL3.1/GLSL/Qt 程序中有一个 Shader 类。我的程序使用多个基于不同 GLSL 源文件的着色器。 我的应用程序可以运行许多基于 QGLWidget 实现的
我在 OpenGL 中有一个项目,我正在尝试加载着色器。为此,我使用 GLuint shader=glCreateShader(shaderType);。问题是,当它尝试运行这一行时,我得到了 EXC
我遇到了 glCreateShaders 的问题。它总是返回 0。我将 Glew 与 SDL 结合使用,每当我运行该程序时,它都会显示: 0(1) : error C0000: syntax erro
我遇到了一个非常奇怪的 OpenGL 错误,其中调用 glCreateShader工作了几次,然后稍后我尝试调用它,但它失败了,返回 0。调用 glGetError紧接着也返回0,表示没有错误。环顾四
我正在使用 glew、glut 和 GLM 编写 C++ 程序。当我像这样创建着色器时: GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER
我有一些代码,我直接按照教程生成三角形。作者在 Windows 中编写他的代码,但他说这也可以在 OSX 中完成。我能够让程序编译,但它在遇到 glCreateShader 时抛出异常。我到处都看过,
我正在开发一个 OpenGL 程序来测试着色器并尝试编译它们。但是,对 glCreateShader() 的调用(以及其他着色器调用,如 glDeleteShader)会出现以下错误: (此错误来自
目前正在尝试让我的 C++ 源代码在 iOS 上运行。它在 Android 上的 OpenGLES 2.0 中运行,我已经完成了编译的所有内容,但在编译着色器时遇到了问题。由于某种原因 glCreat
我正在尝试学习如何编写 OpenGL 着色器。为什么这段代码在我的机器上运行时会段错误? (我使用的是 Ubuntu 10.04,我将其命名为 shader.cpp。) #include #incl
我在 android 上创建着色器程序时遇到了一个非常困难的问题。当我分别调用 glCreateShader 或 glCreateProgram 时,它们总是返回 0。 我已经介绍了有关故障排除的所有
我正在尝试使用 C++ 和 SDL 2 在 OpenGL ES 2.x 中渲染一个简单的三角形。但是 glCreateShader 和 glCreatProgram 返回零。下面是我正在使用的代码 #
我正在关注“OpenGL ES 2 for Android_快速入门指南”,并创建了使用 opengl 构建简单矩形表所需的一切。但是,当我运行应用程序时,我得到 glCreateShader 返回
我是一名优秀的程序员,十分优秀!