I have a problem when I load my Vertex Shader in my effect file in XNA 4.0.
So without this row under it will work exact as I want.
VertexShader = compile vs_2_0 VertexShaderFunction();
So what have I done so far... Nothing really! :)
Load image and effect file.
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
myTexture = Content.Load<Texture2D>("textures\\fan");
effect = Content.Load<Effect>("effects\\ashader");
}
Draw the image on the screen.
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
// Draw the sprite.
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
effect.CurrentTechnique.Passes[0].Apply();
spriteBatch.Draw(myTexture, spritePosition,Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
Shader
float4x4 World;
float4x4 View;
float4x4 Projection;
struct VertexShaderInput
{
float4 Position : POSITION0;
};
struct VertexShaderOutput
{
float4 Position : POSITION0;
};
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;
float4 worldPosition = mul(input.Position, World);
float4 viewPosition = mul(worldPosition, View);
output.Position = mul(viewPosition, Projection);
return output;
}
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
return float4(1, 0, 0, 1);
}
technique Technique1
{
pass Pass1
{
VertexShader = compile vs_2_0 VertexShaderFunction();
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}
This should draw a red square where the texture is positioned. It does this without the VertexShader loaded. But when loading it, no textures shown at all.
So what have i missed?
Related
I am porting an OpenGL framework to JavaScript using Emscriten.
The state is stored in a uniform struct:
struct UniformState {
...
mat4 modelviewprojection_matrix;
mat4 modelview_matrix;
mat3 normal_matrix;
mat4 texture_matrix;
...
};
that I'd like to access in both the vertex and the fragment shaders as:
uniform UniformState GLUP;
when I do that, I get an error at link time:
Uniform `GLUP`is not linkable between attached shaders
Is it forbidden to bind the same uniform in the vertex shader and fragment shader ?
(if it is forbidden, then clearly I can declare two different sets of uniform variables for each shader, but I'd prefer to have only one, since it makes the code cleaner / simpler, this is why I'm asking in case there
is something special to do to make it possible).
Seems to work for me.
Note as Reto mentioned if I don't set the precision I get a link error
"use strict";
var gl = document.createElement("canvas").getContext("webgl");
var program = twgl.createProgramFromScripts(gl, ["vs", "fs"], [], [], log);
log("--done--");
function log(msg) {
var elem = document.createElement("pre");
elem.appendChild(document.createTextNode(msg));
document.body.appendChild(elem);
}
<script id="vs" type="notjs">
struct Test {
vec4 color;
vec4 mult;
};
uniform Test test;
attribute vec4 position;
void main() {
gl_Position = position * test.mult;
}
</script>
<script id="fs" type="notjs">
precision highp float;
struct Test {
vec4 color;
vec4 mult;
};
uniform Test test;
void main() {
gl_FragColor = test.color;
}
</script>
<script src="https://twgljs.org/dist/twgl.min.js"></script>
I am relatively new to both StageXL and Dart and I am having difficulty changing the color of a Sprite on a mouse click. Currently I am just referencing the sprite and calling the graphics fillColor() method however nothing changes. Any ideas on how to change individual Sprite lines by mouse click?
class GameBoard
{
Stage stage;
int xPos;
int yPos;
var circle;
var line;
GameBoard(Stage s)
{
this.stage = s;
this.xPos = 100;
this.yPos = 100;
this.circle = new Sprite();
this.line = new Sprite();
}
Sprite generateDots(int x, int y, int size)
{
circle.graphics.circle(x,y,size);
circle.graphics.fillColor(Color.Black);
circle.addEventListener(MouseEvent.CLICK, reactCircle);
return circle;
}
Sprite generateHorzLines(int x, int y)
{
line.graphics.beginPath();
line.graphics.moveTo(x, y);
line.graphics.lineTo(x+100, y);
line.graphics.closePath();
line.graphics.strokeColor(Color.Gray);
line.addEventListener(MouseEvent.CLICK, reactHorzLine);
return line;
}
Sprite generateVertLines(int x, int y)
{
line.graphics.beginPath();
line.graphics.moveTo(x, y);
line.graphics.lineTo(x, y+100);
// this.line.graphics.closePath();
line.graphics.strokeColor(Color.Gray);
line.addEventListener(MouseEvent.CLICK, reactVertLine);
return line;
}
void generateBoard()
{
for(int i = 0; i < 5;i++)
{
for(int j = 0; j< 5;j++)
{
//render horizontal lines
if(j < 4)
{
stage.addChild(generateHorzLines(xPos,yPos));
}
//render vertical lines
if(i<4)
{
stage.addChild(generateVertLines(xPos,yPos));
}
//render points
stage.addChild(generateDots(xPos,yPos,5));
xPos+=100;
}
yPos+=100;
xPos = 100;
}
}
void reactHorzLine(MouseEvent event)
{
line.graphics.fillColor(Color.Red);
}
void reactVertLine(MouseEvent event)
{
line.graphics.fillColor(Color.Green);
}
void reactCircle(MouseEvent event)
{
circle.graphics.fillColor(Color.Blue);
}
}
The current version of StageXL (0.12) has no support for vector graphics when using the WebGL renderer only the Canvas2D renderer is supported. This will change with the next versions where vector graphics are also supported with the WebGL renderer. In the meantime you can opt-out of the WebGL renderer to use the Canvas2D renderer.
// do this before you construct the Stage
StageXL.stageOptions.renderEngine = RenderEngine.Canvas2D;
Btw. you will get best performance by using Bitmaps/BitmapDatas in StageXL. They are based on textures which will be rendered much faster than vector graphics.
I am trying to translate the OpenCV CascadeClassifier tutorial from C++ to Java. Working good in C++. Also this java tutorial is working fine.
But the translation is simply not detecting the face. I don't get explicit errors. I can see the processing of the video input from the webcam (grey/histogram...) and the video display. Cascade load doesn't give error. But the CascadeClassifier call just doesn't return any faces... So, you probably can skip all the code and just go to my CascadeClassifier call, down to public Mat detect(Mat inputframe). As I am new to Java and OpenCV, I paste the rest (I removed anything I felt may not be significant), just in case, but don't mean for you to debug that...
I have also tried this call (and other portions) in many different ways and nothing... running out of ideas...
Thank you!!
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.highgui.VideoCapture;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
class My_Panel extends JPanel{
private static final long serialVersionUID = 1L;
private BufferedImage image;
private CascadeClassifier face_cascade;
// Create a constructor method
public My_Panel(){
super();
String face_cascade_name = "/haarcascade_frontalface_alt.xml";
//String face_cascade_name = "/lbpcascade_frontalface.xml";
//-- 1. Load the cascades
String str;
str = getClass().getResource(face_cascade_name).getPath();
str = str.replace("/C:","C:");
face_cascade_name=str;
face_cascade=new CascadeClassifier(face_cascade_name);
if( !face_cascade.empty())
{
System.out.println("--(!)Error loading A\n");
return;
}
else
{
System.out.println("Face classifier loooaaaaaded up");
}
}
private BufferedImage getimage(){
return image;
}
public void setimage(BufferedImage newimage){
image=newimage;
return;
}
/**
* Converts/writes a Mat into a BufferedImage.
*
* #param matrix Mat of type CV_8UC3 or CV_8UC1
* #return BufferedImage of type TYPE_3BYTE_BGR or TYPE_BYTE_GRAY
*/
public BufferedImage matToBufferedImage(Mat matrix) {
int cols = matrix.cols();
int rows = matrix.rows();
int elemSize = (int)matrix.elemSize();
byte[] data = new byte[cols * rows * elemSize];
int type;
matrix.get(0, 0, data);
switch (matrix.channels()) {
case 1:
type = BufferedImage.TYPE_BYTE_GRAY;
break;
case 3:
type = BufferedImage.TYPE_3BYTE_BGR;
// bgr to rgb
byte b;
for(int i=0; i<data.length; i=i+3) {
b = data[i];
data[i] = data[i+2];
data[i+2] = b;
}
break;
default:
return null;
}
BufferedImage image2 = new BufferedImage(cols, rows, type);
image2.getRaster().setDataElements(0, 0, cols, rows, data);
return image2;
}
public void paintComponent(Graphics g){
BufferedImage temp=getimage();
g.drawImage(temp,10,10,temp.getWidth(),temp.getHeight(), this);
}
public Mat detect(Mat inputframe){
Mat mRgba=new Mat();
Mat mGrey=new Mat();
MatOfRect faces = new MatOfRect();
//MatOfRect eyes = new MatOfRect();
inputframe.copyTo(mRgba);
inputframe.copyTo(mGrey);
Imgproc.cvtColor( mRgba, mGrey, Imgproc.COLOR_BGR2GRAY);
Imgproc.equalizeHist( mGrey, mGrey );
face_cascade.detectMultiScale(mGrey, faces);
//face_cascade.detectMultiScale(mGrey, faces, 1.1, 2, 0|Objdetect.CASCADE_SCALE_IMAGE, new Size(30, 30), new Size(200,200) );
//face_cascade.detectMultiScale(mGrey, faces, 1.1, 2, 2//CV_HAAR_SCALE_IMAGE,
// ,new Size(30, 30), new Size(200,200) );
System.out.println(String.format("Detected %s faces", faces.toArray().length));
return mGrey;
}
}
public class window {
public static void main(String arg[]){
// Load the native library.
System.loadLibrary("opencv_java245");
String window_name = "Capture - Face detection";
JFrame frame = new JFrame(window_name);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
My_Panel my_panel = new My_Panel();
frame.setContentPane(my_panel);
frame.setVisible(true);
//-- 2. Read the video stream
BufferedImage temp;
Mat webcam_image=new Mat();
VideoCapture capture =new VideoCapture(0);
if( capture.isOpened())
{
while( true )
{
capture.read(webcam_image);
if( !webcam_image.empty() )
{
frame.setSize(webcam_image.width()+40,webcam_image.height()+60);
//-- 3. Apply the classifier to the captured image
// At this point I was wondering where this should be done.
// I put it within the panel class, but maybe one could actually
// create a processor object...
webcam_image=my_panel.detect(webcam_image);
//-- 4. Display the image
temp=my_panel.matToBufferedImage(webcam_image);
my_panel.setimage(temp);
my_panel.repaint();
}
else
{
System.out.println(" --(!) No captured frame -- Break!");
break;
}
}
}
return;
}
}
PS.: Other info, just in case:
mGrey is: Mat [ 480*640*CV_8UC1, isCont=true, isSubmat=false, nativeObj=0x19d9af48, dataAddr=0x19dc3430 ]
face is: Mat [ 0*0*CV_8UC1, isCont=false, isSubmat=false, nativeObj=0x194bb048, dataAddr=0x0 ]
I have tried your code and it works fine! You have only one issue with haarcascade_frontalface_alt.xml file location. Try to use a full path to the file:
face_cascade= new CascadeClassifier("D:/HelloCV/src/haarcascade_frontalface_alt.xml");
Hi, I'm working on a 2D game and I was working on the scrolling background but whatever I try it doesn't get it scrolled.
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
//The size of the Sprite
public Rectangle Size;
//Used to size the Sprite up or down from the original image
public float Scale = 1.0f;
// Create an instance of Texture2D that will
// contain the background texture.
Texture2D background;
// Create a Rectangle that will definee
// the limits for the main game screen.
Rectangle mainFrame;
private GamePadState gamePadState;
private KeyboardState keyboardState;
public class Camera
{
public Camera(Viewport viewport)
{
Origin = new Vector2(viewport.Width / 2.0f, viewport.Height / 2.0f);
Zoom = 1.0f;
}
public Vector2 Position { get; set; }
public Vector2 Origin { get; set; }
public float Zoom { get; set; }
public float Rotation { get; set; }
public Matrix GetViewMatrix(Vector2 parallax)
{
// To add parallax, simply multiply it by the position
return Matrix.CreateTranslation(new Vector3(-Position * parallax, 0.0f)) *
// The next line has a catch. See note below.
Matrix.CreateTranslation(new Vector3(-Origin, 0.0f)) *
Matrix.CreateRotationZ(Rotation) *
Matrix.CreateScale(Zoom, Zoom, 1) *
Matrix.CreateTranslation(new Vector3(Origin, 0.0f));
}
}
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
graphics.IsFullScreen = true;
}
protected override void Initialize()
{
gamePadState = GamePad.GetState(PlayerIndex.One);
keyboardState = Keyboard.GetState();
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// Load the background content.
background = Content.Load<Texture2D>("Images\\muur");
// Set the rectangle parameters.
mainFrame = new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
}
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
// Draw the background.
// Start building the sprite.
spriteBatch.Begin();
// Draw the background.
spriteBatch.Draw(background, mainFrame, Color.White);
// End building the sprite.
spriteBatch.End();
base.Draw(gameTime);
}
}
How can I achieve this functionality?
I don't see where you use the camera transform...
you should have a parallax vector and update it...
Vector2 parallax_position;
float parallax_speed;
public void Update (Gametime time)
{
parallax_position += parallax_speed * Vector2.UnitX * (float) time.elapsed.totalseconds;
}
and then in Draw method, you should use it in your spritebatch....
public void Draw()
{
spriteBatch.begin(..,...,..,..,.., GetCameraTransform(Parallax));
...
}
I'm currently doing an assignment, on which one of the requirements is for a random object to appear on screen and move across. Being new to XNA, i do not know where to even begin implementing such behaviours to the game, thus would really appreciate if someone could give me a nudge towards the right direction.
I'm only really accustomed to invoking something when a key is pressed, however with something completely random, this can't be done. as far as i am aware of.
Thank you.
You need to create and set up a sprite for the UFO first. In your protected override void Update(GameTime gameTime) code you simply need to get the current time and compare it to the rules in which you wish to apply. Then update if you wish to draw and "move" the sprite. Here is an example:
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion
public class Game : Microsoft.Xna.Framework.Game {
#region Game Settings
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
GraphicsDevice device;
int screenWidth = 800;
int screenHeight = 600;
bool fullscreen = false;
string title = "MyGame";
string origionaltitle;
float frames = 0;
float framesPerSecond = 0;
int startTime;
int currentTime;
int nextTime;
#endregion
struct sprite {
public string TextureName;
public Texture2D Texture;
public Vector2 Position;
public Vector2 Speed;
public Color[] TextureData;
};
bool DrawUFO = false;
sprite ufo = new sprite();
public Game() {
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void LoadContent() {
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
device = graphics.GraphicsDevice;
// TODO: use this.Content to load your game content here
ufo.TextureName = "ufo";
ufo.Texture = Content.Load<Texture2D>(ball.TextureName);
ufo.TextureData = new Color[ufo.Texture.Width *ufo.Texture.Height];
ufo.Texture.GetData(ball.TextureData);
}
protected override void Initialize() {
// TODO: Add your initialization logic here
ufo.Position = new Vector2(10f, 10.0f);
ufo.Speed = new Vector2(0.0f, 10.0f);
//
// Set up game window
graphics.PreferredBackBufferWidth = screenWidth;
graphics.PreferredBackBufferHeight = screenHeight;
graphics.IsFullScreen = fullscreen;
graphics.ApplyChanges();
origionaltitle = title;
Window.Title = title;
//
// Set the initial time
startTime = DateTime.Now.Second;
//
// Set "random"/next time for ufo to be rendered
nextTime = startTime + rand.Next(2);
//
base.Initialize();
}
protected override void Update(GameTime gameTime) {
//
// Set the current time
currentTime = DateTime.Now.Second;
//
// if not drawing ufo then
if(!DrawURO) {
//
// check current time and compare it with the next time
if( currentTime == nextTime ) {
DrawURO = true;
}
} else {
//
// Update UFO position (aka move it)
ufo.Posistion += ball.Speed *(float)gameTime.ElapsedGameTime.TotalSeconds;
//
// if ufo goes of the screen then
if(ufo.Position.Y > screenHeight) {
//
// Reset ufo
DrawURO = false;
ufo.Position.X = 10.0f;
ufo.Position.Y = 10.0f;
//
// set next time to render
nextTime = currentTime + rand.Next(2);
}
}
}
protected override void Draw(GameTime gameTime) {
graphics.GraphicsDevice.Clear(Color.Black);
// TODO: Add your drawing code here
spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
if(DrawUFO == true) {
spriteBatch.Draw(ufo.Texture, ufo.Position, Color.White);
}
spriteBatch.End();
//
base.Draw(gameTime);
}
}
I copied this from some code i did at college a few years a ago, so apologies for any bugs.