I am novice to OpenGL and I read that glutWireCubedraws a wire cube. Now that it is not appearing on when I run my code, I am wondering what does it do? Does it draw a cube or where have I gone wrong in my code?
#include<GL/glut.h>
GLdouble cubeSize= 10.0;
//FUNCTIONS DECLARATIONS - PROTOTYPES
void init(void);
void display(void);
int main(int argc, char ** argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE |GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(500,500);
glutCreateWindow("Wire Cube");
init();
glutDisplayFunc(display);
glutMainLoop();
}
//FUNCTIONS IMPLEMENTAION - DEFINITION
void init(void){
glClearColor(0.0,0.0,1.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
}
void display(void){
glBegin(GL_POLYGON);
glutWireCube(5.0);
glEnd();
glFlush();
}
Calling glutWireCube is not allowed inside a glBegin/glEnd block. Only glVertex to specify vertices and all the functions to set the current values of some vertex attributes (like glNormal, glColor, ...) can be used there.
How glutWireCube internally works is not specified. It might as well use immediate mode, but in this case, it will do its own glBegin/glEnd calls.
Conceptually, trying to put a cube into a GL_POLYGON is also not going to work. GL_POLYGON is for drawing a single, flat, convex polygon, and it is totally impossible to draw a whole cube as one polygon.
Furthermore, you do not set up any of the GL_MODELVIEW or GL_PROJECTION matrices. This means you directly draw in clip space, and glutWireCube with size 5 will draw a cube which completely lies outside of your viewing frustum, so you will see nothing.
Related
I'm new to openGL, I need help with animate a triangle that rotates 1 degree every 25 milliseconds. I want to program this triangle to gradually change color from blue to green to red.
float rAngle=0.0;
void handleResize(int w,int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,(double)w/(double)h,1.0,200.0);
}
void drawscene()
{
glColor3f(0.0f,1.0f,0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(-rAngle,0.0f,0.0f,0.1f);
glBegin(GL_TRIANGLES);
glVertex3f(-0.5f,0.5f,-5.0f);
glVertex3f(-1.0f,1.5f,-5.0f);
glVertex3f(-1.5f,0.5f,-5.0f);
glEnd();
glFlush();
}
void update(int value)
{
rAngle+=1.0f;
glutPostRedisplay();
glutTimerFunc(25,update,0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("OpenGL");
glutDisplayFunc(drawscene);
glutReshapeFunc(handleResize);
glutTimerFunc(25,update,0);
glutMainLoop();
return 0;
}
This way of using OpenGL is deprecated.
Let me start off by pointing out that there is no reason to dabble with old outdated OpenGL. Immediate mode has been deprecated for about 10 years now.
Learn modern OpenGL instead.
Although the question is very unclear, I will try to give you the info you need.
Push/Pop Matrix:
glPushMatrix():
There is a stack of matrices for each of the matrix modes. In GL_MODELVIEW mode, the stack depth is at least 32. In the other modes, GL_COLOR, GL_PROJECTION, and GL_TEXTURE, the depth is at least 2. The current matrix in any mode is the matrix on the top of the stack for that mode.
glPushMatrix pushes the current matrix stack down by one, duplicating the current matrix. That is, after a glPushMatrix call, the matrix on top of the stack is identical to the one below it.
glPopMatrix pops the current matrix stack, replacing the current matrix with the one below it on the stack.
Initially, each of the stacks contains one matrix, an identity matrix.
Rotation:
To rotate the current matrix, call glRotate().
Color:
Use these to select rendering color.
Usually, glColor3f() or glColor4f() is used.
Your scenario:
Call glColor3f() or glColor4f() to select rendering color.
Call glPushMatrix() to avoid rotating everything.
Call glRotate() to rotate the matrix.
Render mesh.
Call glPopMatrix() to revert out of the rotated matrix.
To modify per time unit:
Just modify the data sent into the GL functions over time. Increase the rotation/color values a bit each frame.
I suggest reading up on matrix math, and how graphics incorporates this.
I am quite newbie in OpenGL and all my knowledge comes from tutorials in the Internet. For now I can program rotating cube lighted by point source. My new goal is to implement antialiasing - possibly without any magical techniques like a hypothetical glEnable(Smooth_Lines) function. The method I'm using currently does not work.
My first attempt was to try glAccum function and implement jittering anti-aliasing - I don't know if this is not possible on my Radeon graphic card or I just wrote something wrong.
My second attempt is to use glGenFrameBuffers (and rest of family). But now (after ~10 hours of research) I just don't have any strength to continue. Can you tell me where I should implement antialiasing process? Or which functions should I use? Here is cutted version of my whole code:
class Window;
static Window* ptr;
class Window{
GLuint vbo[4],program;
GLint att[5],mvp,mvp2;
void init_res(){
a0=glutGet(GLUT_ELAPSED_TIME);
GLfloat cube[]= ... something
GLfloat colors[]= ... still something
GLushort elements[]= ... oh you can just guess how it should look :)
//now I am binding buffers and send cube data to shaders
program=glCreateProgram();
//little more code here
}
void Draw(){
// here is subfunction for drawing all cube.
}
void onDisplay(){
//here is main function which is send to glutDisplayFunc
Draw();
//should I add something here?
glutSwapBuffers();
}
Window(int s_w,int s_h){
glutInit(&ac, av);
glutInitDisplayMode(GLUT_RGBA|GLUT_ALPHA|GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowSize(s_w, s_h);
glutCreateWindow("My Rotating Cube");
glewInit();
init_res();
ptr=this;
glutDisplayFunc(Display);
glutReshapeFunc(Reshape);
glutIdleFunc(Idle);
glutKeyboardFunc(Key);
glEnable(GL_LINE_SMOOTH_HINT);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_POLYGON_SMOOTH);
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
glEnable(GL_BLEND);
glHint(GL_POLYGON_SMOOTH_HINT,GL_NICEST);
glEnable(GL_DEPTH_TEST);
glEnable(GL_MULTISAMPLE);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//here is a lot of garbage - you can call it 'voodoo programming'
glutMainLoop();
free_resources();
}
};
int main(int argc, char** argv){
ac=argc;
av=argv;
Window *okno;
okno = new Window(800,600);
}
For now, everything in this program works (well, before cuts of course :P ). I did not attach shaders - they are as simple as it is possible (with lighting and added cube borders).
Any ideas how to implement anti-aliasing here?
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.
I am trying to simply draw a triangle in a window. I've drawn shapes before in previous code, and have looked up common issues such as failure to flush or not clearing the color buffer.
No matter what I seem to try though, I can't get anything to draw on screen, even after I've simplified my code to basically look exactly like my previous (working!) code. All I have is a main and a render:
// Declarations //
void Render(void); //Call the drawing functions
int main(int argc, char *argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(20,20);
glutCreateWindow("Triangle Test");
//prepare for drawing
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
//now draw
glutDisplayFunc(Render);
glutMainLoop();
}
// ---- Render Function ----
void Render(void)
{
// Draw a triangle
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_LINE_STRIP);
glVertex2f(100.0f, 20.0f);
glVertex2f(0.0f, 20.0f);
glVertex2f(20.0f, 50.0f);
glEnd();
glFlush();
}
On run, it draws a window with the background color I set (in this case black) and nothing else. I'm completely stumped. All of the other questions on stack seem to be resolved by things I have in here (i.e. glFlush) and its virtually identical to my old code, which draws fine. Any ideas?
You're drawing a line strip that's bigger than your window. You need to either set your matrices so you see a larger area, draw a smaller polygon, or draw a filled polygon by drawing a triangle instead of a line strip.
I have written a simple openGL program in C++. This program draws a sphere in 3D perspective projection and tries to draw a line joining the center of the sphere to the current cursor position in 2D orthographic projection. Now for drawing the line I can't figure out the coordinate of center of the sphere.
This is my code :
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
void passive(int,int);
void reshape(int,int);
void init(void);
void display(void);
void camera(void);
int cursorX,cursorY,width,height;
int main (int argc,char **argv) {
glutInit (&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
glutInitWindowSize(1364,689);
glutInitWindowPosition(0,0);
glutCreateWindow("Sample");
init();
glutDisplayFunc(display);
glutIdleFunc(display);
glutPassiveMotionFunc(passive);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
void display() {
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Render 3D content
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60,(GLfloat)width/(GLfloat)height,1.0,100.0); // create 3D perspective projection matrix
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
camera();
glTranslatef(-6,-2,0);
glColor3f(1,0,0);
glutSolidSphere(5,50,50);
glPopMatrix();
// Render 2D content
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width,height, 0); // create 2D orthographic projection matrix
glMatrixMode(GL_MODELVIEW);
glColor3f(1,1,1);
glBegin(GL_LINES);
glVertex2f( centreX,centreY ); // coordinate of center of the sphere in orthographic projection
glVertex2f( cursorX,cursorY );
glEnd();
glutSwapBuffers();
}
void camera(void) {
glRotatef(0.0,1.0,0.0,0.0);
glRotatef(0.0,0.0,1.0,0.0);
glTranslated(0,0,-20);
}
void init(void) {
glEnable (GL_DEPTH_TEST);
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_COLOR_MATERIAL);
}
void reshape(int w, int h) {
width=w; height=h;
}
void passive(int x1,int y1) {
cursorX=x1; cursorY=y1;
}
I can,t figure out the values for centreX and centreY. Anyway I can get the correct values to draw the line?
You may be interested in using something like gluProject to go from your object coordinates to the actual (projected) position on screen. Once you have the screen coordinates of the object, it's easy to draw a line from one point to another.
In this case, you'll want to project the centre point of the sphere. For more complex objects I've found that it makes sense to project all of the corners of the object's bounding box and then take the extents of the screenspace position of those corners.
You should get the modelview, viewport and projection matrices before you switch to your orthographic projection (2D mode).
Obviously, in order to go from a screen position (say, where you clicked in the window) to a world position, you'll want to use its companion function, gluUnProject.
Note that the coordinates that come out of gluProject do not necessarily correspond directly to the window position; you might have to flip the "Y" coordinate.
Take a look at this GDSE discussion for some other ideas about how to solve the problem.