Most of the time when i encounter a problem i can solve it relatively quickly but this one has me puzzled. Im writing a basic game engine in c++ using visual studio, OpenGL, glew and SDL. the program ran fine on my laptop with integrated graphics but when i switched to my nvidia GPU(GTX 970mx) it would render a black screen, then i moved it over to my desktop and it would only render a black screen also using an nvdia gpu(GTX 960). i get no compilation errors 0 warnings the only OpenGL function that works is glClear(), does anyone know what im doing wrong?
Window.h
#pragma once
#include <SDL\SDL.h>
#include <string>
#include "camera.h"
#include "keyboard_mouse.h"
class Window
{
public:
Window(const std::string& title, int width, int height);
void GetEvent(Mouse* mouse, Keyboard* keyboard);
bool isOpen() { return !isClosed; };
void Clear(float r, float g, float b, float a);
void SwapBuffer();
glm::vec2 GetDeltaMouse() { return glm::vec2(static_cast<GLfloat>(delta_x), static_cast<GLfloat>(delta_y)); }
virtual ~Window();
protected:
private:
bool fullScreen = false;
SDL_Window * window;
int width, height;
int w_full, h_full;
int w_cur, h_cur;
bool isClosed;
SDL_GLContext glContext;
// Mouse stuff
int delta_x, delta_y, x_last, y_last;
};
Window.cpp
// Local Headers
#include "window.h"
// API's
#include <SDL\SDL_keyboard.h>
#include <SDL\SDL_mouse.h>
// Standard Headers
#include <iostream>
using namespace glm;
Window::Window(const std::string& title, int _width, int _height)
{
this->width = _width;
this->height = _height;
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, true);
window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL);
glContext = SDL_GL_CreateContext(window);
SDL_WarpMouseInWindow(this->window, this->width / 2, this->height / 2);
GLenum status = glewInit();
if (status != GLEW_OK)
{
std::cerr << "Could not initialize OpenGL" << std::endl;
}
isClosed = false;
glViewport(0, 0, this->width, this->height);
//glEnable(GL_DEPTH_TEST);
//glEnable(GL_CULL_FACE);
//glCullFace(GL_CULL_FACE);
//glCullFace(GL_BACK);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glOrtho(-1.0F, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
}
Window::~Window()
{
SDL_GL_DeleteContext(glContext);
SDL_DestroyWindow(window);
SDL_Quit();
}
void Window::GetEvent(Mouse* mouse, Keyboard* keyboard)
{
SDL_Event window_event;
while (SDL_PollEvent(&window_event) != 0)
{
switch (window_event.type)
{
case SDL_KEYDOWN:
if (window_event.key.keysym.sym == SDLK_ESCAPE)
this->isClosed = true;
keyboard->keys[window_event.key.keysym.sym] = true;
break;
case SDL_KEYUP:
keyboard->keys[window_event.key.keysym.sym] = false;
break;
case SDL_MOUSEMOTION:
mouse->StoreValue(window_event.motion.x, window_event.motion.y, this->width, this->height);
SDL_WarpMouseInWindow(this->window, this->width / 2, this->height / 2);
break;
case SDL_MOUSEBUTTONDOWN:
break;
case SDL_MOUSEBUTTONUP:
break;
case SDL_MOUSEWHEEL:
mouse->GetWheel() += window_event.wheel.y;
break;
default: break;
}
}
}
void Window::Clear(float r, float g, float b, float a)
{
glClearColor(r, g, b, a);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void Window::SwapBuffer()
{
SDL_GL_SwapWindow(window);
}
main.cpp
#define GLEW_EXPERIMENTAL
// API's
#include <glm.hpp>
#include <GL/glew.h>
#include <SDL\SDL.h>
#include <gtc\matrix_transform.hpp>
#include <gtc\type_ptr.hpp>
#include <GL\GLU.h>
// Standard Headers
#include <iostream>
#include <Windows.h>
// Class Headers
#include "window.h"
#include "shader.h"
#include "camera.h"
#include "sky_box.h"
#include "timer.h"
#include "model.h"
#include "light.h"
//#include "render_handler.h"
// Constants
#define WIDTH 800
#define HEIGHT 600
int main(int argc, char** argv)
{
Window window("Model Loader", WIDTH, HEIGHT);
Camera camera(glm::vec3(0.0f, 5.0f, -20.0f), 70.0f, (float)WIDTH/(float)HEIGHT, 0.1f, 1000.0f);
Mouse mouse;
Keyboard keyboard;
Timer timer;
//RenderHandler RenderEngine;
//
//RenderEngine.AddLight(&Light(vec3(100.0f), vec3(1.0f)));
//RenderEngine.AddLight(&Light(vec3(30.0f, 400.0f, 500.0f), vec3(0.0f, 0.0f, 1.0f)));
//RenderEngine.AddLight(&Light(vec3(600.0f, 800.0f, 100.0f), vec3(1.0f)));
// Shaders
Shader model_shader("../res/Shaders/model");
// Models
//Model nanosuit("../res/Models/rock/rocks_01_model.obj");
Transform trans(vec3(0.0f));
//nanosuit.transform = trans;
//RenderEngine.AddShader(&model_shader, MODEL);
//RenderEngine.AddModel(&nanosuit);
// skybox stuff
Shader skybox("../res/Shaders/skybox");
std::vector<const GLchar*> sky_names;
sky_names.push_back("../res/Textures/Sky_Box_Textures/Beach/right.png");
sky_names.push_back("../res/Textures/Sky_Box_Textures/Beach/left.png");
sky_names.push_back("../res/Textures/Sky_Box_Textures/Beach/up.png");
sky_names.push_back("../res/Textures/Sky_Box_Textures/Beach/bottom.png");
sky_names.push_back("../res/Textures/Sky_Box_Textures/Beach/back.png");
sky_names.push_back("../res/Textures/Sky_Box_Textures/Beach/front.png");
SkyBox sky(sky_names, &skybox, &camera);
system("pause");
float curtime, delta = 0.0f;
timer.Begin();
while (window.isOpen())
{
curtime = timer.GetSeconds();
window.Clear(0.001f, 0.0f, 0.5f, 1.0f); // clear window
window.GetEvent(&mouse, &keyboard);
camera.UpdateVectors(mouse.GetDelta());
camera.SetPos(&keyboard, delta);
//RenderEngine.Render(&camera, &model_shader);
sky.Draw(&skybox, &camera);
delta = timer.GetSeconds() - curtime;
window.SwapBuffer(); // swap dispaly buffers
}
return 0;
}
skybox.h
#pragma once
#include <string>
#include <GL\glew.h>
#include "stb_image.h"
#include <vector>
#include "shader.h"
#include "camera.h"
using namespace std;
class SkyBox
{
public:
SkyBox(vector<const GLchar*> filenames, Shader* shader, Camera* camera);
void Draw(Shader* shader, Camera* camera);
~SkyBox();
private:
GLuint skyboxVAO, skyboxVBO;
GLuint skyBoxID;
GLuint shaderID;
GLfloat vertices[108] =
{
// Positions
-1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, -1.0f,
1.0f, -1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f
};
};
skybox.cpp
#include "sky_box.h"
#include <iostream>
#include <gtc\type_ptr.hpp>
SkyBox::SkyBox(vector<const GLchar*> filenames, Shader* shader, Camera* camera)
{
this->shaderID = shader->programID;
glGenVertexArrays(1, &this->skyboxVAO);
glGenBuffers(1, &this->skyboxVBO);
glBindVertexArray(skyboxVAO);
glBindBuffer(GL_ARRAY_BUFFER, this->skyboxVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(this->vertices), &this->vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glBindVertexArray(0);
glGenTextures(1, &this->skyBoxID);
//glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, this->skyBoxID);
int width, height, components;
unsigned char* data;
for (GLuint i = 0; i < filenames.size(); i++)
{
data = stbi_load(filenames[i], &width, &height, &components, 4);
if (data == NULL)
{
std::cout << "Unable to load texture: " << filenames[i] << std::endl;
return;
}
else
std::cout << "Texture: \"" << filenames[i] << "\" loaded succesfully" << std::endl;
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_BGRA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
data = NULL;
}
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
// Shader stuff
glm::mat4 view = camera->GetViewProjection();
}
void SkyBox::Draw(Shader* shader, Camera* camera) // and camera and link shader to vertex buffer
{
glDepthFunc(GL_LEQUAL);
glUseProgram(shader->programID);
glm::mat4 view = glm::mat4(glm::mat3(camera->GetViewMatrix()));
view = camera->GetProjection() * view;
glm::mat4 transform = camera->GetProjection() * glm::mat4(glm::mat3(camera->GetViewMatrix()));
glUniformMatrix4fv(glGetUniformLocation(shader->programID, "transform"), 1, GL_FALSE, glm::value_ptr(transform));
//glActiveTexture(GL_TEXTURE0);
glBindVertexArray(this->skyboxVAO);
glBindTexture(GL_TEXTURE_CUBE_MAP, this->skyBoxID);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glDepthFunc(GL_LESS);
}
SkyBox::~SkyBox()
{
}