I'm in crisis about draw Cube, as the title says.
I tried the graphic only in Unity. But I was done. Using OpenTK and OpenGL in C#.
It was successful to draw the sphere by inputting the value.
Draw the values of the array as you type.
Ex) 2by2 Array =>
oo
oo
this is my code about draw Sphere
void DrawSphere()
{
OpenTK.Graphics.OpenGL.GL.Enable(OpenTK.Graphics.OpenGL.EnableCap.Lighting);
OpenTK.Graphics.OpenGL.GL.Enable(OpenTK.Graphics.OpenGL.EnableCap.Light0);
OpenTK.Graphics.OpenGL.GL.Enable(OpenTK.Graphics.OpenGL.EnableCap.Normalize);
OpenTK.Graphics.OpenGL.GL.Enable(OpenTK.Graphics.OpenGL.EnableCap.ColorMaterial);
//OpenTK.Graphics.OpenGL.GL.Enable(OpenTK.Graphics.OpenGL.EnableCap.Texture2D);
OpenTK.Graphics.OpenGL.GL.ShadeModel(OpenTK.Graphics.OpenGL.ShadingModel.Smooth);
// Draw green sphere
OpenTK.Graphics.OpenGL.GL.Color3(0.8f, 0.8f, 255.0f);
//textureid = LoadTex("dd.png");
float x, y, z;
y = 0.0F;
x = 0.0F;
x = (((Properties.Settings.Default.simulRow * camera.rowGap) - camera.rowGap) / 2) * -1.0F;
for (int i = 0; i < Properties.Settings.Default.simulRow; i++)
{
z = (((Properties.Settings.Default.simulCol * camera.colGap) - camera.colGap) / 2) * -1.0F;
for (int j = 0; j < Properties.Settings.Default.simulCol; j++)
{
//y = ballImage[i, j].height / camera.calibrate_div;
y = (camera.calibrate_div / 2.0f) - Math.Abs(ballImage[i, j].current_height);
//System.Diagnostics.Debug.WriteLine(ballImage[0, 0].current_height);
OpenTK.Graphics.OpenGL.GL.PushMatrix();
OpenTK.Graphics.OpenGL.GL.Translate(x, y, z);
//OpenTK.Graphics.OpenGL.GL.BindTexture(OpenTK.Graphics.OpenGL.TextureTarget.Texture2D, textureid);
//OpenTK.Graphics.Glu.QuadricTexture(esfera, true);
OpenTK.Graphics.OpenGL.GL.Rotate(90, 1.0f, 0.0f, 0.0f);
//OpenTK.Graphics.OpenGL.GL.Rotate(0.0f, 1.0f, 1.0f, 1.0f);
OpenTK.Graphics.Glu.Sphere(esfera, 1.0f, 24, 24);
//OpenTK.Graphics.Glu.Tex(esfera, 1.0f, 24, 24);
OpenTK.Graphics.OpenGL.GL.PopMatrix();
if (i == 5 && j == 5)
{
// OpenTK.Graphics.Glu.Sphere(esfera, 100.0f, 24, 24);
}
z += camera.rowGap;
}
x += camera.colGap;
}
}
Related
I am using SharpGL.
I am trying to draw three overlapped spheres with three different colors like the following -
However, I am getting output like the following -
In other words, the gl.Color() is having no effect on the drawn object.
How can I fix this?
Source Code:
public static class OpenGLhelper
{
public static void Init(OpenGL gl)
{
float[] mat_specular = { 1.0f, 1.0f, 1.0f, 1.0f };
float[] mat_shininess = { 50.0f };
float[] light_position = { 0.5f, 0.5f, 0.750f, 0.0f };
gl.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.ShadeModel(OpenGL.GL_SMOOTH);
gl.Material(OpenGL.GL_FRONT, OpenGL.GL_SPECULAR, mat_specular);
gl.Material(OpenGL.GL_FRONT, OpenGL.GL_SHININESS, mat_shininess);
gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_POSITION, light_position);
gl.Enable(OpenGL.GL_LIGHTING);
gl.Enable(OpenGL.GL_LIGHT0);
gl.Enable(OpenGL.GL_DEPTH_TEST);
//// Cull back faces.
gl.Enable(OpenGL.GL_CULL_FACE);
gl.CullFace(OpenGL.GL_BACK);
gl.FrontFace(OpenGL.GL_CW);
}
private static void drawSphere(OpenGL gl, Point3d c, Rgb color, double r, int n)
{
int i, j;
double theta1, theta2, theta3;
Point3d e = new Point3d();
Point3d p = new Point3d();
if (c == null)
{
c = new Point3d(0, 0, 0);
}
double twoPi = Math.PI * 2;
double piD2 = Math.PI / 2;
if (r < 0)
r = -r;
if (n < 0)
n = -n;
if (n < 4 || r <= 0)
{
gl.Begin(OpenGL.GL_POINTS);
gl.Color(color.Red, color.Green, color.Blue);
gl.Vertex(c.X, c.Y, c.Z);
gl.End();
return;
}
for (j = 0; j < n / 2; j++)
{
theta1 = j * twoPi / n - piD2;
theta2 = (j + 1) * twoPi / n - piD2;
gl.Begin(OpenGL.GL_QUAD_STRIP);
for (i = 0; i <= n; i++)
{
theta3 = i * twoPi / n;
e.X = Math.Cos(theta2) * Math.Cos(theta3);
e.Y = Math.Sin(theta2);
e.Z = Math.Cos(theta2) * Math.Sin(theta3);
p.X = c.X + r * e.X;
p.Y = c.Y + r * e.Y;
p.Z = c.Z + r * e.Z;
gl.Normal(e.X, e.Y, e.Z);
gl.TexCoord(i / (double)n, 2 * (j + 1) / (double)n);
gl.Color(color.Red, color.Green, color.Blue);
gl.Vertex(p.X, p.Y, p.Z);
e.X = Math.Cos(theta1) * Math.Cos(theta3);
e.Y = Math.Sin(theta1);
e.Z = Math.Cos(theta1) * Math.Sin(theta3);
p.X = c.X + r * e.X;
p.Y = c.Y + r * e.Y;
p.Z = c.Z + r * e.Z;
gl.Normal(e.X, e.Y, e.Z);
gl.TexCoord(i / (double)n, 2 * j / (double)n);
gl.Color(color.Red, color.Green, color.Blue);
gl.Vertex(p.X, p.Y, p.Z);
}
gl.End();
}
}
public static void Display(OpenGL gl)
{
gl.Clear(OpenGL . GL_COLOR_BUFFER_BIT | OpenGL . GL_DEPTH_BUFFER_BIT);
drawSphere(gl, new Point3d(0, 0, 0), new Rgb(1, 0, 0), 0.5, 20);
drawSphere(gl, new Point3d(0, 0.5, 0), new Rgb(0, 1, 0), 0.5, 20);
drawSphere(gl, new Point3d(0, -0.5, 0), new Rgb(0, 0, 1), 0.5, 20);
gl.Flush();
}
public static void Reshape(OpenGL gl, int width, int height)
{
}
}
public partial class SharpGLForm : Form
{
private float rotation = 0.0f;
public SharpGLForm()
{
InitializeComponent();
OpenGL gl = openGLControl1.OpenGL;
OpenGLhelper.Init(gl);
}
private void openGLControl_OpenGLDraw(object sender, RenderEventArgs e)
{
OpenGL gl = openGLControl1.OpenGL;
gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
gl.LoadIdentity();
gl.Translate(0.0f, 0.0f, -4.0f); // Move into the screen
gl.Rotate(rotation, 0.0f, 1.0f, 0.0f);
OpenGLhelper.Display(gl);
rotation += 3.0f;
}
}
When lighting (GL_LIGHTING) is enabled, the render color is defined by the material parameters (glMaterial). If you want to define the color with glColor, you must enable GL_COLOR_MATERIAL
and to set the color material parameters (glColorMaterial):
gl.Enable(OpenGL.GL_LIGHTING);
gl.Enable(OpenGL.GL_COLOR_MATERIAL);
gl.ColorMaterial(OpenGL.GL_FRONT_AND_BACK, OpenGL.GL_AMBIENT_AND_DIFFUSE);
Alternatively set the material color with GL.Material. e.g.:
float[] ambient_diffuse_color = { 1.0f, 0.0f, 0.0f, 1.0f }; // RED
GL.Material(OpenGL.GL_FRONT_AND_BACK, OpenGL.GL_AMBIENT_AND_DIFFUSE,
ambient_diffuse_color);
I am using SharpGL library.
The following source code draws several hundred solid spheres at various locations -
// Initialization routine.
void setup()
{
gl.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.Enable(OpenGL.GL_DEPTH_TEST); // Enable depth testing.
// Turn on OpenGL lighting.
gl.Enable(OpenGL.GL_LIGHTING);
// Light property vectors.
float []lightAmb = { 0.0f, 0.0f, 0.0f, 1.0f };
float[]lightDifAndSpec0 = { 1.0f, 1.0f, 1.0f, 1.0f };
float[]lightDifAndSpec1 = { 0.0f, 1.0f, 0.0f, 1.0f };
float[]globAmb = { 0.2f, 0.2f, 0.2f, 1.0f };
// Light0 properties.
gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_AMBIENT, lightAmb);
gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_DIFFUSE, lightDifAndSpec0);
gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_SPECULAR, lightDifAndSpec0);
// Light1 properties.
gl.Light(OpenGL.GL_LIGHT1, OpenGL.GL_AMBIENT, lightAmb);
gl.Light(OpenGL.GL_LIGHT1, OpenGL.GL_DIFFUSE, lightDifAndSpec1);
gl.Light(OpenGL.GL_LIGHT1, OpenGL.GL_SPECULAR, lightDifAndSpec1);
gl.Enable(OpenGL.GL_LIGHT0); // Enable particular light source.
gl.Enable(OpenGL.GL_LIGHT1); // Enable particular light source.
gl.LightModel(OpenGL.GL_LIGHT_MODEL_AMBIENT, globAmb); // Global ambient light.
gl.LightModel(OpenGL.GL_LIGHT_MODEL_LOCAL_VIEWER, OpenGL.GL_TRUE); // Enable local viewpoint
// Cull back faces.
gl.Enable(OpenGL.GL_CULL_FACE);
gl.CullFace(OpenGL.GL_BACK);
}
//http://www.java2s.com/example/java/javax.media.opengl/opengl-method-to-draw-a-sphere-in-opengl.html
void drawSphere(Point3d c, double r, int n)
{
int i, j;
double theta1, theta2, theta3;
Point3d e = new Point3d();
Point3d p = new Point3d();
if (c == null)
{
c = new Point3d(0, 0, 0);
}//from w ww .j ava2 s . c o m
double twoPi = Math.PI * 2;
double piD2 = Math.PI / 2;
if (r < 0)
r = -r;
if (n < 0)
n = -n;
if (n < 4 || r <= 0)
{
gl.Begin(OpenGL.GL_POINTS);
gl.Vertex(c.X, c.Y, c.Z);
gl.End();
return;
}
for (j = 0; j < n / 2; j++)
{
theta1 = j * twoPi / n - piD2;
theta2 = (j + 1) * twoPi / n - piD2;
gl.Begin(OpenGL.GL_QUAD_STRIP);
// gl.glBegin(GL.GL_TRIANGLE_STRIP);
for (i = 0; i <= n; i++)
{
theta3 = i * twoPi / n;
e.X = Math.Cos(theta2) * Math.Cos(theta3);
e.Y = Math.Sin(theta2);
e.Z = Math.Cos(theta2) * Math.Sin(theta3);
p.X = c.X + r * e.X;
p.Y = c.Y + r * e.Y;
p.Z = c.Z + r * e.Z;
gl.Normal(e.X, e.Y, e.Z);
gl.TexCoord(i / (double)n, 2 * (j + 1) / (double)n);
gl.Vertex(p.X, p.Y, p.Z);
e.X = Math.Cos(theta1) * Math.Cos(theta3);
e.Y = Math.Sin(theta1);
e.Z = Math.Cos(theta1) * Math.Sin(theta3);
p.X = c.X + r * e.X;
p.Y = c.Y + r * e.Y;
p.Z = c.Z + r * e.Z;
gl.Normal(e.X, e.Y, e.Z);
gl.TexCoord(i / (double)n, 2 * j / (double)n);
gl.Vertex(p.X, p.Y, p.Z);
}
gl.End();
}
}
/// <summary>
/// Handles the OpenGLDraw event of the openGLControl control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="RenderEventArgs"/> instance containing the event data.</param>
private void openGLControl_OpenGLDraw(object sender, RenderEventArgs e)
{
// Get the OpenGL object.
gl = openGLControl.OpenGL;
// Clear the color and depth buffer.
gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
// Load the identity matrix.
gl.LoadIdentity();
// Rotate around the Y axis.
////gl.Rotate(rotation, 0.0f, 1.0f, 0.0f);
foreach (var item in atomsList)
{
// Point3d coord = item.Coordinate;
drawSphere(item.Coordinate, 0.75f, 20);
}
// Nudge the rotation.
rotation += 30.0f;
}
My aim is to draw something like this --
However, my output is something like the following --
Probably, the hidden surface removal is not working as expected.
How can I obtain my expected output?
The winding order of your primitives appears to be clockwise. The default winding order is counterclockwise (see Face Culling). Change the winding order that defines front-facing polygons:
gl.Enable(OpenGL.GL_CULL_FACE);
gl.CullFace(OpenGL.GL_BACK);
gl.FrontFace(OpenGL.GL_CW);
I have created a mesh of first image and i want the collider between the pink shaded region
After writing the following Script i got collider in second image
void InnerOuterCircle () {
vertice = new List<Vector3> ();
triangle = new List<int> ();
for (int x = 0; x <= angle; x ++) {
vertice.Add(new Vector3(Mathf.Cos(x * Mathf.Deg2Rad) * innerRadius,Mathf.Sin(x * Mathf.Deg2Rad) * innerRadius));
vertice.Add(new Vector3(Mathf.Cos(x * Mathf.Deg2Rad) * outerRadius,Mathf.Sin(x * Mathf.Deg2Rad) * outerRadius));
}
for (int x = 0; x < vertice.Count - 2; x += 2) {
triangle.Add (x + 0);
triangle.Add (x + 2);
triangle.Add (x + 1);
triangle.Add (x + 2);
triangle.Add (x + 3);
triangle.Add (x + 1);
}
Mesh mesh = new Mesh ();
MeshFilter filter = GetComponent<MeshFilter> ();
filter.mesh = mesh;
mesh.vertices = vertice.ToArray ();
mesh.triangles = triangle.ToArray ();
PolygonCollider2D collider = gameObject.AddComponent<PolygonCollider2D> ();
Vector2[] edgePoints = new Vector2[vertice.Count];
for (int i = 0; i < vertice.Count; i++) {
edgePoints [i] = vertice [i];
}
collider.points = edgePoints;
}
Assuming it's possible to do custom collision detection logic in Unity, collision detection between circles is rather simple; just use the Pythagorean theorem on the vector between their centers:
bool TestCirclesCollision(double x1, double y1, double r1, double x2, double y2, double r2)
{
// Pythagorean theorem to compute distance between two points
var dx = x2 - x1;
var dy = y2 - y1;
var distance = Math.Sqrt(dx * dx + dy * dy);
// compare to combined radii of two circles
// return true if collision, otherwise false
return distance <= r1 + r2;
}
If the circles have holes in them at their centers, you would add an additional test to make sure they're not too close, same as the existing radius test but with a >= instead of a <= on the inner:
return distance <= r1outer + r2outer && distance >= r1inner + r2inner;
I need to know how to draw transparent circle/ellipse using OpenTK in Xamarin.Forms.
I have tried with creating Vertex and Fragment shaders with reference of following link : How to draw circle on OpenGL ES 2.0 cross platform? But did not get anything on screen it is showing blank on screen.
Below is the Sample Code used to draw circle with OpenTK in Xamarin.Forms.
GL.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.Enable(EnableCap.DepthTest);
GL.UseProgram(programID);
// The locations where we pass in our color and vertex data
positionInput = GL.GetAttribLocation(programID, "Position");
colorInput = GL.GetAttribLocation(programID, "SourceColor");
// The locations where we pass in unchanging data
projectionInput = GL.GetUniformLocation(programID, "Projection");
modelviewInput = GL.GetUniformLocation(programID, "Modelview");
Matrix4 modelview = Matrix4.CreateRotationX(rotation) * Matrix4.CreateRotationY(rotation) * Matrix4.CreateRotationZ(rotation) * Matrix4.CreateTranslation(xTranslation, yTranslation, -7f);
GL.UniformMatrix4(modelviewInput, false, ref modelview);
float h = 4.0f * (height / width);
Matrix4 projection = Matrix4.CreatePerspectiveOffCenter(-2, 2, -h / 2f, h / 2f, 4, 10);
GL.Viewport(0, 0, (int)width, (int)height);
GL.UniformMatrix4(projectionInput, false, ref projection);
GL.BindBuffer(BufferTarget.ArrayBuffer, colorDataBuffer);
GL.EnableVertexAttribArray(colorInput);
GL.VertexAttribPointer(colorInput, 4, VertexAttribPointerType.Float, false, 0, 0);
float DEGREE_TO_RAD = (float)( 3.14 / 180);
int M_IN_DEGREE = 370;
int N_IN_DEGREE = 100;
int nCount = 1;
int index = 0;
int size = 2;
float[] stVertexArray = new float[2*360];
stVertexArray[0] = 0;
stVertexArray[1] = 0;
for( int nR =N_IN_DEGREE; nR < M_IN_DEGREE; nR++ )
{
float fX = (float) System.Math.Sin((float)nR * DEGREE_TO_RAD ) ;
float fY = (float) System.Math.Cos((float)nR * DEGREE_TO_RAD );
stVertexArray[nCount*2] = fX;
stVertexArray[nCount*2 + 1] = fY;
nCount++;
}
GL.BindBuffer(BufferTarget.ArrayBuffer, stVertexArray.Length);
GL.EnableVertexAttribArray(0);
GL.VertexAttribPointer (index,size, VertexAttribPointerType.Float, false, stVertexArray.Length, IntPtr.Zero);
GL.DrawElements(BeginMode.LineLoop, stVertexArray.Length, DrawElementsType.UnsignedByte,stVertexArray);
GL.Finish();
I am pretty new to the 3D side of XNA and am converting a 2D game.
Its basically designed as a grid and I am drawing with the code below. This works but is a bit laggy, am I doing this completely wrong? Presumably there shouldn't be much lag at this point when we are only talking about a few small models.
protected override void Draw(GameTime gameTime)
{
fpsTimer += gameTime.ElapsedGameTime;
fpsCount++;
if (fpsTimer >= TimeSpan.FromSeconds(1))
{ fpsTimer = TimeSpan.FromSeconds(0); fps = fpsCount; fpsCount = 0; }
GraphicsDevice.Clear(Color.CornflowerBlue);
projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 1.0f, 1000.0f);
world = Matrix.CreateTranslation(new Vector3(0, 0, 0));
view = Matrix.CreateLookAt(new Vector3(xPos, yPos, zHeight), new Vector3(xPos, yPos + zRotation, 0), new Vector3(0, 5, 0));
// DRAW OBJECTS ON MAP
drawMap(view, world, projection);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
spriteBatch.DrawString(font, "" + fps, new Vector2(100, 100), Color.Black);
spriteBatch.End();
base.Draw(gameTime);
}
private void drawMap(Matrix view, Matrix world, Matrix projection)
{
GraphicsDevice.BlendState = BlendState.Opaque;
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
int scale = Globals.scale;
int screenWidthScale = Globals.screenwidth / scale;
int screenHeightScale = Globals.screenheight / scale;
int mapXtile = Convert.ToInt32(xPos/2);
int mapYtile = Convert.ToInt32(yPos/2);
for (int i = Convert.ToInt32(xPos/2) - 30; i < Convert.ToInt32(xPos/2) + 30; i++)
{
for (int a = Convert.ToInt32(yPos/2); a < Convert.ToInt32(yPos/2) + 50; a++)
{
if (mapXtile > 0 && mapYtile > 0 && mapXtile < Globals.mapsizex && mapYtile < Globals.mapsizey)
{
int currentTile = Globals.levelArray[mapXtile, mapYtile].TyleType;
// DRAW TREES
if (currentTile == tyleType.tree)
{
if (Globals.levelArray[mapXtile, mapYtile].typeModifier == 1)
{
Matrix worldMatrix = Matrix.CreateScale(0.8f, 0.8f, 0.8f) * Matrix.CreateRotationX(1.5f) * Matrix.CreateTranslation(new Vector3(i * 2 + 0.2f, a * 2 - 0.4f, -0.1f));
tree.Draw(worldMatrix, view, projection);
}
if (Globals.levelArray[mapXtile, mapYtile].typeModifier == 2)
{
Matrix worldMatrix = Matrix.CreateScale(0.8f, 0.8f, 0.8f) * Matrix.CreateRotationX(1.5f) * Matrix.CreateTranslation(new Vector3(i * 2+0.2f, a * 2-0.4f, -0.1f));
tree2.Draw(worldMatrix, view, projection);
}
}
}
mapYtile = mapYtile + 1;
//mapYtile++;
}
mapXtile = mapXtile + 1;
mapYtile = Convert.ToInt32(yPos / 2);
}
}