问题描述
我正在创建一个简单的代码来读取我的世界 json 并在屏幕上生成,立方体纹理可以正常工作,但是像火炬这样的纹理很糟糕,这个想法是能够读取任何我的世界 json 并在屏幕上重现它屏幕,我试图映射,但我不能。 每个图像都变成了立方体的一个面,我希望我可以将它塑造成读取纹理的大小。
using OpenTK;
using OpenTK.Graphics.OpenGL;
using System;
using System.Drawing;
using System.Windows.Forms;
namespace OpenGLTestes
{
public partial class Form1 : Form
{
int downX,downY;
float xangle,yangle,fov;
int texTop;
int texBottom;
int texSideFront;
int texSideBack;
int texSideLeft;
int texSideRight;
string AppPath = Application.StartupPath + "\\Images\\";
System.Drawing.Imaging.PixelFormat ForBitmapData = System.Drawing.Imaging.PixelFormat.Format24bppRgb;
OpenTK.Graphics.OpenGL.PixelFormat ForTextimage2D = OpenTK.Graphics.OpenGL.PixelFormat.Bgr;
private int UploadTexture(string pathname)
{
// Create a new OpenGL texture object
int id = GL.GenTexture();
// Select the new texture
GL.BindTexture(TextureTarget.Texture2D,id);
//Verificando o tipo de Imagem
if (pathname.Substring(pathname.Length - 3,3).toupper() == "PNG")
{
ForBitmapData = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
ForTextimage2D = OpenTK.Graphics.OpenGL.PixelFormat.Bgra;
}
else
{
ForBitmapData = System.Drawing.Imaging.PixelFormat.Format24bppRgb;
ForTextimage2D = OpenTK.Graphics.OpenGL.PixelFormat.Bgr;
}
// Load the image
Bitmap bmp = new Bitmap(pathname);
// Lock image data to allow direct access
System.Drawing.Imaging.BitmapData bmp_data = bmp.LockBits(
new Rectangle(0,bmp.Width,bmp.Height),System.Drawing.Imaging.ImageLockMode.ReadOnly,ForBitmapData);
// Import the image data into the OpenGL texture
GL.TexImage2D(TextureTarget.Texture2D,PixelInternalFormat.Rgba,bmp_data.Width,bmp_data.Height,ForTextimage2D,OpenTK.Graphics.OpenGL.PixelType.UnsignedByte,bmp_data.Scan0);
// Unlock the image data
bmp.UnlockBits(bmp_data);
// Configure minification and magnification filters
GL.TexParameter(TextureTarget.Texture2D,TextureParameterName.TextureMinFilter,(int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D,TextureParameterName.TextureMagFilter,(int)TextureMagFilter.Nearest);
//GL.TexParameter(TextureTarget.Texture2D,TextureParameterName.TextureWrapS,(int)TextureMagFilter.Nearest);
//GL.TexParameter(TextureTarget.Texture2D,TextureParameterName.TextureWrapT,(int)TextureMagFilter.Nearest);
// Return the OpenGL object ID for use
return id;
}
public Form1()
{
InitializeComponent();
}
private void glControl_Load(object sender,EventArgs e)
{
GL.ClearColor(Color.Blue);
//xangle = 45;
//yangle = 25;
//Lighting
GL.Enable(EnableCap.Lighting);
//GL.Enable(EnableCap.ColorMaterial); //para o Light não afetar a cor do material
//Definindo a Light0
float[] Light_Position = {10,10,20};
GL.Light(LightName.Light0,LightParameter.Position,Light_Position);
float[] Light_Difuse = { 1.0f,1.0f,1.0f }; //color light
GL.Light(LightName.Light0,LightParameter.Diffuse,Light_Difuse);
float[] Light_Ambient = { 10.0f,10.0f,10.0f };//color light Ambiente
GL.Light(LightName.Light0,LightParameter.Ambient,Light_Ambient);
GL.Enable(EnableCap.Light0);
//texturing
GL.Enable(EnableCap.Texture2D);
GL.GenerateMipmap(GenerateMipmapTarget.Texture2D); //Mapea a mesma imagem de varios tamanhos
texTop = UploadTexture(AppPath + "torch.png");
texBottom = UploadTexture(AppPath + "torch.png");
texSideBack = UploadTexture(AppPath + "torch.png");
texSideFront = UploadTexture(AppPath + "torch.png");
texSideLeft = UploadTexture(AppPath + "torch.png");
texSideRight = UploadTexture(AppPath + "torch.png");
//GL.Enable(EnableCap.DepthTest);
GL.Enable(EnableCap.Blend);
GL.BlendFunc(BlendingFactor.SrcAlpha,BlendingFactor.OneMinusSrcAlpha);
}
private void glControl_Paint(object sender,PaintEventArgs e)
{
GL.Clear(ClearBufferMask.ColorBufferBit);
GL.Clear(ClearBufferMask.DepthBufferBit);
Matrix4 perspetiva = Matrix4.CreatePerspectiveFieldOfView(1.0f,4 / 3,1,10000); //Setup Perspective
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity(); // load the identity matrix
GL.LoadMatrix(ref perspetiva);
Matrix4 camera = Matrix4.LookAt(200,50,0); //Setup camera
GL.MatrixMode(MatrixMode.Modelview); //Load Camera
GL.LoadIdentity();
GL.LoadMatrix(ref camera);
GL.Viewport(0,glControl.Width,glControl.Height); //Size of window
GL.Enable(EnableCap.DepthTest); //Enable correct Z Drawings
GL.DepthFunc(DepthFunction.Less) ; //Enable correct Z Drawings
GL.Rotate((yangle * -1),1);
GL.Rotate(xangle,0);
int Tam = 45;
//Front
GL.BindTexture(TextureTarget.Texture2D,texSideFront);
GL.Begin(PrimitiveType.Quads);
GL.Color3(Color.White);
GL.normal3(1.0,0.0,0.0);
GL.TexCoord2(1,1);
GL.Vertex3(Tam,-Tam,Tam);
GL.TexCoord2(0,-Tam);
GL.TexCoord2(0,0);
GL.Vertex3(Tam,Tam,-Tam);
GL.TexCoord2(1,Tam);
GL.End();
//Back
GL.BindTexture(TextureTarget.Texture2D,texSideBack);
GL.Begin(PrimitiveType.Quads);
GL.Color4(Color.Cyan);
GL.normal3(-1.0,-1.0);
GL.TexCoord2(1,1);
GL.Vertex3(-Tam,0);
GL.Vertex3(-Tam,Tam);
GL.End();
//Right
GL.BindTexture(TextureTarget.Texture2D,texSideRight);
GL.Begin(PrimitiveType.Quads);
GL.Color3(Color.Yellow);
GL.normal3(0.0,-Tam);
GL.End();
//Top
GL.BindTexture(TextureTarget.Texture2D,texTop);
GL.Begin(PrimitiveType.Quads);
GL.Color3(Color.Green);
GL.normal3(0.0,1.0,0.0);
GL.TexCoord2(0,0);
GL.Vertex3( Tam,Tam);
GL.TexCoord2(1,Tam);
GL.End();
//Botton
GL.BindTexture(TextureTarget.Texture2D,texBottom);
GL.Begin(PrimitiveType.Quads);
GL.Color3(Color.Blue);
GL.normal3(0.0,-1.0,1);
GL.Vertex3( Tam,texSideRight);
GL.Begin(PrimitiveType.Quads);
GL.Color3(Color.White);
GL.normal3(0.0,1.0);
GL.TexCoord2(1,Tam);
GL.End();
glControl.VSync = true;
glControl.SwapBuffers();
}
private void glControl_MouseDown(object sender,System.Windows.Forms.MouseEventArgs e)
{
downX = e.X;
downY = e.Y;
}
private void glControl_MouseMove(object sender,System.Windows.Forms.MouseEventArgs e)
{
if (e.X == downX && e.Y == downY) return;
if (e.Button == MouseButtons.Left)
{
xangle += (e.X - downX) * 0.05f;
yangle += (e.Y - downY) * 0.05f;
glControl.Invalidate();
glControl.Refresh();
}
}
}
}
Example:
template_torch.json
{
"ambientocclusion": false,"textures": {
"particle": "#torch"
},"elements": [
{ "from": [ 7,7 ],"to": [ 9,9 ],"shade": false,"faces": {
"down": { "uv": [ 7,13,9,15 ],"texture": "#torch" },"up": { "uv": [ 7,6,8 ],"texture": "#torch" }
}
},{ "from": [ 7,0 ],16,16 ],"faces": {
"west": { "uv": [ 0,"east": { "uv": [ 0,{ "from": [ 0,"to": [ 16,"faces": {
"north": { "uv": [ 0,"south": { "uv": [ 0,"texture": "#torch" }
}
}
]
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)