opengl rendering half a cylinder - c++

ok so im new to opengl and im creating a pool game using only the core opengl and glut
i am writing in c++
i know how to draw a cylinder:
{
GLUquadric *quadric = gluNewQuadric();
glBegin;
gluCylinder(quadric, 0.5f, 0.5f, 5.0f, 40, 40);
glEnd();
}
i want to know if i can half this cylinder so i can use the curve to round off my table/pocket edges
any help wound be appreciated thanks

The function gluCylinder is too specific to accomplish this.
glu is built as a layer on top of opengl so you can always go to more low level drawing functions if the high level ones don't solve your problem.
This tutorial should give you an introduction to some of the lower level drawing functionality in opengl: http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=05

Related

Drawing a simple rectangle in OpenGL 4

According to this wikibook it used to be possible to draw a simple rectangle as easily as this (after creating and initializing the window):
glColor3f(0.0f, 0.0f, 0.0f);
glRectf(-0.75f,0.75f, 0.75f, -0.75f);
This is has been removed however in OpenGL 3.2 and later versions.
Is there some other simple, quick and dirty, way in OpenGL 4 to draw a rectangle with a fixed color (without using shaders or anything fancy)?
Is there some ... way ... to draw a rectangle ... without using shaders ...?
Yes. In fact, AFAIK, it is supported on all OpenGL versions in existence: you can draw a solid rectangle by enabling scissor test and clearing the framebuffer:
glEnable(GL_SCISSOR_TEST);
glScissor(x, y, width, height);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
This is different from glRect in multiple ways:
The coordinates are specified in pixels relative to the window origin.
The rectangle must be axis aligned and cannot be transformed in any way.
Most of the per-sample processing is skipped. This includes blending, depth and stencil testing.
However, I'd rather discourage you from doing this. You're likely to be better off by building a VAO with all the rectangles you want to draw on the screen, then draw them all with a very simple shader.

Native Rendering Plugin with Oculus Rift

I'm working on a project that offloads some rendering to a native plugin I wrote for Unity, in order to make use of instancing and other advanced graphics features. I'm developing it for a cross-platform release, but I work with a Mac so testing is done primarily with OpenGL. At this point, the plugin only renders a quad to the center of the screen colored with hex values. The plugin works as expected in a blank Unity project, but as soon as I incorporate it into my Oculus project, it begins behaving erratically.
In the Rift, the plugin's geometry draws twice, one time stretching across both eyes and another time drawing only within the bounds of the right eye. Additionally, any primitive colors I apply to the geometry are lost and the geometry appears to pick up surrounding colors; on a black screen with red text, the geometry will be mostly black with some red bleeding into the lines. As soon as my green terrain is loaded, the geometry drawn by the plugin becomes green.
Below is a screenshot of the geometry being drawn in a blank Unity project with nothing else:
And here is a screenshot of the same geometry being drawn on top of my Oculus Rift application:
Here's the creation of the vertices that I'm rendering (three coordinates and color):
Vertex verts[4] =
{
{ -0.5f, 0.5f, 0, 0xFF0000ff },
{ 0.5f, 0.5f, 0, 0xFFff0000 },
{ 0.5f, -0.5f, 0, 0xFF00ff00 },
{ -0.5f, -0.5f, 0, 0xFFff0000 },
};
Here's the draw function, called every frame within the plugin:
// OpenGL case
if (g_DeviceType == kGfxRendererOpenGL)
{
//initialize model view matrices
glMatrixMode (GL_MODELVIEW);
float modelMatrix[16] =
{
1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1,
};
glLoadMatrixf (modelMatrix); //assign our matrix to the current MatrixMode
//initialize projection matrix
glMatrixMode (GL_PROJECTION);
projectionMatrix[10] = 2.0f; //tweak projection matrix to match D3D
projectionMatrix[14] = -1.0f;
glLoadMatrixf (projectionMatrix);
// Vertex layout
glVertexPointer (3, GL_FLOAT, sizeof(verts[0]), &verts[0].x);
glEnableClientState (GL_VERTEX_ARRAY);
glColorPointer (4, GL_UNSIGNED_BYTE, sizeof(verts[0]), &verts[0].color);
glEnableClientState (GL_COLOR_ARRAY);
glDrawArrays(GL_LINE_LOOP, 0, 4);
}
Any insight from experienced native plugin/Rift graphics coders would be appreciated!
You can make use of Unity UI system to render sprites according to your needs. Here is an article from Oculus describing how to tweak Unity UI system for VR : https://developer.oculus.com/blog/unitys-ui-system-in-vr/
Outside Unity, you would use quad layers to render on top of eyes FOV. Here is layers described in Oculus Rift documentation : https://developer.oculus.com/documentation/pcsdk/latest/concepts/dg-render/#dg_render_layers
Quad Layer :
A monoscopic image that is displayed as a rectangle at a given pose and size in the virtual world. This is useful for heads-up-displays, text information, object labels and so on. By default the pose is specified relative to the user's real-world space and the quad will remain fixed in space rather than moving with the user's head or body motion. For head-locked quads, use the ovrLayerFlag_HeadLocked flag as described below.

Visible lines beetween textures in SkyBox?

I made a skybox in opengl using five textures on five quads, the problem is that the lines(boarders) between the textures are visible:
How do you get of those lines?
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f(-15.0f,-14.0f,-15.0f);
GL11.glTexCoord2f(1f, 1.0f);
GL11.glVertex3f(15.0f,-14.0f,-15.0f);
GL11.glTexCoord2f(1f, 0f);
GL11.glVertex3f(15.0f,16.0f,-15.0f);
GL11.glTexCoord2f(0.0f, 0f);
GL11.glVertex3f(-15.0f,16.0f,-15.0f);
GL11.glEnd();
This is one quad of the skybox, should it be done differently?
I had the same problem in OpenSceneGraph and adapted the OpenGL solution, which should be pretty straight forward. Basically just use glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS) when your program starts before you start your rendering loop and the lines should disappear.
There was some ambiguity in my research, and it is possible that the enum is actually GL_TEXTURE_CUBEMAP_SEAMLESS, but one or the other should work for you. It is a bit more complicated in OSG because you need to #define GL_TEXTURE_CUBE_MAP_SEAMLESS 0x884F then enable seamless cubemapping by using state->setMode(GL_TEXTURE_CUBE_MAP_SEAMLESS, osg::StateAttribute::ON), where state is a state set from the root of your scene graph.
I hope this helps, it worked for me.

C++ OpenGL load image in GL_QUAD, glVertex2f

Using WIN32_FIND_DATA and FindFirstFile I'm searching for files in a directory an with fileName.find(".jpg") != std::string::npos I filter the jpg images out.
I'm using OpenGL for creating Boxes with a red color:
glBegin( GL_QUADS );
glColor4f( 1.0f, 0.0f, 0.0f, 0.0f ); glVertex2f( 0.35f, 0.7f );
glColor4f( 1.0f, 0.0f, 0.0f, 0.0f ); glVertex2f( -0.35f, 0.7f );
glColor4f( 1.0f, 0.0f, 0.0f, 0.0f ); glVertex2f( -0.35f, -0.3f );
glColor4f( 1.0f, 0.0f, 0.0f, 0.0f ); glVertex2f( 0.35f, -0.3f );
This is the box in the center with a red color.
My Question is how can I load the Images each in a Cube instead of the Red color (glColor4f)?
I think this is not the best way to make this, but this code is not my own Code, I'm trying to make this better for a friend.
Thank you!
You need to learn about texturing. See NeHe's tutorial on the subject as an example.
However, that tutorial is a bit old (as is your code, since you use glVertex(), so it might not matter to you right now... :).
Anyway, starting from OpenGL 3.1 and OpenGL ES 2.0, you should do it with using GLSL, fragment shaders and samplers instead. See another tutorial for that. It's actually simpler than learning all the fixed function stuff.
It's not really a good practice to use WinAPI together with OpenGL applications unless you really have reasons to - and loading textures from the disk is not a good reason.
Think this way: OpenGL is a platform-independent API, why to dimnish this advantage by using non-portable subroutines when portable alternatives exist and are more convenient to use in most cases?
For the loading textures, I recommend the SOIL library. This is likely to be much better a solution than what the NeHe tutorials recommend.
For finding files on the disk, you might want to use boost::filesystem if you want to get rid of the WinAPI dependency. But that's not a priority now.
When you have the texture loaded by SOIL (a GLuint value being the texture ID), you can do the following:
enable 2D texturing (glEnable(GL_TEXTURE_2D)),
bind the texture as active 2D texture (glBindTexture(GL_TEXTURE_2D,tex);),
set the active color to pure white so that the texture image will be full-bright,
draw the vertices as usual, but for each vertex you'll need to specify a texture coordinate (glTexCoord2f) instead of a color. (0,0) is upper left coord of the texture image, (1,1) is the lower right.
Note that the texture image must have dimensions being powers of two (like 16x16 or 256x512). If you want to use any texture size, switch to a newer OpenGL version which supports GL_TEXTURE_RECTANGLE.
Not really a lot of explaining, as far as the basics are concerned. :)
BTW- +1 for what Marcus said in his answer. You're learning an outdated OpenGL version right now; while you can do a lot of fun things with it, you can do more with at least OpenGL 2 and shaders... and it's usually easier with shaders too.

opengl - How to draw square pixel with GL_POINTS

I try to use the following code to draw a square-shaped pixel with opengl
glPointSize(5.0f);
glBegin(GL_POINTS);
glVertex3f(1.0f, 1.0f, 1.0f);
glEnd();
However, the final result is a circle-shaped pixel.
Please take a look the reference http://risknfun.com/compform/w1.html
See the "Problem 4. A Grid". On the right side, the display image has square-shaped pixel.
It's partly up to the OpenGL implementation (i.e., it can vary with your graphics driver), but with a bit of luck, you can turn this on or off with glEnable(GL_POINT_SMOOTH); or glDisable(GL_POINT_SMOOTH); With point smoothing turned on, you'll normally get round points, but with it turned off you'll get square points.
You can also try to tell OpenGL not to spend time making GL_POINTS nice and round by calling:
glHint(GL_POINT_SMOOTH_HINT, GL_FASTEST);
But keep in mind that's just an hint. The OpenGL driver has ultimately the last word.