First try - graphical program - c++

I used a book called Computer Graphices using OpenGL.
at page number 51 i found this code
#include <windows.h>
#include "glut.h"
//<<<<<<<<<<<<<<<<< method(s) >>>>>>>>>>>>>>>>>>>
void My_Display(void);
void My_Inti(void);
//<<<<<<<<<<<<<<<<< main method >>>>>>>>>>>>>>>>>>
int main(int argc, char ** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 150);
glutCreateWindow("my second try ");
glutDisplayFunc(My_Display);
My_Inti();
glutMainLoop();
return 0;
}
//<<<<<<<<<<<<<<<<<<<<<<<< IMPLEMENTING METHOD(S) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.
//<<<<<<<<<<<<<<<< My_Inti >>>>>>>>>>>>>>>>>>>>>>>>>
void My_Inti(void)
{
glClearColor(1, 1, 1, 0); // white color
glColor3f(0, 0, 0); // Black color
glPointSize(10); // point size is 10 pixel this is big .
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 640, 0, 480);
}
//<<<<<<<<<<<<<<<< My_Display >>>>>>>>>>>>>>>>>>>>>>
void My_Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINT);
glVertex2i(100, 50);
glVertex2i(100, 130);
glVertex2i(150, 130);
glEnd();
glFlush();
}
all what i add to this code is the comments and i change a little at the variable ; nothing more .
When we come to the problem this code work fine but it didn't creat the three points at the display method ?

The problem is just one missing letter. Instead of this:
glBegin(GL_POINT);
The correct value is:
glBegin(GL_POINTS);
The first thing I would do any time you get no rendering, or not the expected rendering, is to call glGetError(), and see if it returns an error. I admit that I didn't see this problem initially, but calling glGetError() would have pointed it out quickly.
BTW: In case anybody else is surprised that there are both GL_POINT and GL_POINTS enums in OpenGL. GL_POINT is one of the possible arguments to glPolygonMode(), as opposed to GL_POINTS which denotes one of the possible primitive types for draw calls.

It does not work because there is an error in the source code (unlikely) or you mis-copied the argument for glBegin(GL_POINT); (likely).
Using
glBegin(GL_POINTS);
I got this image:

Related

Does the order of a function's declaration matter in Visual Studio C++ 2019 (Unity Version)?

I am learning C++ coming from a Java/C#/Lua/Python background.
I decided to learn the language by writing some small games using the OpenGL package (I'm also writing some text parsers and re-writing a scripting language I wrote in C#, in C++ to the same ends). I have the following test code I am using to set up my project:
// OpenGLExample01.cpp : This file contains the 'main' function. Program execution begins and ends there.
#include <iostream>
#ifdef __APPLE_CC__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
extern "C"
{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
// Initializes GLUT, the display mode, and main window; registers callbacks;
// enters the main event loop.
int main(int argc, char** argv) {
// Use a single buffered window in RGB mode (as opposed to a double-buffered
// window or color-index mode).
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
// Position window at (80,80)-(480,380) and give it a title.
glutInitWindowPosition(80, 80);
glutInitWindowSize(400, 300);
glutCreateWindow("A Simple Triangle");
// Tell GLUT that whenever the main window needs to be repainted that it
// should call the function display().
glutDisplayFunc(display);
// Tell GLUT to start reading and processing events. This function
// never returns; the program only exits when the user closes the main
// window or kills the process.
glutMainLoop();
}
static void display() {
// Set every pixel in the frame buffer to the current clear color.
glClear(GL_COLOR_BUFFER_BIT);
// Drawing is done by specifying a sequence of vertices. The way these
// vertices are connected (or not connected) depends on the argument to
// glBegin. GL_POLYGON constructs a filled polygon.
glBegin(GL_POLYGON);
glColor3f(1, 0, 0); glVertex3f(-0.6, -0.75, 0.5);
glColor3f(0, 1, 0); glVertex3f(0.6, -0.75, 0);
glColor3f(0, 0, 1); glVertex3f(0, 0.75, 0);
glEnd();
// Flush drawing command buffer to make drawing happen as soon as possible.
glFlush();
}
This code won't compile when I try to build it. The reason? In glutDisplayFunc(display), "display" is undefined. However, if I flip the functions around (i.e. define display() first, then define main()), the program builds without a hitch.
#include <iostream>
#ifdef __APPLE_CC__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
extern "C"
{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
static void display() {
// Set every pixel in the frame buffer to the current clear color.
glClear(GL_COLOR_BUFFER_BIT);
// Drawing is done by specifying a sequence of vertices. The way these
// vertices are connected (or not connected) depends on the argument to
// glBegin. GL_POLYGON constructs a filled polygon.
glBegin(GL_POLYGON);
glColor3f(1, 0, 0); glVertex3f(-0.6, -0.75, 0.5);
glColor3f(0, 1, 0); glVertex3f(0.6, -0.75, 0);
glColor3f(0, 0, 1); glVertex3f(0, 0.75, 0);
glEnd();
// Flush drawing command buffer to make drawing happen as soon as possible.
glFlush();
}
// Initializes GLUT, the display mode, and main window; registers callbacks;
// enters the main event loop.
int main(int argc, char** argv) {
// Use a single buffered window in RGB mode (as opposed to a double-buffered
// window or color-index mode).
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
// Position window at (80,80)-(480,380) and give it a title.
glutInitWindowPosition(80, 80);
glutInitWindowSize(400, 300);
glutCreateWindow("A Simple Triangle");
// Tell GLUT that whenever the main window needs to be repainted that it
// should call the function display().
glutDisplayFunc(display);
// Tell GLUT to start reading and processing events. This function
// never returns; the program only exits when the user closes the main
// window or kills the process.
glutMainLoop();
}
Here's the issue: Everything I've read thus far suggests that the first code snippet should be functionally the same as the second snippet. What's going on here?
yes, in order to use it, the compiler need to see the declaration.
Only the declaration is needed though, not necessary the definition.
static void bar(); // declaration
void foo(){
bar();
}
static void bar(){ // function body here (definition)
/*do something*/
};

Translate a triangle using animation(one by one after some interval) with opengl and c++

I want to make a space invader game in opengl. So I thought of creating the enemies using triangles. Before making the game, I want to try out my hand in animation. I have triangle. i want to translate it left upto some point with animation(i.e, triangle is translated after some interval of time. It should look as if it is moving).After reaching some point in the left, I want to translate it back to the same position or some distances right. The process should go on till the screen is open. I used sleep function. But it is not working. No animation is shown. Only the translated triangle is drawn at different translated position. Help me.
Here is my code-
#include "windows.h"
#include <gl/glut.h>
#include<stdio.h>
void init( void )
{
printf( "OpenGL version: %s\n", (char*)glGetString(GL_VERSION));
printf( "OpenGL renderer: %s\n", (char*)glGetString(GL_RENDERER));
//Configure basic OpenGL settings
glClearColor(1.0, 1.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,640.0,0.0,480.0);
glColor3f(1.0,0.0,0.0);
glPointSize(3);
}
void house(int x, int y,int z)
{
glPushMatrix();
glTranslatef(x, y,z);
glBegin (GL_LINES);
glVertex2i (0,30);
glVertex2i (15,60);
glVertex2i (15,60);
glVertex2i (30,30);
glVertex2i (30,30);
glVertex2i (0,30);
glEnd();
//Sleep(200);
glPopMatrix();
//glutSwapBuffers();
}
// Main drawing routine. Called repeatedly by GLUT's main loop
void display( void )
{
//Clear the screen and set our initial view matrix
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
int i;
for(i = 10; i < 350; i = i + 50)
{
house(i,20,0);
Sleep(200);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glFlush();
}
// Entry point - GLUT setup and initialization
int main( int argc, char** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode (GLUT_DEPTH | GLUT_SINGLE| GLUT_RGB);
glutInitWindowSize (800, 600);
glutInitWindowPosition (100, 100);
glutCreateWindow( "OpenGL Test" );
glutDisplayFunc( display );
init();
glutMainLoop();
return 0;
}
In main() you have declared your display() as display callback function. GLUT will call this function either when it determines that the window need to be redrawn or when it is told to redraw it for example by the function glutPostRedisplay().
The display function is expected to call redraw the windows at a specific point in time. The glFlush() will force the execution of the GL commands.
The problem is that your animation loop is inside the redraw function and glFlush() is called at the end, showing the result at once. And you don't tell GLUT to redraw the windows. This is why you don't seee the animation.
For the purpose of the tutorial, I propose you to define a global variable for the initial position of the house drawing. Of course, you'll have to improve this as soon as you understood how all this works.
static int pos = 10; // temporary work around, just for the demo
Then define a timer function, that gets called after a time interval. This will be the core of your animation, organizing the moving, and the redrawing of the window by calling glutPostRedisplay() :
void timerfunc(int value) // handle animation
{
pos += 50; // update the postion
if (pos<350) // as in your originial loop
glutTimerFunc(200, timerfunc, 0); // plan next occurence
glutPostRedisplay(); // redraw the window
}
Activate your timer function, in main() just before launching glutMainLoop():
glutTimerFunc(200, timerfunc, 0); // call a timer function
glutMainLoop(); // this call is already in your code
Your display function can then be changed into:
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
house(pos, 20, 0); // draw the house at its last calculated position
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glFlush();
}
Then it works in animated modus !

Glut Open GL object wont animate

My polygon wont move, I tried many things and i think glClear(GL_COLOR_BUFFER_BIT);or glutMainLoop(); wont do their job. So first picture stay the same. There is no animation.
float x=0;
float y=0;
float b=0;
void displayCB(void)
{
glClear(GL_COLOR_BUFFER_BIT);
kvadrat();
}
void kvadrat()
{
glBegin(GL_POLYGON);
glColor3f(1, 0, 0); glVertex2d(0.5-x, 0.5-y);
glColor3f(1, 0, 0); glVertex2d(0.5-x, -0.5-y);
glColor3f(1, 0, 0); glVertex2d(-0.5-x, -0.5-y);
glColor3f(1, 0, 0); glVertex2d(-0.5-x, 0.5-y);
glEnd();
Sleep(1999);
glFlush();
x=x+0.01; // I modified this value so it will always be between 0.5 and -0.5,
//this is just example
}
int main(int argc, char *argv[])
{
int win;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(800,600);
win = glutCreateWindow("Elementi");
glClearColor(0.0,0.0,0.0,0.0);
glutDisplayFunc(displayCB);
glutKeyboardFunc(keyCB);
glutMainLoop();
return 0;
}
I think you may be moving the position only a very small increment every two seconds, so the image appears to be static, but it's not, it's just changing very slowly. Try taking out the Sleep(1999) and see whether your animation works any better.
If you need to animate over time, it is better to instead use glutGet(GLUT_ELAPSED_TIME) to figure out how much time has elapsed since the last frame and use that to scale your deltas.
I fixed problem with glutPostRedisplay(); // Inform GLUT that the display has changed

simple graphic pipeline in OpenGL

I am reading Schaum's outlines COMPUTER GRAPHICS. Book says that a simple graphic pipeline is something like this: geometric representation --> transformation --> scan conversion
(though the author has decided to teach scan conversion chapter before transformation chapter). I wish to learn this simple pipeline through an example in openGL. suppose I wish to create a line with end coordinates (150,400) and (700,100) in window of size (750,500). Below code works very well. All I am asking to experts is to explain the 'steps in sequence' when is transformation happening and when scan conversion. I know it may sound stupid but I really need to get this straight. I am just an adult beginner learning graphics at my own as a hobby.
My guess is that scan conversion is not happening here in program. it is done by openGL automatically between glBegin and glEnd calls. Am I right?
#include <GL/glut.h>
void init(void)
{
glClearColor (0.5, 0.2, 0.3, 0.0);
glClear (GL_COLOR_BUFFER_BIT);
glColor4f(0.5,0.7,0.3,0.0);
glLineWidth(3);
}
void display(void)
{
glBegin(GL_LINES);
glVertex2i(50, 400);
glVertex2i(700, 100);
glEnd();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);
}
int main (int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_RGBA | GLUT_DEPTH);
glutInitWindowPosition(100,150);
glutInitWindowSize(750,500); // aspect ratio of 3/2
glutCreateWindow (argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop(); // this is when the frame buffer is displayed on the screen
return (0);
}
All stages done within OpenGL implementation (mostly in hardware). You specify states and data, then GL will - if speaking in terms of old GL 1.0 - assemble data into vertices, pass every vertex through transformation stage, rasterize resulting primitives into fragments, perform per-fragment tests (that may discard some fragments), and update resulting pixels on render target.
There is no point in user code that may be on 'one stage' in pipeline - it doesn't have callbacks, and usually as many as possible stages working at the same time.

What's going on with my program (openGL in c++)?

I am starting with openGL and c++, and I was wondering why I don't see anything on the window. Here is my code:
#include <GLUT/GLUT.h>
#include <stdlib.h>
void init() {
glClearColor(0, 0, 1, 0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, 1.0, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, -10);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex3i(-0.5, -0.5, 0);
glVertex3i(0, 0.5, 0);
glVertex3i(0.5, -0.5, 0);
glEnd();
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE| GLUT_RGBA);
glutInitWindowPosition(200, 200);
glutInitWindowSize(400, 400);
glutCreateWindow("Window");
init();
glutDisplayFunc(display);
glutMainLoop();
}
I have a few questions:
If I run the program like this all I see is a white window... Didn't I set the color to blue?
When I do glutSwapBuffers() at the end of display function and run the program, I see the blue window without the triangle. So, I thought glutSwapBuffers() function only worked with double buffering.
And the most important, where the hell is my triangle? O.o Didn't I translate the camera with glTranslatf() function to -10 in the z-axes? If you are wondering why I used gluPerspective, I have to say that I am trying out new things, but neither works if I use gluOrtho2D().
I don't know if I am missing something or what. Maybe I need to search more information about this, but I think most of the code is correct.
1 & 2) Well you don't have to call glutSwapBuffers() when using single buffer. But you have to call glFlush(), so the draw commands are executed on the GPU.
3) I noticed that you are creating vertices with double coordinates, but you are calling integer version of glVertex** function (decimal part will be truncated) - it means that you will be drawing triangle with zero size.
Use glVertex3d() or glVertex3f() instead of glVertex3i().
Small note: intermediate mode is deprecated in the latest OpenGL.