OpenGL Drawing Axis in Corner - c++

I've been searching on how to draw an Indicator-Axis in my OpenGL scene. The project's nested in a Qt OpenGL widget, but I think the problem is independent of Qt.
I have found on here and forums from years ago that suggest storing the viewport and data, loading new ones for the botttom corner, apply my rotations and draw, then restore the matrices. This seems the most beneficial to me, but I'm guessing I'm still missing some critical info in my OpenGL knowledge.
For now I just have it drawing a red line from -x to x, so I expected to have a red square in the bottom left of the screen:
void GLWidget::drawAxis()
{
float tempPro[16];
float tempMod[16];
glGetFloatv(GL_PROJECTION_MATRIX, &tempPro[0]);
glGetFloatv(GL_MODELVIEW_MATRIX, &tempMod[0]);
glViewport(0, 0, 50, 50);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, 1.0f, 0.1f, 20.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glBegin(GL_LINES);
glColor3f(1.0f, 0.0f, 0.0f);
glEnable( GL_LINE_SMOOTH );
glLineWidth( 1.5 );
glVertex3f(-1000, 0, 0);
glVertex3f(1000, 0, 0);
glEnd();
glPopMatrix();
glViewport(0, 0, 960, 600);
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(tempPro);
gluPerspective(45.0f, (960.0/600.0), 0.1f, 400.0f);
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(tempMod);
}
Instead I get nothing, just a large empty scene, and I'm unsure how to proceed. My paintGL is essentially:
void GLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
Camera.Render();
glTranslatef(0.0, 0.0, 0.0);
glBegin(GL_QUADS);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex3f(50.0f, 0.0f, 50.0f);
glVertex3f(50.0f, 0.0f, -50.0f);
glVertex3f(-50.0f, 0.0f, -50.0f);
glVertex3f(-50.0f, 0.0f, 50.0f);
glEnd();
drawAxis();
}
Not calling the draw-axis function still gives me my plane, with it, I get a large blank scene. Am I missing something in how I'm implementing the drawAxis? Should I setup another camera for the function or something like that?

You can use glPushMatrix() and glPopMatrix() to save and restore the state of your Projection and ModelView matrices.
Your not setting up your ModelView matrix to anything useful.
Try something like this:
void GLWidget::drawAxis()
{
glViewport(0, 0, 50, 50);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
gluPerspective(45.0f, 1.0f, 0.1f, 20.0f);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
//This really has to come from your camera....
gluLookAt(10.0f,10.0f,10.0f, 0.0f,0.0f,0.0f, 0.0f,0.1f,0.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glEnable( GL_LINE_SMOOTH );
glLineWidth( 1.5 );
glBegin(GL_LINES);
glVertex3f(-1000, 0, 0);
glVertex3f(1000, 0, 0);
glEnd();
//Restore View
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glViewport(0, 0, 960, 600);
}

Related

How do I change the viewing angle in OpenGL?

I need to create a 3d cube, and so far I've created all the vertices but when I run the program I can only see the cube (or what I hope is a cube, I can't tell) from one face, so it looks like a square. I want to know how to view my cube from above, so I can check whether or not it actually looks the way I want it to.
I created the 24 vertices using glVertex3f but like I said I can't tell if it is a cube or not because I cannot look at it from an angle other than the default.
I tried downloading GLM but I am very confused on how, if at all, to use that to change the viewing perspective.
glEnable(GL_DEPTH_TEST);
// Loop until the user closes the window
while (!glfwWindowShouldClose(window))
{
// Render here
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, 0.5f);
... // Repeating drawing the vertices for each vertex of the cube
glEnd();
// Swap front and back buffers
glfwSwapBuffers(window);
// Poll for and process events
glfwPollEvents();
}
No error messages but I cant tell if its a cube or not.
// Render here
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// need the window width & height to compute aspect ratio
int width, height;
glfwGetWindowSize(window, &width, &height);
// set up the camera projection (if you haven't done this in init)
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, float(width) / height, 0.1f, 100.0f);
// set camera position & orientation
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1, 1, -3, //< eye position
0, 0, 0, //< aim position
0, 1, 0); //< up direction
// now draw stuff
glBegin(GL_QUADS);
glEnd();

opengl texture coordinates for full-screen effect

how can i create the full-screen effect with a texture image ?
till now i do this :
static void Draw(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-5.0f);
texture[0] = SOIL_load_OGL_texture // load an image file directly as a new OpenGL texture
(
"my_img.jpg",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_POLYGON);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glEnd();
glutSwapBuffers();
}
this code places my image in the top-left position of the screen, but i would like the full-screen texture effect.
So, How can i accomplish that ?
As #BrettHale already suggested in the comment, just set all the matrices to the identity transformation:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
Then get rid of any other transformations you have in place, like the glTranslatef() call you currently have in the code. Use 0.0f instead of 1.0f for the last coordinate of your vertices, and you should be good to go.
ok , I did it , and now it works. for a full-screen image i added this function
void resize(int height, int width) {
const float ar = (float) widthX / (float) heightY;
glViewport(0, 20, widthX, heightY);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-ar+1, ar-1, -1.0, 1.0, 2.0, 90.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity() ;
}
make sure to add this :
glutReshapeFunc(resize); // calling the "resize function"

Text color is not correct

void text(string str)
{
for (int i = 0; i < str.length(); i++)
{
glColor3f(0.0f, 0.0f, 0.0f);
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, str[i]);
}
}
void render(void)
{
int width = glutGet(GLUT_WINDOW_WIDTH);
int height = glutGet(GLUT_WINDOW_HEIGHT);
if (height == 0) height = 1;
GLfloat aspect = (GLfloat)width / (GLfloat)height;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_SCISSOR_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, aspect, 0.1f, 100.0f);
// Top view - top left
glViewport(0, 0, width/2, height/2);
glScissor(0, 0, width/2, height/2);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
PilotView(0.0f, 0.0f, -5.0f, 0.0f, 0.0f, 0.0f, 1.0f);
glRasterPos3f(-0.1f, -0.1f, 4.0f);
text("Front");
diode();
// Corner view - top right
glViewport(width/2, 0, width/2, height/2);
glScissor(width/2, 0, width/2, height/2);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
PilotView(0.0f, 0.0f, -5.0f, 0.0f, -90.0f, 0.0f, 1.0f);
glRasterPos3f(4.0f, -0.1f, 0.1f);
text("Right");
diode();
// Front view - bottom left
glViewport(0, height/2, width/2, height/2);
glScissor(0, height/2, width/2, height/2);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
PilotView(0.0f, 0.0f, -5.0f, 90.0f, 0.0f, 0.0f, 1.0f);
glRasterPos3f(-0.1f, 4.0f, 0.0f);
text("Top");
diode();
// Right view - bottom right
glViewport(width/2, height/2, width/2, height/2);
glScissor(width/2, height/2, width/2, height/2);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
PilotView(0.0f, 0.0f, -5.0f, 20.0f, 0.0f, 0.0f, 1.0f);
glRasterPos3f(-0.1f, 4.0f, 0.0f);
text("Fro4nt");
diode();
glDisable(GL_SCISSOR_TEST);
glutSwapBuffers();
}
I'm not sure where the white "Front" and the yellow "Top"/"Right" is coming from (in terms of color). They all should be black. Does anyone know what the issue is?
Here is what the output looks like:
As suspected, this may come as a shock but glRasterPos (...) actually tracks the "current" color when you call that function. That is, whatever color was set before glRasterPos (...) was called, applies as the "current color" for drawing operations at that position. Think of it almost as the rasterizer's analog to glVertex (...), as I will explain below.
You need to set the current color before you call glRasterPos (...), to that end you should remove the glColor3f (...) call completely from your text (...) function, or perhaps modify that function to do both - set the color and then the raster pos, then draw the text.
glRasterPos — specify the raster position for pixel operations:
The current raster position consists of three window coordinates (x, y, z), a clip coordinate value (w), an eye coordinate distance, a valid bit, and associated color data and texture coordinates.

Unexpected projection

It should be rect, which will takes whole width of window. I am expecting this, because frustum width is -5 to 5 and rect size is 10x10 and rect is on same z-axis position like near plnane of frustum. Result is very small and doesn't look like a rect, i don't know why?
void GLWidget::initializeGL() {
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
}
void GLWidget::resizeGL(int w, int h) {
glViewport(0,0, w, h);
glMatrixMode(GL_PROJECTION);
double ratio = (double)w/(double)h;
double size = ((10.0/ratio)/2.0);
glFrustum(-5.0, 5.0, -size, size, 10.0, 50.0);
}
void GLWidget::paintGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -10.0f);
glColor3f(0.0f, 0.0f, 0.0f);
glBegin(GL_QUADS);
glVertex3f(5.0f, -5.0f, 0.0f);
glVertex3f(5.0f, 5.0f, 0.0f);
glVertex3f(-5.0f, 5.0f, 0.0f);
glVertex3f(-5.0f, -5.0f, 0.0f);
glEnd();
glFlush();
}
It should be rect, which will takes whole width of window.
Well, then switch your projection to something better suited, an ortho projection, for the task when drawing the rect. Also you must reset your projection matrix to identity before applying frustum or ortho.
Hint: All the code in your resizeGL should go into the paintGL method. I see you're using Qt, so you can get the widget's width and height conveniently using the width and height getter functions. Also glClearColor goes into painGL as well:
void GLWidget::initializeGL() {
}
void GLWidget::resizeGL(int w, int h) {
}
void GLWidget::paintGL()
{
double const ratio = (double)width()/(double)height();
double const size = ((10.0/ratio)/2.0);
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0,0, width(), height());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-5.0, 5.0, -size, size, 10.0, 50.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -10.0f);
draw_perspective_stuff();
glViewport(0,0, width(), height());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// using identity projection, this fills the viewport
glColor3f(0.0f, 0.0f, 0.0f);
glBegin(GL_QUADS);
glVertex3f(-1.f, -1.f, 0.0f);
glVertex3f( 1.f, -1.f, 0.0f);
glVertex3f( 1.f, 1.f, 0.0f);
glVertex3f(-1.f, 1.f, 0.0f);
glEnd();
glFlush();
}

How to use multiple viewports in OpenGL?

I need to show the same object in OpenGL in two different viewports, for instance, one using ortographic projection and the other using perspective. In order to do this, do I need to draw again the object after each call to glViewport()?
Nehe has a good tutorial on how to do this, and his site is generally a good resource for OpenGL questions.
// normal mode
if(!divided_view_port)
glViewport(0, 0, w, h);
else
{
// right bottom
glViewport(w/2, h/2, w, h);
glLoadIdentity ();
gluLookAt(5.0f, 5.0f, 5.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
display();
// left bottom
glViewport(0, h/2, w/2, h);
glLoadIdentity();
gluLookAt (5.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
display();
// top right
glViewport(w/2, 0, w, h/2);
glLoadIdentity();
gluLookAt(0.0f, 0.0f, 5.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
display();
// top left
glViewport(0, 0, w/2, h/2);
glLoadIdentity();
gluLookAt(0.0f, 5.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
display();
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-2.0, 2.0,
-2.0 * (GLfloat) h / (GLfloat) w, 2.0 * (GLfloat) h / (GLfloat) w,
-10.0, 100.0);
else
glOrtho(-2.0 * (GLfloat) w / (GLfloat) h, 2.0 * (GLfloat) w / (GLfloat) h,
-2.0, 2.0,
-10.0, 100.0);
glMatrixMode(GL_MODELVIEW);
Minimal runnable example
Similar to this answer, but more direct and compilable. Output:
main.c
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
static int width;
static int height;
static void display(void) {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glViewport(0, 0, width/2, height/2);
glLoadIdentity();
gluLookAt(0.0, 0.0, -3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glutWireTeapot(1);
glViewport(width/2, 0, width/2, height/2);
glLoadIdentity();
gluLookAt(0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glutWireTeapot(1);
glViewport(0, height/2, width/2, height/2);
glLoadIdentity();
gluLookAt(0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0);
glutWireTeapot(1);
glViewport(width/2, height/2, width/2, height/2);
glLoadIdentity();
gluLookAt(0.0, -3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0);
glutWireTeapot(1);
glFlush();
}
static void reshape(int w, int h) {
width = w;
height = h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return EXIT_SUCCESS;
}
Compile and run:
gcc -o main.out main.c -lGL -lGLU -lglut
./main.out
I think that in modern OpenGL 4 you should just render to textures, and then place those textures orthogonaly on the screen, see this as a starting point: http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-14-render-to-texture/
Tested on OpenGL 4.5.0 NVIDIA 352.63, Ubuntu 15.10.
yes,
and you should also change the scissor settings to have a clean separation between the two views if they are in the same window.
In GL 4 you can render to many viewports in one rendering pass. See ARB_viewport_array and related concepts.
Think of OpenGL as being nothing more than commands which prepare you to output to the window you're currently working with.
There's two commands with OpenGL that even NEHE's tutorials don't tell you the importance of:
wglCreateContext - which takes a window device context DC, can be obtained from ANY window - whether it's a user control, a windows form, a GL window, or another application window (like notepad). This creates an OpenGL device context - they refer to as a resource context - which you later use with ...
wglMakeCurrent - which takes two parameters, the Device Context you're dealing with (the parameter passed in for the Windows Device Context in wglCreateContext) - and the Resource Context that returns.
Leveraging ONLY these two things - here's my advice:
NEHE's tutorial provides a solution that leverages the existing window ONLY and segments the screen for drawing. Here's the tutorial: http://nehe.gamedev.net/tutorial/multiple_viewports/20002/
Leveraging glViewport you'll need to re-draw on every update.
That's one method.
But there's another - less graphically and processor intense method:
Create a window for each view by leveraging a user control.
Each window has it's own hWnd.
Get the DC, process the wglcreatecontext, and then, on a timer (mine is 30 frames a second), if you detect state change, then select wglMakeCurrent for that view and redraw. Otherwise, just skip the section entirely.
This conserves valuable processing power, and also reduces the code from having to manage the window and viewport calculations manually.