Verticies未显示

时间:2015-12-26 19:08:20

标签: c++ opengl sdl-2

我的代码存在问题。没有任何错误,但没有任何内容。我不确定发生了什么。我做错了什么?

Main.cpp的

<div id="floating-panel"><input type="button" id="btnRotate" value="Auto Rotate"></div>
<div id="map"></div>
<script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap"></script>

Sprite.cpp

//
//  MainGame.cpp
//
#include "MainGame.hpp"

#include <iostream>
#include <string>

#include "Errors.hpp"

// Constructor, initialize private variables
MainGame::MainGame() {

    _window = nullptr;
    _screenWidth = 1080;
    _screenHeight = 720;
    _gameState =  GameState::PLAY;
}


MainGame::~MainGame() {

    SDL_DestroyWindow(_window);
    SDL_Quit();
}

// Run the game
void MainGame::run() {

    initSystem();

    _sprite.init(-1.0f, -1.0f, 1.0f, 1.0f);

    gameLoop();
}

// Initialize SDL2, OpenGL, and whatever else
void MainGame::initSystem() {

    if (0 != SDL_Init(SDL_INIT_EVERYTHING)) {
        fprintf(stderr, "\nUnable to initalize SDL: %s\n", SDL_GetError());
        fatalError("Unable to initiaize SDL");
    }

    SDL_LogSetAllPriority(SDL_LOG_PRIORITY_VERBOSE);

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
    SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

    // Create the window...
    _window = SDL_CreateWindow("My Game",
                               SDL_WINDOWPOS_CENTERED,
                               SDL_WINDOWPOS_CENTERED,
                               _screenWidth,
                               _screenHeight,
                               SDL_WINDOW_OPENGL |
                               SDL_WINDOW_SHOWN  |
                               SDL_WINDOW_RESIZABLE);

    if (_window == nullptr) {
        fatalError("Window could not be created");
    }

    SDL_GLContext glContext = SDL_GL_CreateContext(_window);
    if (glContext == nullptr) {
        fatalError("SDL_GL context could not be created!");
    }

    // Clear to black
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

    initShaders();
}

void MainGame::initShaders() {

    _colorProgram.compileShaders("/Shaders/colorShader.vert", "/Shaders/colorShader.frag");
    _colorProgram.addAttribute("vertexPosition");
    _colorProgram.linkShaders();
}

void MainGame::gameLoop() {

    while (_gameState != GameState::EXIT) {
        processInput();
        drawGame();
    }
}

// Process input with SDL2
void MainGame::processInput() {

    SDL_Event event;

    while (SDL_PollEvent(&event)) {

        switch (event.type) {
            case SDL_QUIT:
                _gameState = GameState::EXIT;
                break;
            case SDL_APP_DIDENTERFOREGROUND:
                SDL_Log("SDL_APP_DIDENTERFOREGROUND");
                break;

            case SDL_APP_DIDENTERBACKGROUND:
                SDL_Log("SDL_APP_DIDENTERBACKGROUND");
                break;

            case SDL_APP_LOWMEMORY:
                SDL_Log("SDL_APP_LOWMEMORY");
                break;
        }
    }
}

void MainGame::drawGame() {

    glClearDepth(1.0f);
    //glClearColor(rand() % 255 / 255.0f, rand() % 255 / 255.0f, rand() % 255 / 255.0f, 1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    _colorProgram.use();

    // Draw sprite
    _sprite.draw();

    _colorProgram.unuse();

    SDL_GL_SwapWindow(_window);
}

1 个答案:

答案 0 :(得分:2)

在OpenGL中&gt; 3.3核心简介,VAO的使用是强制性的。

通常这会在draw命令中出现GL_INVALID_OPERATION错误(必须由glGetError()查询)