Am currently working on a project as part of my coursework and realised I needed to have a HUD to display information to the user, stumbled upon glOrtho and have been trying to use it however am having some issues with it as it's rendering the 2D point at the moment but not the all the 3D objects in the background.
Have made different functions for drawing 2D and 3D so the main draw function is less cluttered.
void draw3D
{
glEnable(GL_DEPTH_TEST);
GLfloat ambientLight[] = {0.3f, 0.3f, 0.3f, 1.0f};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
GLfloat lightColor[] = {0.7f, 0.7f, 0.7f, 1.0f};
GLfloat lightPos[] = {-2 * floorSize, floorSize, 4 * floorSize, 1.0f};
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (float)resizeWindowW/(float)resizeWindowH, 1.0, 50000000000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -9.0f);
Cam.setView();
glColor3f(1.0f, 1.0f, 1.0f);
oldRocket();
drawEarth();
}
.
void draw2D()
{
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glDisable(GL_TEXTURE_2D);
//glDisable(GL_LIGHTING);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-windowW/2, windowW/2, -windowH/2, windowH/2, -1000.0, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
drawCrosshair();
}
.
void drawScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
draw3D();
draw2D();
//glDisable(GL_CULL_FACE);
glDisable(GL_TEXTURE_2D);
glutSwapBuffers();
}
All it's drawing at the moment is the point in the middle of the screen with a black background, and have been trying to get it to draw properly for some time now with no result.
Have followed this approach as it worked on rendering the point where as before I just had the black screen.
clean the screen
3D:
enable lighting, z-test, etc
set active matrix mode to projection
load identity and establish a perspective projection
set active matrix mode back to modelview
draw everything 3D
2D:
disable lighting, z-test, etc
set active matrix mode to projection
load identity and establish an ortogonal projection
set active matrix mode back to modelview
draw everything 2D
swap buffers
Updated code with the perspective matrix, and it now displays properly.
Adding the perspective matrix between matrices fixed the problem and everything now displays properly.
Related
I was wondering if anyone could help me figure out how to add a light source to my 3D objects. I have four objects that are rotating and I want the light source to be at a fixed position, and I want to be able to see lighting on the object.
I tried doing this (********):
//*******Initializing the light position
GLfloat pos[] = {-2,4,5,1};
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
//*******adding the light to the display method
glLoadIdentity();
glLightfv(GL_LIGHT0, GL_POSITION, pos);
// rectangle
glPushMatrix();
glTranslatef(0.0f, 2.5f, -8.0f);
glRotatef(angleRectangle, 0.0f, 1.0f, 0.0f);
drawRectangle();
glPopMatrix();
//small cylinder
glPushMatrix();
glTranslatef(0.0f, 2.0f, -8.0f);
glRotatef(90, 1, 0, 0);
glRotatef(anglePyramid, 0.0f, 0.0f, 1.0f);
drawCylinder(0.2, 0.7);
glPopMatrix();
//big cylinder
glPushMatrix();
glTranslatef(0.0f, 1.5f, -8.0f);
glRotatef(90, 1, 0, 0);
glRotatef(anglePyramid, 0.0f, 0.0f, 1.0f);
drawCylinder(0.7, 2.7);
glPopMatrix();
//pyramid
glPushMatrix();
glTranslatef(0.0f, -2.2f, -8.0f);
glRotatef(180, 1, 0, 0);
glRotatef(anglePyramid, 0.0f, 1.0f, 0.0f);
drawPyramid();
glPopMatrix();
glutSwapBuffers();
anglePyramid += k * 0.2f; //- is CW, + is CCW
angleRectangle += -k * 0.2f;
}
//******* Then i added these to the main method
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
However when I do this and I run the entire program, my objects turn gray, and at certain points in the rotation they turn white. And this isnt what I want. I want to keep my colorful objects, but I want to be able to see the light source on them.
Any help would be greatly appreciated. Also let me know if you need to see more of my code to figure out the issue. Thanks
When lighting (GL_LIGHTING) is enabled, then the color is taken from the material parameters (glMaterial).
If you still want to use the current color, the you have to enable GL_COLOR_MATERIAL
and to set the color material paramters (glColorMaterial):
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
See also Basic OpenGL Lighting.
But note, that drawing by glBegin/glEnd sequences, the fixed function pipeline matrix stack and fixed function pipeline per vertex light model, is deprecated since decades.
Read about Fixed Function Pipeline and see Vertex Specification and Shader for a state of the art way of rendering.
I'm trying to draw a red sphere on a white background using OpenGL. Here's the part of my code that should draw the sphere.
Part of the main entry point:
initialise();
glutDisplayFunc(draw_Sphere);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
Initialisation:
void initialise()
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
Drawing the sphere:
void draw_Sphere()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(200, 200, 0);
glColor3f(1.0, 0.0, 0.0);
glPushMatrix();
glutSolidSphere(0.25, 20, 20);
glPopMatrix();
glutSwapBuffers();
}
I have no idea what's going wrong, but this won't render the sphere (it will draw the white background, though). Any suggestions to fix it?
Using Matrices
Consider setting up a proper model, view and projection matrices for this. Then you will have more control on what you see.
Solution
Drop the glTranslatef(200, 200, 0); line as with this you are getting the sphere out of the frustum. And you should see the sphere.
I am currently trying to get into OpenGL shading and lighting to display a 3D model exported from Blender in a simple GLUT window.
I tried to display the typical glutSolidTeapot to validate my OpenGL settings.
With the Teapot everything looks perfectly fine as can be seen in the pic below.
If I now want to replace the teapot with my own Blender-exported model, the shading doesn't work. The model ( a car rim ) just looks solid-colored.
The color changes when the model is rotated, but it stays solid all the time.
What does glutSolidTeapot do under the hood to draw the teapot model?
Here's the code I'm using to set up OpenGL :
void SetupRC()
{
glClearColor(0.4, 0.4, 0.4, 1.0);
// Enable lighting and the light we have set up
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
//Set lighting parameters
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
glLightfv(GL_LIGHT0, GL_AMBIENT,light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK,GL_DIFFUSE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Enable shading
glShadeModel(GL_SMOOTH);
// Set up the projection parameters
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 2.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
}
And the code to repaint the frames :
void RenderScene(void)
{
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glTranslatef(0.0, 0.0, -2.5f);
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// set input data to arrays
glVertexPointer(3, GL_FLOAT, 0, BlenderGuru_CarWheelVerts);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
GLfloat ambConst[] = { 0.24725, 0.1995, 0.0745, 1.0};
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambConst);
GLfloat diffConst[] = { 0.75164, 0.60648, 0.22648, 1.0 };
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffConst);
GLfloat specConst[] = { 0.628281, 0.555802, 0.366065, 1.0 };
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specConst);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 51.2);
// draw data
glEnableClientState(GL_VERTEX_ARRAY);
glDrawArrays(GL_TRIANGLES, 0, BlenderGuru_CarWheelNumVerts);
glDisableClientState(GL_VERTEX_ARRAY);
glFlush();
}
Since you are exporting from Blender, your exported data will most likely also contain normals. If not, make sure you generate them for your model and export them. You need normal data for your lighting to work.
The call to glEnable(GL_AUTO_NORMAL); does not do what you might think it does. It will only
generate normal vectors when either GL_MAP2_VERTEX_3 or GL_MAP2_VERTEX_4 is used to generate vertices.
So export normal data yourself and use this, or perhaps calculate the normals from the mesh data that is available to you.
Your code lacks loading/setting normal data. Without normals, lighting doesn't work.
I heard that I should use normals instead of colors, because colors are deprecated. (Is that true?) Normals have something to do with the reflection of light, but I can't find a clear and intuitive explanation. What is a normal?
A normal in general is a unit vector whose direction is perpendicular to a surface at a specific point. Therefore it tells you in which direction a surface is facing. The main use case for normals are lighting calculations, where you have to determine the angle (or practically often its cosine) between the normal at a given surface point and the direction towards a lightsource or a camera.
glNormal minimal example
glNormal is a deprecated OpenGL 2 method, but it is simple to understand, so let's look into it. The modern shader alternative is discussed below.
This example illustrates some details of how glNormal works with diffuse lightning.
The comments of the display function explain what each triangle means.
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
/* Triangle on the x-y plane. */
static void draw_triangle() {
glBegin(GL_TRIANGLES);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f( 1.0f, -1.0f, 0.0f);
glEnd();
}
/* A triangle tilted 45 degrees manually. */
static void draw_triangle_45() {
glBegin(GL_TRIANGLES);
glVertex3f( 0.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f( 1.0f, -1.0f, 0.0f);
glEnd();
}
static void display(void) {
glColor3f(1.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
/*
Triangle perpendicular to the light.
0,0,1 also happens to be the default normal if we hadn't specified one.
*/
glNormal3f(0.0f, 0.0f, 1.0f);
draw_triangle();
/*
This triangle is as bright as the previous one.
This is not photorealistic, where it should be less bright.
*/
glTranslatef(2.0f, 0.0f, 0.0f);
draw_triangle_45();
/*
Same as previous triangle, but with the normal set
to the photorealistic value of 45, making it less bright.
Note that the norm of this normal vector is not 1,
but we are fine since we are using `glEnable(GL_NORMALIZE)`.
*/
glTranslatef(2.0f, 0.0f, 0.0f);
glNormal3f(0.0f, 1.0f, 1.0f);
draw_triangle_45();
/*
This triangle is rotated 45 degrees with a glRotate.
It should be as bright as the previous one,
even though we set the normal to 0,0,1.
So glRotate also affects the normal!
*/
glTranslatef(2.0f, 0.0f, 0.0f);
glNormal3f(0.0, 0.0, 1.0);
glRotatef(45.0, -1.0, 0.0, 0.0);
draw_triangle();
glPopMatrix();
glFlush();
}
static void init(void) {
GLfloat light0_diffuse[] = {1.0, 1.0, 1.0, 1.0};
/* Plane wave coming from +z infinity. */
GLfloat light0_position[] = {0.0, 0.0, 1.0, 0.0};
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glColorMaterial(GL_FRONT, GL_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_NORMALIZE);
}
static void reshape(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 7.0, -1.0, 1.0, -1.5, 1.5);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 200);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return EXIT_SUCCESS;
}
Theory
In OpenGL 2 each vertex has its own associated normal vector.
The normal vector determines how bright the vertex is, which is then used to determine how bright the triangle is.
OpenGL 2 used the Phong reflection model, in which light is separated into three components: ambient, diffuse and specular. Of those, diffuse and specular components are affected by the normal:
if the diffuse light is perpendicular to the surface, it makes is brighter, no matter where the observer is
if the specular light hits the surface, and bounces off right into the eye of the observer, that point becomes brigher
glNormal sets the current normal vector, which is used for all following vertexes.
The initial value for the normal before we all glNormal is 0,0,1.
Normal vectors must have norm 1, or else colors change! glScale also alters the length of normals! glEnable(GL_NORMALIZE); makes OpenGL automatically set their norm to 1 for us. This GIF illustrates that beautifully.
Why it is useful to have normals per vertexes instead of per faces
Both spheres below have the same number of polygons. The one with normals on the vertexes looks much smoother.
OpenGL 4 fragment shaders
In newer OpenGL API's, you pass the normal direction data to the GPU as an arbitrary chunk of data: the GPU does not know that it represents the normals.
Then you write a hand-written fragment shader, which is an arbitrary program that runs in the GPU, which reads the normal data you pass to it, and implements whatever lightning algorithm you want. You can implement Phong efficiently if you feel like it, by manually calculating some dot products.
This gives you full flexibility to change the algorithm design, which is a major features of modern GPUs. See: https://stackoverflow.com/a/36211337/895245
Examples of this can be found in any of the "modern" OpenGL 4 tutorials, e.g. https://github.com/opengl-tutorials/ogl/blob/a9fe43fedef827240ce17c1c0f07e83e2680909a/tutorial08_basic_shading/StandardShading.fragmentshader#L42
Bibliography
https://gamedev.stackexchange.com/questions/50653/opengl-why-do-i-have-to-set-a-normal-with-glnormal
https://www.opengl.org/sdk/docs/man2/xhtml/glNormal.xml
http://www.tomdalling.com/blog/modern-opengl/06-diffuse-point-lighting/
http://learnopengl.com/#!Advanced-Lighting/Normal-Mapping
Many things are now deprecated, including normals and colors. That just means that you have to implement them yourself. With normals you can shade your objects. It's up to you to make the calculations but there are a lot of tutorials on e.g. Gouraud/Phong shading.
Edit: There are two types of normals: face normals and vertex normals. Face normals point away from the triangle, vertex normals point away from the vertex. With vertex normals you can achieve better quality, but there are many uses also for face normals, e.g. they can be used in collision detection and shadow volumes.
I'm trying to display "transparent" surfaces (not closed volumes) with both the front face and back face are visible (not culled).
For example displaying a cone or cylinder where the transparency is applied on both sides.
There are some visible artifacts where some part of the surface does not seems to be handling the alpha values correctly.
The issue it seems is when I (opengl) is trying to apply the alpha from the front side of the surface to the backside of the surface. (when both the inside/outside of the surface is visible).
void init()
{
glMatrixMode(GL_PROJECTION);
gluPerspective( /* field of view in degree */ 40.0,
/* aspect ratio */ 1.0,
/* Z near */ 1.0, /* Z far */ 10.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0.0, 0.0, 5.0, /* eye is at (0,0,5) */
0.0, 0.0, 0.0, /* center is at (0,0,0) */
0.0, 1.0, 0.); /* up is in positive Y direction */
glTranslatef(0.0, 0.6, -1.0);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
glLightfv(GL_LIGHT2, GL_DIFFUSE, light2_diffuse);
glLightfv(GL_LIGHT2, GL_POSITION, light2_position);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
//glEnable(GL_CULL_FACE);
glFrontFace( GL_CW );
glShadeModel(GL_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
void draw ()
{
static GLfloat amb[] = {0.4f, 0.4f, 0.4f, 0.0f};
static GLfloat dif[] = {1.0f, 1.0f, 1.0f, 0.0f};
static GLfloat back_amb[] = {0.4f, 0.4f, 0.4f, 1.0f};
static GLfloat back_dif[] = {1.0f, 1.0f, 1.0f, 1.0f};
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_LIGHT1);
glDisable(GL_LIGHT2);
amb[3] = dif[3] = 0.5f;// cos(s) / 2.0f + 0.5f;
glMaterialfv(GL_FRONT, GL_AMBIENT, amb);
glMaterialfv(GL_FRONT, GL_DIFFUSE, dif);
glMaterialfv(GL_BACK, GL_AMBIENT, back_amb);
glMaterialfv(GL_BACK, GL_DIFFUSE, back_dif);
glPushMatrix();
glTranslatef(-0.3f, -0.3f, 0.0f);
glRotatef(angle1, 1.0f, 5.0f, 0.0f);
glutSolidCone(1.0, 1.0, 50, 2 );
glPopMatrix();
///...
SwapBuffers(wglGetCurrentDC()); // glutSwapBuffers();
}
The code is based on : http://www.sgi.com/products/software/opengl/examples/glut/examples/source/blender.c
tinyurled links to 2 images on flickr showing the issue (but from out production code, not the above code, but both have the same kind of problems):
http://flic.kr/p/99soxy and http://flic.kr/p/99pg18
Thanks.
Max.
Your problem is probably a mix of two problems.
as your depth test is enabled, and all polygons write to depth buffer, the polygons that are further away cannot contribute to the pixel color if they are drawn after the nearest polygons. You should try to disable your depth test.
alphablending is a non-commutative operation, so the order is which the triangles are drawn is important and changes the result. To have a consistent result, you'd have to draw the triangles in consistent order. In your code, this order may vary, depending on the viewpoint for instance. As your objects are convex, you could draw your object in two passes, both with culling enabled, the first pass drawing the backfacing triangles (that are the most distant to the camera), then the frontfacing triangles (that are nearest)
More generally, correct blending for arbitrary geometry is a hard problem, you may want to look at Order Independent Transparency algorithms (depth peeling, ...) if you want to go further.