I am writing a game engine in OpenGL and GLFW. However, somehow my window can't be closed. I tried many things but it doesn't have effect. What's wrong with my code?
I can't find the wrong thing in it - everything seems fine to me.
Code:
int running;
GLFWwindow* window;
Window::~Window()
{
glfwTerminate();
}
Window::Window(int width, int height, const std::string& title)
: m_width(width),
m_height(height),
m_title(title){
glfwInit();
if (!glfwInit())
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
window = glfwCreateWindow(height, width, __FILE__, NULL, NULL);
if (!window) {
glfwTerminate();
}
running = true;
}
void Window::MainLoop()
{
do
{
glfwMakeContextCurrent(window);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
glfwPollEvents();
Draw();
glfwSwapBuffers(window);
}
while(running);
}
void Window::Draw()
{
glBegin(GL_TRIANGLES);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glEnd();
}
Thanks!
There are several things but the issue seems to be that you never set running = false.
Try making your while condition looking like this while(!glfwWindowShouldClose(window));
Also if you would like to be able to close the window by pressing escape this should work: while(!glfwWindowShouldClose(window) && glfwGetKey(window_, GLFW_KEY_ESCAPE) != GLFW_PRESS);
Also consider making your int running a bool.
And things such as glfwMakeContextCurrent(window); and glClearColor(0.2f, 0.3f, 0.3f, 1.0f); do not need to be inside your loop if you do not intend to change them.
For more information about openGL and to gain some basic understanding and working examples consider reading https://learnopengl.com/.
Related
I am trying to set the background color to blue, but glClearColor is completely ignored.
Code:
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLFWwindow* window = glfwCreateWindow(640, 480, "3D Shooter", NULL, NULL);
glClearColor(0.5f, 0.5f, 1.0f, 1.0f);
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
}
You seem to be missing a
glfwMakeContextCurrent(window);
https://www.glfw.org/docs/3.3/quick.html
According to a book I'm reading, vertex and fragment shaders are required for the newest versions of OpenGL, and if they're not provided, rendering will not happen properly.
I'm using the GLFW library (3.0.4), and in the spinning triangle Hello World type example, there is no code to load the GPU with these shaders. I cannot find anything that says whether GLFW provides default shaders or not...
Below is the example code I'm referring to. There are calls to glColor3f(), which might be involved.
Also, I notice that the OpenGL.org documentation doesn't seem to list glColor*() at all! Why is this?
#include "GLFW\glfw3.h"
static void error_callback(int error, const char* description)
{
fputs(description, stderr);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
int main(void)
{
GLFWwindow* window;
glfwSetErrorCallback(error_callback);
if (!glfwInit())
exit(EXIT_FAILURE);
window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, key_callback);
while (!glfwWindowShouldClose(window))
{
float ratio;
int width, height;
glfwGetFramebufferSize(window, &width, &height);
ratio = width / (float)height;
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef((float)glfwGetTime() * 50.f, 0.f, 0.f, 1.f);
glBegin(GL_TRIANGLES);
glColor3f(1.f, 0.f, 0.f);
glVertex3f(-0.6f, -0.4f, 0.f);
glColor3f(0.f, 1.f, 0.f);
glVertex3f(0.6f, -0.4f, 0.f);
glColor3f(0.f, 0.f, 1.f);
glVertex3f(0.f, 0.6f, 0.f);
glEnd();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}
glBegin and glEnd indicate you use immediate mode aka the fixed function pipeline which predates shaders. That pipeline is also outdated and removed from 4.0+ core profiles.
So I am working on making a game in c++ and I am using SDL2 and OpenGL/GLEW. I am attempting to just draw basic shapes to my window, however, it is just creating a black screen when it should be drawing a triangle. This is the code.
Game Loop:
int main(int argc, char** argv) {
Window window("Test", 600, 400);
RenderingEngine e(window);
e.Init();
while (!window.IsCloseRequested()) {
window.SwapBuffers();
e.Render();
}
return 0;
}
Window Class:
Window::Window(const std::string& title, unsigned int width, unsigned int height) :
m_title(title), m_width(width), m_height(height), m_isCloseRequested(false) {
SDL_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
m_window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
m_glContext = SDL_GL_CreateContext(m_window);
SDL_GL_MakeCurrent(m_window, m_glContext);
SDL_GL_SetSwapInterval(1);
glewExperimental = GL_TRUE;
GLenum res = glewInit();
if (res != GLEW_OK) {
std::cerr << "Error initializting OpenGL." << std::endl;
exit(1);
}
}
void Window::SwapBuffers() {
SDL_GL_SwapWindow(m_window);
}
RenderingEngine Class:
RenderingEngine::RenderingEngine(const Window& window) : m_window(&window) {
}
RenderingEngine::~RenderingEngine() {
if (m_window) delete m_window;
}
void RenderingEngine::Init() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glFrontFace(GL_CW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_FRAMEBUFFER_SRGB);
}
void RenderingEngine::Render() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_TRIANGLES);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glEnd();
}
I cannot figure out why it isn't drawing to the screen, so any help is greatly appreciated. Thanks!
You're setting up back face culling specifying that your front facing triangles have clockwise winding order, and you want to cull everything that is not front facing:
glFrontFace(GL_CW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
But the triangle you specify has counter-clockwise winding order:
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
You can verify that by drawing the points on a piece of paper, with the x-axis going right and the y-axis up. You will see the order of points is counter-clockwise around the origin.
Based on this, your triangle is eliminated by back face culling. Since using counter-clockwise winding order is much more commonly used with OpenGL, I would suggest using it by changing the glFrontFace() call to:
glFrontFace(GL_CCW);
Or don't make the call at all, since this is the default.
you didn`t loaded View/Projection/Model matrices ... i guess that would be a problem... also you should use shaders if you want to make in 3.2 style
I'm trying to render a set of 200 RGB frames. For doing the same i'm creating a texture of height 416 and width 240. However i just get a black screen with the print statements working in the background.
Below is my code:
#include <stdio.h>
#include <stdlib.h>
#include "glew.h"
#include "glfw.h"
#include "glaux.h"
int index;
AUX_RGBImageRec texture1;
GLuint texture;
unsigned long long pos;
unsigned char *guibuffer;
HDC hDC = NULL;
void initGL(void)
{
int maxSz;
int maxwidth = 416;
int maxheight = 240;
if( !glfwInit() )
{
exit( EXIT_FAILURE );
}
// if( !glfwOpenWindow(4096, 2118, 0,0,0,0,0,0, GLFW_WINDOW ) )
if( !glfwOpenWindow(maxwidth, maxheight, 0,0,0,0,0,0, GLFW_WINDOW ) ) //GLFW_FULLSCREEN
{
glfwTerminate();
exit( EXIT_FAILURE );
}
glfwSetWindowTitle("sample");
glGetIntegerv(GL_MAX_TEXTURE_SIZE,&maxSz);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // This Will Clear The Background Color To Black
glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
glDepthFunc(GL_LESS); // The Type Of Depth Test To Do
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading
glMatrixMode(GL_PROJECTION);
//glLoadIdentity();
hDC= wglGetCurrentDC();
#if 1
{ // TSS
HWND hCurrentWindow = GetActiveWindow();
char szTitle[256]="sample";
//SetWindowText(hCurrentWindow,szTitle );
// SetWindowLongA (hCurrentWindow , GWL_STYLE, (GetWindowLongA (hCurrentWindow , GWL_STYLE) & ~(WS_CAPTION)));
SetWindowLongA (hCurrentWindow, GWL_STYLE, (WS_VISIBLE));
}
#endif
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
}
int GL_Disply()
{
FILE *fptr=fopen("E:\\myRGB.rgb","rb");
fseek(fptr,pos,SEEK_SET);
fread(guibuffer,sizeof(unsigned char),sizeof(unsigned char)*416*240*3,fptr);
pos+=416*240*3;
texture1.sizeX =416;
texture1.sizeY =240;
texture1.data = guibuffer;
glDepthFunc(GL_ALWAYS);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glDisable(GL_LIGHTING);
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texture1.sizeX, texture1.sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glBegin(GL_QUADS);
//glNormal3f( 0.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 0.0f);
glEnd();
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
// Swap front and back rendering buffers
glfwSwapBuffers();
glDeleteTextures(1, &texture);
}
int main(int argc, char *argv[])
{
initGL(); // GL initialization
/* CPU memory allocation using C - malloc */
guibuffer=(unsigned char*)malloc(sizeof(unsigned char)*416*240*3);
for(index=0;index<200;index++)
{
printf("frame %d displayed....\r",index);
GL_Disply();
}
return 0;
}
Can anyone please tell me where i'm going wrong?
You are not passing data (last parameter) to the glTexImage2D function. I guess you would want to pass data readed from your E:\myRGB.rgb file.
It may have something to do with the matrix stack. I think you want to be in glMatrixMode (GL_MODELVIEW) when you draw stuff, not GL_PROJECTION, and you may need to set up your matrices correctly. Though, glMatrixMode, glBegin, glEnd, glTexCoord, glVertex3f, is the old fixed function way of rendering. Using shaders is the modern way of rendering with OpenGL.
I am trying to load and draw a 2d texture using OpenGL with GLFW and SOIL. I have this code, but I only get one solid color (which seems to come from the texture).
I have tested whether the .png loads with an example that came with SOIL, and it worked fine so there has to be some issue in my code.
This is my code:
#include <cstdio>
#include "GL/glfw.h"
#include "SOIL.h"
// function declarations
void drawscene();
void idlefunc();
void updatedisplay();
// global data
GLuint texture; // our example texture
int main(int argc, char **argv) {
if (!glfwInit()) {
fprintf(stderr, "Failed to initialize GLFW\n");
return 1;
}
if (!glfwOpenWindow(640, 480, 0, 0, 0, 0, 16, 0, GLFW_WINDOW)) {
fprintf(stderr, "Failed to open GLFW window\n");
return 1;
}
// enable vsync (if available)
glfwSwapInterval(1);
// load textures
texture = SOIL_load_OGL_texture(
"tex.png",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_DDS_LOAD_DIRECT
);
// check for an error during the texture loading
if (!texture) {
printf("SOIL loading error: '%s'\n", SOIL_last_result());
}
while (glfwGetWindowParam(GLFW_OPENED)) {
idlefunc();
}
// if we get here something went wrong
return 0;
}
// this function gets called every frame
void idlefunc() {
updatedisplay();
drawscene();
}
// set up te display
void updatedisplay() {
int screen_width, screen_height;
glfwGetWindowSize(&screen_width, &screen_height);
if (screen_height <= 0) screen_height = 1;
if (screen_width <= 0) screen_width = 1;
glViewport(0, 0, screen_width, screen_height);
glClearColor(0.02f, 0.02f, 0.02f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, screen_width, screen_height, 0.0, 0.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// displacement trick for exact pixelization
glTranslatef(0.375f, 0.375f, 0.0f);
}
// draw the scene in this function
void drawscene() {
glBindTexture(GL_TEXTURE_2D, texture);
glPushMatrix();
glTranslatef(10.0f, 10.0f, 0);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(0.0f, 0.0f);
glTexCoord2f(0.0f, 128.0f);
glVertex2f(0.0f, 128.0f);
glTexCoord2f(128.0f, 128.0f);
glVertex2f(128.0f, 128.0f);
glTexCoord2f(128.0f, 0.0f);
glVertex2f(128.0f, 0.0f);
glEnd();
glPopMatrix();
glfwSwapBuffers();
}
Found the issue (thanks to user786653). No matter the vertex coords, the tex coords are between 0.0 and 1.0. This is the fixed code:
// draw the scene in this function
void drawscene() {
glBindTexture(GL_TEXTURE_2D, texture);
glPushMatrix();
glTranslatef(10.0f, 10.0f, 0);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(0.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(0.0f, 128.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(128.0f, 128.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex2f(128.0f, 0.0f);
glEnd();
glPopMatrix();
glfwSwapBuffers();
}