using glutBitmapCharacter() to draw text closing opengl program - c++

When I try draw text using a function called drawText() the function runs smoothly but when it reaches glutBitmapCharacter(GLUT_BITMAP_9_BY_15, (int)text[i]); the program unexpectedly closes. I have tested that it is this specific part of the function causing this as if I where to remove it and replace it with printf("Hello World!\n"); the program doesn't crash.
Main function:
int main(void) {
GLFWwindow* window;
int width = 840, height = 640;
if (!glfwInit()) {
printf("failed to init glfw");
return -1;
}
window = glfwCreateWindow(width, height, "ElectroCraft", NULL, NULL);
glfwMakeContextCurrent(window);
if (!window) {
printf("failed to start window");
glfwTerminate();
return -1;
}
glEnable(GL_DEPTH_TEST);
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, 0, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
while (!glfwWindowShouldClose(window)) {
glClearColor(53.0f / 255.0f, 81.0f / 255.0f, 92.0f / 255.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_LINES);
glVertex2f(0, height-80);
glVertex2f(width, height - 80);
glEnd();
string text;
text = "Hello World!";
//glutBitmapCharacter(GLUT_BITMAP_9_BY_15, int(text[1]));
drawText(text.data(), text.size(), 50, 100);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 1;
}
drawText function:
void drawText(const char* text, int length, int x, int y) {
glMatrixMode(GL_PROJECTION);
double* matrix = new double[16];
glGetDoublev(GL_PROJECTION_MATRIX, matrix);
glLoadIdentity();
glOrtho(0, 800, 0, 600, -5, 5);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glLoadIdentity();
glRasterPos2i(x, y);
for (int i=0; i < length; i++) {
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, (int)text[i]);
printf("Hello world!\n");
}
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glLoadMatrixd(matrix);
glMatrixMode(GL_MODELVIEW);
}
I am not exactly sure if it's my code not being the best or if there's a bug in freeglut library. Just to be clear I'm using freeglut.

If you want to use any function of the GLUT library, then GLUT has to be initialized by glutInit. e.g.:
int main(int argc, char** argv)
{
glutInit(&argc, argv);
// [...]
}

Related

Drawing a line in Opengl not displaying C++

I am trying to draw a line straight across my window The screen colour is working but the line doesn't seem to draw. I am fairly certain this is because I might of set the position wrong and the line is being clipped from the window but I'm not sure how to fix this.
my full code
#include <GL\glew.h>
#include <GLFW/glfw3.h>
#include <GL\glut.h>
#include <glm.hpp>
#include <GL\freeglut.h>
#include <GL\GL.h>
#include <IL/il.h>
using namespace std;
int main() {
int windowWidth = 1024, windowHeight = 1024;
if (!glfwInit())
return -1;
GLFWwindow* window;
window = glfwCreateWindow(windowWidth, windowHeight, "electroCraft", NULL, NULL);
glfwMakeContextCurrent(window); // stes the specified window as active INACTIVE SCREEN IF WINDOW NOT CURRENT!!!!
if (!window) {
glfwTerminate();
printf("Screen failed to start. ABORTING...\n");
return -1;
}
glViewport(0, 0, windowWidth, windowHeight);
glOrtho(0, windowWidth, 0, windowHeight, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_DEPTH_TEST);
while (!glfwWindowShouldClose(window)) {
glClearColor(62.0f / 255.0f, 85.9f / 255.0f, 255.0 / 255.0, 0.0);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
//begin drawing
glBegin(GL_LINE);
glVertex2f(20, 100);
glVertex2f(600, 100);
glEnd();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
As mentioned in the comment you've to use GL_LINES rather than GL_LINE, because GL_LINE is not a valid Primitive type.
glBegin(GL_LINES); // <----
glVertex2f(20, 100);
glVertex2f(600, 100);
glEnd();
But there is another issue. The default matrix mode is GL_MODELVIEW (see glMatrixMode), so the orthographic projection is set to the model view matrix and is overwritten by the identity matrix (glLoadIdentity). You've to set the matrix mode GL_PROJECTION, before glOrtho:
glMatrixMode(GL_PROJECTION); // <----
glOrtho(0, windowWidth, 0, windowHeight, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

Wrong display when using glutReshapeFunc to change window size

I have know that glutReshapeFunc can be used to react to window resize, so I used it to reset projection matrix and render shape to match the window. The code is as the following:
#include <GLUT/glut.h>
GLsizei wndWidth = 400;
GLsizei wndHeight = 300;
void init() {
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 400, 0, 300);
}
void drawSegment() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0, 0, 1.0);
glBegin(GL_LINE_LOOP);
glVertex2i(10, 10);
glVertex2i(wndWidth-10, wndHeight-10);
glVertex2i(wndWidth-10, 10);
glEnd();
glFlush();
}
void reshapeFunc(int width, int height) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, GLdouble(width), 0, GLdouble(height));
glClear(GL_COLOR_BUFFER_BIT);
wndWidth = width;
wndHeight = height;
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowSize(400, 300);
glutInitWindowPosition(0, 0);
glutCreateWindow("Draw Window");
init();
glutDisplayFunc(drawSegment);
glutReshapeFunc(reshapeFunc);
glutMainLoop();
}
In my opinion, After window resize, the triangle should always take up half of the window. But in fact, the triangle remains its size after window resize, as if wndWidth and wndHeight does not change in drawSegment.
So why i get the wrong result and how can i fix it?
The projection matrix transforms the view coordinates to clip coordinates and clip coordinates are transformed to normalized device coordinates (at orthographic projection clip coordinates and normalized device coordinates are equal).
How the normalized device coordinates are mapped to window coordinates is specified by glViewport.
After the size of the window has changed you have to respecify the mapping:
void reshapeFunc(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, GLdouble(width), 0, GLdouble(height));
wndWidth = width;
wndHeight = height;
}

need help moving a camera in OpenGL

I am working on a simple OpenGL project.
I want to get a simple camera to move in perspective mode.
I keep reading about the projection matrix, gluLookAt, and the model view matrix. I keep reading that all I should do are my perspective calls in the projection matrix and then all of my transformations and camera movement in the model view matrix.
#include "GLheaders.h"
void drawWorldAxis() {
glLoadIdentity();
glBegin(GL_LINES);
glNormal3f(0, 0, 1);
glColor3ub(255, 0, 0);
glVertex3f(0,0,0);
glVertex3f(1,0,0);
glColor3ub(0, 255, 0);
glVertex3f(0,0,0);
glVertex3f(0,1,0);
glColor3ub(0, 0, 255);
glVertex3f(0,0,0);
glVertex3f(0,0,1);
glEnd();
}
void keyboard(unsigned char key, int x, int y) {
glutPostRedisplay();
}
static float eye[3] = {.5, .5, .5};
#include <stdio.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(55.0, 1, .1, 10000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
drawWorldAxis();
printf("eye at <%f, %f, %f>\n", eye[0], eye[1], eye[2]);
fflush(stdout);
gluLookAt(eye[0], eye[1], eye[2], 0, 0, 0, 0, 1, 0);
eye[0] += .1;
eye[1] += .1;
glFlush();
glutSwapBuffers();
}
void reshape(int w, int h) {
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(55.0, 1, -1, 10000);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE| GLUT_DEPTH);
glutInitWindowSize(400,400);
glutCreateWindow("Tiny Test");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glEnable(GL_NORMALIZE);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return EXIT_SUCCESS;
}
I am expecting this code to display three lines representing the world coordinate system's x, y, and z axises, and as keys are pressed the camera should move and start to look at the origin/coordinate axises from more and more drastic angles.
What is going wrong here? I've been bashing my head into a wall trying to figure out why nothing is moving. It only changes if I put the gluLookAt call in the projection matrix which I keep being told is a terrible idea.
The coordinate cross is drawn before you set the lookAt matrix, thus the matrix has no effect.
You have to change the order such that the matrix is already present when drawing:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eye[0], eye[1], eye[2], 0, 0, 0, 0, 1, 0);
drawWorldAxis();
printf("eye at <%f, %f, %f>\n", eye[0], eye[1], eye[2]);
fflush(stdout);
Then there is a second problem: You are resetting the model matrix in the first line of drawWorldAxis. Here, you can either remove the glLoadIdentity call or push the previous matrix to the stack first:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eye[0], eye[1], eye[2], 0, 0, 0, 0, 1, 0);
glPushMatrix();
drawWorldAxis();
glPopMatrix();
printf("eye at <%f, %f, %f>\n", eye[0], eye[1], eye[2]);
fflush(stdout);
thanks to #BDL for helping fix this! This is the correct code that I wanted
#include "GLheaders.h"
void drawWorldAxis() {
glBegin(GL_LINES);
glNormal3f(0, 0, 1);
glColor3ub(255, 0, 0);
glVertex3f(0,0,0);
glVertex3f(1,0,0);
glColor3ub(0, 255, 0);
glVertex3f(0,0,0);
glVertex3f(0,1,0);
glColor3ub(0, 0, 255);
glVertex3f(0,0,0);
glVertex3f(0,0,1);
glEnd();
}
void keyboard(unsigned char key, int x, int y) {
glutPostRedisplay();
}
static float eye[3] = {-.1, -.1, 1};
#include <stdio.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(55.0, 1, .1, 10000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
printf("eye at <%f, %f, %f>\n", eye[0], eye[1], eye[2]);
fflush(stdout);
gluLookAt(eye[0], eye[1], eye[2], 0, 0, 0, 0, 1, 0);
drawWorldAxis();
eye[0] += .1;
eye[1] += .1;
glFlush();
glutSwapBuffers();
}
void reshape(int w, int h) {
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(55.0, 1, -1, 10000);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE| GLUT_DEPTH);
glutInitWindowSize(400,400);
glutCreateWindow("Tiny Test");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glEnable(GL_NORMALIZE);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return EXIT_SUCCESS;
}

GlOrtho not working properly

I am using GLUT and for some reason my glOrtho isn't working properly.
This is my code:
#include"Dependencies\glew\glew.h"
#include"Dependencies\freeglut\freeglut.h"
#include<iostream>
void render(void)
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(1, 1, 1, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 1280, 720, 0, 0, 1);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
float x = 0.0;
float y = 0;
glBegin(GL_QUADS);
glColor3f(1, 0, 0);
glVertex2f(x, y);
glVertex2f(x + .1, y);
glVertex2f(x + .1, y + .1);
glVertex2f(x, y + .1);
glEnd();
glutSwapBuffers();
}
void closeCallback()
{
std::cout << "GLUT:\t Finished" << std::endl;
glutLeaveMainLoop();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(32, 32);
glutInitWindowSize(1280, 720);
glutCreateWindow("MahGame");
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);
glewInit();
glutDisplayFunc(render);
glutCloseFunc(closeCallback);
glutMainLoop();
return 0;
}
Even though I set my x and y floats to 0 I renders it in the middle, and when I try something like x + 50, it is SUPER huge, I have to make it x + 0.5f so that my triangle is small.
You are not setting your projection matrix correctly:
glLoadIdentity();
glOrtho(0, 1280, 720, 0, 0, 1);
glLoadIdentity();
The second glLoadIdentity() completely overwrites whatever matrix was set before, so that your glOrtho() does not have any effect at all (besides consuming some CPU cycles).

OpenGL with GLUT. Not Drawing?

#include <GL/gl.h>
#include <GL/glut.h>
void display();
void init();
int main(int argc, char* argv[])
{
init();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(320, 240);
glutCreateWindow("Main Window");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
void init()
{
glDisable(GL_DEPTH_TEST);
}
void display()
{
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glBegin(GL_QUADS);
glColor3i(255,255,255);
glVertex2f(10, 10);
glVertex2f(100, 10);
glVertex2f(100, 100);
glVertex2f(10, 100);
glEnd();
glutSwapBuffers();
}
Theoretically, this code should draw a white rectangle. But all I see is a black empty screen. What's wrong?
Here is my working example with the changes I made noted by comments:
#include <gl/glut.h>
#include <gl/gl.h>
#define WINDOW_WIDTH 320
#define WINDOW_HEIGHT 240
void display();
void init();
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutCreateWindow("Main Window");
init(); // changed the init function to come directly before display function
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
void init()
{
glClearColor(0, 0, 0, 0); // moved this line to be in the init function
glDisable(GL_DEPTH_TEST);
// next four lines are new
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, WINDOW_WIDTH-1, WINDOW_HEIGHT-1, 0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glBegin(GL_QUADS);
glColor3ub(255,255,255); // changed glColor3i to glColor3ub (see below)
glVertex2f(10, 10);
glVertex2f(100, 10);
glVertex2f(100, 100);
glVertex2f(10, 100);
glEnd();
glFlush(); // added this line
//glutSwapBuffers(); // removed this line
}
glColor3ub is the function you want to use if you want to provide colors in the 0-255 range.
Hope this helps.