DDA Line Drawing Algorithm has errors - c

Why am I getting an error saying 'setPixel not defined' with this code?
#include <windows.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include<GL/glut.h>
inline int round(const float a)
{
return int (a+0.5);
}
void init(void)
{
glClearColor(0.0f,0.0f,1.0f,1.0f);
gluOrtho2D(0.0,200.0,0.0,200.0);
glMatrixMode(GL_PROJECTION);
}
void LineSegment(int xa, int ya,int xb,int yb)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f,0.0f,0.0f);
printf("Enter the initial value");
scanf("%d%d",&xa,&ya);
printf("Enter the final value");
scanf("%d%d",&xb,&yb);
int dx=xb-xa;
int dy=yb-ya;
int steps,k;
float xIncrement,yIncrement,x=xa,y=ya;
if(fabs(dx)>fabs(dy))
steps=fabs(dx);
else
steps=fabs(dy);
xIncrement=dx/(float)steps;
yIncrement=dy/(float)steps;
setPixel(round(x),round(y));
for(k=0;k<steps;k++);
{
x += xIncrement;
y += yIncrement;
setPixel(round(x),round(y));
}
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA);
glutCreateWindow("DDA Line Algorithm");
glutDisplayFunc(LineSegment);
init();
glutMainLoop();
return 0;
}

Because there is no setPixel method in OpenGL or GLUT and as far as I can see from your code, you do not define one either. OpenGL deals with rendering primitives like points, lines, triangels etc, but not directly with setting single pixels on the screen. Since it is unclear what you want to achieve some suggestions:
If you want to draw a line in OpenGL, use the appropriate methods like glBegin(GL_LINES), etc. (although they are deprecated and should not be used anymore.) or glDrawArrays(GL_LINES, ....
If the goal is to implement a dda software rasterizer, then you might have to write the pixels to a texture and then display this texture.

Because you haven't defined setPixel anywhere. It's not an OpenGL call. You need to write it yourself, and it should set pixels on a buffer (if you're using double buffering) which you then later use as an argument to glDrawPixels(), or a call to the display buffer using glVertex2i(x,y). You can see an example of both approaches here and here.
Also, your LineSegment function is broken. In OpenGL you call glutDisplayFunc to specify a function which is called as fast as possible to render the display. However, in this function you call scanf() to prompt the user for data - this is broken. You should prompt them once at the start, and then pass that data into the function (which will then run as often as possible once glutMainLoop is called).

Related

How do I can call initgraph more than one time?

Please look at the following code:
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
using namespace std;
void drawrect()
{
int gdriver = IBM8514, gmode;
initgraph(&gdriver, &gmode, "");
rectangle(500, 500, 700, 700);
getch();
cleardevice();
closegraph();
}
int main()
{
int f=1;
while(f)
{
char c;
printf("Press \"e\" to end, and any other character to draw a rectangle");
scanf("%c",&c);
c=='e' ? f=0:f=1;
drawrect();
fflush(stdin);
}
}
at the first time when I run this program, It works correctly and draws a rectangle, but after the first time, the rectangle function doesn't work and the GUI screen is completely blank, While I've cleared and closed previous graphic
So why it doesn't work at second time?
You code has undefined behaviour. The call to initgraph
int gdriver = IBM8514, gmode;
initgraph(&gdriver, &gmode, "");
should pass a pointer to the graphics mode you want to use. This page describes the function and its arguments, and about the mode it says:
*graphmode is an integer that specifies the initial graphics mode (unless *graphdriver equals DETECT; in which case, *graphmode is set
by initgraph to the highest resolution available for the detected
driver). You can give *graphmode a value using a constant of the
graphics_modes enumeration type, which is defined in graphics.h and
listed below.
graphdriver and graphmode must be set to valid values from the
following tables, or you will get unpredictable results. The exception
is graphdriver = DETECT.
But you have not set the mode, and as the second paragraph quoted says, the result is unpredictable. This can be: working how you intended, not working, working strangely, or frying the processor.
So set the graphics mode you want to use with say
int gdriver = IBM8514, gmode = 0;
or whatever mode you need to use. Alternatively you can tell the system to detect for itself, in which case you can use
int gdriver = DETECT, gmode;
Init and close should be called just once and not be called in the drawrect but usually in the main instead ... also having getch in rendering routine makes no sense too...
I will not touch other issues here of your code as I am not coding console stuff for years and BGI even longer but I would start with reordering the code to this:
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
using namespace std;
void drawrect()
{
rectangle(500, 500, 700, 700);
}
int main()
{
int f=1;
int gdriver = IBM8514, gmode;
initgraph(&gdriver, &gmode, "");
while(f)
{
char c;
printf("Press \"e\" to end, and any other character to draw a rectangle");
scanf("%c",&c);
c=='e' ? f=0:f=1;
drawrect();
getch();
fflush(stdin);
}
cleardevice();
closegraph();
}
Also in future address the library by its real name BGI because graphics.h has no meaning as almost all gfx api/libs got a file with that name ...

Because when using math it takes time to compile, and it marks me error

I'm learning to use OpenGL, and a few days ago, I wanted to use the library math.h, and at the time of execution, the compiler takes too long, and at the end I mark an error "include nested too deeply".
I do not know what happens, but I wanted to see what the problem was, and every time I include the library math.ho cmath "which is the same", the same problem happens to me.
What I did now was to do a project on the console, something easy, hello world, all good, but I included the library math.h, and again the error occurred.
It's as if that library was damaged, or not.
I would like someone to help me with these issues, since he could not solve it.
#include <iostream>
#include <math.h>
using namespace std;
int main () {
cout << "hello world";
return 0;
}
The normal code (the error):
#include <windows.h> // for MS Windows
#include <GL / glut.h> // GLUT, include glu.h and gl.h
#include <Math.h> // Needed for sin, cos
#define PI 3.14159265f
// Global variables
char title [] = "Full-Screen & Windowed Mode"; // Windowed mode's title
int windowWidth = 640; // Windowed mode's width
int windowHeight = 480; // Windowed mode's height
int windowPosX = 50; // Windowed mode's top-left corner x
int windowPosY = 50; // Windowed mode's top-left corner and
 
... etc., due to space problems.
I will not put all the code, but it is inside the Math.h library when I use it, it always happens the same, it takes time to compile, and I get these errors, and this happens when I add this.
It does not matter if I perform in OpenGl or in console as they see and the error message says "include nested too deeply".

How to use the move function under curses.h

It doesn't print at coordinates y=10, x=20.
#include <stdio.h>
#include <curses.h>
int main()
{
initscr();
refresh();
WINDOW *win;
wmove(win, 10, 20);
refresh();
printf("hi\n");
return 0;
}
When I execute it like this...
./a.out > op_file
This is what is op_file
[?1049h[1;24r(B[m[4l[?7h[H[2J-1
hi
Can someone explain...??
You must use initscr() function to initialize the screen and endwin() at the end to close the window...
If you move(), you must use refresh() or the cursor won't move physically.
To move the cursor to a new position on a window, use the function int wmove(WINDOW *win, int y, int x)
wmove(win, y, x);
where (x, y) are the coordinates of the new position in the window. If the window has nlines lines and ncolumns columns, then
0 <= y < nlines
0 <= x < ncolumns
Refresh. The actual cursor motion is not shown on the screen untill you do a wrefresh(win).
move(y, x) is equivalent to the wmove(stdscr, y, x).`
The move() and wmove() functions move the cursor associated with the current or specified window to (y, x) relative to the window's origin. This function does not move the terminal's cursor until the next refresh operation.
To move the logical cursor in the user-defined window my_window to the coordinates y = 5, x = 10, use :
#include <stdio.h>
#include <curses.h>
int main(){
refresh();//First refresh
WINDOW *my_window;
int a = wmove(my_window, 5, 10);
refresh();////Second refresh
printf("%d\n",a);
printf("hi\n");
return 0;
}
The output
[?1049h[1;24r(B[m[4l[?7h[H[2J-1
hi
shows the printable characters written. If you look at the complete text, e.g., in a text-editor, there'll be ASCII escape characters before the [ and ( characters since that's part of the escape sequence.
Your example doesn't show cursor movement (aside from the home position which you'd see as ^[[H near the end) because there's no reason for the curses library to actually move the cursor. If you had asked it to read a character, e.g., using getch, it would have to stop and decide where the cursor should be — and your wmove would do that — except that win is not initialized. The simplest thing to do is use stdscr (which is initialized by initscr).
The program quits curses calls without doing an endwin (which leaves the terminal in raw mode). The data does get written to the screen with the refresh call. The data written with printf happens to come out in the right order, but that's only coincidental since it does not use the same output buffering as ncurses.
Both of the other answers contain similar errors.
This works.
#include <stdio.h>
#include <curses.h>
int main()
{
initscr();
refresh();
WINDOW *win;
win = stdscr;
wmove(win, 10, 10);
refresh();
printf("hi\n");
return 0;
}
Thanks to #interjay.

How to implement functions that work on turtle graphics in C?

So i've been trying to get this function to draw a red box using turtle graphics but it seems like the function is being ignored. Here is my code:
int main(int agrc, char*argc[])
{
create_turtle_world();
void draw_red();
return (p1world_shutdown());
}
void draw_red()
{
pen_colour(RED);
forward(150);
turn(90);
forward(50);
turn(90);
forward(150)
turn(90);
forward(50);
turn(90);
turn(-90);
}
I don't know what I did wrong here, it's compiled correctly, just not drawing a box.
In main(), instead of
void draw_red(); //function declaration
you've to use
draw_red(); //function call

OpenGL in Ubuntu (C Language) saying things are undeclared

So I just started learning OpenGL and I'm doing so Ubuntu with the C language.
I have done a few examples from my lecturers notes and they worked however this one is giving me errors.
callbackexample.c: In function ‘main’:
callbackexample.c:17:18: error: ‘displays’ undeclared (first use in this function)
callbackexample.c:17:18: note: each undeclared identifier is reported only once for each function it appears in
and so on for every method in my file.
I followed his notes word for word and I'm getting this so I'm not sure whats going wrong.
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#define DEG_TO_RAD 0.017453
int singleb, doubleb; //window ids
GLfloat theta = 0.0;
int main(int argc, char **argv){
glutInit(&argc, argv);
//create single buffered window
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
singleb = glutCreateWindow("single_buffered");
glutDisplayFunc(displays);
glutReshapeFunc(myReshape);
glutIdleFunc(spinDisplay);
glutMouseFunc(mouse);
glutKeyboardFunc(mykey);
//create double buffered window
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(400,0); //create window to the right
doubleb = glutCreateWindow("double_buffered");
glutDisplayFunc(displayd);
glutReshapeFunc(myReshape);
glutIdleFunc(spinDisplay);
glutMouseFunc(mouse);
glutCreateMenu(quit_menu);
glutAddMenuEntry("quit", 1);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop();
}
void displays() {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glVertex2f ( cos(DEG_TO_RAD * theta),
sin(DEG_TO_RAD * theta));
glVertex2f ( -sin(DEG_TO_RAD * theta),
cos(DEG_TO_RAD * theta));
glVertex2f ( -cos(DEG_TO_RAD * theta),
-sin(DEG_TO_RAD * theta));
glVertex2f ( sin(DEG_TO_RAD * theta),
-cos(DEG_TO_RAD * theta));
glEnd();
glFlush();
}
This is just some code of the main method and the first method after it which I get an error on. By undeclared I guess it means that the methods aren't declared but I followed his code so I'm not sure
You try to use the displays method in the main function, before declaring it. You need to either move the entire function before the main function or add the stub to the top:
void displays();
int main(int argc, char **argv){
...
}
void displays(){
...
}
C parses everything in the order it sees it - you cannot use a method that hasn't been declared in its entirety, or at least had a declaration that it will exist at some point.
In regards to your comments:
The cannot find -l* stuff you are getting implies that you either haven't installed the development libraries for OpenGL or have them set up strangely - this is saying that it cannot find the library files to link against.
Additionally, the mykey problem implies that you either haven;t declared a mykey function or are not declaring it as per the prototype:
void mykey(unsigned char key, int x, int y)

Categories

Resources