• OpenGL Shaders issue
    5 replies, posted
This SHOULD draw a rectangle to the screen. Getting no shader or c++ errors.... MainGame.cpp [CODE]#include "MainGame.h" #include <iostream> #include <string> #include "Errors.h" MainGame::MainGame() : _window(NULL), _screenWidth(1024), _screenHeight(768), _gameState(GameState::PLAY) { } MainGame::~MainGame() { } void MainGame::run() { //Initialize SDL and OpenGL initSystems(); _sprite.init(-1, -1, 1, 1); //Start the game loop gameLoop(); } void MainGame::initSystems() { //Initialize SDL SDL_Init(SDL_INIT_EVERYTHING); //Create the SDL window _window = SDL_CreateWindow("Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, _screenWidth, _screenHeight, SDL_WINDOW_OPENGL); //Crash if the window was not created if (_window == nullptr) fatalError("SDL window could not be created!"); //Create the opengl context and handle errors SDL_GLContext glContext = SDL_GL_CreateContext(_window); if (glContext == nullptr) fatalError("SDL_GL context could not be created!"); //Initialize glew and handle errors if (glewInit() != GLEW_OK) fatalError("Glew failed to initialize!"); //Set SDL to double buffer SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); //Set GL clear color glClearColor(0.0f, 0.15f, 0.3f, 1.0f); initShaders(); } void MainGame::initShaders() { _colorProgram.compileShaders("Shaders/colorShading.vert", "Shaders/colorShading.frag"); _colorProgram.addAttribute("vertexPosition"); _colorProgram.linkShaders(); } void MainGame::processInput() { SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: _gameState = GameState::EXIT; break; case SDL_KEYDOWN: break; case SDL_KEYUP: if (event.key.keysym.sym == SDLK_ESCAPE) _gameState = GameState::EXIT; break; case SDL_MOUSEMOTION: break; case SDL_MOUSEBUTTONDOWN: break; } } } void MainGame::drawGame() { //Tells opengl what depth to clear to glClearDepth(1.0); //Clear the screen glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); _colorProgram.use(); _sprite.draw(); _colorProgram.unuse(); //Swap buffers SDL_GL_SwapWindow(_window); } void MainGame::gameLoop() { while (_gameState != GameState::EXIT) { processInput(); drawGame(); } } [/CODE] GLSLProgram.cpp [CODE] #include "GLSLProgram.h" #include "Errors.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) { _programID = glCreateProgram(); _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::linkShaders() { //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<GLchar> 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!"); } //Always detach shaders after a successful link. glDetachShader(_programID, _vertexShaderID); glDetachShader(_programID, _fragmentShaderID); glDeleteShader(_vertexShaderID); glDeleteShader(_fragmentShaderID); } void GLSLProgram::addAttribute(const std::string& attributeName) { glBindAttribLocation(_programID, _numAttributes++, attributeName.c_str()); } 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 file(filePath); if (file.fail()) { perror(filePath.c_str()); fatalError("Failed to open " + filePath); } std::string fileContents = ""; std::string line; while (std::getline(file, line)) { fileContents += line + "\n"; } file.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); std::vector<char> errorLog(maxLength); glGetShaderInfoLog(id, maxLength, &maxLength, &errorLog[0]); glDeleteShader(id); std::printf("%s\n", &errorLog[0]); fatalError("Shader " + filePath + " failed to compile!"); } } [/CODE] sprite.cpp [CODE]#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]; vertexData[0] = x + width; vertexData[1] = y + height; vertexData[2] = x; vertexData[3] = y + height; vertexData[4] = x; vertexData[5] = y; vertexData[6] = x; vertexData[7] = y; vertexData[8] = x + width; vertexData[9] = y; vertexData[10] = x + width; vertexData[11] = y + height; glBindBuffer(GL_ARRAY_BUFFER, _vboID); glBufferData(_vboID, 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); }[/CODE]
Post the shaders too? As an aside, don't declare a dtor unless you need one, you fuck up automatic move operator/ctor. Oh you're also not enabling the depth test, but I don't think that's the issue.
[CODE]#version 130 in vec2 vertexPosition; void main() { gl_Position.xy = vertexPosition; gl_Position.z = 0.0; gl_Position.w = 1.0; }[/CODE] [CODE]#version 130 out vec3 color; void main() { color = vec3(1.0, 0.0, 0.0); }[/CODE]
1. Try setting the z to 1 or -1 2. change color to equal vec4(1.0, 0.0 0.0, 1.0) That might work?
[QUOTE=Duskling;49785336]1. Try setting the z to 1 or -1 2. change color to equal vec4(1.0, 0.0 0.0, 1.0) That might work?[/QUOTE] Didn't fix it... I tried printing out what opengl version I have and it said null. But my graphics card drivers say i have it installed.
You can't install OpenGL, that's not exactly how it works. Find out what version your graphics card can support up until. Also, I don't see you binding the color output from the frag shader. Not sure if you have to do this since I always use layout positions instead, but that's version 330. To be honest I'm not 100% sure what it does, but the command is glBindFragDataLocation&#8203;.
Sorry, you need to Log In to post a reply to this thread.