我正在使用OpenGL和GLSL。我正在按照一个教程,准确地说这个:Exact video of tutorial that I am having problem with但他没有使用xcode,这就是我正在使用的,它导致了这个问题。
我将从我的系统信息开始,我认为所有相关的信息足以分享这将有助于解决问题。然后我会告诉你问题是什么,以及我所学到的其他我认为相关且有帮助的事情。在那之后,我将包含我使用的所有代码的副本,因此您可以查看它的错误和真正的问题等。
系统信息:
iMac(21.5英寸,2011年中) OS X Yosesemite(V 10.10.1)处理器:2.5 GHz Intel Core i5 内存:4 GB 1333 MHz DDR3
图形: OpenGL版本:4.1 ATI-1.28.29 GLSL版本:4.10 供应商:ATI Technologies Inc. 渲染器:AMD Radeon HD 6750M OpenGL引擎
Xcode v 6.1.1
我的问题:
问题是Shader无法编译。
片段shader.txt:
#version 130
out vec3 color;
void main() {
color = vec3(1.0, 0.0, 0.0);
}
顶点shader.txt
#version 130
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;
}
这两个都无法编译。编译调用将在GLSLProgram.cpp中,更具体地说是void GLSLProgram :: compileShader。
我检查错误,收到的错误信息是:“错误:0:1:'':版本'130'不受支持”
我发现此错误的典型原因是过时的图形卡。我知道情况并非如此,但是,无论如何,我只是在运行时检查过。我检查我的OpenGl版本和glsl版本,得到以下结果:
OpenGL版本:2.1 ATI-1.28.29 GLSL版本:1.20 供应商:ATI Technologies Inc. 渲染器:AMD Radeon HD 6750M OpenGL引擎
它正在读取我的存储卡,但OpenGL和GLSL版本是错误的。我不知道为什么。我一直无法弄清楚为什么或如何通过互联网自行修复它,这就是我在这里的原因。
我下载了一个也使用GLSL的随机跨平台程序,运行正常。我对openGL并不熟悉,所以我无法查看代码并找出问题所在。我所能做的只是给出我最好的猜测。我认为这是一个预处理器错误。我可能正在使用不正确的预处理器,或者过时的版本,或者我不知道。那就是问题所在。然而,有效的代码在运行时打印出正确的OpenGL版本和GLSL版本。所以我知道有些事情导致我不使用最新版本。我不知道这是否是唯一的问题,但我知道这是问题之一。
我在前一段中提到的代码来自this tutorial,对该代码的访问位于该教程第一段的链接中
代码
我只是要包含cpp文件。前向声明在头文件中,但我没有看到需要包含它们。如果你确实需要它们,请问,我会发布它们。
的main.cpp
#include "MainGame.h"
#include "Sprite.h"
#include <iostream>
int main( int argc, char** argv) {
MainGame mainGame;
mainGame.run();
return 0;
}
MainGame.cpp(将所有东西捆绑在一起)
#include "MainGame.h"
#include <iostream>
#include <string>
MainGame::MainGame(){
_window = nullptr;
_screenHeight = 768;
_screenWidth = 1024;
_gameState = PLAY;
}
MainGame::~MainGame(){
}
void MainGame::run(){
initSystems();
_sprite.init(-1.0f, -1.0f, 1.0f, 1.0f);
gameLoop();
}
void MainGame::initSystems(){
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. :(");
}
std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;
std::cout << "GLSL version: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl;
std::cout << "Vendor: " << glGetString(GL_VENDOR) << std::endl;
std::cout << "Renderer: " << glGetString(GL_RENDERER) << std::endl;
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
glClearColor(0.0f, 0.0f, 1.0f, 1.0);
initShaders();
}
void MainGame::initShaders(){
_colorProgram.compileShaders("/Users/user/filepath.../vertex-shader.txt", "/Users/user/filepath.../fragment-shader.txt");
_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 = EXIT;
break;
case SDL_MOUSEMOTION:
std::cout << 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);
}
GLSLProgram.cpp(处理大多数与GLSL相关的事情)
#include "GLSLProgram.h"
#include "Errors.h"
#include <fstream>
#include <vector>
#include <iostream>
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");
}
_fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
if (_fragmentShaderID == 0){
fatalError("Fragment Shader failed to be created");
}
compileShader(vertexShaderFilePath, _vertexShaderID);
compileShader(fragmentShaderFilePath, _fragmentShaderID);
}
void GLSLProgram::addAttribute(const std::string& attributeName){
glBindAttribLocation(_programID, _numAttributes++, attributeName.c_str());
}
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> errorLog(maxLength);
glGetProgramInfoLog(_programID, maxLength, &maxLength, &errorLog[0]);
//We don't need the program anymore.
glDeleteProgram(_programID);
//Don't leak shaders either.
glDeleteShader(_vertexShaderID);
glDeleteShader(_fragmentShaderID);
std::printf("%s/n", &(errorLog[0]));
fatalError("Shaders failed to link"); //In this simple program, we'll just leave
}
//Always detach shaders after a successful link.
glDetachShader(_programID, _vertexShaderID);
glDetachShader(_programID, _fragmentShaderID);
glDeleteShader(_vertexShaderID);
glDeleteShader(_fragmentShaderID);
}
void GLSLProgram::use(){
glUseProgram(_programID);
for (int i = 0; i < _numAttributes; i++) {
glEnableVertexAttribArray(i);
}
}
void GLSLProgram::unuse(){
glUseProgram(0);
for (int i = 0; i < _numAttributes; i++) {
glDisableVertexAttribArray(i);
}
}
void GLSLProgram::compileShader(const std::string& filePath, GLuint id){
std::ifstream shaderFile(filePath);
if (shaderFile.fail()){
perror(filePath.c_str());
fatalError("Failed to open " + filePath);
}
std::string fileContents ="";
std::string line;
while (std::getline(shaderFile, line)) {
fileContents += line + "\n";
}
shaderFile.close();
const char* contentsPtr = fileContents.c_str();
glShaderSource(id, 1, &contentsPtr, nullptr);
glCompileShader(id);
GLint success = 0;
glGetShaderiv(id, GL_COMPILE_STATUS, &success);
if(success == 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.
std::printf("%s\n", &(errorLog[0]));
fatalError("Shader " + filePath + " failed to compile");
}
// Shader compilation is successful.
}
Sprite.cpp(绘制一个简单的三角形)
#include "Sprite.h"
Sprite::Sprite(){
_vboID = 0;
}
Sprite::~Sprite(){
if (_vboID != 0){
glDeleteBuffers(1, &_vboID);
}
}
void Sprite::init(float x, float y, float width, float height){
_x = x;
_y = y;
_width = width;
_height = height;
if (_vboID == 0){
glGenBuffers(1, &_vboID);
}
float vertexData[12];
//first triangle
vertexData[0] = x + width;
vertexData[1] = y + height;
vertexData[2] = x;
vertexData[3] = y + height;
vertexData[4] = x;
vertexData[5] = y;
//second triangle
vertexData[6] = x + width;
vertexData[7] = y + height;
vertexData[8] = x;
vertexData[9] = y;
vertexData[10] = x + width;
vertexData[11] = y;
glBindBuffer(GL_ARRAY_BUFFER, _vboID);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void Sprite::draw(){
glBindBuffer(GL_ARRAY_BUFFER, _vboID);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
error.cpp(出现错误时退出)
#include "Errors.h"
#include <iostream>
#include <SDL2/SDL.h>
void fatalError(std::string errorString) {
std::cout << errorString << std::endl;
std::cout << "Enter any key to quit...";
int tmp;
std::cin >> tmp;
SDL_Quit();
}
此外,此代码至少可以在Windows上运行。几乎所有这些都是从上面的youtube链接中获取的,只需稍加修改就可以使用Xcode。在youtube vid的描述中有一个指向该代码的链接。
答案 0 :(得分:1)
您需要在创建窗口之前调用SDL_GL_SetAttribute来设置您期望的最小OpenGL版本。
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);