Segmentation Fault on OpenGL program - c++

I'm trying to draw the Sierpinski carpet plane fractal using OpenGL, but my program keeps receiving a SegFault error.
My code is as follows:
#include <GL/gl.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
class GLintPoint
{
public:
GLint x, y;
};
int random(int m)
{
return rand() % m;
}
void drawDot(GLint x, GLint y)
{
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
void init()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
int isSierpinskiCarpetPixelFilled(int x, int y, int width, int height)
{
GLintPoint point;
// base case 1 of 2
if ((x <= 0)||(y <= 0)||(x>=width)||(y>=height)) //top row or left column or out of bounds should be full
{
point.x = x;
point.y = y;
drawDot(point.x, point.y);
}
{
/*
If the grid was split in 9 parts, what part(x2,y2) would x,y fit into?
*/
int x2 = x * 3 / width; // an integer from 0..2 inclusive
int y2 = y * 3 / height; // an integer from 0..2 inclusive
// base case 2 of 2
if (x2 == 1 && y2 == 1) // if in the center square, it should be empty
return 0;
// general case
/* offset x and y so it becomes bounded by 0..width/3 and 0..height/3
and prepares for recursive call
some offset is added to make sure the parts have all the correct size when
width and height isn't divisible by 3 */
x -= (x2 * width+2) / 3;
y -= (y2 * height+2) / 3;
width = (width +2-x2)/3;
height = (height+2-y2)/3;
}
return isSierpinskiCarpetPixelFilled(x, y, width, height);
}
void drawSierpinskiCarpet()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 1.0, 0.0);
int x = 50;
int y = 50;
isSierpinskiCarpetPixelFilled(x,y,50,50);
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(640,480);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutCreateWindow("The Sierpinski Carpet");
glutDisplayFunc(drawSierpinskiCarpet);
init();
glutMainLoop();
return EXIT_SUCCESS;
}

It is likely getting a stack overflow. It appears to be recursing infinitely. Each call to isSierpinskiCarpetPixelFilled results in another call:
return isSierpinskiCarpetPixelFilled(x, y, width, height);
With the given input values, the input parameters (in order) will be:
50, 50, 50, 50
0, 0, 16, 16
0, 0, 6, 6
0, 0, 2, 2
0, 0, 1, 1
0, 0, 1, 1
... (until stack overflow)

Related

OpenGL- glFrustum, glLoadIdentity and glLookAt Confusion

I am trying to achieve a perspective view for a rectangular plane in the X-Z axis like below.
The code below almost achieves that.
What am i doing wrong? I find the documentation to be lacking/confusing.
#include <GL/glut.h>
const int RED_COLOR[3] = {255, 0, 0};
// color to draw in
void setDrawColor(const int decimalCodeRGB[3]) {
float denominator = 255.0;
//convert to float
float r = decimalCodeRGB[0] / denominator;
float g = decimalCodeRGB[1] / denominator;
float b = decimalCodeRGB[2] / denominator;
glColor3f(r, g, b); // set draw color
}
void drawPlane() {
GLfloat A[3] = { -1, 0, 1 };
GLfloat B[3] = { 1, 0, 1 };
GLfloat C[3] = { 1, 0, -1 };
GLfloat D[3] = { -1, 0, -1 };
glBegin(GL_POLYGON);
glVertex3fv(A);
glVertex3fv(B);
glVertex3fv(C);
glVertex3fv(D);
glEnd();
}
GLfloat CamX = 0, CamY = 2, CamZ = 1;
// Display Call Back
void draw() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear all drawings in buffer
glLoadIdentity();
gluLookAt(CamX, CamY, CamZ, 0, 0, 0, 0, 1, 0);
drawPlane();
glutSwapBuffers();// Render Now i.e convert Buffer to Picture
return;
}
// Initialization
void initialize()
{
glClearColor(0.1f, 0.1f, 0.1f, 0.1f); // Set Background Color
setDrawColor(RED_COLOR); // sets the drawing color to red
glEnable(GL_DEPTH_TEST); // enable viewing the 3d
glMatrixMode(GL_PROJECTION); // change to perspective projection
glLoadIdentity(); // what does this do?
glFrustum(-1, 1, -1, 1, 2, 10); // what does this do?
glMatrixMode(GL_MODELVIEW); // what does this do?
}
// Main
int main(int argc, char* argv[])
{
glutInit(&argc, argv); // Initialize GLUT
int x = 512, y = 512; // x and y value
glutInitWindowPosition(
(int)(glutGet(GLUT_SCREEN_WIDTH) - x) / 2,
(int)(glutGet(GLUT_SCREEN_HEIGHT) - y) / 2); // Position the window's center
glutInitWindowSize(x, y); // Set the window's initial width & height
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); // initialise the buffers needed. double buff, one for RGB color the other for x,y, z
glutCreateWindow("3D Bowling Game"); // Create a window with the given title
initialize(); // Custom initialisation
glutDisplayFunc(draw); // Register display callback handler for window re-paint
glutMainLoop(); // Enter the event-processing loop
return 0;
}
// End
How do I change the camera to 45 degrees to Z axis?
Am i calling the functions in the right Order?

adding mouse event to change the color of simple circle [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I draw a circle and trying to add mouse event, that change the color of the circle i cannot find good sources
Here is the whole code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define window_width 1080
#define window_height 720
void drawFilledSun() {
//static float angle;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0, 0, -10);
int i, x, y;
double radius = 0.30;
//glColor3ub(253, 184, 19);
glColor3ub(255, 0, 0);
double twicePi = 2.0 * 3.142;
x = 0, y = 0;
glBegin(GL_TRIANGLE_FAN); //BEGIN CIRCLE
glVertex2f(x, y); // center of circle
for (i = 0; i <= 20; i++) {
glVertex2f(
(x + (radius * cos(i * twicePi / 20))), (y + (radius * sin(i * twicePi / 20)))
);
}
glEnd(); //END
}
void main_loop_function() {
int c;
drawFilledSun();
glutSwapBuffers();
c = getchar();
}
void GL_Setup(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glEnable(GL_DEPTH_TEST);
gluPerspective(45, (float)width / height, .1, 100);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitWindowSize(window_width, window_height);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("GLUT Example!!!");
glutIdleFunc(main_loop_function);
GL_Setup(window_width, window_height);
glutMainLoop();
}
Add an array for the red green and blue color components of the sun and a color index:
int color_i = 0;
GLubyte color[][3] = { {255, 0, 0 }, {253, 184, 19} };
Use the color arrays to set the color attribute
void drawFilledSun() {
// [...]
glColor3ubv(color[color_i]);
Get rid of the getchar in main_loop_function, it prevents the window from responding. But call glutPostRedisplay for continuously updating the window:
void main_loop_function() {
drawFilledSun();
glutSwapBuffers();
glutPostRedisplay();
}
Use glutDisplayFunc rather than glutIdleFunc and add a glutMouseFunc callback:
int main(int argc, char** argv) {
// [...]
glutDisplayFunc(main_loop_function);
glutMouseFunc( mouseFunc );
Change the color index in the mouse callback:
void mouseFunc(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
color_i = (color_i == 0) ? 1 : 0;
}
}
See the example:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define window_width 1080
#define window_height 720
int color_i = 0;
GLubyte color[][3] = { {255, 0, 0 }, {253, 184, 19} };
void drawFilledSun() {
//static float angle;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0, 0, -10);
int i, x, y;
double radius = 0.30;
glColor3ubv(color[color_i]);
double twicePi = 2.0 * 3.142;
x = 0, y = 0;
glBegin(GL_TRIANGLE_FAN); //BEGIN CIRCLE
glVertex2f(x, y); // center of circle
for (i = 0; i <= 20; i++) {
glVertex2f(
(x + (radius * cos(i * twicePi / 20))), (y + (radius * sin(i * twicePi / 20)))
);
}
glEnd(); //END
}
void main_loop_function() {
drawFilledSun();
glutSwapBuffers();
glutPostRedisplay();
}
void GL_Setup(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glEnable(GL_DEPTH_TEST);
gluPerspective(45, (float)width / height, .1, 100);
glMatrixMode(GL_MODELVIEW);
}
void mouseFunc(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
color_i = (color_i == 0) ? 1 : 0;
}
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitWindowSize(window_width, window_height);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("GLUT Example!!!");
glutDisplayFunc(main_loop_function);
glutMouseFunc( mouseFunc );
GL_Setup(window_width, window_height);
glutMainLoop();
}
GLFW provided some callback functions which you need such as glfwSetCursorPosCallback.
In this case you can simply check it by a if branch while drawing, main_loop_function in your code. The rest work is about C/C++, and there are many paths to reach it such as kbhit.
By the way, it's not elegant to ues several naming styles.

How to correctly translate mouse coords to opengl coords?

I need to make a game for a project on college. Everything works fine, the only problem is translating mouse coords (0,0 in top left corner) to openGL coords (0,0 in center of the screen).
In class we got the formula for doing this:
double openglX = ((double)x - [half horizontal res]) / [horizontal res] * [horizontal opengl range];
double openglY = -((double)y - [half vertical res]) / [vertical res] * [vertical opengl range];
#include <iostream>
#include <list>
#include <vector>
#include <cstdlib>
#include <ctime>
#define NDEBUG
#include <GL/freeglut.h>
const int GAME_LOGIC_REFRESH_TIME = 10;
/* GLUT callback Handlers */
void resize(int width, int height)
{
const float ar = (float)width / (float)height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
gluLookAt(0, 0, 45, 0, 0, 0, 0, 1, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void gameLogic(int value)
{
if (value == 0)
{
}
else if(value==1)
{
}
}
void move_ship(int x, int y)
{
double openglX = ((double)x - 600) / 1200 * 46;
double openglY = -((double)y - 500) / 1000 * 38;
player.SetPosition(openglX);
}
/* helper functions for settings options and parameters */
void InitGLUTScene(const char* window_name)
{
glutInitWindowSize(1200, 1000);
glutInitWindowPosition(40, 40);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
glutCreateWindow(window_name);
glClearColor(0, 0, 0, 0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
}
void SetCallbackFunctions()
{
glutReshapeFunc(resize);
glutDisplayFunc(display);
glutIdleFunc(idle);
glutTimerFunc(GAME_LOGIC_REFRESH_TIME, gameLogic, 0);
glutMouseFunc(mouse);
glutPassiveMotionFunc(move_ship);
}
void SetObjectsPositions()
{
}
int main(int argc, char *argv[])
{
srand(time(NULL));
glutInit(&argc, argv);
InitGLUTScene("Space Invaders");
SetCallbackFunctions();
SetObjectsPositions();
glutMainLoop();
return 0;
}
Calculations in move_ship are quite good close to center of the screen, but at the edges they are off enough, that it's a big problem and the player can't reach the edge of the screen.
The first thing I notice is you have some magical numbers in your method move_ship() . Your callback resize() is able to change the width and height, so using constants in move_ship() seems like a bug to me.
The formula should be
void mouseToGl(
double& x, double& y, // output GL coords
double mx, double my, // mouse coords in pixels (0,0 is top left)
double W, double H) // screen dimension width, height
{
x = 2.0 * (mx / W) - 1.0;
y = 2.0 * ((my - W + 1) / W) - 1.0;
}

How to correctly translate objects independently of one another?

I am attempting to translate one circle independent of a separate, stationary circle, utilizing glTranslatef();. However, with my current, full code, each of my circles remains immobile. To investigate why this may be so, I have researched several answers, each comparable to those found here and here. Additionally, I read up on glLoadIdentity as well as the differences between GL_MODELVIEW and GL_PROJECTION, just to see if their details would offer any further clarification. I've also consulted the OpenGL API for the proper definitions of each of the above.
In the style of these solutions, I produced the following do...while loop:
do{
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, fb_width, fb_height, 0, 0, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(0,1,0);
drawCircle(1280 * 0.50, 720 * 0.25,e[2]);
glPopMatrix();
glPushMatrix();
glTranslatef(0,0,0);
drawTarget(1280 * 0.50, 720 * 0.75,50);
glPopMatrix();
glfwSwapBuffers(w);
glfwPollEvents();
}
while (!glfwWindowShouldClose(w));
In this snippet, the drawCircle drawing remains stationary, but I would like for it to follow the written glTranslatef(0,1,0) instead. Is the stationary nature of the circle due to misplaced a glMatrixMode or glLoadIdentity, or perhaps due to the fact that they are being called within the do...while loop and the proper matrix is never really being utilized? I would appreciate any guidance you may have as to why the aforementioned and accepted answers are not functioning quite as well within my program.
For the sake of full transparency, here is the entirety of my code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include <math.h>
#include <stddef.h>
#include <stdbool.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
GLFWwindow *w;
int fb_width, fb_height;
static void error(int error, const char *desc)
{
fputs(desc, stderr);
}
static void key_callback(GLFWwindow *w, int key, int scancode, int action, int mods)
{
if ((key == GLFW_KEY_ESCAPE || key == GLFW_KEY_Q) && action == GLFW_PRESS)
glfwSetWindowShouldClose(w, GL_TRUE);
}
void drawCircle(float cx, float cy, float radius)
{
float num_segments = 360;
float theta = 2 * 3.1415926 / num_segments;
float c = cosf(theta);//precalculate the sine and cosine
float s = sinf(theta);
float t;
float x = radius;//we start at angle = 0
float y = 0;
glBegin(GL_TRIANGLE_FAN);
glColor3f(1, 0, 1);
for(int ii = 0; ii < num_segments; ii++)
{
glVertex2f(x + cx, y + cy);//output vertex
//apply the rotation matrix
t = x;
x = c * x - s * y;
y = s * t + c * y;
}
glEnd();
}
void drawTarget(float cx, float cy, float radius)
{
float num_segments = 360;
float theta = 2 * 3.1415926 / num_segments;
float c = cosf(theta);//precalculate the sine and cosine
float s = sinf(theta);
float t;
float x = radius;//we start at angle = 0
float y = 0;
glBegin(GL_LINE_LOOP);
glColor3f(1, 1, 1);
for(int ii = 0; ii < num_segments; ii++)
{
glVertex2f(x + cx, y + cy);//output vertex
//apply the rotation matrix
t = x;
x = c * x - s * y;
y = s * t + c * y;
}
glEnd();
}
int main(void)
{
int i;
float e[3] = {140,120,100};
float m[3] = {90,80,70};
float h[3] = {60,50,40};
glfwSetErrorCallback(error);
if (!glfwInit())
exit(EXIT_FAILURE);
w = glfwCreateWindow(1280, 720, "AxTest", NULL, NULL);
if (!w)
{
glfwTerminate();
return 1;
}
glfwMakeContextCurrent(w);
glfwSetKeyCallback(w, key_callback);
glfwGetFramebufferSize(w, &fb_width, &fb_height);
do{
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, fb_width, fb_height, 0, 0, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(0,1,0);
drawCircle(1280 * 0.50, 720 * 0.25,e[2]);
glPopMatrix();
glPushMatrix();
glTranslatef(0,0,0);
drawTarget(1280 * 0.50, 720 * 0.75,50);
glPopMatrix();
glfwSwapBuffers(w);
glfwPollEvents();
}
while (!glfwWindowShouldClose(w));
glfwDestroyWindow(w);
glfwTerminate();
exit(EXIT_SUCCESS);
return 0;
}
The values for the vertex positions with which you draw your circles are in the order of hundreds (likely, because you want to address pixels as indicated by the values for the projection matrix). But glTranslates sees only a small number, so the shift is miniscule (one pixel) and hence you think nothing did happen. If you rewrite your code so that you don't specify the circle/target center by explicit modification of the vertex position offset it'd be clearer.
void drawCircle(float radius)
{
/* ... */
for(int ii = 0; ii < num_segments; ii++)
{
glVertex2f(x, y); // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/* ... */
}
void drawTarget(float radius)
{
/* ... */
for(int ii = 0; ii < num_segments; ii++)
{
glVertex2f(x, y); // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/* ... */
}
int main(void)
{
/* ... */
glPushMatrix();
glTranslatef(1280*0.50, 720*0.25, 0);
drawCircle(e[2]);
glPopMatrix();
glPushMatrix();
glTranslatef(1280 * 0.50, 720 * 0.25, 0);
drawTarget(50);
glPopMatrix();
/* ... */
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, fb_width, fb_height, 0, 0, 1);
You don't have to make the projection matrix at every loop, put it before the loop.
Then the error you have is surely due to :
glMatrixMode(GL_MODELVIEW);
// it miss glLoadIdentity() here
glPushMatrix();
glTranslatef(0,1,0);

Drawing shape using opengl

how can i draw shape like (sin) if i describe the shape like half circle closed and the other half circle connected with the first one. using Cartesian method this is my trying:
#include <windows.h>
#include <gl/Gl.h>
#include <gl/glut.h>
#include<math.h>
#include<cstdlib>
static void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT); // clear the screen
glColor3f(1.0,0.0,0.0);
glColor3f(0.0,0.0,0.0);
glBegin(GL_LINE_LOOP);
double xc=200, yc=200,r=100;
double x,y;
for (x = xc - r; x<= xc + r;x++)
{
y = sqrt((r*r)-((xc - x)*(xc - x)));
glVertex2d(x, yc + y);
}
for (x = xc +r ; x<= xc - r ; x++)
{
y = sqrt((r*r)-((xc - x)*(xc - x)));
glVertex2d(x , yc - y);
}
glEnd();
glFlush();
}
void myInit(void)
{
glClearColor(1.0,1.0,1.0,0.0); // set white background color
glColor3f(0.0f, 0.0f, 0.0f); // set the drawing color
glPointSize(4.0); // a ‘dot’ is 4 by 4 pixels
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
void main(int argc, char** argv)
{
glutInit(&argc, argv); // initialize the toolkit
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set display mode
glutInitWindowSize(640,480); // set window size
glutInitWindowPosition(100, 150); // set window position on screen
glutCreateWindow("Line Scan Conversion"); // open the screen window
glutDisplayFunc(myDisplay); // register redraw function
myInit();
glutMainLoop(); // go into a perpetual loop
}
Just sample the function (sin()) directly:
#include <GL/glut.h>
#include <cmath>
static void myDisplay()
{
glClearColor( 0, 0, 0, 1 ); // set white background color
glClear(GL_COLOR_BUFFER_BIT); // clear the screen
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho( -0.5, 7, -1.2, 1.2, -1, 1 );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// axes
glColor3ub( 255, 255, 255 );
glBegin( GL_LINES );
glVertex2i( 0, 0 );
glVertex2i( 7, 0 );
glVertex2i( 0, -1 );
glVertex2i( 0, 1 );
glEnd();
// sin() function plot
glColor3ub( 255, 0, 0 );
glBegin(GL_LINE_STRIP);
const unsigned int samples = 100;
for( unsigned int i = 0; i <= samples; ++i )
{
const float pct = ( (float)i / samples );
const float x = 2 * 3.14159 * pct;
const float y = sin( x );
glVertex2f( x, y );
}
glEnd();
glutSwapBuffers();
}
void main(int argc, char** argv)
{
glutInit(&argc, argv); // initialize the toolkit
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); // set display mode
glutInitWindowSize(600, 600); // set window size
glutCreateWindow("Line Scan Conversion"); // open the screen window
glutDisplayFunc(myDisplay); // register redraw function
glutMainLoop(); // go into a perpetual loop
}
There is an issue in your second for loop:
for( x = xc + r; x <= xc - r; x++ )
this is being executed as x = 400; x <= 0; x++ which clearly will never run. Your issue (other than the obvious pointed out above) is that you are trying to account for the x offset in the loop iterations. I suggest changing to the following:
for( x = 0; x < 2r; x++ )
and then instead of
glVertex2d(x, yc + y);
glVertex2d(x, yc - y);
do
glVertex2d((xc - r) + x, yc + y);
glVertex2d((xc + r) + x, yc - y);
or something similar (not in a position to actual test this, but you should get the idea).