Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using OpenTK;
- using OpenTK.Graphics.OpenGL;
- using System;
- using System.Drawing;
- using System.Windows.Forms;
- using Template._3DObj;
- using System.Drawing.Imaging;
- namespace Template
- {
- public partial class MainForm : Form
- { int[] textures = new int[2];
- bool brick;
- int colorTex=1;
- float camDepth;
- static bool iluminare=true;
- int eyePosX, eyePosY, eyePosZ;
- private Point mousePos;
- Color backColor = Color.Gray;
- Random rnd = new Random();
- Coords v1 = new Coords(10, 0, 10);
- Coords v2 = new Coords(40, 0, 10);
- Coords v3 = new Coords(40, 0, 40);
- Coords v4 = new Coords(10, 0, 40);
- Coords v5 = new Coords(10, 30, 10);
- Coords v6 = new Coords(40, 30, 10);
- Coords v7 = new Coords(40, 30, 40);
- Coords v8 = new Coords(10, 30, 40);
- Axes mainAx;
- Cube cb;
- public MainForm()
- {
- InitializeComponent();
- }
- private void MainForm_Load(object sender, EventArgs e)
- {
- SetupDefaultValues();
- SetupWindowGUI();
- SetupSceneObjects();
- }
- private void SetupDefaultValues()
- {
- camDepth = 1.04f;
- eyePosX = 100;
- eyePosY = 100;
- eyePosZ = 50;
- }
- private void SetupWindowGUI()
- {
- }
- private void SetupSceneObjects()
- {
- GL.Enable(EnableCap.DepthTest);
- GL.DepthFunc(DepthFunction.Less);
- GL.Enable(EnableCap.Texture2D);
- mainAx = new Axes(chkAxesVisibility.Checked);
- cb = new Cube(v1, v2, v3, v4, v5, v6, v7, v8, Color.LightBlue);
- }
- private void MainViewport_Load(object sender, EventArgs e)
- {
- GL.ClearColor(backColor);
- }
- private void chkAxesVisibility_CheckedChanged(object sender, EventArgs e)
- {
- mainAx.SetVisibility(chkAxesVisibility.Checked);
- MainViewport.Invalidate();
- }
- private void chkPolygonalStyle_CheckedChanged(object sender, EventArgs e)
- {
- if (chkPolygonalStyle.Checked == true)
- {
- cb.PolygonDrawingStyle("line");
- } else
- {
- cb.PolygonDrawingStyle("surface");
- }
- MainViewport.Invalidate();
- }
- private void btnChangeBack_Click(object sender, EventArgs e)
- {
- backColor = Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255));
- MainViewport.Invalidate();
- }
- private void MainViewport_MouseMove(object sender, MouseEventArgs e)
- {
- mousePos = new Point(e.X, e.Y);
- MainViewport.Invalidate();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- if (iluminare == true)
- {
- iluminare = false;
- button1.Text = "iluminare off";
- }
- else
- {
- iluminare = true;
- button1.Text = "iluminare on";
- }
- MainViewport.Invalidate();
- }
- private void LoadTexture(int textureId, string filename)
- {
- Bitmap bmp = new Bitmap(filename);
- BitmapData data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
- System.Drawing.Imaging.ImageLockMode.ReadOnly,
- System.Drawing.Imaging.PixelFormat.Format32bppArgb);
- GL.BindTexture(TextureTarget.Texture2D, textureId);
- GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba,
- bmp.Width, bmp.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra,
- PixelType.UnsignedByte, data.Scan0);
- bmp.UnlockBits(data);
- GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (float)TextureMinFilter.Linear);
- GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (float)TextureMagFilter.Linear);
- }
- private void button2_Click(object sender, EventArgs e)
- {
- GL.Enable(EnableCap.Texture2D);
- GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
- GL.GenTextures(textures.Length, textures);
- LoadTexture(textures[0], "C:/Users/popov/OneDrive/Desktop/EGC/labs/OpenTK_WinForms_StandardTemplate/ReTester/Content/Textures/wooden_crate.jpg");
- LoadTexture(textures[1], "C:/Users/popov/OneDrive/Desktop/EGC/labs/OpenTK_WinForms_StandardTemplate/ReTester/Content/Textures/grates.png");
- brick = true;
- MainViewport.Invalidate();
- }
- private void button3_Click(object sender, EventArgs e)
- {
- GL.Enable(EnableCap.Texture2D);
- GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
- GL.GenTextures(textures.Length, textures);
- LoadTexture(textures[0], "C:/Users/popov/OneDrive/Desktop/EGC/labs/OpenTK_WinForms_StandardTemplate/ReTester/Content/Textures/wooden_crate.jpg");
- LoadTexture(textures[1], "C:/Users/popov/OneDrive/Desktop/EGC/labs/OpenTK_WinForms_StandardTemplate/ReTester/Content/Textures/grates.png");
- brick = false;
- MainViewport.Invalidate();
- }
- private void radioButton1_CheckedChanged(object sender, EventArgs e)
- {
- colorTex = 1;
- }
- private void radioButton2_CheckedChanged(object sender, EventArgs e)
- {
- colorTex = 2;
- }
- private void radioButton3_CheckedChanged(object sender, EventArgs e)
- {
- colorTex = 3;
- }
- private void MainViewport_Paint(object sender, PaintEventArgs e)
- {
- GL.Clear(ClearBufferMask.ColorBufferBit);
- GL.Clear(ClearBufferMask.DepthBufferBit);
- GL.ClearColor(backColor);
- SetView();
- if (chkRotationOn.Checked == true)
- {
- // Doar după Ox.
- GL.Rotate(Math.Max(mousePos.X, mousePos.Y), 1, 1, 1);
- }
- // GRAPHICS PAYLOAD
- mainAx.DrawMe();
- if (brick == true)
- {
- GL.BindTexture(TextureTarget.Texture2D, textures[0]);
- }
- else
- {
- GL.BindTexture(TextureTarget.Texture2D, textures[1]);
- }
- if(colorTex==1)
- GL.Color3(Color.White);
- if (colorTex == 2)
- GL.Color3(Color.Red);
- if (colorTex == 3)
- GL.Color3(Color.Blue);
- cb.DrawMe();
- if (iluminare == true)
- {
- GL.Enable(EnableCap.Lighting);
- }
- else GL.Disable(EnableCap.Lighting);
- MainViewport.SwapBuffers();
- }
- private void SetView() {
- Matrix4 perspective = Matrix4.CreatePerspectiveFieldOfView(camDepth, 4 / 3, 1, 256);
- Matrix4 lookat = Matrix4.LookAt(eyePosX, eyePosY, eyePosZ, 0, 0, 0, 0, 1, 0);
- GL.MatrixMode(MatrixMode.Projection);
- GL.LoadIdentity();
- GL.LoadMatrix(ref perspective);
- GL.MatrixMode(MatrixMode.Modelview);
- GL.LoadIdentity();
- GL.LoadMatrix(ref lookat);
- GL.Viewport(0, 0, MainViewport.Width, MainViewport.Height);
- }
- }
- }
- //in cube.cs
- /*public void DrawMe()
- {
- // GL.PolygonMode(MaterialFace.FrontAndBack, currentPolygonState);
- GL.Begin(PrimitiveType.Quads);
- int R = color.R;
- int G = color.G;
- int B = color.B;
- int offset = 20;
- //GL.Color3(Color.FromArgb(R,G,B - offset));
- // baza
- GL.TexCoord2(0.0, 1.0);
- GL.Vertex3(v1.x, v1.y, v1.z);
- GL.TexCoord2(1.0, 1.0);
- GL.Vertex3(v2.x, v2.y, v2.z);
- GL.TexCoord2(1.0, 0.0);
- GL.Vertex3(v3.x, v3.y, v3.z);
- GL.TexCoord2(0.0, 0.0);
- GL.Vertex3(v4.x, v4.y, v4.z);
- //GL.Color3(Color.FromArgb(R, G- offset, B));
- //perete dreapta
- GL.TexCoord2(0.0, 1.0);
- GL.Vertex3(v2.x, v2.y, v2.z); GL.TexCoord2(1.0, 1.0);
- GL.Vertex3(v3.x, v3.y, v3.z); GL.TexCoord2(1.0, 0.0);
- GL.Vertex3(v7.x, v7.y, v7.z); GL.TexCoord2(0.0, 0.0);
- GL.Vertex3(v6.x, v6.y, v6.z);
- //GL.Color3(Color.FromArgb(R - offset, G, B));
- // perete fata
- GL.TexCoord2(0.0, 1.0); GL.Vertex3(v3.x, v3.y, v3.z); GL.TexCoord2(1.0, 1.0);
- GL.Vertex3(v4.x, v4.y, v4.z); GL.TexCoord2(1.0, 0.0);
- GL.Vertex3(v8.x, v8.y, v8.z); GL.TexCoord2(0.0, 0.0);
- GL.Vertex3(v7.x, v7.y, v7.z);
- //GL.Color3(Color.FromArgb(R - offset, G, B - offset));
- // perete stanga
- GL.TexCoord2(0.0, 1.0); GL.Vertex3(v1.x, v1.y, v1.z); GL.TexCoord2(1.0, 1.0);
- GL.Vertex3(v4.x, v4.y, v4.z); GL.TexCoord2(1.0, 0.0);
- GL.Vertex3(v8.x, v8.y, v8.z); GL.TexCoord2(0.0, 0.0);
- GL.Vertex3(v5.x, v5.y, v5.z);
- //GL.Color3(Color.FromArgb(R - offset, G - offset, B));
- // perete spate
- GL.TexCoord2(0.0, 1.0); GL.Vertex3(v1.x, v1.y, v1.z); GL.TexCoord2(1.0, 1.0);
- GL.Vertex3(v2.x, v2.y, v2.z); GL.TexCoord2(1.0, 0.0);
- GL.Vertex3(v6.x, v6.y, v6.z); GL.TexCoord2(0.0, 0.0);
- GL.Vertex3(v5.x, v5.y, v5.z);
- //GL.Color3(Color.FromArgb(R - offset, G - offset, B - offset));
- // capac
- GL.TexCoord2(0.0, 1.0); GL.Vertex3(v5.x, v5.y, v5.z); GL.TexCoord2(1.0, 1.0);
- GL.Vertex3(v6.x, v6.y, v6.z); GL.TexCoord2(1.0, 0.0);
- GL.Vertex3(v7.x, v7.y, v7.z); GL.TexCoord2(0.0, 0.0);
- GL.Vertex3(v8.x, v8.y, v8.z);
- GL.End();
- }*/
- /*GL.TexCoord2(0.0, 1.0);
- GL.TexCoord2(1.0, 1.0);
- GL.TexCoord2(1.0, 0.0);
- GL.TexCoord2(0.0, 0.0);*/
Advertisement
Add Comment
Please, Sign In to add comment